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

Add support for git_graph_descendant_of #816

Merged
merged 5 commits into from
Sep 15, 2018
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
1 change: 1 addition & 0 deletions docs/repository.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Below there are some general attributes and methods:
.. automethod:: pygit2.Repository.read
.. automethod:: pygit2.Repository.write
.. automethod:: pygit2.Repository.ahead_behind
.. automethod:: pygit2.Repository.descendant_of
.. automethod:: pygit2.Repository.create_reference
.. automethod:: pygit2.Repository.describe
.. automethod:: pygit2.Repository.path_is_ignored
Expand Down
25 changes: 20 additions & 5 deletions pygit2/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,13 +1130,19 @@ def revert_commit(self, revert_commit, our_commit, mainline=0):


class Branches(object):
def __init__(self, repository, flag=GIT_BRANCH_ALL):
def __init__(self, repository, flag=GIT_BRANCH_ALL, commit=None):
self._repository = repository
self._flag = flag
if commit is not None:
if isinstance(commit, Commit):
commit = commit.id
elif not isinstance(commit, Oid):
commit = self._repository.expand_id(commit)
self._commit = commit

if flag == GIT_BRANCH_ALL:
self.local = Branches(repository, flag=GIT_BRANCH_LOCAL)
self.remote = Branches(repository, flag=GIT_BRANCH_REMOTE)
self.local = Branches(repository, flag=GIT_BRANCH_LOCAL, commit=commit)
self.remote = Branches(repository, flag=GIT_BRANCH_REMOTE, commit=commit)

def __getitem__(self, name):
branch = None
Expand All @@ -1146,7 +1152,7 @@ def __getitem__(self, name):
if branch is None and self._flag & GIT_BRANCH_REMOTE:
branch = self._repository.lookup_branch(name, GIT_BRANCH_REMOTE)

if branch is None:
if branch is None or not self._valid(branch):
raise KeyError('Branch not found: {}'.format(name))

return branch
Expand All @@ -1159,14 +1165,23 @@ def get(self, key):

def __iter__(self):
for branch_name in self._repository.listall_branches(self._flag):
yield branch_name
if self._commit is None or self.get(branch_name) is not None:
yield branch_name

def create(self, name, commit, force=False):
return self._repository.create_branch(name, commit, force)

def delete(self, name):
self[name].delete()

def _valid(self, branch):
return (self._commit is None or branch.target == self._commit or
self._repository.descendant_of(branch.target, self._commit))

def with_commit(self, commit):
assert self._commit is None
return Branches(self._repository, self._flag, commit)

def __contains__(self, name):
return self.get(name) is not None

Expand Down
30 changes: 30 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,35 @@ Repository_workdir__set__(Repository *self, PyObject *py_workdir)
return 0;
}

PyDoc_STRVAR(Repository_descendant_of__doc__,
"descendant_of(oid, oid) -> bool\n"
"\n"
"Determine if the second commit is a descendant of the first commit.\n"
"Note that a commit is not considered a descendant of itself.");

PyObject *
Repository_descendant_of(Repository *self, PyObject *args)
{
PyObject *value1;
PyObject *value2;
git_oid oid1;
git_oid oid2;
int err;

if (!PyArg_ParseTuple(args, "OO", &value1, &value2))
return NULL;

err = py_oid_to_git_oid_expand(self->repo, value1, &oid1);
if (err < 0)
return NULL;

err = py_oid_to_git_oid_expand(self->repo, value2, &oid2);
if (err < 0)
return NULL;

return PyBool_FromLong(git_graph_descendant_of(self->repo, &oid1, &oid2));
}

PyDoc_STRVAR(Repository_merge_base__doc__,
"merge_base(oid, oid) -> Oid\n"
"\n"
Expand Down Expand Up @@ -1884,6 +1913,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, create_tag, METH_VARARGS),
METHOD(Repository, TreeBuilder, METH_VARARGS),
METHOD(Repository, walk, METH_VARARGS),
METHOD(Repository, descendant_of, METH_VARARGS),
METHOD(Repository, merge_base, METH_VARARGS),
METHOD(Repository, merge_analysis, METH_O),
METHOD(Repository, merge, METH_O),
Expand Down
20 changes: 20 additions & 0 deletions test/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
LAST_COMMIT = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
I18N_LAST_COMMIT = '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'
ORIGIN_MASTER_COMMIT = '784855caf26449a1914d2cf62d12b9374d76ae78'
EXCLUSIVE_MASTER_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
SHARED_COMMIT = '4ec4389a8068641da2d6578db0419484972284c8'


class BranchesObjectTestCase(utils.RepoTestCase):
Expand Down Expand Up @@ -116,6 +118,24 @@ def test_branch_name(self):
self.assertEqual(branch.branch_name, 'i18n')
self.assertEqual(branch.name, 'refs/heads/i18n')

def test_with_commit(self):
branches = self.repo.branches.with_commit(EXCLUSIVE_MASTER_COMMIT)
self.assertEqual(sorted(branches), ['master'])
self.assertTrue(branches.get('i18n') is None)
self.assertEqual(branches['master'].branch_name, 'master')

branches = self.repo.branches.with_commit(SHARED_COMMIT)
self.assertEqual(sorted(branches), ['i18n', 'master'])

branches = self.repo.branches.with_commit(LAST_COMMIT)
self.assertEqual(sorted(branches), ['master'])

branches = self.repo.branches.with_commit(self.repo[LAST_COMMIT])
self.assertEqual(sorted(branches), ['master'])

branches = self.repo.branches.remote.with_commit(LAST_COMMIT)
self.assertEqual(sorted(branches), [])


class BranchesObjectEmptyRepoTestCase(utils.EmptyRepoTestCase):
def setUp(self):
Expand Down
14 changes: 14 additions & 0 deletions test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ def test_merge_base(self):

self.assertEqual(None, self.repo.merge_base(indep, commit))

def test_descendent_of(self):
self.assertFalse(self.repo.descendant_of(
'5ebeeebb320790caf276b9fc8b24546d63316533',
'4ec4389a8068641da2d6578db0419484972284c8'))
self.assertFalse(self.repo.descendant_of(
'5ebeeebb320790caf276b9fc8b24546d63316533',
'5ebeeebb320790caf276b9fc8b24546d63316533'))
self.assertTrue(self.repo.descendant_of(
'5ebeeebb320790caf276b9fc8b24546d63316533',
'acecd5ea2924a4b900e7e149496e1f4b57976e51'))
self.assertFalse(self.repo.descendant_of(
'acecd5ea2924a4b900e7e149496e1f4b57976e51',
'5ebeeebb320790caf276b9fc8b24546d63316533'))

def test_ahead_behind(self):
ahead, behind = self.repo.ahead_behind('5ebeeebb320790caf276b9fc8b24546d63316533',
'4ec4389a8068641da2d6578db0419484972284c8')
Expand Down