Merge lp:~cjwatson/launchpad/git-patch-headers into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 17835
Proposed branch: lp:~cjwatson/launchpad/git-patch-headers
Merge into: lp:launchpad
Diff against target: 89 lines (+20/-5)
2 files modified
lib/lp/code/mail/patches.py (+11/-0)
lib/lp/code/mail/tests/test_codereviewcomment.py (+9/-5)
To merge this branch: bzr merge lp:~cjwatson/launchpad/git-patch-headers
Reviewer Review Type Date Requested Status
Kit Randel (community) Approve
Review via email: mp+275792@code.launchpad.net

Commit message

Parse extended header lines in git diffs correctly.

Description of the change

Parse extended header lines in git diffs correctly.

https://git.kernel.org/cgit/git/git.git/tree/Documentation/diff-generate-patch.txt documents the file format here. Rather than either (a) hardcoding the longish list of possible extended header lines or (b) being more liberal with all "dirty header" lines and risking disturbing parsing of Bazaar diffs, I opted for (c) instead, namely to accept anything starting with a lower-case letter as an extended header line provided that it hasn't already matched one of dirty_headers, but only if it's after a line starting with "diff --git".

To post a comment you must log in.
Revision history for this message
Kit Randel (blr) wrote :

That seems like a reasonably future-proof approach.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/mail/patches.py'
2--- lib/lp/code/mail/patches.py 2015-07-09 05:40:01 +0000
3+++ lib/lp/code/mail/patches.py 2015-10-27 13:03:57 +0000
4@@ -360,6 +360,7 @@
5 dirty_head = []
6 orig_range = 0
7 beginning = True
8+ in_git_patch = False
9
10 dirty_headers = ('=== ', 'diff ', 'index ')
11 for line in iter_lines:
12@@ -372,7 +373,16 @@
13 dirty_head = []
14 else:
15 yield saved_lines
16+ in_git_patch = False
17 saved_lines = []
18+ if line.startswith('diff --git'):
19+ in_git_patch = True
20+ dirty_head.append(line)
21+ continue
22+ if in_git_patch and line and line[0].islower():
23+ # Extended header line in a git diff. All extended header lines
24+ # currently start with a lower-case character, and nothing else
25+ # in the patch before the next "diff" header line can do so.
26 dirty_head.append(line)
27 continue
28 if line.startswith('*** '):
29@@ -395,6 +405,7 @@
30 dirty_head = []
31 else:
32 yield saved_lines
33+ in_git_patch = False
34 saved_lines = []
35 elif line.startswith('@@'):
36 hunk = hunk_from_header(line)
37
38=== modified file 'lib/lp/code/mail/tests/test_codereviewcomment.py'
39--- lib/lp/code/mail/tests/test_codereviewcomment.py 2015-09-11 12:20:23 +0000
40+++ lib/lp/code/mail/tests/test_codereviewcomment.py 2015-10-27 13:03:57 +0000
41@@ -468,7 +468,9 @@
42 "-bar\n"
43 "+baz\n"
44 "diff --git a/fulano b/fulano\n"
45- "index 5716ca5..7601807 100644\n"
46+ "old mode 100644\n"
47+ "new mode 100755\n"
48+ "index 5716ca5..7601807\n"
49 "--- a/fulano\n"
50 "+++ b/fulano\n"
51 "@@ -1,3 +1,3 @@\n"
52@@ -556,7 +558,7 @@
53 self.getSection(comments).splitlines()[7:11])
54
55 def test_comments_in_git_diff(self):
56- comments = {'1': 'foo', '5': 'bar', '15': 'baz'}
57+ comments = {'1': 'foo', '5': 'bar', '17': 'baz'}
58 section = self.getSection(comments, diff_text=self.git_diff_text)
59 self.assertEqual(
60 map(unicode, [
61@@ -574,7 +576,9 @@
62 "> -bar",
63 "> +baz",
64 "> diff --git a/fulano b/fulano",
65- "> index 5716ca5..7601807 100644",
66+ "> old mode 100644",
67+ "> new mode 100755",
68+ "> index 5716ca5..7601807",
69 "> --- a/fulano",
70 "> +++ b/fulano",
71 "> @@ -1,3 +1,3 @@",
72@@ -585,7 +589,7 @@
73 "baz",
74 "",
75 "> +zutano"]),
76- section.splitlines()[4:29])
77+ section.splitlines()[4:31])
78
79 def test_commentless_hunks_ignored(self):
80 # Hunks without inline comments are not returned in the diff text.
81@@ -694,7 +698,7 @@
82 self.getSection(comments).splitlines()[6:12])
83
84 def test_multiple_comments(self):
85- # Multiple inline comments are redered appropriately.
86+ # Multiple inline comments are rendered appropriately.
87 comments = {'4': 'Foo', '5': 'Bar'}
88 self.assertEqual(
89 ['> +++ bar.py\t1969-12-31 19:00:00.000000000 -0500',