diff options
author | Guido Günther <agx@sigxcpu.org> | 2017-01-22 17:34:41 +0100 |
---|---|---|
committer | Michael Stapelberg <stapelberg@test.invalid> | 2017-01-22 18:21:19 +0100 |
commit | d6aae91e6b65564757eb8380d5e9112f5175417e (patch) | |
tree | 70a5b6d836ef1576ad20d94583d61aec1d2804cd | |
parent | 8bf30ffeb8431583dbd447153527a9ae53cdc55a (diff) |
GitRepository: allow to init git repository from a subdir
-rw-r--r-- | gbp/git/repository.py | 33 | ||||
-rw-r--r-- | tests/doctests/test_GitRepository.py | 11 |
2 files changed, 36 insertions, 8 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 78b15d39..300f92ce 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -98,18 +98,33 @@ class GitRepository(object): else: self._git_dir = os.path.abspath(os.path.join(self.path, git_dir)) - def __init__(self, path): - self._path = os.path.abspath(path) - self._bare = False + def _check_repo(self, path, toplevel): try: out, dummy, ret = self._git_inout('rev-parse', ['--show-cdup'], + cwd=path, capture_stderr=True) - if ret or out.strip(): - raise GitRepositoryError("No Git repository at '%s': '%s'" % (self.path, out)) + cdup = out.strip() + if ret: + raise GitRepositoryError("No Git repository at '%s': '%s'" % (path, cdup)) + if toplevel and cdup: + raise GitRepositoryError("Not the toplevel of a Git repository at '%s': '%s'" % (path, cdup)) + ret = os.path.abspath(os.path.join(path, cdup or '.')) except GitRepositoryError: raise # We already have a useful error message - except: - raise GitRepositoryError("No Git repository at '%s'" % self.path) + except Exception as e: + raise GitRepositoryError("No Git repository at '%s'" % path) + return ret + + def __init__(self, path, toplevel=True): + """ + @param path: path to git repo (or subdir) + @type path: C{str} + @param toplevel: whether path points to the toplevel dir of + git repository + @type toplevel: C{bool} + """ + self._bare = False + self._path = self._check_repo(path, toplevel) self._check_bare() self._get_git_dir() @@ -132,7 +147,7 @@ class GitRepository(object): @type args: C{list} @param extra_env: extra environment variables to pass @type extra_env: C{dict} - @param cwd: directory to swith to when running the command, defaults to I{self.path} + @param cwd: directory to switch to when running the command, defaults to I{self.path} @type cwd: C{str} @return: stdout, return code @rtype: C{tuple} of C{list} of C{str} and C{int} @@ -166,6 +181,8 @@ class GitRepository(object): @type args: C{list} @param extra_env: extra environment variables to pass @type extra_env: C{dict} + @param cwd: directory to switch to when running the command, defaults to I{self.path} + @type cwd: C{str} @param capture_stderr: whether to capture stderr @type capture_stderr: C{bool} @return: stdout, stderr, return code diff --git a/tests/doctests/test_GitRepository.py b/tests/doctests/test_GitRepository.py index 30a846ee..30ac385e 100644 --- a/tests/doctests/test_GitRepository.py +++ b/tests/doctests/test_GitRepository.py @@ -72,6 +72,17 @@ def test_empty(): """ +def test_subdir(): + """ + Make surewe can init repos froma subdir + >>> import gbp.git, os + >>> os.mkdir(os.path.join(dirs['repo'], 'subdir')) + >>> repo = gbp.git.GitRepository(os.path.join(dirs['repo'], 'subdir'), toplevel=False) + >>> repo.path == dirs['repo'] + True + """ + + def test_add_files(): """ Add some dummy data |