From cf37c150593bdd6a57e4184ca10502aaf7fc9ba1 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 8 Jul 2011 11:38:12 +0200 Subject: [PATCH] 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. --- pygit2.c | 16 +++++++++++----- test/test_repository.py | 7 ++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pygit2.c b/pygit2.c index e4761f7..0de7a58 100644 --- a/pygit2.c +++ b/pygit2.c @@ -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; diff --git a/test/test_repository.py b/test/test_repository.py index b8573bc..d584e46 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -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)