Merge pull request #434 from carlosmn/diff-refactor

Repository: make use of peel for diff()
This commit is contained in:
Carlos Martín Nieto 2014-10-10 11:57:18 +02:00
commit b8efdde626
7 changed files with 98 additions and 50 deletions

@ -28,6 +28,7 @@ The Reference type
.. automethod:: pygit2.Reference.delete
.. automethod:: pygit2.Reference.rename
.. automethod:: pygit2.Reference.resolve
.. automethod:: pygit2.Reference.peel
.. automethod:: pygit2.Reference.log
Example::

@ -385,26 +385,24 @@ class Repository(_Repository):
if obj is None:
return None
# Would be better to test by the type of obj, but it is boring to
# deal with Python 2 & 3 differences
try:
# If it's a string, then it has to be valid revspec
if is_string(obj):
obj = self.revparse_single(obj)
except TypeError:
# First we try to get to a blob
try:
obj = obj.peel(Blob)
except Exception:
pass
# If reference, resolve
if isinstance(obj, Reference):
oid = obj.resolve().target
obj = self[oid]
if isinstance(obj, Commit):
return obj.tree
if isinstance(obj, (Blob, Tree)):
return obj
raise TypeError('unexpected "%s"' % type(obj))
# And if that failed, try to get a tree, raising a type
# error if that still doesn't work
try:
obj = obj.peel(Tree)
except Exception:
raise TypeError('unexpected "%s"' % type(obj))
return obj
a = whatever_to_tree_or_blob(a)
b = whatever_to_tree_or_blob(b)

@ -135,24 +135,6 @@ Object_read_raw(Object *self)
return aux;
}
static git_otype
py_type_to_git_type(PyTypeObject *py_type)
{
git_otype type = GIT_OBJ_BAD;
if (py_type == &CommitType) {
type = GIT_OBJ_COMMIT;
} else if (py_type == &TreeType) {
type = GIT_OBJ_TREE;
} else if (py_type == &BlobType) {
type = GIT_OBJ_BLOB;
} else if (py_type == &TagType) {
type = GIT_OBJ_TAG;
}
return type;
}
PyDoc_STRVAR(Object_peel__doc__,
"peel(target_type) -> Object\n"
"\n"
@ -164,18 +146,9 @@ Object_peel(Object *self, PyObject *py_type)
int type = -1, err;
git_object *peeled;
if (PyLong_Check(py_type)) {
type = PyLong_AsLong(py_type);
if (type == -1 && PyErr_Occurred())
return NULL;
} else if (PyType_Check(py_type)) {
type = py_type_to_git_type((PyTypeObject *) py_type);
}
if (type == -1) {
PyErr_SetString(PyExc_ValueError, "invalid target type");
type = py_object_to_object_type(py_type);
if (type == -1)
return NULL;
}
err = git_object_peel(&peeled, self->obj, (git_otype)type);
if (err < 0)

@ -367,24 +367,46 @@ Reference_log(Reference *self)
PyDoc_STRVAR(Reference_get_object__doc__,
"get_object() -> object\n"
"\n"
"Retrieves the object the current reference is pointing to.");
"Retrieves the object the current reference is pointing to.\n"
"\n"
"This method is deprecated, please use Reference.peel() instead.");
PyObject *
Reference_get_object(Reference *self)
{
int err;
git_object* obj;
return PyObject_CallMethod((PyObject *) self, "peel", NULL);
}
PyDoc_STRVAR(Reference_peel__doc__,
"peel(type=None) -> object\n"
"\n"
"Retrieve an object of the given type by recursive peeling.\n"
"\n"
"If no type is provided, the first non-tag object will be returned.");
PyObject *
Reference_peel(Reference *self, PyObject *args)
{
int err, type;
git_object *obj;
PyObject *py_type = Py_None;
CHECK_REFERENCE(self);
err = git_reference_peel(&obj, self->reference, GIT_OBJ_ANY);
if (!PyArg_ParseTuple(args, "|O", &py_type))
return NULL;
type = py_object_to_object_type(py_type);
if (type == -1)
return NULL;
err = git_reference_peel(&obj, self->reference, type);
if (err < 0)
return Error_set(err);
return wrap_object(obj, self->repo);
}
PyDoc_STRVAR(RefLogEntry_committer__doc__, "Committer.");
PyObject *
@ -479,6 +501,7 @@ PyMethodDef Reference_methods[] = {
METHOD(Reference, log, METH_NOARGS),
METHOD(Reference, get_object, METH_NOARGS),
METHOD(Reference, set_target, METH_VARARGS | METH_KEYWORDS),
METHOD(Reference, peel, METH_VARARGS),
{NULL}
};

@ -31,6 +31,10 @@
#include "utils.h"
extern PyTypeObject ReferenceType;
extern PyTypeObject TreeType;
extern PyTypeObject CommitType;
extern PyTypeObject BlobType;
extern PyTypeObject TagType;
/**
* py_str_to_c_str() returns a newly allocated C string holding the string
@ -153,3 +157,44 @@ on_error:
return -1;
}
static git_otype
py_type_to_git_type(PyTypeObject *py_type)
{
git_otype type = GIT_OBJ_BAD;
if (py_type == &CommitType) {
type = GIT_OBJ_COMMIT;
} else if (py_type == &TreeType) {
type = GIT_OBJ_TREE;
} else if (py_type == &BlobType) {
type = GIT_OBJ_BLOB;
} else if (py_type == &TagType) {
type = GIT_OBJ_TAG;
}
return type;
}
int
py_object_to_object_type(PyObject *py_type)
{
int type = -1;
if (py_type == Py_None)
return GIT_OBJ_ANY;
if (PyLong_Check(py_type)) {
type = PyLong_AsLong(py_type);
if (type == -1 && PyErr_Occurred())
return -1;
} else if (PyType_Check(py_type)) {
type = py_type_to_git_type((PyTypeObject *) py_type);
}
if (type == -1) {
PyErr_SetString(PyExc_ValueError, "invalid target type");
}
return type;
}

@ -119,6 +119,8 @@ const char *py_str_borrow_c_str(PyObject **tvaue, PyObject *value, const char *e
PyObject * get_pylist_from_git_strarray(git_strarray *strarray);
int get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist);
int py_object_to_object_type(PyObject *py_type);
#define py_path_to_c_str(py_path) \
py_str_to_c_str(py_path, Py_FileSystemDefaultEncoding)

@ -32,6 +32,7 @@ from __future__ import unicode_literals
import unittest
from pygit2 import GitError, GIT_REF_OID, GIT_REF_SYMBOLIC, Signature
from pygit2 import Commit, Tree
from . import utils
@ -223,6 +224,11 @@ class ReferencesTest(utils.RepoTestCase):
ref = repo.lookup_reference('refs/heads/master')
self.assertEqual(repo[ref.target].id, ref.get_object().id)
def test_peel(self):
ref = self.repo.lookup_reference('refs/heads/master')
commit = ref.peel(Commit)
self.assertEqual(commit.tree.id, ref.peel(Tree).id)
if __name__ == '__main__':
unittest.main()