10000 Commit: allow retrieval of the parents' ids · libgit2/pygit2@1b473b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b473b7

Browse files
committed
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.
1 parent 5a80091 commit 1b473b7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/commit.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ Commit_parents__get__(Commit *self)
199199
return list;
200200
}
201201

202+
PyDoc_STRVAR(Commit_parent_ids__doc__, "The list of parent commits' ids.");
203+
204+
PyObject *
205+
Commit_parent_ids__get__(Commit *self)
206+
{
207+
unsigned int i, parent_count;
208+
const git_oid *id;
209+
PyObject *list;
210+
211+
parent_count = git_commit_parentcount(self->commit);
212+
list = PyList_New(parent_count);
213+
if (!list)
214+
return NULL;
215+
216+
for (i=0; i < parent_count; i++) {
217+
id = git_commit_parent_id(self->commit, i);
218+
PyList_SET_ITEM(list, i, git_oid_to_python(id));
219+
}
220+
221+
return list;
222+
}
223+
202224
PyGetSetDef Commit_getseters[] = {
203225
GETTER(Commit, message_encoding),
204226
GETTER(Commit, message),
@@ -210,6 +232,7 @@ PyGetSetDef Commit_getseters[] = {
210232
GETTER(Commit, tree),
211233
GETTER(Commit, tree_id),
212234
GETTER(Commit, parents),
235+
GETTER(Commit, parent_ids),
213236
{NULL}
214237
};
215238

test/test_commit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def test_new_commit(self):
9595
self.assertEqual(Oid(hex=tree), commit.tree_id)
9696
self.assertEqual(1, len(commit.parents))
9797
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
98+
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
9899

99100
def test_new_commit_encoding(self):
100101
repo = self.repo
@@ -122,6 +123,7 @@ def test_new_commit_encoding(self):
122123
self.assertEqual(Oid(hex=tree), commit.tree_id)
123124
self.assertEqual(1, len(commit.parents))
124125
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
126+
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
125127

126128
def test_modify_commit(self):
127129
message = 'New commit.\n\nMessage.\n'

0 commit comments

Comments
 (0)
0