Skip to content

Commit

Permalink
papesr restore-backup [--restore-files] command added #51
Browse files Browse the repository at this point in the history
tests are also included
  • Loading branch information
perrette committed Apr 29, 2023
1 parent 48bf4f5 commit c9d7a43
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
22 changes: 17 additions & 5 deletions papers/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ def undocmd(parser, o, config):
shutil.move(tmp, back)
# o.savebib()

def restorecmd(parser, o, config):
if not config.git:
parser.print_help()
raise PapersExit('only valid with --git enabled')
_restore_from_backupdir(config, restore_files=o.restore_files)


def gitcmd(parser, o, config):
try:
out = sp.check_output(['git']+o.gitargs, cwd=config.gitdir)
Expand Down Expand Up @@ -1064,12 +1071,15 @@ def get_parser(config=None):

# undo
# ====
restorep = argparse.ArgumentParser(add_help=False)
restorep.add_argument('--restore-files', action='store_true', help='Use this option to restore files that have been renamed. By default the file link points to the back-up repository. This command has no effect without --git-lfs, and will result in broken file links.')
restorep.add_argument('-n', '--steps', type=int, default=1, help='number of times undo/redo should be performed')
_restorep = argparse.ArgumentParser(add_help=False)
_restorep.add_argument('--restore-files', action='store_true', help='Use this option to restore files that have been renamed. By default the file link points to the back-up repository. This command has no effect without --git-lfs, and will result in broken file links.')

_stepsp = argparse.ArgumentParser(add_help=False)
_stepsp.add_argument('-n', '--steps', type=int, default=1, help='number of times undo/redo should be performed')

undop = subparsers.add_parser('undo', parents=[cfg, restorep], help='this command is modified and more powerful if git-tracking is enabled (infinite memory vs back-and-forth switch)')
redop = subparsers.add_parser('redo', parents=[cfg, restorep], help='this command is modified and more powerful if git-tracking is enabled (infinite memory vs back-and-forth switch)')
undop = subparsers.add_parser('undo', parents=[cfg, _restorep, _stepsp], help='Undo changes on bibtex (if --git is not enabled, only back and forth with last modification). If --git-lfs is enabled, the file entry may differ if it does not exist on disk any more, unless --restore-files was passed.')
redop = subparsers.add_parser('redo', parents=[cfg, _restorep, _stepsp], help='Redo changes on bibtex (if --git is not enabled, this has the same effect as papers undo)')
restorep = subparsers.add_parser('restore-backup', parents=[cfg, _restorep], help='Restore bibtex from backup. Also restore files if --restore-files if passed (--git-lfs only).')

# git
# ===
Expand Down Expand Up @@ -1145,6 +1155,8 @@ def main(args=None):
check_install(subp, o, config) and undocmd(subp, o, config)
elif o.cmd == 'redo':
check_install(subp, o, config) and redocmd(subp, o, config)
elif o.cmd == 'restore-backup':
check_install(subp, o, config, bibtex_must_exist=False) and restorecmd(subp, o, config)
elif o.cmd == 'git':
if not installed:
subp.error('papers must be installed to use git command')
Expand Down
1 change: 1 addition & 0 deletions papers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

CONFIG_FILE_LOCAL = '.papers/config.json'
DATA_DIR = os.path.join(DATA_HOME, 'papers')
BACKUP_DIR = os.path.join(DATA_HOME, 'papers', 'backups')
CONFIG_FILE = os.path.join(DATA_DIR, 'config.json')
CONFIG_FILE_LEGACY = os.path.join(CONFIG_HOME, 'papersconfig.json')
CACHE_DIR = os.path.join(CACHE_HOME, 'papers')
Expand Down
18 changes: 18 additions & 0 deletions tests/test_undo.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ class TestTimeTravelGitGlobal(GlobalGitInstallTest, TimeTravelBase):
pass


class TestRestoreGitLocal(LocalGitInstallTest):

def get_commit(self):
return sp.check_output(f"git rev-parse HEAD", shell=True, cwd=self.config.gitdir).strip().decode()

def test_undo(self):
## Make sure git undo / redo travels as expected

self.papers(f'add {self.anotherbib}')
biblio = Biblio.load(self._path(self.mybib), '')

# Remove bibtex
sp.check_call(f"rm -f {self._path(self.mybib)}", shell=True)

self.papers(f'restore-backup')
biblio2 = Biblio.load(self._path(self.mybib), '')

self.assertMultiLineEqual(biblio.format(), biblio2.format())

class TestUndoGitLocal(LocalGitLFSInstallTest):

Expand Down

0 comments on commit c9d7a43

Please sign in to comment.