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+
3549int
3650py_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+
162189PyDoc_STRVAR (Oid_raw__doc__ , "Raw oid." );
163190
164191PyObject *
165192Oid_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 */
0 commit comments