Add Object.oid to get the raw object id

This commit is contained in:
J. David Ibáñez 2011-09-02 16:12:05 +02:00
parent 54dd6714f2
commit 11ff1842b7
2 changed files with 20 additions and 3 deletions

@ -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}
};

@ -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)