Diff now invoked immediately and its result stored inside object to

avoid calls on repeated accesses and allow implementation of other
variants
Fixed indentation in diff implementation
This commit is contained in:
Petr Hosek
2012-05-28 15:41:49 +01:00
parent e7b5560590
commit 6aba5b65a2
3 changed files with 65 additions and 95 deletions

View File

@@ -29,6 +29,7 @@ OBJECT_STRUCT(Blob, git_blob, blob)
OBJECT_STRUCT(Tag, git_tag, tag) OBJECT_STRUCT(Tag, git_tag, tag)
OBJECT_STRUCT(Index, git_index, index) OBJECT_STRUCT(Index, git_index, index)
OBJECT_STRUCT(Walker, git_revwalk, walk) OBJECT_STRUCT(Walker, git_revwalk, walk)
OBJECT_STRUCT(Diff, git_diff_list, diff)
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
@@ -36,12 +37,6 @@ typedef struct {
const git_tree_entry *entry; const git_tree_entry *entry;
} TreeEntry; } TreeEntry;
typedef struct {
PyObject_HEAD
Tree *t0;
Tree *t1;
} Diff;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
int old_start; int old_start;

View File

@@ -114,33 +114,16 @@ static int diff_file_cb(void *cb_data, git_diff_delta *delta, float progress)
PyObject * PyObject *
Diff_changes(Diff *self) Diff_changes(Diff *self)
{ {
git_diff_options opts = {0};
git_diff_list *changes;
int err;
err = git_diff_tree_to_tree(
self->t0->repo->repo,
&opts,
self->t0->tree,
self->t1->tree,
&changes);
if(err < 0) {
Error_set(err);
return NULL;
}
PyObject *payload; PyObject *payload;
payload = PyDict_New(); payload = PyDict_New();
git_diff_foreach( git_diff_foreach(
changes, self->diff,
payload, payload,
&diff_file_cb, &diff_file_cb,
&diff_hunk_cb, &diff_hunk_cb,
&diff_data_cb &diff_data_cb
); );
git_diff_list_free(changes);
return payload; return payload;
} }
@@ -159,29 +142,12 @@ static int diff_print_cb(
return 0; return 0;
} }
PyObject * PyObject *
Diff_patch(Diff *self) Diff_patch(Diff *self)
{ {
git_diff_options opts = {0};
git_diff_list *changes;
int err;
err = git_diff_tree_to_tree(
self->t0->repo->repo,
&opts,
self->t0->tree,
self->t1->tree,
&changes);
if(err < 0) {
Error_set(err);
return NULL;
}
PyObject *patch = PyBytes_FromString(""); PyObject *patch = PyBytes_FromString("");
git_diff_print_patch(changes, &patch, &diff_print_cb); git_diff_print_patch(self->diff, &patch, &diff_print_cb);
return patch; return patch;
} }
@@ -271,23 +237,20 @@ PyTypeObject HunkType = {
0, /* tp_new */ 0, /* tp_new */
}; };
static void static void
Diff_dealloc(Diff *self) Diff_dealloc(Diff *self)
{ {
Py_XDECREF(self->t0); git_diff_list_free(self->diff);
Py_XDECREF(self->t1); Py_XDECREF(self->repo);
PyObject_Del(self); PyObject_Del(self);
} }
PyGetSetDef Diff_getseters[] = { PyGetSetDef Diff_getseters[] = {
{"changes", (getter)Diff_changes, NULL, "raw changes", NULL}, {"changes", (getter)Diff_changes, NULL, "raw changes", NULL},
{"patch", (getter)Diff_patch, NULL, "patch", NULL}, {"patch", (getter)Diff_patch, NULL, "patch", NULL},
{NULL} {NULL}
}; };
PyTypeObject DiffType = { PyTypeObject DiffType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"pygit2.Diff", /* tp_name */ "pygit2.Diff", /* tp_name */

View File

@@ -231,21 +231,33 @@ Tree_getitem(Tree *self, PyObject *value)
PyObject * PyObject *
Tree_diff_tree(Tree *self, PyObject *args) Tree_diff_tree(Tree *self, PyObject *args)
{ {
git_diff_options opts = {0};
git_diff_list *diff;
int err;
Diff *py_diff; Diff *py_diff;
Tree *py_tree; Tree *py_tree;
if (!PyArg_ParseTuple(args, "O!", &TreeType, &py_tree)) { if (!PyArg_ParseTuple(args, "O!", &TreeType, &py_tree))
return NULL; return NULL;
} if (py_tree->repo->repo != self->repo->repo)
return Error_set(GIT_ERROR);
err = git_diff_tree_to_tree(
self->repo->repo,
&opts,
self->tree,
py_tree->tree,
&diff);
if (err < 0)
return Error_set(err);
py_diff = PyObject_New(Diff, &DiffType); py_diff = PyObject_New(Diff, &DiffType);
if (py_diff) { if (py_diff) {
Py_INCREF(py_diff); Py_INCREF(py_diff);
Py_INCREF(py_tree); Py_INCREF(self->repo);
Py_INCREF(self); py_diff->repo = self->repo;
py_diff->diff = diff;
py_diff->t0 = self;
py_diff->t1 = py_tree;
} }
return (PyObject*)py_diff; return (PyObject*)py_diff;