Merge commit 'byron/repo_write_improved~2'
This commit is contained in:
21
pygit2.c
21
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;
|
||||
@@ -318,11 +324,14 @@ Repository_read(Repository *self, PyObject *py_hex) {
|
||||
if (err < 0)
|
||||
return Error_set_py_obj(err, py_hex);
|
||||
|
||||
return Py_BuildValue(
|
||||
PyObject* tuple = Py_BuildValue(
|
||||
"(ns#)",
|
||||
git_odb_object_type(obj),
|
||||
git_odb_object_data(obj),
|
||||
git_odb_object_size(obj));
|
||||
|
||||
git_odb_object_close(obj);
|
||||
return tuple;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
@@ -791,6 +800,8 @@ Object_read_raw(Object *self) {
|
||||
git_odb_object_data(obj),
|
||||
git_odb_object_size(obj));
|
||||
|
||||
git_odb_object_close(obj);
|
||||
|
||||
cleanup:
|
||||
Py_XDECREF(py_sha);
|
||||
return result;
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user