diff --git a/include/pygit2/types.h b/include/pygit2/types.h index feff58e..27bccda 100644 --- a/include/pygit2/types.h +++ b/include/pygit2/types.h @@ -67,16 +67,21 @@ typedef struct { typedef struct { PyObject_HEAD - git_diff_list* list; + Diff* diff; size_t i; size_t n; } DiffIter; typedef struct { PyObject_HEAD - PyObject* files; PyObject* hunks; -} DiffEntry; + const char * old_file_path; + const char * new_file_path; + char* old_oid; + char* new_oid; + unsigned status; + unsigned similarity; +} Patch; typedef struct { PyObject_HEAD @@ -86,18 +91,11 @@ typedef struct { typedef struct { PyObject_HEAD - const char *header; + PyObject* lines; int old_start; int old_lines; - char* old_oid; - int old_mode; - const char* old_file; int new_start; int new_lines; - char* new_oid; - int new_mode; - const char* new_file; - PyObject *data; } Hunk; typedef struct { diff --git a/src/diff.c b/src/diff.c index c9b4983..b8289d5 100644 --- a/src/diff.c +++ b/src/diff.c @@ -40,123 +40,106 @@ extern PyTypeObject IndexType; extern PyTypeObject DiffType; extern PyTypeObject HunkType; -PyTypeObject DiffEntryType; +PyTypeObject PatchType; PyObject* -diff_get_patch_byindex(git_diff_list* list, size_t i) +diff_get_patch_byindex(git_diff_list* list, size_t idx) { const git_diff_delta* delta; const git_diff_range* range; git_diff_patch* patch = NULL; - - char buffer[41]; - const char* hunk_content; - size_t hunk_amounts, j, hunk_header_len, hunk_lines; + size_t i, j, hunk_amounts, lines_in_hunk, line_len, header_len; + const char* line, *header; int err; + Hunk *py_hunk = NULL; + Patch *py_patch = NULL; - PyObject *file; - Hunk *py_hunk; - DiffEntry *py_entry = NULL; + err = git_diff_get_patch(&patch, &delta, list, idx); + if (err < 0) + return Error_set(err); - err = git_diff_get_patch(&patch, &delta, list, i); + py_patch = (Patch*) PyType_GenericNew(&PatchType, NULL, NULL); + if (py_patch != NULL) { + py_patch->old_file_path = delta->old_file.path; + py_patch->new_file_path = delta->new_file.path; + py_patch->status = delta->status; + py_patch->similarity = delta->similarity; + py_patch->old_oid = git_oid_allocfmt(&delta->old_file.oid); + py_patch->new_oid = git_oid_allocfmt(&delta->new_file.oid); - if (err == GIT_OK) { - py_entry = (DiffEntry*) INSTANCIATE_CLASS(DiffEntryType, NULL); - if (py_entry != NULL) { - if (err == GIT_OK) { - file = Py_BuildValue("(s,s,i,i)", - delta->old_file.path, - delta->new_file.path, - delta->status, - delta->similarity - ); - PyList_Append((PyObject*) py_entry->files, file); - } + hunk_amounts = git_diff_patch_num_hunks(patch); + py_patch->hunks = PyList_New(hunk_amounts); + for (i=0; i < hunk_amounts; ++i) { + err = git_diff_patch_get_hunk(&range, &header, &header_len, + &lines_in_hunk, patch, i); - hunk_amounts = git_diff_patch_num_hunks(patch); + if (err < 0) + goto cleanup; - for (j=0; j < hunk_amounts; ++j) { - err = git_diff_patch_get_hunk(&range, &hunk_content, - &hunk_header_len, &hunk_lines, patch, j); + py_hunk = (Hunk*)PyType_GenericNew(&HunkType, NULL, NULL); + if (py_hunk != NULL) { + py_hunk->old_start = range->old_start; + py_hunk->old_lines = range->old_lines; + py_hunk->new_start = range->new_start; + py_hunk->new_lines = range->new_lines; - if (err == GIT_OK) { - py_hunk = (Hunk*)PyType_GenericNew(&HunkType, NULL, NULL); - if (py_hunk != NULL) { - py_hunk->old_file = delta->old_file.path; - py_hunk->new_file = delta->new_file.path; - py_hunk->header = hunk_content; - py_hunk->old_start = range->old_start; - py_hunk->old_lines = range->old_lines; - py_hunk->new_start = range->new_start; - py_hunk->new_lines = range->new_lines; + py_hunk->lines = PyList_New(lines_in_hunk + 1); + PyList_SetItem(py_hunk->lines, 0, + PyUnicode_FromStringAndSize(header, header_len)); + for (j=1; j < lines_in_hunk + 1; ++j) { + err = git_diff_patch_get_line_in_hunk(NULL, &line, + &line_len, NULL, NULL, patch, i, j - 1); - git_oid_fmt(buffer, &delta->old_file.oid); - py_hunk->old_oid = calloc(41, sizeof(char)); - memcpy(py_hunk->old_oid, buffer, 40); + if (err < 0) + goto cleanup; - git_oid_fmt(buffer, &delta->new_file.oid); - py_hunk->new_oid = calloc(41, sizeof(char)); - memcpy(py_hunk->new_oid, buffer, 40); - - py_hunk->data = Py_BuildValue("(s#,i)", - hunk_content, hunk_header_len, - hunk_lines); - - PyList_Append((PyObject*) py_entry->hunks, - (PyObject*) py_hunk); - } + PyList_SetItem(py_hunk->lines, j, + PyUnicode_FromStringAndSize(line, line_len)); } + + PyList_SetItem((PyObject*) py_patch->hunks, i, + (PyObject*) py_hunk); } } } - if (err < 0) - return Error_set(err); +cleanup: + git_diff_patch_free(patch); - return (PyObject*) py_entry; -} - -PyObject * -DiffEntry_call(DiffEntry *self, PyObject *args, PyObject *kwds) -{ - self->files = PyList_New(0); - if (self->files == NULL) { - Py_XDECREF(self); - return NULL; - } - - self->hunks = PyList_New(0); - if (self->hunks == NULL) { - Py_XDECREF(self); - return NULL; - } - - return (PyObject*) self; + return (err < 0) ? Error_set(err) : (PyObject*) py_patch; } static void -DiffEntry_dealloc(DiffEntry *self) +Patch_dealloc(Patch *self) { - Py_DECREF((PyObject*) self->files); - Py_DECREF((PyObject*) self->hunks); + Py_CLEAR(self->hunks); + free(self->old_oid); + free(self->new_oid); + // we do not have to free old_file_path and new_file_path, they will + // be freed by git_diff_list_free in Diff_dealloc PyObject_Del(self); } -PyMemberDef DiffEntry_members[] = { - MEMBER(DiffEntry, files, T_OBJECT, "files"), - MEMBER(DiffEntry, hunks, T_OBJECT, "hunks"), +PyMemberDef Patch_members[] = { + MEMBER(Patch, old_file_path, T_STRING, "old file path"), + MEMBER(Patch, new_file_path, T_STRING, "new file path"), + MEMBER(Patch, old_oid, T_STRING, "old oid"), + MEMBER(Patch, new_oid, T_STRING, "new oid"), + MEMBER(Patch, status, T_INT, "status"), + MEMBER(Patch, similarity, T_INT, "similarity"), + MEMBER(Patch, hunks, T_OBJECT, "hunks"), {NULL} }; -PyDoc_STRVAR(DiffEntry__doc__, "Diff entry object."); +PyDoc_STRVAR(Patch__doc__, "Diff patch object."); -PyTypeObject DiffEntryType = { +PyTypeObject PatchType = { PyVarObject_HEAD_INIT(NULL, 0) - "_pygit2.DiffEntry", /* tp_name */ - sizeof(DiffEntry), /* tp_basicsize */ + "_pygit2.Patch", /* tp_name */ + sizeof(Patch), /* tp_basicsize */ 0, /* tp_itemsize */ - (destructor)DiffEntry_dealloc, /* tp_dealloc */ + (destructor)Patch_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ @@ -166,13 +149,13 @@ PyTypeObject DiffEntryType = { 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ - (ternaryfunc) DiffEntry_call, /* tp_call */ + 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ - DiffEntry__doc__, /* tp_doc */ + Patch__doc__, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -180,7 +163,7 @@ PyTypeObject DiffEntryType = { 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ - DiffEntry_members, /* tp_members */ + Patch_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -197,7 +180,7 @@ PyObject * DiffIter_iternext(DiffIter *self) { if (self->i < self->n) - return diff_get_patch_byindex(self->list, self->i++); + return diff_get_patch_byindex(self->diff->list, self->i++); PyErr_SetNone(PyExc_StopIteration); return NULL; @@ -206,7 +189,7 @@ DiffIter_iternext(DiffIter *self) void DiffIter_dealloc(DiffIter *self) { - Py_CLEAR(self->list); + Py_CLEAR(self->diff); PyObject_Del(self); } @@ -251,37 +234,39 @@ Diff_patch__get__(Diff *self) { const git_diff_delta* delta; git_diff_patch* patch; - char* str = NULL, *buffer = NULL; - int err = 0; - size_t i, len, num, size; + char **strings = NULL; + char *buffer = NULL; + int err = GIT_ERROR; + size_t i, len, num; PyObject *py_patch = NULL; num = git_diff_num_deltas(self->list); - for (i = 0; i < num ; ++i) { + MALLOC(strings, num * sizeof(char*), cleanup); + + for (i = 0, len = 1; i < num ; ++i) { err = git_diff_get_patch(&patch, &delta, self->list, i); + if (err < 0) + goto cleanup; + + err = git_diff_patch_to_str(&(strings[i]), patch); + if (err < 0) + goto cleanup; - if (err < 0 || (err = git_diff_patch_to_str(&str, patch)) < 0) - goto error; - - len = strlen(str) + 1; - size = (buffer == NULL) ? len : strlen(buffer) + len; - MALLOC(buffer, size, error); - - if (len == size) - strcpy(buffer, str); - else - strcat(buffer, str); - - FREE(str); + len += strlen(strings[i]); + git_diff_patch_free(patch); } + CALLOC(buffer, (len + 1), sizeof(char), cleanup); + for (i = 0; i < num; ++i) { + strcat(buffer, strings[i]); + free(strings[i]); + } + free(strings); + py_patch = PyUnicode_FromString(buffer); + free(buffer); -error: - FREE(str); - FREE(buffer); - FREE_FUNC(patch, git_diff_patch_free); - +cleanup: return (err < 0) ? Error_set(err) : py_patch; } @@ -289,34 +274,16 @@ error: static void Hunk_dealloc(Hunk *self) { - if (self->header != NULL) { - free((void*) self->header); - } - if (self->new_file != NULL) { - free((void*) self->new_file); - } - if (self->old_file != NULL) { - free((void*) self->old_file); - } - Py_XDECREF(self->old_oid); - Py_XDECREF(self->new_oid); - Py_XDECREF(self->data); + Py_CLEAR(self->lines); PyObject_Del(self); } PyMemberDef Hunk_members[] = { - MEMBER(Hunk, header, T_STRING, "Header."), MEMBER(Hunk, old_start, T_INT, "Old start."), MEMBER(Hunk, old_lines, T_INT, "Old lines."), - MEMBER(Hunk, old_mode, T_INT, "Old mode."), - MEMBER(Hunk, old_file, T_STRING, "Old file."), - MEMBER(Hunk, old_oid, T_STRING, "Old oid."), MEMBER(Hunk, new_start, T_INT, "New start."), MEMBER(Hunk, new_lines, T_INT, "New lines."), - MEMBER(Hunk, new_mode, T_INT, "New mode."), - MEMBER(Hunk, new_file, T_STRING, "New file."), - MEMBER(Hunk, new_oid, T_STRING, "New oid."), - MEMBER(Hunk, data, T_OBJECT, "Data."), + MEMBER(Hunk, lines, T_OBJECT, "Lines."), {NULL} }; @@ -393,7 +360,7 @@ Diff_merge(Diff *self, PyObject *args) PyDoc_STRVAR(Diff_find_similar__doc__, "find_similar([flags])\n" "\n" - "Find renamed files in diff."); + "Find renamed files in diff and updates them in-place in the diff itself."); PyObject * Diff_find_similar(Diff *self, PyObject *args) @@ -417,9 +384,9 @@ Diff_iter(Diff *self) DiffIter *iter; iter = PyObject_New(DiffIter, &DiffIterType); - if (iter) { + if (iter != NULL) { Py_INCREF(self); - iter->list = self->list; + iter->diff = self; iter->i = 0; iter->n = git_diff_num_deltas(self->list); } @@ -444,7 +411,7 @@ static void Diff_dealloc(Diff *self) { git_diff_list_free(self->list); - Py_XDECREF(self->repo); + Py_CLEAR(self->repo); PyObject_Del(self); } diff --git a/src/pygit2.c b/src/pygit2.c index e713c0c..09573ef 100644 --- a/src/pygit2.c +++ b/src/pygit2.c @@ -42,7 +42,7 @@ extern PyTypeObject ObjectType; extern PyTypeObject CommitType; extern PyTypeObject DiffType; extern PyTypeObject DiffIterType; -extern PyTypeObject DiffEntryType; +extern PyTypeObject PatchType; extern PyTypeObject HunkType; extern PyTypeObject TreeType; extern PyTypeObject TreeBuilderType; @@ -201,7 +201,7 @@ moduleinit(PyObject* m) return NULL; if (PyType_Ready(&DiffIterType) < 0) return NULL; - if (PyType_Ready(&DiffEntryType) < 0) + if (PyType_Ready(&PatchType) < 0) return NULL; if (PyType_Ready(&HunkType) < 0) return NULL; diff --git a/test/test_diff.py b/test/test_diff.py index 8cb7b2c..54290fc 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -31,7 +31,6 @@ from __future__ import absolute_import from __future__ import unicode_literals import unittest import pygit2 -import itertools from pygit2 import GIT_DIFF_INCLUDE_UNMODIFIED from . import utils @@ -84,22 +83,27 @@ DIFF_WORKDIR_EXPECTED = [ 'subdir/modified_file' ] +HUNK_EXPECTED = """@@ -1 +1 @@ +a contents 2 +a contents +""" + class DiffDirtyTest(utils.DirtyRepoTestCase): def test_diff_empty_index(self): repo = self.repo head = repo[repo.lookup_reference('HEAD').resolve().oid] diff = head.tree.diff(repo.index) - files = [[x[0] for x in entry.files] for entry in diff] - self.assertEqual(DIFF_INDEX_EXPECTED, list(itertools.chain(*files))) + files = [patch.new_file_path for patch in diff] + self.assertEqual(DIFF_INDEX_EXPECTED, files) def test_workdir_to_tree(self): repo = self.repo head = repo[repo.lookup_reference('HEAD').resolve().oid] diff = head.tree.diff() - files = [[x[0] for x in entry.files] for entry in diff] - self.assertEqual(DIFF_WORKDIR_EXPECTED, list(itertools.chain(*files))) + files = [patch.new_file_path for patch in diff] + self.assertEqual(DIFF_WORKDIR_EXPECTED, files) class DiffTest(utils.BareRepoTestCase): @@ -113,8 +117,8 @@ class DiffTest(utils.BareRepoTestCase): head = repo[repo.lookup_reference('HEAD').resolve().oid] diff = head.tree.diff(repo.index) - files = [[x[0].split('/')[0] for x in entry.files] for entry in diff] - self.assertEqual([x.name for x in head.tree], list(itertools.chain(*files))) + files = [patch.new_file_path.split('/')[0] for patch in diff] + self.assertEqual([x.name for x in head.tree], files) def test_diff_tree(self): commit_a = self.repo[COMMIT_SHA1_1] @@ -125,20 +129,17 @@ class DiffTest(utils.BareRepoTestCase): # self.assertIsNotNone is 2.7 only self.assertTrue(diff is not None) # self.assertIn is 2.7 only - self.assertAny(lambda x: ('a', 'a', 3, 0) in x.files, diff) self.assertEqual(2, sum(map(lambda x: len(x.hunks), diff))) - hunk = diff[0].hunks[0] + patch = diff[0] + hunk = patch.hunks[0] self.assertEqual(hunk.old_start, 1) self.assertEqual(hunk.old_lines, 1) self.assertEqual(hunk.new_start, 1) self.assertEqual(hunk.new_lines, 1) - self.assertEqual(hunk.old_file, 'a') - self.assertEqual(hunk.new_file, 'a') - - #self.assertEqual(hunk.data[0][0], b'a contents 2\n') - #self.assertEqual(hunk.data[1][0], b'a contents\n') + self.assertEqual(patch.old_file_path, 'a') + self.assertEqual(patch.new_file_path, 'a') def test_diff_tree_opts(self): commit_c = self.repo[COMMIT_SHA1_3] @@ -168,25 +169,23 @@ class DiffTest(utils.BareRepoTestCase): self.assertTrue(diff_c is not None) # assertIn / assertNotIn are 2.7 only - self.assertAll(lambda x:('b', 'b', 3, 0) not in x.files, diff_b) - self.assertAny(lambda x:('b', 'b', 3, 0) in x.files, diff_c) + self.assertFalse('b' in [patch.new_file_path for patch in diff_b]) + self.assertTrue('b' in [patch.new_file_path for patch in diff_c]) diff_b.merge(diff_c) # assertIn is 2.7 only - self.assertAny(lambda x:('b', 'b', 3, 0) in x.files, diff_b) + self.assertTrue('b' in [patch.new_file_path for patch in diff_b]) - hunk = diff_b[1].hunks[0] + patch = diff_b[0] + hunk = patch.hunks[0] self.assertEqual(hunk.old_start, 1) self.assertEqual(hunk.old_lines, 1) self.assertEqual(hunk.new_start, 1) self.assertEqual(hunk.new_lines, 1) - self.assertEqual(hunk.old_file, 'b') - self.assertEqual(hunk.new_file, 'b') - - #self.assertEqual(hunk.data[0][0], b'b contents\n') - #self.assertEqual(hunk.data[1][0], b'b contents 2\n') + self.assertEqual(patch.old_file_path, 'a') + self.assertEqual(patch.new_file_path, 'a') def test_diff_patch(self): commit_a = self.repo[COMMIT_SHA1_1] @@ -195,23 +194,22 @@ class DiffTest(utils.BareRepoTestCase): diff = commit_a.tree.diff(commit_b.tree) self.assertEqual(diff.patch, PATCH) - def test_diff_header(self): - commit_a = self.repo[COMMIT_SHA1_1] - commit_b = self.repo[COMMIT_SHA1_2] - diff = commit_a.tree.diff(commit_b.tree) - - self.assertEqual(diff[0].hunks[0].header, "@@ -1 +1 @@\n") - def test_diff_oids(self): commit_a = self.repo[COMMIT_SHA1_1] commit_b = self.repo[COMMIT_SHA1_2] - diff = commit_a.tree.diff(commit_b.tree) - hunk = diff[0].hunks[0] - self.assertEqual(hunk.old_oid, + patch = commit_a.tree.diff(commit_b.tree)[0] + self.assertEqual(patch.old_oid, '7f129fd57e31e935c6d60a0c794efe4e6927664b') - self.assertEqual(hunk.new_oid, + self.assertEqual(patch.new_oid, 'af431f20fc541ed6d5afede3e2dc7160f6f01f16') + def test_hunk_content(self): + commit_a = self.repo[COMMIT_SHA1_1] + commit_b = self.repo[COMMIT_SHA1_2] + patch = commit_a.tree.diff(commit_b.tree)[0] + hunk = patch.hunks[0] + self.assertEqual(HUNK_EXPECTED, ''.join(hunk.lines)) + def test_find_similar(self): commit_a = self.repo[COMMIT_SHA1_6] commit_b = self.repo[COMMIT_SHA1_7] @@ -219,10 +217,9 @@ class DiffTest(utils.BareRepoTestCase): #~ Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate #~ --find-copies-harder during rename transformion... diff = commit_a.tree.diff(commit_b.tree, GIT_DIFF_INCLUDE_UNMODIFIED) - entry = ('lorem', 'ipsum', pygit2.GIT_DELTA_RENAMED, 100) - self.assertAll(lambda x: entry not in x.files, diff) + self.assertAll(lambda x: x.status is not pygit2.GIT_DELTA_RENAMED, diff) diff.find_similar() - self.assertAny(lambda x: entry in x.files, diff) + self.assertAny(lambda x: x.status is pygit2.GIT_DELTA_RENAMED, diff) if __name__ == '__main__': unittest.main()