From f3dab28082c819674724bd05c414cf056ea3b813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 15 Aug 2012 20:28:33 +0200 Subject: [PATCH] diff: add old and new OID values to Hunk These can be important, and are necessary to create a patch from this data. --- include/pygit2/types.h | 2 ++ src/pygit2/diff.c | 8 ++++++++ test/test_diff.py | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/include/pygit2/types.h b/include/pygit2/types.h index d6311d2..59d4055 100644 --- a/include/pygit2/types.h +++ b/include/pygit2/types.h @@ -71,9 +71,11 @@ typedef struct { char *header; int old_start; int old_lines; + PyObject *old_oid; char* old_file; int new_start; int new_lines; + PyObject *new_oid; char* new_file; PyObject *data; } Hunk; diff --git a/src/pygit2/diff.c b/src/pygit2/diff.c index 4d3b3e4..5207b91 100644 --- a/src/pygit2/diff.c +++ b/src/pygit2/diff.c @@ -84,6 +84,7 @@ static int diff_hunk_cb( Hunk *hunk; int len; char* old_path, *new_path; + char oid[GIT_OID_HEXSZ]; hunks = PyDict_GetItemString(cb_data, "hunks"); @@ -101,6 +102,11 @@ static int diff_hunk_cb( hunk->new_start = range->new_start; hunk->new_lines = range->new_lines; + git_oid_fmt(oid, &delta->old_file.oid); + hunk->old_oid = PyUnicode_FromStringAndSize(oid, GIT_OID_HEXSZ); + git_oid_fmt(oid, &delta->new_file.oid); + hunk->new_oid = PyUnicode_FromStringAndSize(oid, GIT_OID_HEXSZ); + if (header) { hunk->header = malloc(header_len+1); @@ -244,9 +250,11 @@ PyMemberDef Hunk_members[] = { {"old_start", T_INT, offsetof(Hunk, old_start), 0, "old start"}, {"old_lines", T_INT, offsetof(Hunk, old_lines), 0, "old lines"}, {"old_file", T_STRING, offsetof(Hunk, old_file), 0, "old file"}, + {"old_oid", T_OBJECT, offsetof(Hunk, old_oid), 0, "old_oid"}, {"new_start", T_INT, offsetof(Hunk, new_start), 0, "new start"}, {"new_lines", T_INT, offsetof(Hunk, new_lines), 0, "new lines"}, {"new_file", T_STRING, offsetof(Hunk, new_file), 0, "old file"}, + {"new_oid", T_OBJECT, offsetof(Hunk, new_oid), 0, "new_oid"}, {"data", T_OBJECT, offsetof(Hunk, data), 0, "data"}, {NULL} }; diff --git a/test/test_diff.py b/test/test_diff.py index 7cf5529..7133bb0 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -181,5 +181,12 @@ class DiffTest(utils.BareRepoTestCase): self.assertEqual(diff.changes['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) + self.assertEqual(diff.changes['hunks'][0].old_oid, '7f129fd57e31e935c6d60a0c794efe4e6927664b') + self.assertEqual(diff.changes['hunks'][0].new_oid, 'af431f20fc541ed6d5afede3e2dc7160f6f01f16') + if __name__ == '__main__': unittest.main()