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:
@@ -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;
|
||||||
|
@@ -38,7 +38,7 @@ static int diff_data_cb(
|
|||||||
if(usage != GIT_DIFF_LINE_ADDITION)
|
if(usage != GIT_DIFF_LINE_ADDITION)
|
||||||
PyBytes_Concat(&hunk->old_data, tmp);
|
PyBytes_Concat(&hunk->old_data, tmp);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int diff_hunk_cb(
|
static int diff_hunk_cb(
|
||||||
@@ -48,46 +48,46 @@ static int diff_hunk_cb(
|
|||||||
const char *header,
|
const char *header,
|
||||||
size_t header_len)
|
size_t header_len)
|
||||||
{
|
{
|
||||||
PyObject *hunks;
|
PyObject *hunks;
|
||||||
Hunk *hunk;
|
Hunk *hunk;
|
||||||
|
|
||||||
hunks = PyDict_GetItemString(cb_data, "hunks");
|
hunks = PyDict_GetItemString(cb_data, "hunks");
|
||||||
if(hunks == NULL) {
|
if(hunks == NULL) {
|
||||||
hunks = PyList_New(0);
|
hunks = PyList_New(0);
|
||||||
PyDict_SetItemString(cb_data, "hunks", hunks);
|
PyDict_SetItemString(cb_data, "hunks", hunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
hunk = (Hunk*) PyType_GenericNew(&HunkType, NULL, NULL);
|
hunk = (Hunk*) PyType_GenericNew(&HunkType, NULL, NULL);
|
||||||
|
|
||||||
hunk->old_start = range->old_start;
|
hunk->old_start = range->old_start;
|
||||||
hunk->old_lines = range->old_lines;
|
hunk->old_lines = range->old_lines;
|
||||||
hunk->new_start = range->new_start;
|
hunk->new_start = range->new_start;
|
||||||
hunk->new_lines = range->new_lines;
|
hunk->new_lines = range->new_lines;
|
||||||
|
|
||||||
int len;
|
int len;
|
||||||
char* old_path, *new_path;
|
char* old_path, *new_path;
|
||||||
|
|
||||||
len = strlen(delta->old_file.path) + 1;
|
len = strlen(delta->old_file.path) + 1;
|
||||||
old_path = malloc(sizeof(char) * len);
|
old_path = malloc(sizeof(char) * len);
|
||||||
memcpy(old_path, delta->old_file.path, len);
|
memcpy(old_path, delta->old_file.path, len);
|
||||||
hunk->old_file = old_path;
|
hunk->old_file = old_path;
|
||||||
|
|
||||||
len = strlen(delta->new_file.path) + 1;
|
len = strlen(delta->new_file.path) + 1;
|
||||||
new_path = malloc(sizeof(char) * len);
|
new_path = malloc(sizeof(char) * len);
|
||||||
memcpy(new_path, delta->new_file.path, len);
|
memcpy(new_path, delta->new_file.path, len);
|
||||||
hunk->new_file = new_path;
|
hunk->new_file = new_path;
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
hunk->old_data = Py_BuildValue("y", "");
|
hunk->old_data = Py_BuildValue("y", "");
|
||||||
hunk->new_data = Py_BuildValue("y", "");
|
hunk->new_data = Py_BuildValue("y", "");
|
||||||
#else
|
#else
|
||||||
hunk->old_data = Py_BuildValue("s", "");
|
hunk->old_data = Py_BuildValue("s", "");
|
||||||
hunk->new_data = Py_BuildValue("s", "");
|
hunk->new_data = Py_BuildValue("s", "");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PyList_Append(hunks, (PyObject*) hunk);
|
PyList_Append(hunks, (PyObject*) hunk);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int diff_file_cb(void *cb_data, git_diff_delta *delta, float progress)
|
static int diff_file_cb(void *cb_data, git_diff_delta *delta, float progress)
|
||||||
@@ -114,74 +114,40 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int diff_print_cb(
|
static int diff_print_cb(
|
||||||
void *cb_data,
|
void *cb_data,
|
||||||
git_diff_delta *delta,
|
git_diff_delta *delta,
|
||||||
git_diff_range *range,
|
git_diff_range *range,
|
||||||
char usage,
|
char usage,
|
||||||
const char *line,
|
const char *line,
|
||||||
size_t line_len)
|
size_t line_len)
|
||||||
{
|
{
|
||||||
PyObject *data = PyBytes_FromStringAndSize(line, line_len);
|
PyObject *data = PyBytes_FromStringAndSize(line, line_len);
|
||||||
PyBytes_ConcatAndDel((PyObject**) cb_data, data);
|
PyBytes_ConcatAndDel((PyObject**) cb_data, data);
|
||||||
|
|
||||||
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 */
|
||||||
|
@@ -231,24 +231,36 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
PySequenceMethods Tree_as_sequence = {
|
PySequenceMethods Tree_as_sequence = {
|
||||||
|
Reference in New Issue
Block a user