diff options
author | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2012-06-05 14:55:19 +0300 |
---|---|---|
committer | Tzafrir Cohen <tzafrir@debian.org> | 2015-03-26 14:21:52 +0200 |
commit | 10828dcea31ef3131fe09b6052a1d9c8c6ed18be (patch) | |
tree | b1a8c08abddc56bcf39ab6b6b4bf2a9a4a5c7249 | |
parent | 272113558e2bab6e6a4f4a235cbe655b1c5d066c (diff) |
GitRepository: allow repo inititialization from subdir
This allows initialization of a GitRepository object, even if the
current working directory (or path given to GitRepository) is not the
top level directory of the git repository.
Don't guess the git meta data dir, but, take it as reported by git
itself.
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r-- | gbp/git/repository.py | 42 | ||||
-rw-r--r-- | tests/test_GitRepository.py | 4 |
2 files changed, 32 insertions, 14 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 848fd57..b038866 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -77,29 +77,47 @@ class GitRepository(object): all methods. """ - def _check_bare(self): - """Check whether this is a bare repository""" - out, dummy, ret = self._git_inout('rev-parse', ['--is-bare-repository'], - capture_stderr=True) + def _check_dirs(self): + """Get top level dir and git meta data dir""" + out, dummy, ret = self._git_inout('rev-parse', ['--git-dir'], + capture_stderr=True) if ret: raise GitRepositoryError( - "Failed to get repository state at '%s'" % self.path) - self._bare = False if out.strip() != 'true' else True - self._git_dir = '' if self._bare else '.git' + "Failed to get repository git dir at '%s'" % self.path) + + # Set git meta data dir + git_dir = out.strip() + if os.path.isabs(git_dir): + self._git_dir = git_dir + else: + self._git_dir = os.path.abspath(os.path.join(self.path, git_dir)) + + # Set top level dir correctly (in case repo was initialized + # from a subdir, for example) + if self.bare: + self._path = self._git_dir + else: + out, dummy, ret = self._git_inout('rev-parse', ['--show-toplevel'], + capture_stderr=True) + self._path = os.path.abspath(out.strip()) def __init__(self, path): self._path = os.path.abspath(path) - self._bare = False try: - out, dummy, ret = self._git_inout('rev-parse', ['--show-cdup'], + # Check for bare repository + out, dummy, ret = self._git_inout('rev-parse', ['--is-bare-repository'], capture_stderr=True) - if ret or out.strip(): + if ret: raise GitRepositoryError("No Git repository at '%s': '%s'" % (self.path, out)) + self._bare = False if out.strip() != 'true' else True + + self._check_dirs() + except GitRepositoryError: raise # We already have a useful error message except: - raise GitRepositoryError("No Git repository at '%s'" % self.path) - self._check_bare() + raise GitRepositoryError("No Git repository at '%s' (or any parent dir)" % self.path) + @staticmethod def __build_env(extra_env): diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py index 8803e85..6427340 100644 --- a/tests/test_GitRepository.py +++ b/tests/test_GitRepository.py @@ -679,7 +679,7 @@ def test_create_bare(): >>> bare = gbp.git.GitRepository.create(bare_dir, bare=True, description="msg") >>> bare.path == bare_dir True - >>> bare.git_dir[:-1] == bare_dir + >>> bare.git_dir == bare_dir True >>> type(bare) == gbp.git.GitRepository True @@ -700,7 +700,7 @@ def test_nonexistant(): >>> bare = gbp.git.GitRepository("/does/not/exist") Traceback (most recent call last): ... - GitRepositoryError: No Git repository at '/does/not/exist' + GitRepositoryError: No Git repository at '/does/not/exist' (or any parent dir) """ def test_create_noperm(): |