From 89ed8acdbe468fd76d531cd8b7b2ace5b414f0bd Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 6 Jan 2022 05:42:24 -0500 Subject: project: abort a bit earlier before migrating .git/ Verify all the .git/ paths will be handled by the migration logic before starting the migration. This way we still abort & log an error, but the user gets to see it before we put the tree into a state that they have to manually recover. Also add a few more known-safe-to-clobber paths. Bug: https://crbug.com/gerrit/15273 Change-Id: If49d69b341bc960ddcafa30da333fb5ec7145b51 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/327557 Reviewed-by: Colin Cross Tested-by: Mike Frysinger --- tests/test_project.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'tests/test_project.py') diff --git a/tests/test_project.py b/tests/test_project.py index d578fe84..4f449227 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -347,6 +347,10 @@ class MigrateWorkTreeTests(unittest.TestCase): } _FILES = { 'COMMIT_EDITMSG', 'FETCH_HEAD', 'HEAD', 'index', 'ORIG_HEAD', + 'unknown-file-should-be-migrated', + } + _CLEAN_FILES = { + 'a-vim-temp-file~', '#an-emacs-temp-file#', } @classmethod @@ -365,10 +369,9 @@ class MigrateWorkTreeTests(unittest.TestCase): dotgit.mkdir(parents=True) for name in cls._SYMLINKS: (dotgit / name).symlink_to(f'../../../.repo/projects/src/test.git/{name}') - for name in cls._FILES: + for name in cls._FILES | cls._CLEAN_FILES: (dotgit / name).write_text(name) - subprocess.run(['tree', '-a', str(dotgit)]) yield tempdir def test_standard(self): @@ -385,3 +388,24 @@ class MigrateWorkTreeTests(unittest.TestCase): gitdir = tempdir / '.repo/projects/src/test.git' for name in self._FILES: self.assertEqual(name, (gitdir / name).read_text()) + # Make sure files were removed. + for name in self._CLEAN_FILES: + self.assertFalse((gitdir / name).exists()) + + def test_unknown(self): + """A checkout with unknown files should abort.""" + with self._simple_layout() as tempdir: + dotgit = tempdir / 'src/test/.git' + (tempdir / '.repo/projects/src/test.git/random-file').write_text('one') + (dotgit / 'random-file').write_text('two') + with self.assertRaises(error.GitError): + project.Project._MigrateOldWorkTreeGitDir(str(dotgit)) + + # Make sure no content was actually changed. + self.assertTrue(dotgit.is_dir()) + for name in self._FILES: + self.assertTrue((dotgit / name).is_file()) + for name in self._CLEAN_FILES: + self.assertTrue((dotgit / name).is_file()) + for name in self._SYMLINKS: + self.assertTrue((dotgit / name).is_symlink()) -- cgit v1.2.3-54-g00ecf