14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from __future__ import absolute_import
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
23
class BinaryFiles(Exception):
27
binary_files_re = b'Binary files (.*) and (.*) differ\n'
30
class PatchSyntax(BzrError):
31
"""Base class for patch syntax errors."""
34
class BinaryFiles(BzrError):
36
_fmt = 'Binary files section encountered.'
25
38
def __init__(self, orig_name, mod_name):
26
39
self.orig_name = orig_name
27
40
self.mod_name = mod_name
28
Exception.__init__(self, 'Binary files section encountered.')
31
class PatchSyntax(Exception):
32
def __init__(self, msg):
33
Exception.__init__(self, msg)
36
43
class MalformedPatchHeader(PatchSyntax):
37
def __init__(self, desc, line):
40
msg = "Malformed patch header. %s\n%r" % (self.desc, self.line)
41
PatchSyntax.__init__(self, msg)
44
class MalformedHunkHeader(PatchSyntax):
45
def __init__(self, desc, line):
48
msg = "Malformed hunk header. %s\n%r" % (self.desc, self.line)
49
PatchSyntax.__init__(self, msg)
45
_fmt = "Malformed patch header. %(desc)s\n%(line)r"
47
def __init__(self, desc, line):
52
52
class MalformedLine(PatchSyntax):
54
_fmt = "Malformed line. %(desc)s\n%(line)r"
53
56
def __init__(self, desc, line):
56
msg = "Malformed line. %s\n%s" % (self.desc, self.line)
57
PatchSyntax.__init__(self, msg)
60
class PatchConflict(Exception):
61
class PatchConflict(BzrError):
63
_fmt = ('Text contents mismatch at line %(line_no)d. Original has '
64
'"%(orig_line)s", but patch says it should be "%(patch_line)s"')
61
66
def __init__(self, line_no, orig_line, patch_line):
62
orig = orig_line.rstrip('\n')
63
patch = str(patch_line).rstrip('\n')
64
msg = 'Text contents mismatch at line %d. Original has "%s",'\
65
' but patch says it should be "%s"' % (line_no, orig, patch)
66
Exception.__init__(self, msg)
67
self.line_no = line_no
68
self.orig_line = orig_line.rstrip('\n')
69
self.patch_line = patch_line.rstrip('\n')
72
class MalformedHunkHeader(PatchSyntax):
74
_fmt = "Malformed hunk header. %(desc)s\n%(line)r"
76
def __init__(self, desc, line):
69
81
def get_patch_names(iter_lines):
82
line = next(iter_lines)
71
line = iter_lines.next()
72
84
match = re.match(binary_files_re, line)
73
85
if match is not None:
74
86
raise BinaryFiles(match.group(1), match.group(2))
75
if not line.startswith("--- "):
87
if not line.startswith(b"--- "):
76
88
raise MalformedPatchHeader("No orig name", line)
78
orig_name = line[4:].rstrip("\n")
90
orig_name = line[4:].rstrip(b"\n")
79
91
except StopIteration:
80
92
raise MalformedPatchHeader("No orig line", "")
82
line = iter_lines.next()
83
if not line.startswith("+++ "):
94
line = next(iter_lines)
95
if not line.startswith(b"+++ "):
84
96
raise PatchSyntax("No mod name")
86
mod_name = line[4:].rstrip("\n")
98
mod_name = line[4:].rstrip(b"\n")
87
99
except StopIteration:
88
100
raise MalformedPatchHeader("No mod line", "")
89
101
return (orig_name, mod_name)
111
123
def hunk_from_header(line):
113
matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
125
matches = re.match(br'\@\@ ([^@]*) \@\@( (.*))?\n', line)
114
126
if matches is None:
115
127
raise MalformedHunkHeader("Does not match format.", line)
117
(orig, mod) = matches.group(1).split(" ")
118
except (ValueError, IndexError), e:
129
(orig, mod) = matches.group(1).split(b" ")
130
except (ValueError, IndexError) as e:
119
131
raise MalformedHunkHeader(str(e), line)
120
if not orig.startswith('-') or not mod.startswith('+'):
132
if not orig.startswith(b'-') or not mod.startswith(b'+'):
121
133
raise MalformedHunkHeader("Positions don't start with + or -.", line)
123
135
(orig_pos, orig_range) = parse_range(orig[1:])
124
136
(mod_pos, mod_range) = parse_range(mod[1:])
125
except (ValueError, IndexError), e:
137
except (ValueError, IndexError) as e:
126
138
raise MalformedHunkHeader(str(e), line)
127
139
if mod_range < 0 or orig_range < 0:
128
140
raise MalformedHunkHeader("Hunk range is negative", line)
130
142
return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
145
class HunkLine(object):
134
147
def __init__(self, contents):
135
148
self.contents = contents
137
150
def get_str(self, leadchar):
138
if self.contents == "\n" and leadchar == " " and False:
140
if not self.contents.endswith('\n'):
141
terminator = '\n' + NO_NL
151
if self.contents == b"\n" and leadchar == b" " and False:
153
if not self.contents.endswith(b'\n'):
154
terminator = b'\n' + NO_NL
144
157
return leadchar + self.contents + terminator
160
raise NotImplementedError
147
163
class ContextLine(HunkLine):
148
165
def __init__(self, contents):
149
166
HunkLine.__init__(self, contents)
152
return self.get_str(" ")
169
return self.get_str(b" ")
155
172
class InsertLine(HunkLine):
156
173
def __init__(self, contents):
157
174
HunkLine.__init__(self, contents)
160
return self.get_str("+")
177
return self.get_str(b"+")
163
180
class RemoveLine(HunkLine):
164
181
def __init__(self, contents):
165
182
HunkLine.__init__(self, contents)
168
return self.get_str("-")
170
NO_NL = '\\ No newline at end of file\n'
171
__pychecker__="no-returnvalues"
185
return self.get_str(b"-")
188
NO_NL = b'\\ No newline at end of file\n'
189
__pychecker__ = "no-returnvalues"
173
192
def parse_line(line):
174
if line.startswith("\n"):
193
if line.startswith(b"\n"):
175
194
return ContextLine(line)
176
elif line.startswith(" "):
195
elif line.startswith(b" "):
177
196
return ContextLine(line[1:])
178
elif line.startswith("+"):
197
elif line.startswith(b"+"):
179
198
return InsertLine(line[1:])
180
elif line.startswith("-"):
199
elif line.startswith(b"-"):
181
200
return RemoveLine(line[1:])
183
202
raise MalformedLine("Unknown line type", line)
188
210
def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
189
211
self.orig_pos = orig_pos
190
212
self.orig_range = orig_range
214
236
:return: a string in the format 1,4 except when range == pos == 1
219
return "%i,%i" % (pos, range)
241
return b"%i,%i" % (pos, range)
222
244
lines = [self.get_header()]
223
245
for line in self.lines:
224
lines.append(str(line))
225
return "".join(lines)
246
lines.append(line.as_bytes())
247
return b"".join(lines)
227
251
def shift_to_mod(self, pos):
228
if pos < self.orig_pos-1:
252
if pos < self.orig_pos - 1:
230
elif pos > self.orig_pos+self.orig_range:
254
elif pos > self.orig_pos + self.orig_range:
231
255
return self.mod_range - self.orig_range
233
257
return self.shift_to_mod_lines(pos)
235
259
def shift_to_mod_lines(self, pos):
236
position = self.orig_pos-1
260
position = self.orig_pos - 1
238
262
for line in self.lines:
239
263
if isinstance(line, InsertLine):
380
405
first patch are stripped away in iter_hunks() if it is also passed
381
406
allow_dirty=True. Default False.
383
### FIXME: Docstring is not quite true. We allow certain comments no
408
# FIXME: Docstring is not quite true. We allow certain comments no
384
409
# matter what, If they startwith '===', '***', or '#' Someone should
385
410
# reexamine this logic and decide if we should include those in
386
411
# allow_dirty or restrict those to only being before the patch is found
387
412
# (as allow_dirty does).
388
413
regex = re.compile(binary_files_re)
392
419
for line in iter_lines:
393
if line.startswith('=== ') or line.startswith('*** '):
395
if line.startswith('#'):
420
if line.startswith(b'=== '):
421
if len(saved_lines) > 0:
422
if keep_dirty and len(dirty_head) > 0:
423
yield {'saved_lines': saved_lines,
424
'dirty_head': dirty_head}
429
dirty_head.append(line)
431
if line.startswith(b'*** '):
433
if line.startswith(b'#'):
397
435
elif orig_range > 0:
398
if line.startswith('-') or line.startswith(' '):
436
if line.startswith(b'-') or line.startswith(b' '):
400
elif line.startswith('--- ') or regex.match(line):
438
elif line.startswith(b'--- ') or regex.match(line):
401
439
if allow_dirty and beginning:
402
440
# Patches can have "junk" at the beginning
403
441
# Stripping junk from the end of patches is handled when we
404
442
# parse the patch
405
443
beginning = False
406
444
elif len(saved_lines) > 0:
445
if keep_dirty and len(dirty_head) > 0:
446
yield {'saved_lines': saved_lines,
447
'dirty_head': dirty_head}
409
elif line.startswith('@@'):
452
elif line.startswith(b'@@'):
410
453
hunk = hunk_from_header(line)
411
454
orig_range = hunk.orig_range
412
455
saved_lines.append(line)
413
456
if len(saved_lines) > 0:
457
if keep_dirty and len(dirty_head) > 0:
458
yield {'saved_lines': saved_lines,
459
'dirty_head': dirty_head}
417
464
def iter_lines_handle_nl(iter_lines):
438
def parse_patches(iter_lines, allow_dirty=False):
485
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
440
487
:arg iter_lines: iterable of lines to parse for patches
441
488
:kwarg allow_dirty: If True, allow text that's not part of the patch at
442
489
selected places. This includes comments before and after a patch
443
490
for instance. Default False.
491
:kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
445
return [parse_patch(f.__iter__(), allow_dirty) for f in
446
iter_file_patch(iter_lines, allow_dirty)]
494
for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
495
if 'dirty_head' in patch_lines:
496
yield ({'patch': parse_patch(patch_lines['saved_lines'], allow_dirty),
497
'dirty_head': patch_lines['dirty_head']})
499
yield parse_patch(patch_lines, allow_dirty)
449
502
def difference_index(atext, btext):