Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement is_ancestor #322

Merged
merged 1 commit into from
Jul 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,21 @@ def merge_base(self, *rev, **kwargs):

return res

def is_ancestor(self, ancestor_rev, rev):
"""Check if a commit is an ancestor of another

:param ancestor_rev: Rev which should be an ancestor
:param rev: Rev to test against ancestor_rev
:return: ``True``, ancestor_rev is an accestor to rev.
"""
try:
self.git.merge_base(ancestor_rev, rev, is_ancestor=True)
except GitCommandError as err:
if err.status == 1:
return False
raise
return True

def _get_daemon_export(self):
filename = join(self.git_dir, self.DAEMON_EXPORT_FILE)
return os.path.exists(filename)
Expand Down
14 changes: 14 additions & 0 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import sys
import tempfile
import shutil
import itertools
from io import BytesIO


Expand Down Expand Up @@ -765,3 +766,16 @@ def test_merge_base(self):

# Test for no merge base - can't do as we have
self.failUnlessRaises(GitCommandError, repo.merge_base, c1, 'ffffff')

def test_is_ancestor(self):
repo = self.rorepo
c1 = 'f6aa8d1'
c2 = '763ef75'
self.assertTrue(repo.is_ancestor(c1, c1))
self.assertTrue(repo.is_ancestor("master", "master"))
self.assertTrue(repo.is_ancestor(c1, c2))
self.assertTrue(repo.is_ancestor(c1, "master"))
self.assertFalse(repo.is_ancestor(c2, c1))
self.assertFalse(repo.is_ancestor("master", c1))
for i, j in itertools.permutations([c1, 'ffffff', ''], r=2):
self.assertRaises(GitCommandError, repo.is_ancestor, i, j)