aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Dehennin <daniel.dehennin@baby-gnu.org>2012-05-13 20:37:11 +0200
committerGuido Günther <agx@sigxcpu.org>2012-05-14 13:16:15 +0200
commitc57d4af675910ec151cf982532db0f877aef413f (patch)
tree2e7a49ace9e6f5991f62c773a5574b0f2c665471
parentae850dae9a1dfbe9233275dc11c5f8bb517070e4 (diff)
gbp.git.repository: Add a "git merge-base" wrapper
Closes: #672642 Signed-off-by: Guido Günther <agx@sigxcpu.org>
-rw-r--r--gbp/git/repository.py20
-rw-r--r--tests/test_GitRepository.py18
2 files changed, 38 insertions, 0 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py
index 2cc5eda..16bad60 100644
--- a/gbp/git/repository.py
+++ b/gbp/git/repository.py
@@ -299,6 +299,26 @@ class GitRepository(object):
remote += merge.replace("refs/heads","", 1)
return remote
+ def get_merge_base(self, commit1, commit2):
+ """
+ Get the common ancestor between two commits
+
+ @param commit1: commit SHA1 or name of a branch or tag
+ @type commit1: C{str}
+ @param commit2: commit SHA1 or name of a branch or tag
+ @type commit2: C{str}
+ @return: SHA1 of the common ancestor
+ @rtype: C{str}
+ """
+ args = GitArgs()
+ args.add(commit1)
+ args.add(commit2)
+ sha1, stderr, ret = self._git_inout('merge-base', args.args, capture_stderr=True)
+ if not ret:
+ return sha1.strip()
+ else:
+ raise GitRepositoryError("Failed to get common ancestor: %s" % stderr.strip())
+
def merge(self, commit, verbose=False, edit=False):
"""
Merge changes from the named commit into the current branch
diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py
index 4367f93..2157d80 100644
--- a/tests/test_GitRepository.py
+++ b/tests/test_GitRepository.py
@@ -631,6 +631,24 @@ def test_update_submodules():
>>> repo.update_submodules()
"""
+def test_get_merge_base():
+ """
+ Find the common ancestor of two objects
+
+ Methods tested:
+ - L{gbp.git.GitRepository.get_merge_bsae}
+
+ >>> import gbp.git
+ >>> repo = gbp.git.GitRepository(repo_dir)
+ >>> sha1 = repo.get_merge_base('master', 'foo')
+ >>> len(sha1)
+ 40
+ >>> repo.get_merge_base('master', 'doesnotexist')
+ Traceback (most recent call last):
+ ...
+ GitRepositoryError: Failed to get common ancestor: fatal: Not a valid object name doesnotexist
+ """
+
def test_teardown():
"""
Perform the teardown