From: Daniel Dehennin Date: Sun, 13 May 2012 18:37:11 +0000 (+0200) Subject: gbp.git.repository: Add a "git merge-base" wrapper X-Git-Tag: debian/0.6.0_git20120524~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c57d4af675910ec151cf982532db0f877aef413f;p=tools%2Fgit-buildpackage.git gbp.git.repository: Add a "git merge-base" wrapper Closes: #672642 Signed-off-by: Guido Günther --- 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