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 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'morphlib/gitdir.py') 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) - -- cgit v1.2.1