diff --git a/src/commit.c b/src/commit.c index 725a3c44e..258f082ba 100644 --- a/src/commit.c +++ b/src/commit.c @@ -32,6 +32,7 @@ #include "signature.h" #include "commit.h" #include "object.h" +#include "oid.h" extern PyTypeObject TreeType; @@ -120,7 +121,6 @@ Commit_author__get__(Commit *self) return build_signature((Object*)self, signature, encoding); } - PyDoc_STRVAR(Commit_tree__doc__, "The tree object attached to the commit."); PyObject * @@ -146,6 +146,13 @@ Commit_tree__get__(Commit *commit) return (PyObject*)py_tree; } +PyDoc_STRVAR(Commit_tree_id__doc__, "The id of the tree attached to the commit."); + +PyObject * +Commit_tree_id__get__(Commit *commit) +{ + return git_oid_to_python(git_commit_tree_id(commit->commit)); +} PyDoc_STRVAR(Commit_parents__doc__, "The list of parent commits."); @@ -192,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), @@ -201,7 +230,9 @@ PyGetSetDef Commit_getseters[] = { GETTER(Commit, committer), GETTER(Commit, author), GETTER(Commit, tree), + GETTER(Commit, tree_id), GETTER(Commit, parents), + GETTER(Commit, parent_ids), {NULL} }; diff --git a/test/test_commit.py b/test/test_commit.py index 85f3d2350..a944c40d6 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -31,7 +31,7 @@ from __future__ import unicode_literals import unittest -from pygit2 import GIT_OBJ_COMMIT, Signature +from pygit2 import GIT_OBJ_COMMIT, Signature, Oid from . import utils @@ -92,8 +92,10 @@ def test_new_commit(self): self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) + 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 @@ -118,8 +120,10 @@ def test_new_commit_encoding(self): self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) + 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'