Add Repository.expand_id()

As we allow users of the library to use short strings, we must expand
them. This is however only available through a function in C. Expose
that function from the repository to allow python code to get a full id
from a short hex string.
This commit is contained in:
Carlos Martín Nieto 2014-08-28 13:18:19 +02:00
parent 7f21f6eb63
commit 1361b2cce9
3 changed files with 25 additions and 0 deletions

@ -1433,6 +1433,24 @@ Repository_reset(Repository *self, PyObject* args)
Py_RETURN_NONE;
}
PyDoc_STRVAR(Repository_expand_id__doc__,
"expand_id(hex) -> Oid\n"
"\n"
"Expand a string into a full Oid according to the objects in this repsitory.\n");
PyObject *
Repository_expand_id(Repository *self, PyObject *py_hex)
{
git_oid oid;
int err;
err = py_oid_to_git_oid_expand(self->repo, py_hex, &oid);
if (err < 0)
return NULL;
return git_oid_to_python(&oid);
}
PyMethodDef Repository_methods[] = {
METHOD(Repository, create_blob, METH_VARARGS),
METHOD(Repository, create_blob_fromworkdir, METH_VARARGS),
@ -1461,6 +1479,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, listall_branches, METH_VARARGS),
METHOD(Repository, create_branch, METH_VARARGS),
METHOD(Repository, reset, METH_VARARGS),
METHOD(Repository, expand_id, METH_O),
METHOD(Repository, _from_c, METH_VARARGS),
METHOD(Repository, _disown, METH_NOARGS),
{NULL}

1
test/data/testrepo Submodule

@ -0,0 +1 @@
Subproject commit 2be5719152d4f82c7302b1c0932d8e5f0a4a0e98

@ -156,6 +156,11 @@ class RepositoryTest(utils.BareRepoTestCase):
commit.message)
self.assertRaises(ValueError, self.repo.__getitem__, too_short_prefix)
def test_expand_id(self):
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
expanded = self.repo.expand_id(commit_sha[:7])
self.assertEqual(commit_sha, expanded.hex)
@unittest.skipIf(__pypy__ is not None, "skip refcounts checks in pypy")
def test_lookup_commit_refcount(self):
start = sys.getrefcount(self.repo)