Skip to content

Commit 406c317

Browse files
committed
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
1 parent f8544cc commit 406c317

File tree

16 files changed

+77
-44
lines changed

16 files changed

+77
-44
lines changed

src/index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ Index_write_tree(Index *self)
423423
if (err < 0)
424424
return Error_set(err);
425425

426-
return git_oid_to_python(oid.id);
426+
return git_oid_to_python(&oid);
427427
}
428428

429429
PyMethodDef Index_methods[] = {
@@ -585,7 +585,7 @@ PyDoc_STRVAR(IndexEntry_oid__doc__, "Object id.");
585585
PyObject *
586586
IndexEntry_oid__get__(IndexEntry *self)
587587
{
588-
return git_oid_to_python(self->entry->oid.id);
588+
return git_oid_to_python(&self->entry->oid);
589589
}
590590

591591

src/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Object_oid__get__(Object *self)
6060
oid = git_object_id(self->obj);
6161
assert(oid);
6262

63-
return git_oid_to_python(oid->id);
63+
return git_oid_to_python(oid);
6464
}
6565

6666

src/oid.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@
3232
#include "error.h"
3333
#include "oid.h"
3434

35+
PyTypeObject OidType;
36+
37+
38+
PyObject *
39+
git_oid_to_python(const git_oid *oid)
40+
{
41+
Oid *py_oid;
42+
43+
py_oid = PyObject_New(Oid, &OidType);
44+
git_oid_cpy(&(py_oid->oid), oid);
45+
return (PyObject*)py_oid;
46+
}
47+
48+
3549
int
3650
py_str_to_git_oid(PyObject *py_str, git_oid *oid)
3751
{
@@ -40,7 +54,13 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
4054
int err;
4155
Py_ssize_t len;
4256

43-
/* Case 1: raw sha */
57+
/* Case 1: Git Oid */
58+
if (PyObject_TypeCheck(py_str, (PyTypeObject*)&OidType)) {
59+
git_oid_cpy(oid, &((Oid*)py_str)->oid);
60+
return GIT_OID_RAWSZ;
61+
}
62+
63+
/* Case 2: raw sha (bytes) */
4464
if (PyBytes_Check(py_str)) {
4565
err = PyBytes_AsStringAndSize(py_str, &hex_or_bin, &len);
4666
if (err)
@@ -53,7 +73,7 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
5373
return len * 2;
5474
}
5575

56-
/* Case 2: hex sha */
76+
/* Case 3: hex sha (unicode) */
5777
if (PyUnicode_Check(py_str)) {
5878
py_hex = PyUnicode_AsASCIIString(py_str);
5979
if (py_hex == NULL)
@@ -159,12 +179,19 @@ Oid_init(Oid *self, PyObject *args, PyObject *kw)
159179
}
160180

161181

182+
int
183+
Oid_compare(PyObject *o1, PyObject *o2)
184+
{
185+
return git_oid_cmp(&((Oid*)o1)->oid, &((Oid*)o2)->oid);
186+
}
187+
188+
162189
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid.");
163190

164191
PyObject *
165192
Oid_raw__get__(Oid *self)
166193
{
167-
return git_oid_to_python(self->oid.id);
194+
return PyBytes_FromStringAndSize((const char*)self->oid.id, GIT_OID_RAWSZ);
168195
}
169196

170197

@@ -193,7 +220,7 @@ PyTypeObject OidType = {
193220
0, /* tp_print */
194221
0, /* tp_getattr */
195222
0, /* tp_setattr */
196-
0, /* tp_compare */
223+
(cmpfunc)Oid_compare, /* tp_compare */
197224
0, /* tp_repr */
198225
0, /* tp_as_number */
199226
0, /* tp_as_sequence */

src/oid.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@
3434

3535
int py_str_to_git_oid(PyObject *py_str, git_oid *oid);
3636
int py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str,
37-
git_oid *oid);
37+
git_oid *oid);
38+
PyObject* git_oid_to_python(const git_oid *oid);
3839
PyObject* git_oid_to_py_str(const git_oid *oid);
3940

40-
#define git_oid_to_python(id) \
41-
PyBytes_FromStringAndSize((const char*)id, GIT_OID_RAWSZ)
42-
4341
#endif

src/pygit2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ hashfile(PyObject *self, PyObject *args)
142142
if (err < 0)
143143
return Error_set(err);
144144

145-
return git_oid_to_python(oid.id);
145+
return git_oid_to_python(&oid);
146146
}
147147

148148
PyDoc_STRVAR(hash__doc__,
@@ -166,7 +166,7 @@ hash(PyObject *self, PyObject *args)
166166
return Error_set(err);
167167
}
168168

169-
return git_oid_to_python(oid.id);
169+
return git_oid_to_python(&oid);
170170
}
171171

172172

src/reference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Reference_oid__get__(Reference *self)
276276
}
277277

278278
/* Convert and return it */
279-
return git_oid_to_python(oid->id);
279+
return git_oid_to_python(oid);
280280
}
281281

282282
int

src/repository.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ Repository_write(Repository *self, PyObject *args)
403403
stream->write(stream, buffer, buflen);
404404
err = stream->finalize_write(&oid, stream);
405405
stream->free(stream);
406-
return git_oid_to_python(oid.id);
406+
return git_oid_to_python(&oid);
407407
}
408408

409409

@@ -578,7 +578,7 @@ Repository_create_blob(Repository *self, PyObject *args)
578578
if (err < 0)
579579
return Error_set(err);
580580

581-
return git_oid_to_python(oid.id);
581+
return git_oid_to_python(&oid);
582582
}
583583

584584

@@ -601,7 +601,7 @@ Repository_create_blob_fromfile(Repository *self, PyObject *args)
601601
if (err < 0)
602602
return Error_set(err);
603603

604-
return git_oid_to_python(oid.id);
604+
return git_oid_to_python(&oid);
605605
}
606606

607607

@@ -674,7 +674,7 @@ Repository_create_commit(Repository *self, PyObject *args)
674674
goto out;
675675
}
676676

677-
py_result = git_oid_to_python(oid.id);
677+
py_result = git_oid_to_python(&oid);
678678

679679
out:
680680
free(message);
@@ -722,7 +722,7 @@ Repository_create_tag(Repository *self, PyObject *args)
722722
git_object_free(target);
723723
if (err < 0)
724724
return Error_set_oid(err, &oid, len);
725-
return git_oid_to_python(oid.id);
725+
return git_oid_to_python(&oid);
726726
}
727727

728728

@@ -1171,7 +1171,7 @@ Repository_create_note(Repository *self, PyObject* args)
11711171
if (err < 0)
11721172
return Error_set(err);
11731173

1174-
return git_oid_to_python(note_id.id);
1174+
return git_oid_to_python(&note_id);
11751175
}
11761176

11771177

src/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Tag_target__get__(Tag *self)
4343
const git_oid *oid;
4444

4545
oid = git_tag_target_id(self->tag);
46-
return git_oid_to_python(oid->id);
46+
return git_oid_to_python(oid);
4747
}
4848

4949

src/tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TreeEntry_oid__get__(TreeEntry *self)
7474
const git_oid *oid;
7575

7676
oid = git_tree_entry_id(self->entry);
77-
return git_oid_to_python(oid->id);
77+
return git_oid_to_python(oid);
7878
}
7979

8080

test/test_blob.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BlobTest(utils.RepoTestCase):
4949
def test_read_blob(self):
5050
blob = self.repo[BLOB_SHA]
5151
self.assertEqual(blob.hex, BLOB_SHA)
52-
sha = utils.oid_to_hex(blob.oid)
52+
sha = blob.oid.hex
5353
self.assertEqual(sha, BLOB_SHA)
5454
self.assertTrue(isinstance(blob, pygit2.Blob))
5555
self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type)
@@ -66,9 +66,8 @@ def test_create_blob(self):
6666

6767
self.assertEqual(blob_oid, blob.oid)
6868
self.assertEqual(
69-
utils.gen_blob_sha1(BLOB_NEW_CONTENT),
70-
utils.oid_to_hex(blob_oid)
71-
)
69+
utils.gen_blob_sha1(BLOB_NEW_CONTENT),
70+
blob_oid.hex)
7271

7372
self.assertEqual(BLOB_NEW_CONTENT, blob.data)
7473
self.assertEqual(len(BLOB_NEW_CONTENT), blob.size)
@@ -84,9 +83,8 @@ def test_create_blob_fromfile(self):
8483

8584
self.assertEqual(blob_oid, blob.oid)
8685
self.assertEqual(
87-
utils.gen_blob_sha1(BLOB_FILE_CONTENT),
88-
utils.oid_to_hex(blob_oid)
89-
)
86+
utils.gen_blob_sha1(BLOB_FILE_CONTENT),
87+
blob_oid.hex)
9088

9189
self.assertEqual(BLOB_FILE_CONTENT, blob.data)
9290
self.assertEqual(len(BLOB_FILE_CONTENT), blob.size)

0 commit comments

Comments
 (0)