Skip to content

Commit

Permalink
Commit: allow retrieval of the parents' ids
Browse files Browse the repository at this point in the history
Don't force the user to load the parents in order to get their ids, but
expose a list of the ids directly.
  • Loading branch information
carlosmn committed Jan 19, 2014
1 parent 5a80091 commit 1b473b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ Commit_parents__get__(Commit *self)
return list;
}

PyDoc_STRVAR(Commit_parent_ids__doc__, "The list of parent commits' ids.");

PyObject *
Commit_parent_ids__get__(Commit *self)
{
unsigned int i, parent_count;
const git_oid *id;
PyObject *list;

parent_count = git_commit_parentcount(self->commit);
list = PyList_New(parent_count);
if (!list)
return NULL;

for (i=0; i < parent_count; i++) {
id = git_commit_parent_id(self->commit, i);
PyList_SET_ITEM(list, i, git_oid_to_python(id));
}

return list;
}

PyGetSetDef Commit_getseters[] = {
GETTER(Commit, message_encoding),
GETTER(Commit, message),
Expand All @@ -210,6 +232,7 @@ PyGetSetDef Commit_getseters[] = {
GETTER(Commit, tree),
GETTER(Commit, tree_id),
GETTER(Commit, parents),
GETTER(Commit, parent_ids),
{NULL}
};

Expand Down
2 changes: 2 additions & 0 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def test_new_commit(self):
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])

def test_new_commit_encoding(self):
repo = self.repo
Expand Down Expand Up @@ -122,6 +123,7 @@ def test_new_commit_encoding(self):
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])

def test_modify_commit(self):
message = 'New commit.\n\nMessage.\n'
Expand Down

0 comments on commit 1b473b7

Please sign in to comment.