From c9d7a437474c31afb4164f268c071510e1a166f7 Mon Sep 17 00:00:00 2001 From: perrette Date: Sun, 30 Apr 2023 00:24:25 +0200 Subject: [PATCH] papesr restore-backup [--restore-files] command added #51 tests are also included --- papers/__main__.py | 22 +++++++++++++++++----- papers/config.py | 1 + tests/test_undo.py | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/papers/__main__.py b/papers/__main__.py index 8eb4f7d..08b0e69 100644 --- a/papers/__main__.py +++ b/papers/__main__.py @@ -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) @@ -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 # === @@ -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') diff --git a/papers/config.py b/papers/config.py index a9033d3..e24eb91 100644 --- a/papers/config.py +++ b/papers/config.py @@ -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') diff --git a/tests/test_undo.py b/tests/test_undo.py index 6660525..69211a6 100644 --- a/tests/test_undo.py +++ b/tests/test_undo.py @@ -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):