allow 20 byte sha strings

Adjusted py_str_to_git_oid to allow 20 byte binary strings. This improves usability to other clients which already store their shas as binary strings, increasing overall performance as they won't have to convert them into hex beforehand, which would have to be converted back to binary form by libgit2 in turn.
Adjusted tests to accept binary shas.
This commit is contained in:
Sebastian Thiel 2011-07-08 11:38:12 +02:00
parent 84cb4616b7
commit cf37c15059
2 changed files with 15 additions and 8 deletions

@ -230,16 +230,22 @@ wrap_reference(git_reference * c_reference)
static int
py_str_to_git_oid(PyObject *py_str, git_oid *oid) {
char *hex;
const char *hex_or_bin;
int err;
hex = PyString_AsString(py_str);
if (hex == NULL) {
hex_or_bin = PyString_AsString(py_str);
if (hex_or_bin == NULL) {
Error_set_py_obj(GIT_ENOTOID, py_str);
return 0;
}
err = git_oid_fromstr(oid, hex);
if (PyString_Size(py_str) == 20) {
git_oid_fromraw(oid, (const unsigned char*)hex_or_bin);
err = 0;
} else {
err = git_oid_fromstr(oid, hex_or_bin);
}
if (err < 0) {
Error_set_py_obj(err, py_str);
return 0;

@ -44,10 +44,11 @@ class RepositoryTest(utils.BareRepoTestCase):
def test_read(self):
self.assertRaises(TypeError, self.repo.read, 123)
self.assertRaises(ValueError, self.repo.read, A_BIN_SHA)
self.assertRaisesWithArg(KeyError, '1' * 40, self.repo.read, '1' * 40)
ab = self.repo.read(A_BIN_SHA)
a = self.repo.read(A_HEX_SHA)
self.assertEqual(ab, a)
self.assertEqual((pygit2.GIT_OBJ_BLOB, 'a contents\n'), a)
a2 = self.repo.read('7f129fd57e31e935c6d60a0c794efe4e6927664b')
@ -55,13 +56,13 @@ class RepositoryTest(utils.BareRepoTestCase):
def test_contains(self):
self.assertRaises(TypeError, lambda: 123 in self.repo)
self.assertRaises(ValueError, lambda: A_BIN_SHA in self.repo)
self.assertTrue(A_BIN_SHA in self.repo)
self.assertTrue(A_HEX_SHA in self.repo)
self.assertFalse('a' * 40 in self.repo)
def test_lookup_blob(self):
self.assertRaises(TypeError, lambda: self.repo[123])
self.assertRaises(ValueError, lambda: self.repo[A_BIN_SHA])
self.assertEqual(self.repo[A_BIN_SHA].sha, A_HEX_SHA)
a = self.repo[A_HEX_SHA]
self.assertEqual('a contents\n', a.read_raw())
self.assertEqual(A_HEX_SHA, a.sha)