Skip to content

Commit

Permalink
Merge pull request #434 from carlosmn/diff-refactor
Browse files Browse the repository at this point in the history
Repository: make use of peel for diff()
  • Loading branch information
carlosmn committed Oct 10, 2014
2 parents fa20589 + 5d4c955 commit b8efdde
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
30 changes: 14 additions & 16 deletions pygit2/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,26 +385,24 @@ def whatever_to_tree_or_blob(obj):
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:
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
# First we try to get to a blob
try:
obj = obj.peel(Blob)
except Exception:
pass

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)
Expand Down
31 changes: 2 additions & 29 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
33 changes: 28 additions & 5 deletions src/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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}
};

Expand Down
45 changes: 45 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,3 +157,44 @@ get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)

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;
}
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions test/test_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import unittest

from pygit2 import GitError, GIT_REF_OID, GIT_REF_SYMBOLIC, Signature
from pygit2 import Commit, Tree
from . import utils


Expand Down Expand Up @@ -223,6 +224,11 @@ def test_get_object(self):
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()

0 comments on commit b8efdde

Please sign in to comment.