From 65c07d64a7b1dc85c41083c60a8082b3705154c3 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Tue, 21 Jul 2015 11:02:24 -0400 Subject: [PATCH] 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` --- git/repo/base.py | 15 +++++++++++++++ git/test/test_repo.py | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/git/repo/base.py b/git/repo/base.py index b04112353..16fb58e59 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -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) diff --git a/git/test/test_repo.py b/git/test/test_repo.py index 9c08e2e4c..bc9f3e92c 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -42,6 +42,7 @@ import sys import tempfile import shutil +import itertools from io import BytesIO @@ -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) 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