Commit: allow retrieval of the tree's ID

Let the user retrieve the ID of the commit's tree instead of having to
load the tree just to retrieve this information.
This commit is contained in:
Carlos Martín Nieto 2014-01-19 15:43:53 +01:00
parent dcc9051a8c
commit 5a80091c2d
2 changed files with 12 additions and 2 deletions

@ -32,6 +32,7 @@
#include "signature.h" #include "signature.h"
#include "commit.h" #include "commit.h"
#include "object.h" #include "object.h"
#include "oid.h"
extern PyTypeObject TreeType; extern PyTypeObject TreeType;
@ -120,7 +121,6 @@ Commit_author__get__(Commit *self)
return build_signature((Object*)self, signature, encoding); return build_signature((Object*)self, signature, encoding);
} }
PyDoc_STRVAR(Commit_tree__doc__, "The tree object attached to the commit."); PyDoc_STRVAR(Commit_tree__doc__, "The tree object attached to the commit.");
PyObject * PyObject *
@ -146,6 +146,13 @@ Commit_tree__get__(Commit *commit)
return (PyObject*)py_tree; 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."); PyDoc_STRVAR(Commit_parents__doc__, "The list of parent commits.");
@ -201,6 +208,7 @@ PyGetSetDef Commit_getseters[] = {
GETTER(Commit, committer), GETTER(Commit, committer),
GETTER(Commit, author), GETTER(Commit, author),
GETTER(Commit, tree), GETTER(Commit, tree),
GETTER(Commit, tree_id),
GETTER(Commit, parents), GETTER(Commit, parents),
{NULL} {NULL}
}; };

@ -31,7 +31,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import unittest import unittest
from pygit2 import GIT_OBJ_COMMIT, Signature from pygit2 import GIT_OBJ_COMMIT, Signature, Oid
from . import utils from . import utils
@ -92,6 +92,7 @@ class CommitTest(utils.BareRepoTestCase):
self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(committer, commit.committer)
self.assertEqualSignature(author, commit.author) self.assertEqualSignature(author, commit.author)
self.assertEqual(tree, commit.tree.hex) self.assertEqual(tree, commit.tree.hex)
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents)) self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
@ -118,6 +119,7 @@ class CommitTest(utils.BareRepoTestCase):
self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(committer, commit.committer)
self.assertEqualSignature(author, commit.author) self.assertEqualSignature(author, commit.author)
self.assertEqual(tree, commit.tree.hex) self.assertEqual(tree, commit.tree.hex)
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents)) self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(COMMIT_SHA, commit.parents[0].hex)