Commit: allow retrieval of the parents' ids

Don't force the user to load the parents in order to get their ids, but
expose a list of the ids directly.
This commit is contained in:
Carlos Martín Nieto
2014-01-19 15:52:01 +01:00
parent 5a80091c2d
commit 1b473b7183
2 changed files with 25 additions and 0 deletions

View File

@@ -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),
@@ -210,6 +232,7 @@ PyGetSetDef Commit_getseters[] = {
GETTER(Commit, tree),
GETTER(Commit, tree_id),
GETTER(Commit, parents),
GETTER(Commit, parent_ids),
{NULL}
};

View File

@@ -95,6 +95,7 @@ class CommitTest(utils.BareRepoTestCase):
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
@@ -122,6 +123,7 @@ class CommitTest(utils.BareRepoTestCase):
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'