diff: add old and new OID values to Hunk

These can be important, and are necessary to create a patch from this
data.
This commit is contained in:
Carlos Martín Nieto
2012-08-15 20:28:33 +02:00
parent 1138949a69
commit f3dab28082
3 changed files with 17 additions and 0 deletions

View File

@@ -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;

View File

@@ -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}
};

View File

@@ -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()