diff --git a/src/diff.c b/src/diff.c index 8e87c70..0994209 100644 --- a/src/diff.c +++ b/src/diff.c @@ -50,6 +50,7 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx) git_diff_patch* patch = NULL; size_t i, j, hunk_amounts, lines_in_hunk, line_len, header_len; const char* line, *header; + char line_origin; int err; Hunk *py_hunk = NULL; Patch *py_patch = NULL; @@ -84,18 +85,19 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx) 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, - to_unicode_n(header, header_len, NULL, NULL)); - for (j=1; j < lines_in_hunk + 1; ++j) { - err = git_diff_patch_get_line_in_hunk(&py_hunk->origin, - &line, &line_len, NULL, NULL, patch, i, j - 1); + py_hunk->lines = PyList_New(lines_in_hunk); + for (j=0; j < lines_in_hunk; ++j) { + err = git_diff_patch_get_line_in_hunk(&line_origin, + &line, &line_len, NULL, NULL, patch, i, j); if (err < 0) goto cleanup; PyList_SetItem(py_hunk->lines, j, - to_unicode_n(line, line_len, NULL, NULL)); + Py_BuildValue("cO", line_origin, + to_unicode_n(line, line_len, NULL, NULL) + ) + ); } PyList_SetItem((PyObject*) py_patch->hunks, i, @@ -279,7 +281,6 @@ Hunk_dealloc(Hunk *self) } PyMemberDef Hunk_members[] = { - MEMBER(Hunk, origin, T_CHAR, "origin."), MEMBER(Hunk, old_start, T_INT, "Old start."), MEMBER(Hunk, old_lines, T_INT, "Old lines."), MEMBER(Hunk, new_start, T_INT, "New start."), diff --git a/src/types.h b/src/types.h index a1a0105..3c559ea 100644 --- a/src/types.h +++ b/src/types.h @@ -118,7 +118,6 @@ typedef struct { typedef struct { PyObject_HEAD PyObject* lines; - char origin; int old_start; int old_lines; int new_start; diff --git a/test/test_diff.py b/test/test_diff.py index 3246b51..7dd5a01 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -83,9 +83,8 @@ DIFF_WORKDIR_EXPECTED = [ 'subdir/modified_file' ] -HUNK_EXPECTED = """@@ -1 +1 @@ -a contents 2 -a contents +HUNK_EXPECTED = """- a contents 2 ++ a contents """ class DiffDirtyTest(utils.DirtyRepoTestCase): @@ -134,7 +133,6 @@ class DiffTest(utils.BareRepoTestCase): patch = diff[0] hunk = patch.hunks[0] - self.assertEqual(hunk.origin, '+') self.assertEqual(hunk.old_start, 1) self.assertEqual(hunk.old_lines, 1) self.assertEqual(hunk.new_start, 1) @@ -216,7 +214,8 @@ class DiffTest(utils.BareRepoTestCase): 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)) + lines = ('{0} {1}'.format(*x) for x in hunk.lines) + self.assertEqual(HUNK_EXPECTED, ''.join(lines)) def test_find_similar(self): commit_a = self.repo[COMMIT_SHA1_6]