Skip to content

Commit f8544cc

Browse files
committed
Add Oid type
1 parent c7785e5 commit f8544cc

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
lines changed

src/oid.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,102 @@ git_oid_to_py_str(const git_oid *oid)
125125
return to_unicode_n(hex, GIT_OID_HEXSZ, "utf-8", "strict");
126126
}
127127

128+
129+
int
130+
Oid_init(Oid *self, PyObject *args, PyObject *kw)
131+
{
132+
char *keywords[] = {"raw", "hex", NULL};
133+
PyObject *raw = NULL, *hex = NULL;
134+
int err;
135+
136+
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OO", keywords, &raw, &hex))
137+
return -1;
138+
139+
/* We expect one or the other, but not both. */
140+
if (raw == NULL && hex == NULL) {
141+
PyErr_SetString(PyExc_ValueError, "Expected raw or hex.");
142+
return -1;
143+
}
144+
if (raw != NULL && hex != NULL) {
145+
PyErr_SetString(PyExc_ValueError, "Expected raw or hex, not both.");
146+
return -1;
147+
}
148+
149+
/* Get the oid. */
150+
if (raw != NULL)
151+
err = py_str_to_git_oid(raw, &self->oid);
152+
else
153+
err = py_str_to_git_oid(hex, &self->oid);
154+
155+
if (err < 0)
156+
return -1;
157+
158+
return 0;
159+
}
160+
161+
162+
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid.");
163+
164+
PyObject *
165+
Oid_raw__get__(Oid *self)
166+
{
167+
return git_oid_to_python(self->oid.id);
168+
}
169+
170+
171+
PyDoc_STRVAR(Oid_hex__doc__, "Hex oid.");
172+
173+
PyObject *
174+
Oid_hex__get__(Oid *self)
175+
{
176+
return git_oid_to_py_str(&self->oid);
177+
}
178+
179+
PyGetSetDef Oid_getseters[] = {
180+
GETTER(Oid, raw),
181+
GETTER(Oid, hex),
182+
{NULL},
183+
};
184+
185+
PyDoc_STRVAR(Oid__doc__, "Object id.");
186+
187+
PyTypeObject OidType = {
188+
PyVarObject_HEAD_INIT(NULL, 0)
189+
"_pygit2.Oid", /* tp_name */
190+
sizeof(Oid), /* tp_basicsize */
191+
0, /* tp_itemsize */
192+
0, /* tp_dealloc */
193+
0, /* tp_print */
194+
0, /* tp_getattr */
195+
0, /* tp_setattr */
196+
0, /* tp_compare */
197+
0, /* tp_repr */
198+
0, /* tp_as_number */
199+
0, /* tp_as_sequence */
200+
0, /* tp_as_mapping */
201+
0, /* tp_hash */
202+
0, /* tp_call */
203+
0, /* tp_str */
204+
0, /* tp_getattro */
205+
0, /* tp_setattro */
206+
0, /* tp_as_buffer */
207+
Py_TPFLAGS_DEFAULT, /* tp_flags */
208+
Oid__doc__, /* tp_doc */
209+
0, /* tp_traverse */
210+
0, /* tp_clear */
211+
0, /* tp_richcompare */
212+
0, /* tp_weaklistoffset */
213+
0, /* tp_iter */
214+
0, /* tp_iternext */
215+
0, /* tp_methods */
216+
0, /* tp_members */
217+
Oid_getseters, /* tp_getset */
218+
0, /* tp_base */
219+
0, /* tp_dict */
220+
0, /* tp_descr_get */
221+
0, /* tp_descr_set */
222+
0, /* tp_dictoffset */
223+
(initproc)Oid_init, /* tp_init */
224+
0, /* tp_alloc */
225+
0, /* tp_new */
226+
};

src/pygit2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
extern PyObject *GitError;
3939

4040
extern PyTypeObject RepositoryType;
41+
extern PyTypeObject OidType;
4142
extern PyTypeObject ObjectType;
4243
extern PyTypeObject CommitType;
4344
extern PyTypeObject DiffType;
@@ -193,6 +194,10 @@ moduleinit(PyObject* m)
193194
INIT_TYPE(RepositoryType, NULL, PyType_GenericNew)
194195
ADD_TYPE(m, Repository);
195196

197+
/* Oid */
198+
INIT_TYPE(OidType, NULL, PyType_GenericNew)
199+
ADD_TYPE(m, Oid);
200+
196201
/* Objects (make them with the Repository.create_XXX methods). */
197202
INIT_TYPE(ObjectType, NULL, NULL)
198203
INIT_TYPE(CommitType, &ObjectType, NULL)

src/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ typedef struct {
4646
} Repository;
4747

4848

49+
typedef struct {
50+
PyObject_HEAD
51+
git_oid oid;
52+
} Oid;
53+
54+
4955
#define SIMPLE_TYPE(_name, _ptr_type, _ptr_name) \
5056
typedef struct {\
5157
PyObject_HEAD\

test/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
import unittest
3636

3737

38-
names = ['blob', 'commit', 'config', 'diff', 'index', 'refs', 'remote',
39-
'repository', 'revwalk', 'signature', 'status', 'tag', 'tree',
40-
'treebuilder', 'note']
38+
names = ['blob', 'commit', 'config', 'diff', 'index', 'note', 'oid', 'refs',
39+
'remote', 'repository', 'revwalk', 'signature', 'status', 'tag',
40+
'tree', 'treebuilder']
4141

4242
def test_suite():
4343
# Sometimes importing pygit2 fails, we try this first to get an

test/test_oid.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Copyright 2010-2013 The pygit2 contributors
4+
#
5+
# This file is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License, version 2,
7+
# as published by the Free Software Foundation.
8+
#
9+
# In addition to the permissions in the GNU General Public License,
10+
# the authors give you unlimited permission to link the compiled
11+
# version of this file into combinations with other programs,
12+
# and to distribute those combinations without any restriction
13+
# coming from the use of this file. (The General Public License
14+
# restrictions do apply in other respects; for example, they cover
15+
# modification of the file, and distribution when not linked into
16+
# a combined executable.)
17+
#
18+
# This file is distributed in the hope that it will be useful, but
19+
# WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
# General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with this program; see the file COPYING. If not, write to
25+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26+
# Boston, MA 02110-1301, USA.
27+
28+
"""Tests for Object ids."""
29+
30+
# Import from the future
31+
from __future__ import absolute_import
32+
from __future__ import unicode_literals
33+
34+
# Import from the Standard Library
35+
from binascii import unhexlify
36+
import unittest
37+
38+
# Import from pygit2
39+
from pygit2 import Oid
40+
from . import utils
41+
42+
43+
HEX = "15b648aec6ed045b5ca6f57f8b7831a8b4757298"
44+
RAW = unhexlify(HEX.encode('ascii'))
45+
46+
class OidTest(utils.BareRepoTestCase):
47+
48+
def test_raw(self):
49+
oid = Oid(raw=RAW)
50+
self.assertEqual(oid.raw, RAW)
51+
self.assertEqual(oid.hex, HEX)
52+
53+
def test_hex(self):
54+
oid = Oid(hex=HEX)
55+
self.assertEqual(oid.raw, RAW)
56+
self.assertEqual(oid.hex, HEX)
57+
58+
def test_none(self):
59+
self.assertRaises(ValueError, Oid)
60+
61+
def test_both(self):
62+
self.assertRaises(ValueError, Oid, raw=RAW, hex=HEX)
63+
64+
def test_long(self):
65+
self.assertRaises(ValueError, Oid, raw=RAW + b'a')
66+
self.assertRaises(ValueError, Oid, hex=HEX + 'a')
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()

0 commit comments

Comments
 (0)