diff --git a/pygit2.c b/pygit2.c index e781744..62e2933 100644 --- a/pygit2.c +++ b/pygit2.c @@ -959,9 +959,15 @@ Object_dealloc(Object* self) } static PyObject * -Object_get_type(Object *self) +Object_get_oid(Object *self) { - return PyInt_FromLong(git_object_type(self->obj)); + const git_oid *oid; + + oid = git_object_id(self->obj); + if (!oid) + Py_RETURN_NONE; + + return PyString_FromStringAndSize(oid->id, GIT_OID_RAWSZ); } static PyObject * @@ -976,6 +982,12 @@ Object_get_sha(Object *self) return git_oid_to_py_str(oid); } +static PyObject * +Object_get_type(Object *self) +{ + return PyInt_FromLong(git_object_type(self->obj)); +} + static PyObject * Object_read_raw(Object *self) { @@ -1007,8 +1019,9 @@ cleanup: } static PyGetSetDef Object_getseters[] = { - {"type", (getter)Object_get_type, NULL, "type number", NULL}, + {"oid", (getter)Object_get_oid, NULL, "object id", NULL}, {"sha", (getter)Object_get_sha, NULL, "hex SHA", NULL}, + {"type", (getter)Object_get_type, NULL, "type number", NULL}, {NULL} }; diff --git a/test/test_blob.py b/test/test_blob.py index 42d92d1..50a9eb5 100644 --- a/test/test_blob.py +++ b/test/test_blob.py @@ -29,6 +29,7 @@ from __future__ import absolute_import from __future__ import unicode_literals +from binascii import b2a_hex import unittest import pygit2 @@ -44,6 +45,9 @@ class BlobTest(utils.BareRepoTestCase): def test_read_blob(self): blob = self.repo[BLOB_SHA] + self.assertEqual(blob.sha, BLOB_SHA) + sha = b2a_hex(blob.oid).decode('ascii') + self.assertEqual(sha, BLOB_SHA) self.assertTrue(isinstance(blob, pygit2.Blob)) self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type) self.assertEqual(b'a contents\n', blob.data)