From 987dceee2471e1611f0fe8a0cfe43d9986b1c7c1 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 11 Nov 2014 15:05:50 +0000 Subject: Check that directory passed to GitDirectory is a git repo Previously, creating a GitDirectory object for something that wasn't a Git repository would succeed, but most operations would raise a cliapp.AppException with the following error message: ERROR: Command failed: git config -z core.bare Now, the constructor will raise a NoGitRepoError if the directory isn't a Git repo, which (unless handled) gives the user the following sort of message: ERROR: Directory /src/ws/definitions is not a Git repository --- morphlib/gitdir.py | 17 ++++++++++++++++- morphlib/gitdir_tests.py | 26 +++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index a0f35eef..ed71e422 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -24,6 +24,13 @@ import re import morphlib +class NoGitRepoError(cliapp.AppException): + + def __init__(self, dirname): + cliapp.AppException.__init__( + self, 'Directory %s is not a Git repository. ' % dirname) + + class NoWorkingTreeError(cliapp.AppException): def __init__(self, repo): @@ -375,6 +382,8 @@ class GitDirectory(object): self.dirname = dirname self._config = {} + self._ensure_is_git_repo() + def _runcmd(self, argv, **kwargs): '''Run a command at the root of the git directory. @@ -390,6 +399,13 @@ class GitDirectory(object): def _runcmd_unchecked(self, *args, **kwargs): return cliapp.runcmd_unchecked(*args, cwd=self.dirname, **kwargs) + def _ensure_is_git_repo(self): + try: + self._runcmd(['git', 'rev-parse', '--git-dir']) + except cliapp.AppException as e: + # Exact error is logged already by the runcmd() function. + raise NoGitRepoError(self.dirname) + def checkout(self, branch_name): # pragma: no cover '''Check out a git branch.''' morphlib.git.gitcmd(self._runcmd, 'checkout', branch_name) @@ -752,4 +768,3 @@ def clone_from_cached_repo(cached_repo, dirname, ref): # pragma: no cover cached_repo.clone_checkout(ref, dirname) return GitDirectory(dirname) - diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 55b3caa5..fe71ab3a 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -34,17 +34,24 @@ class GitDirectoryTests(unittest.TestCase): def tearDown(self): shutil.rmtree(self.tempdir) - def fake_git_clone(self): + def empty_git_directory(self): os.mkdir(self.dirname) - os.mkdir(os.path.join(self.dirname, '.git')) + return morphlib.gitdir.init(self.dirname) + + def test_ensures_is_a_git_repo(self): + self.assertRaises(OSError, + morphlib.gitdir.GitDirectory, self.dirname) + + os.mkdir(self.dirname) + self.assertRaises(morphlib.gitdir.NoGitRepoError, + morphlib.gitdir.GitDirectory, self.dirname) def test_has_dirname_attribute(self): - self.fake_git_clone() - gitdir = morphlib.gitdir.GitDirectory(self.dirname) + gitdir = self.empty_git_directory() self.assertEqual(gitdir.dirname, self.dirname) def test_can_search_for_top_directory(self): - self.fake_git_clone() + self.empty_git_directory() path_inside_working_tree = os.path.join(self.dirname, 'a', 'b', 'c') os.makedirs(path_inside_working_tree) @@ -54,20 +61,17 @@ class GitDirectoryTests(unittest.TestCase): self.assertEqual(gitdir.dirname, self.dirname) def test_runs_command_in_right_directory(self): - self.fake_git_clone() - gitdir = morphlib.gitdir.GitDirectory(self.dirname) + gitdir = self.empty_git_directory() output = gitdir._runcmd(['pwd']) self.assertEqual(output.strip(), self.dirname) def test_sets_and_gets_configuration(self): - os.mkdir(self.dirname) - gitdir = morphlib.gitdir.init(self.dirname) + gitdir = self.empty_git_directory() gitdir.set_config('foo.bar', 'yoyo') self.assertEqual(gitdir.get_config('foo.bar'), 'yoyo') def test_gets_index(self): - os.mkdir(self.dirname) - gitdir = morphlib.gitdir.init(self.dirname) + gitdir = self.empty_git_directory() self.assertIsInstance(gitdir.get_index(), morphlib.gitindex.GitIndex) -- cgit v1.2.1