From 3fc9a840a16df03993ab6c142737b646f72e6b64 Mon Sep 17 00:00:00 2001 From: John Szakmeister Date: Sat, 26 Feb 2011 16:31:15 -0500 Subject: [PATCH] Add the parents attribute to commit. Only support getting the attribute for now. --- pygit2.c | 26 ++++++++++++++++++++++++++ test/test_commit.py | 4 ++++ 2 files changed, 30 insertions(+) diff --git a/pygit2.c b/pygit2.c index fa1a17e..355848f 100644 --- a/pygit2.c +++ b/pygit2.c @@ -644,6 +644,30 @@ Commit_set_author(Commit *commit, PyObject *value) { return 0; } +static PyObject * +Commit_get_parents(Commit *commit) +{ + unsigned int parent_count = git_commit_parentcount(commit->commit); + unsigned int i; + git_commit *parent; + Object *obj; + + PyObject *list = PyList_New(parent_count); + if (!list) + return NULL; + + for (i=0; i < parent_count; i++) + { + parent = git_commit_parent(commit->commit, i); + obj = wrap_object((git_object *)parent, commit->repo); + obj->own_obj = 0; + + PyList_SET_ITEM(list, i, (PyObject *)obj); + } + + return list; +} + static PyGetSetDef Commit_getseters[] = { {"message_short", (getter)Commit_get_message_short, NULL, "short message", NULL}, @@ -655,6 +679,8 @@ static PyGetSetDef Commit_getseters[] = { (setter)Commit_set_committer, "committer", NULL}, {"author", (getter)Commit_get_author, (setter)Commit_set_author, "author", NULL}, + {"parents", (getter)Commit_get_parents, NULL, "parents of this commit", + NULL}, {NULL} }; diff --git a/test/test_commit.py b/test/test_commit.py index 9da958c..b6c7e0f 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -42,6 +42,10 @@ class CommitTest(utils.BareRepoTestCase): def test_read_commit(self): commit = self.repo[COMMIT_SHA] self.assertEqual(COMMIT_SHA, commit.sha) + parents = commit.parents + self.assertEqual(1, len(parents)) + self.assertEqual('c2792cfa289ae6321ecf2cd5806c2194b0fd070c', + parents[0].sha) self.assertEqual('Second test data commit.', commit.message_short) self.assertEqual(('Second test data commit.\n\n' 'This commit has some additional text.\n'),