diff: expose the hunk header

We can rebuild it from the data we already have, but this will allow
us to get the function headers in it once the library supports it.
This commit is contained in:
Carlos Martín Nieto
2012-08-15 19:58:31 +02:00
parent db35bda219
commit 11d0c9a30f
3 changed files with 18 additions and 0 deletions

View File

@@ -68,6 +68,7 @@ typedef struct {
typedef struct {
PyObject_HEAD
char *header;
int old_start;
int old_lines;
char* old_file;

View File

@@ -101,6 +101,16 @@ static int diff_hunk_cb(
hunk->new_start = range->new_start;
hunk->new_lines = range->new_lines;
if (header) {
hunk->header = malloc(header_len+1);
if (hunk->header == NULL)
return -1;
memcpy(hunk->header, header, header_len);
hunk->header[header_len] = '\0';
}
if (delta->old_file.path != NULL) {
len = strlen(delta->old_file.path) + 1;
old_path = malloc(sizeof(char) * len);
@@ -219,6 +229,7 @@ Hunk_dealloc(Hunk *self)
}
PyMemberDef Hunk_members[] = {
{"header", T_STRING, offsetof(Hunk, header), 0, "header"},
{"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"},

View File

@@ -174,6 +174,12 @@ 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.changes['hunks'][0].header, "@@ -1 +1 @@\n")
if __name__ == '__main__':
unittest.main()