repository: add Repository.revparse_single().

This provides access to libgit2's 'git_revparse_single'.
This commit is contained in:
W. Trevor King
2012-09-13 17:09:12 -04:00
parent 5464eaaf50
commit 0238fb72df
3 changed files with 38 additions and 0 deletions

View File

@@ -270,6 +270,12 @@ The interface for RefLogEntry::
RefLogEntry.oid_old -- oid of old reference RefLogEntry.oid_old -- oid of old reference
RefLogEntry.oid_new -- oid of new reference RefLogEntry.oid_new -- oid of new reference
Revision parsing
================
You can use any of the fancy `<rev>` forms supported by libgit2::
>>> commit = repo.revparse_single('HEAD^')
Revision walking Revision walking
================= =================

View File

@@ -198,6 +198,30 @@ Repository_getitem(Repository *self, PyObject *value)
return lookup_object_prefix(self, &oid, len, GIT_OBJ_ANY); return lookup_object_prefix(self, &oid, len, GIT_OBJ_ANY);
} }
PyObject *
Repository_revparse_single(Repository *self, PyObject *py_spec)
{
git_object *c_obj;
char *c_spec;
char *encoding = "ascii";
int err;
/* 1- Get the C revision spec */
c_spec = py_str_to_c_str(py_spec, encoding);
if (c_spec == NULL)
return NULL;
/* 2- Lookup */
err = git_revparse_single(&c_obj, self->repo, c_spec);
if (err < 0) {
PyObject *err_obj = Error_set_str(err, c_spec);
free(c_spec);
return err_obj;
}
return wrap_object(c_obj, self);
}
git_odb_object * git_odb_object *
Repository_read_raw(git_repository *repo, const git_oid *oid, size_t len) Repository_read_raw(git_repository *repo, const git_oid *oid, size_t len)
{ {
@@ -792,6 +816,10 @@ PyMethodDef Repository_methods[] = {
"Return a list with all the references in the repository."}, "Return a list with all the references in the repository."},
{"lookup_reference", (PyCFunction)Repository_lookup_reference, METH_O, {"lookup_reference", (PyCFunction)Repository_lookup_reference, METH_O,
"Lookup a reference by its name in a repository."}, "Lookup a reference by its name in a repository."},
{"revparse_single", (PyCFunction)Repository_revparse_single, METH_O,
"Find an object, as specified by a revision string. See "
"`man gitrevisions`, or the documentation for `git rev-parse` for "
"information on the syntax accepted."},
{"create_blob", (PyCFunction)Repository_create_blob, {"create_blob", (PyCFunction)Repository_create_blob,
METH_VARARGS, METH_VARARGS,
"Create a new blob from memory"}, "Create a new blob from memory"},

View File

@@ -40,6 +40,7 @@ from . import utils
HEAD_SHA = '2cdae28389c059815e951d0bb9eed6533f61a46b' HEAD_SHA = '2cdae28389c059815e951d0bb9eed6533f61a46b'
PARENT_SHA = '5fe808e8953c12735680c257f56600cb0de44b10' # HEAD^
A_HEX_SHA = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16' A_HEX_SHA = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
A_BIN_SHA = binascii.unhexlify(A_HEX_SHA.encode('ascii')) A_BIN_SHA = binascii.unhexlify(A_HEX_SHA.encode('ascii'))
@@ -127,6 +128,9 @@ class RepositoryTest(utils.BareRepoTestCase):
def test_get_workdir(self): def test_get_workdir(self):
self.assertEqual(self.repo.workdir, None) self.assertEqual(self.repo.workdir, None)
def test_revparse_single(self):
parent = self.repo.revparse_single('HEAD^')
self.assertEqual(parent.hex, PARENT_SHA)
class RepositoryTest_II(utils.RepoTestCase): class RepositoryTest_II(utils.RepoTestCase):