diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-11 15:05:50 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-17 12:55:49 +0000 |
commit | 987dceee2471e1611f0fe8a0cfe43d9986b1c7c1 (patch) | |
tree | cd01ae82e02b7f1dd2afcb4b77decea1006034f4 /morphlib/gitdir.py | |
parent | db759870b43782bf86801a6b55d14af24445b121 (diff) | |
download | morph-987dceee2471e1611f0fe8a0cfe43d9986b1c7c1.tar.gz |
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
Diffstat (limited to 'morphlib/gitdir.py')
-rw-r--r-- | morphlib/gitdir.py | 17 |
1 files changed, 16 insertions, 1 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) - |