Add Oid type

This commit is contained in:
J. David Ibáñez 2013-04-13 13:04:52 +02:00
parent c7785e591b
commit f8544cc514
5 changed files with 183 additions and 3 deletions

View File

@ -125,3 +125,102 @@ git_oid_to_py_str(const git_oid *oid)
return to_unicode_n(hex, GIT_OID_HEXSZ, "utf-8", "strict");
}
int
Oid_init(Oid *self, PyObject *args, PyObject *kw)
{
char *keywords[] = {"raw", "hex", NULL};
PyObject *raw = NULL, *hex = NULL;
int err;
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OO", keywords, &raw, &hex))
return -1;
/* We expect one or the other, but not both. */
if (raw == NULL && hex == NULL) {
PyErr_SetString(PyExc_ValueError, "Expected raw or hex.");
return -1;
}
if (raw != NULL && hex != NULL) {
PyErr_SetString(PyExc_ValueError, "Expected raw or hex, not both.");
return -1;
}
/* Get the oid. */
if (raw != NULL)
err = py_str_to_git_oid(raw, &self->oid);
else
err = py_str_to_git_oid(hex, &self->oid);
if (err < 0)
return -1;
return 0;
}
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid.");
PyObject *
Oid_raw__get__(Oid *self)
{
return git_oid_to_python(self->oid.id);
}
PyDoc_STRVAR(Oid_hex__doc__, "Hex oid.");
PyObject *
Oid_hex__get__(Oid *self)
{
return git_oid_to_py_str(&self->oid);
}
PyGetSetDef Oid_getseters[] = {
GETTER(Oid, raw),
GETTER(Oid, hex),
{NULL},
};
PyDoc_STRVAR(Oid__doc__, "Object id.");
PyTypeObject OidType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.Oid", /* tp_name */
sizeof(Oid), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
Oid__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
Oid_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Oid_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};

View File

@ -38,6 +38,7 @@
extern PyObject *GitError;
extern PyTypeObject RepositoryType;
extern PyTypeObject OidType;
extern PyTypeObject ObjectType;
extern PyTypeObject CommitType;
extern PyTypeObject DiffType;
@ -193,6 +194,10 @@ moduleinit(PyObject* m)
INIT_TYPE(RepositoryType, NULL, PyType_GenericNew)
ADD_TYPE(m, Repository);
/* Oid */
INIT_TYPE(OidType, NULL, PyType_GenericNew)
ADD_TYPE(m, Oid);
/* Objects (make them with the Repository.create_XXX methods). */
INIT_TYPE(ObjectType, NULL, NULL)
INIT_TYPE(CommitType, &ObjectType, NULL)

View File

@ -46,6 +46,12 @@ typedef struct {
} Repository;
typedef struct {
PyObject_HEAD
git_oid oid;
} Oid;
#define SIMPLE_TYPE(_name, _ptr_type, _ptr_name) \
typedef struct {\
PyObject_HEAD\

View File

@ -35,9 +35,9 @@ import sys
import unittest
names = ['blob', 'commit', 'config', 'diff', 'index', 'refs', 'remote',
'repository', 'revwalk', 'signature', 'status', 'tag', 'tree',
'treebuilder', 'note']
names = ['blob', 'commit', 'config', 'diff', 'index', 'note', 'oid', 'refs',
'remote', 'repository', 'revwalk', 'signature', 'status', 'tag',
'tree', 'treebuilder']
def test_suite():
# Sometimes importing pygit2 fails, we try this first to get an

70
test/test_oid.py Normal file
View File

@ -0,0 +1,70 @@
# -*- coding: UTF-8 -*-
#
# Copyright 2010-2013 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Tests for Object ids."""
# Import from the future
from __future__ import absolute_import
from __future__ import unicode_literals
# Import from the Standard Library
from binascii import unhexlify
import unittest
# Import from pygit2
from pygit2 import Oid
from . import utils
HEX = "15b648aec6ed045b5ca6f57f8b7831a8b4757298"
RAW = unhexlify(HEX.encode('ascii'))
class OidTest(utils.BareRepoTestCase):
def test_raw(self):
oid = Oid(raw=RAW)
self.assertEqual(oid.raw, RAW)
self.assertEqual(oid.hex, HEX)
def test_hex(self):
oid = Oid(hex=HEX)
self.assertEqual(oid.raw, RAW)
self.assertEqual(oid.hex, HEX)
def test_none(self):
self.assertRaises(ValueError, Oid)
def test_both(self):
self.assertRaises(ValueError, Oid, raw=RAW, hex=HEX)
def test_long(self):
self.assertRaises(ValueError, Oid, raw=RAW + b'a')
self.assertRaises(ValueError, Oid, hex=HEX + 'a')
if __name__ == '__main__':
unittest.main()