Fix #113: Fix whitespace in filenames in diff
The 'diff.py' module used to parse unified diff lines like
+++ a/foo/bar /spam/eggs
as
"/foo/bar"
i.e. stripping anything behind whitespace. This is because it tried to
recognize the part after whitespace as a commit revision id. As dulwich
never uses the latter, I simply removed that feature. Whitespace isn't
special-cased anymore now.
Jonas Haag
10 years ago
26 | 26 | """:param udiff: a text in udiff format""" |
27 | 27 | self.lines = [escape(line) for line in udiff.splitlines()] |
28 | 28 | |
29 | def _extract_rev(self, line1, line2): | |
30 | def _extract(line): | |
31 | parts = line.split(None, 1) | |
32 | if parts[0].startswith(('a/', 'b/')): | |
33 | parts[0] = parts[0][2:] | |
34 | return parts[0], (len(parts) == 2 and parts[1] or None) | |
35 | try: | |
36 | if line1.startswith('--- ') and line2.startswith('+++ '): | |
37 | return _extract(line1[4:]), _extract(line2[4:]) | |
38 | except (ValueError, IndexError): | |
39 | pass | |
40 | return (None, None), (None, None) | |
29 | def _extract_filename(self, line): | |
30 | """ | |
31 | Extract file name from unified diff line: | |
32 | --- a/foo/bar ==> foo/bar | |
33 | +++ b/foo/bar ==> foo/bar | |
34 | """ | |
35 | if line.startswith(("--- /dev/null", "+++ /dev/null")): | |
36 | return line[len("--- "):] | |
37 | else: | |
38 | return line[len("--- a/"):] | |
41 | 39 | |
42 | 40 | def _highlight_line(self, line, next): |
43 | 41 | """Highlight inline changes in both lines.""" |
90 | 88 | |
91 | 89 | in_header = False |
92 | 90 | chunks = [] |
93 | old, new = self._extract_rev(line, lineiter.next()) | |
94 | 91 | adds, dels = 0, 0 |
95 | 92 | files.append({ |
96 | 93 | 'is_header': False, |
97 | 'old_filename': old[0], | |
98 | 'old_revision': old[1], | |
99 | 'new_filename': new[0], | |
100 | 'new_revision': new[1], | |
94 | 'old_filename': self._extract_filename(line), | |
95 | 'new_filename': self._extract_filename(lineiter.next()), | |
101 | 96 | 'additions': adds, |
102 | 97 | 'deletions': dels, |
103 | 98 | 'chunks': chunks |