Skip to content

Commit acc559f

Browse files
author
Sandy Carter
committed
Implement is_ancestor
Wrap `git merge-base --is-ancestor` into its own function because it acts as a boolean check unlike base `git merge-base call`
1 parent f360ecd commit acc559f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

git/repo/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,21 @@ def merge_base(self, *rev, **kwargs):
504504

505505
return res
506506

507+
def is_ancestor(self, ancestor_rev, rev):
508+
"""Check if a commit is an ancestor of another
509+
510+
:param ancestor_rev: Rev which should be an ancestor
511+
:param rev: Rev to test against ancestor_rev
512+
:return: ``True``, ancestor_rev is an accestor to rev.
513+
"""
514+
try:
515+
self.git.merge_base(ancestor_rev, rev, is_ancestor=True)
516+
except GitCommandError as err:
517+
if err.status == 1:
518+
return False
519+
raise
520+
return True
521+
507522
def _get_daemon_export(self):
508523
filename = join(self.git_dir, self.DAEMON_EXPORT_FILE)
509524
return os.path.exists(filename)

git/test/test_repo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import sys
4343
import tempfile
4444
import shutil
45+
import itertools
4546
from io import BytesIO
4647

4748

@@ -765,3 +766,17 @@ def test_merge_base(self):
765766

766767
# Test for no merge base - can't do as we have
767768
self.failUnlessRaises(GitCommandError, repo.merge_base, c1, 'ffffff')
769+
770+
def test_is_ancestor(self):
771+
repo = self.rorepo
772+
c1 = 'f6aa8d1'
773+
c2 = '763ef75'
774+
self.assertTrue(repo.is_ancestor(c1, c1))
775+
self.assertTrue(repo.is_ancestor("master", "master"))
776+
self.assertTrue(repo.is_ancestor(c1, c2))
777+
self.assertTrue(repo.is_ancestor(c1, "master"))
778+
self.assertFalse(repo.is_ancestor(c2, c1))
779+
self.assertFalse(repo.is_ancestor("master", c1))
780+
for i, j in itertools.permutations([c1, 'ffffff', ''], r=2):
781+
with self.assertRaises(GitCommandError):
782+
repo.merge_base(c1, 'ffffff')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy