Return Oid wherever we returned raw oid (bytes) before

Changes:

- Return Oid wherever we returned raw oid (bytes) before
- Now py_str_to_git_oid accepts Oid objects
- Add ability to compare two Oid objects
This commit is contained in:
J. David Ibáñez
2013-04-14 12:26:22 +02:00
parent f8544cc514
commit 406c317572
16 changed files with 77 additions and 44 deletions

View File

@@ -423,7 +423,7 @@ Index_write_tree(Index *self)
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
PyMethodDef Index_methods[] = { PyMethodDef Index_methods[] = {
@@ -585,7 +585,7 @@ PyDoc_STRVAR(IndexEntry_oid__doc__, "Object id.");
PyObject * PyObject *
IndexEntry_oid__get__(IndexEntry *self) IndexEntry_oid__get__(IndexEntry *self)
{ {
return git_oid_to_python(self->entry->oid.id); return git_oid_to_python(&self->entry->oid);
} }

View File

@@ -60,7 +60,7 @@ Object_oid__get__(Object *self)
oid = git_object_id(self->obj); oid = git_object_id(self->obj);
assert(oid); assert(oid);
return git_oid_to_python(oid->id); return git_oid_to_python(oid);
} }

View File

@@ -32,6 +32,20 @@
#include "error.h" #include "error.h"
#include "oid.h" #include "oid.h"
PyTypeObject OidType;
PyObject *
git_oid_to_python(const git_oid *oid)
{
Oid *py_oid;
py_oid = PyObject_New(Oid, &OidType);
git_oid_cpy(&(py_oid->oid), oid);
return (PyObject*)py_oid;
}
int int
py_str_to_git_oid(PyObject *py_str, git_oid *oid) py_str_to_git_oid(PyObject *py_str, git_oid *oid)
{ {
@@ -40,7 +54,13 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
int err; int err;
Py_ssize_t len; Py_ssize_t len;
/* Case 1: raw sha */ /* Case 1: Git Oid */
if (PyObject_TypeCheck(py_str, (PyTypeObject*)&OidType)) {
git_oid_cpy(oid, &((Oid*)py_str)->oid);
return GIT_OID_RAWSZ;
}
/* Case 2: raw sha (bytes) */
if (PyBytes_Check(py_str)) { if (PyBytes_Check(py_str)) {
err = PyBytes_AsStringAndSize(py_str, &hex_or_bin, &len); err = PyBytes_AsStringAndSize(py_str, &hex_or_bin, &len);
if (err) if (err)
@@ -53,7 +73,7 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
return len * 2; return len * 2;
} }
/* Case 2: hex sha */ /* Case 3: hex sha (unicode) */
if (PyUnicode_Check(py_str)) { if (PyUnicode_Check(py_str)) {
py_hex = PyUnicode_AsASCIIString(py_str); py_hex = PyUnicode_AsASCIIString(py_str);
if (py_hex == NULL) if (py_hex == NULL)
@@ -159,12 +179,19 @@ Oid_init(Oid *self, PyObject *args, PyObject *kw)
} }
int
Oid_compare(PyObject *o1, PyObject *o2)
{
return git_oid_cmp(&((Oid*)o1)->oid, &((Oid*)o2)->oid);
}
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid."); PyDoc_STRVAR(Oid_raw__doc__, "Raw oid.");
PyObject * PyObject *
Oid_raw__get__(Oid *self) Oid_raw__get__(Oid *self)
{ {
return git_oid_to_python(self->oid.id); return PyBytes_FromStringAndSize((const char*)self->oid.id, GIT_OID_RAWSZ);
} }
@@ -193,7 +220,7 @@ PyTypeObject OidType = {
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_compare */ (cmpfunc)Oid_compare, /* tp_compare */
0, /* tp_repr */ 0, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */

View File

@@ -35,9 +35,7 @@
int py_str_to_git_oid(PyObject *py_str, git_oid *oid); int py_str_to_git_oid(PyObject *py_str, git_oid *oid);
int py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str, int py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str,
git_oid *oid); git_oid *oid);
PyObject* git_oid_to_python(const git_oid *oid);
PyObject* git_oid_to_py_str(const git_oid *oid); PyObject* git_oid_to_py_str(const git_oid *oid);
#define git_oid_to_python(id) \
PyBytes_FromStringAndSize((const char*)id, GIT_OID_RAWSZ)
#endif #endif

View File

@@ -142,7 +142,7 @@ hashfile(PyObject *self, PyObject *args)
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
PyDoc_STRVAR(hash__doc__, PyDoc_STRVAR(hash__doc__,
@@ -166,7 +166,7 @@ hash(PyObject *self, PyObject *args)
return Error_set(err); return Error_set(err);
} }
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }

View File

@@ -276,7 +276,7 @@ Reference_oid__get__(Reference *self)
} }
/* Convert and return it */ /* Convert and return it */
return git_oid_to_python(oid->id); return git_oid_to_python(oid);
} }
int int

View File

@@ -403,7 +403,7 @@ Repository_write(Repository *self, PyObject *args)
stream->write(stream, buffer, buflen); stream->write(stream, buffer, buflen);
err = stream->finalize_write(&oid, stream); err = stream->finalize_write(&oid, stream);
stream->free(stream); stream->free(stream);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
@@ -578,7 +578,7 @@ Repository_create_blob(Repository *self, PyObject *args)
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
@@ -601,7 +601,7 @@ Repository_create_blob_fromfile(Repository *self, PyObject *args)
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
@@ -674,7 +674,7 @@ Repository_create_commit(Repository *self, PyObject *args)
goto out; goto out;
} }
py_result = git_oid_to_python(oid.id); py_result = git_oid_to_python(&oid);
out: out:
free(message); free(message);
@@ -722,7 +722,7 @@ Repository_create_tag(Repository *self, PyObject *args)
git_object_free(target); git_object_free(target);
if (err < 0) if (err < 0)
return Error_set_oid(err, &oid, len); return Error_set_oid(err, &oid, len);
return git_oid_to_python(oid.id); return git_oid_to_python(&oid);
} }
@@ -1171,7 +1171,7 @@ Repository_create_note(Repository *self, PyObject* args)
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
return git_oid_to_python(note_id.id); return git_oid_to_python(&note_id);
} }

View File

@@ -43,7 +43,7 @@ Tag_target__get__(Tag *self)
const git_oid *oid; const git_oid *oid;
oid = git_tag_target_id(self->tag); oid = git_tag_target_id(self->tag);
return git_oid_to_python(oid->id); return git_oid_to_python(oid);
} }

View File

@@ -74,7 +74,7 @@ TreeEntry_oid__get__(TreeEntry *self)
const git_oid *oid; const git_oid *oid;
oid = git_tree_entry_id(self->entry); oid = git_tree_entry_id(self->entry);
return git_oid_to_python(oid->id); return git_oid_to_python(oid);
} }

View File

@@ -49,7 +49,7 @@ class BlobTest(utils.RepoTestCase):
def test_read_blob(self): def test_read_blob(self):
blob = self.repo[BLOB_SHA] blob = self.repo[BLOB_SHA]
self.assertEqual(blob.hex, BLOB_SHA) self.assertEqual(blob.hex, BLOB_SHA)
sha = utils.oid_to_hex(blob.oid) sha = blob.oid.hex
self.assertEqual(sha, BLOB_SHA) self.assertEqual(sha, BLOB_SHA)
self.assertTrue(isinstance(blob, pygit2.Blob)) self.assertTrue(isinstance(blob, pygit2.Blob))
self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type) self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type)
@@ -67,8 +67,7 @@ class BlobTest(utils.RepoTestCase):
self.assertEqual(blob_oid, blob.oid) self.assertEqual(blob_oid, blob.oid)
self.assertEqual( self.assertEqual(
utils.gen_blob_sha1(BLOB_NEW_CONTENT), utils.gen_blob_sha1(BLOB_NEW_CONTENT),
utils.oid_to_hex(blob_oid) blob_oid.hex)
)
self.assertEqual(BLOB_NEW_CONTENT, blob.data) self.assertEqual(BLOB_NEW_CONTENT, blob.data)
self.assertEqual(len(BLOB_NEW_CONTENT), blob.size) self.assertEqual(len(BLOB_NEW_CONTENT), blob.size)
@@ -85,8 +84,7 @@ class BlobTest(utils.RepoTestCase):
self.assertEqual(blob_oid, blob.oid) self.assertEqual(blob_oid, blob.oid)
self.assertEqual( self.assertEqual(
utils.gen_blob_sha1(BLOB_FILE_CONTENT), utils.gen_blob_sha1(BLOB_FILE_CONTENT),
utils.oid_to_hex(blob_oid) blob_oid.hex)
)
self.assertEqual(BLOB_FILE_CONTENT, blob.data) self.assertEqual(BLOB_FILE_CONTENT, blob.data)
self.assertEqual(len(BLOB_FILE_CONTENT), blob.size) self.assertEqual(len(BLOB_FILE_CONTENT), blob.size)

View File

@@ -98,8 +98,7 @@ class IndexTest(utils.RepoTestCase):
self.assertEqual(len(index), 1) self.assertEqual(len(index), 1)
# Test read-write returns the same oid # Test read-write returns the same oid
oid = index.write_tree() oid = index.write_tree()
oid = utils.oid_to_hex(oid) self.assertEqual(oid.hex, tree_oid)
self.assertEqual(oid, tree_oid)
# Test the index is only modified in memory # Test the index is only modified in memory
index.read() index.read()
self.assertEqual(len(index), 2) self.assertEqual(len(index), 2)
@@ -107,8 +106,7 @@ class IndexTest(utils.RepoTestCase):
def test_write_tree(self): def test_write_tree(self):
oid = self.repo.index.write_tree() oid = self.repo.index.write_tree()
sha = utils.oid_to_hex(oid) self.assertEqual(oid.hex, 'fd937514cb799514d4b81bb24c5fcfeb6472b245')
self.assertEqual(sha, 'fd937514cb799514d4b81bb24c5fcfeb6472b245')
def test_iter(self): def test_iter(self):
index = self.repo.index index = self.repo.index

View File

@@ -49,7 +49,7 @@ class NotesTest(utils.BareRepoTestCase):
annotated_id = self.repo.revparse_single('HEAD~3').hex annotated_id = self.repo.revparse_single('HEAD~3').hex
author = committer = Signature('Foo bar', 'foo@bar.com', 12346, 0) author = committer = Signature('Foo bar', 'foo@bar.com', 12346, 0)
note_id = self.repo.create_note(NOTE[1], author, committer, annotated_id) note_id = self.repo.create_note(NOTE[1], author, committer, annotated_id)
self.assertEqual(NOTE[0], utils.oid_to_hex(note_id)) self.assertEqual(NOTE[0], note_id.hex)
# check the note blob # check the note blob
self.assertEqual(NOTE[1].encode(), self.repo[note_id].data) self.assertEqual(NOTE[1].encode(), self.repo[note_id].data)

View File

@@ -65,6 +65,14 @@ class OidTest(utils.BareRepoTestCase):
self.assertRaises(ValueError, Oid, raw=RAW + b'a') self.assertRaises(ValueError, Oid, raw=RAW + b'a')
self.assertRaises(ValueError, Oid, hex=HEX + 'a') self.assertRaises(ValueError, Oid, hex=HEX + 'a')
def test_cmp(self):
oid1 = Oid(raw=RAW)
oid2 = Oid(hex=HEX)
self.assertEqual(oid1, oid2)
oid2 = Oid(hex="15b648aec6ed045b5ca6f57f8b7831a8b4757299")
self.assertNotEqual(oid1, oid2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@@ -27,18 +27,22 @@
"""Tests for Repository objects.""" """Tests for Repository objects."""
# Import from the future
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
# Import from the Standard Library
import binascii import binascii
import unittest import unittest
import tempfile import tempfile
import os import os
from os.path import join, realpath from os.path import join, realpath
# Import from pygit2
from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT
from pygit2 import init_repository, discover_repository, Commit, hashfile from pygit2 import init_repository, discover_repository, Commit, hashfile
from pygit2 import Oid
import pygit2 import pygit2
from . import utils from . import utils
@@ -85,8 +89,7 @@ class RepositoryTest(utils.BareRepoTestCase):
self.assertRaises(ValueError, self.repo.write, GIT_OBJ_ANY, data) self.assertRaises(ValueError, self.repo.write, GIT_OBJ_ANY, data)
oid = self.repo.write(GIT_OBJ_BLOB, data) oid = self.repo.write(GIT_OBJ_BLOB, data)
self.assertEqual(type(oid), bytes) self.assertEqual(type(oid), Oid)
self.assertEqual(len(oid), 20)
def test_contains(self): def test_contains(self):
self.assertRaises(TypeError, lambda: 123 in self.repo) self.assertRaises(TypeError, lambda: 123 in self.repo)
@@ -222,16 +225,18 @@ class RepositoryTest_II(utils.RepoTestCase):
self.repo.checkout(pygit2.GIT_CHECKOUT_FORCE, head=True) self.repo.checkout(pygit2.GIT_CHECKOUT_FORCE, head=True)
self.assertTrue('bye.txt' not in self.repo.status()) self.assertTrue('bye.txt' not in self.repo.status())
class NewRepositoryTest(utils.NoRepoTestCase): class NewRepositoryTest(utils.NoRepoTestCase):
def test_new_repo(self): def test_new_repo(self):
repo = init_repository(self._temp_dir, False) repo = init_repository(self._temp_dir, False)
oid = repo.write(GIT_OBJ_BLOB, "Test") oid = repo.write(GIT_OBJ_BLOB, "Test")
self.assertEqual(type(oid), bytes) self.assertEqual(type(oid), Oid)
self.assertEqual(len(oid), 20)
assert os.path.exists(os.path.join(self._temp_dir, '.git')) assert os.path.exists(os.path.join(self._temp_dir, '.git'))
class InitRepositoryTest(utils.NoRepoTestCase): class InitRepositoryTest(utils.NoRepoTestCase):
# under the assumption that repo.is_bare works # under the assumption that repo.is_bare works

View File

@@ -72,7 +72,7 @@ class TagTest(utils.BareRepoTestCase):
self.assertEqual('3ee44658fd11660e828dfc96b9b5c5f38d5b49bb', tag.hex) self.assertEqual('3ee44658fd11660e828dfc96b9b5c5f38d5b49bb', tag.hex)
self.assertEqual(name, tag.name) self.assertEqual(name, tag.name)
self.assertEqual(target, utils.oid_to_hex(tag.target)) self.assertEqual(target, tag.target.hex)
self.assertEqualSignature(tagger, tag.tagger) self.assertEqualSignature(tagger, tag.tagger)
self.assertEqual(message, tag.message) self.assertEqual(message, tag.message)
self.assertEqual(name, self.repo[tag.hex].name) self.assertEqual(name, self.repo[tag.hex].name)

View File

@@ -47,8 +47,6 @@ def force_rm_handle(remove_path, path, excinfo):
) )
remove_path(path) remove_path(path)
def oid_to_hex(oid):
return b2a_hex(oid).decode('ascii')
def gen_blob_sha1(data): def gen_blob_sha1(data):
# http://stackoverflow.com/questions/552659/assigning-git-sha1s-without-git # http://stackoverflow.com/questions/552659/assigning-git-sha1s-without-git
@@ -58,6 +56,7 @@ def gen_blob_sha1(data):
return m.hexdigest() return m.hexdigest()
def rmtree(path): def rmtree(path):
"""In Windows a read-only file cannot be removed, and shutil.rmtree fails. """In Windows a read-only file cannot be removed, and shutil.rmtree fails.
So we implement our own version of rmtree to address this issue. So we implement our own version of rmtree to address this issue.