1
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
23
class BinaryFiles(Exception):
25
def __init__(self, orig_name, mod_name):
26
self.orig_name = orig_name
27
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
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)
52
class MalformedLine(PatchSyntax):
53
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
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)
69
def get_patch_names(iter_lines):
71
line = iter_lines.next()
72
match = re.match(binary_files_re, line)
74
raise BinaryFiles(match.group(1), match.group(2))
75
if not line.startswith("--- "):
76
raise MalformedPatchHeader("No orig name", line)
78
orig_name = line[4:].rstrip("\n")
80
raise MalformedPatchHeader("No orig line", "")
82
line = iter_lines.next()
83
if not line.startswith("+++ "):
84
raise PatchSyntax("No mod name")
86
mod_name = line[4:].rstrip("\n")
88
raise MalformedPatchHeader("No mod line", "")
89
return (orig_name, mod_name)
92
def parse_range(textrange):
93
"""Parse a patch range, handling the "1" special-case
95
:param textrange: The text to parse
97
:return: the position and range, as a tuple
100
tmp = textrange.split(',')
111
def hunk_from_header(line):
113
matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
115
raise MalformedHunkHeader("Does not match format.", line)
117
(orig, mod) = matches.group(1).split(" ")
118
except (ValueError, IndexError), e:
119
raise MalformedHunkHeader(str(e), line)
120
if not orig.startswith('-') or not mod.startswith('+'):
121
raise MalformedHunkHeader("Positions don't start with + or -.", line)
123
(orig_pos, orig_range) = parse_range(orig[1:])
124
(mod_pos, mod_range) = parse_range(mod[1:])
125
except (ValueError, IndexError), e:
126
raise MalformedHunkHeader(str(e), line)
127
if mod_range < 0 or orig_range < 0:
128
raise MalformedHunkHeader("Hunk range is negative", line)
129
tail = matches.group(3)
130
return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
134
def __init__(self, contents):
135
self.contents = contents
137
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
144
return leadchar + self.contents + terminator
147
class ContextLine(HunkLine):
148
def __init__(self, contents):
149
HunkLine.__init__(self, contents)
152
return self.get_str(" ")
155
class InsertLine(HunkLine):
156
def __init__(self, contents):
157
HunkLine.__init__(self, contents)
160
return self.get_str("+")
163
class RemoveLine(HunkLine):
164
def __init__(self, contents):
165
HunkLine.__init__(self, contents)
168
return self.get_str("-")
170
NO_NL = '\\ No newline at end of file\n'
171
__pychecker__="no-returnvalues"
173
def parse_line(line):
174
if line.startswith("\n"):
175
return ContextLine(line)
176
elif line.startswith(" "):
177
return ContextLine(line[1:])
178
elif line.startswith("+"):
179
return InsertLine(line[1:])
180
elif line.startswith("-"):
181
return RemoveLine(line[1:])
183
raise MalformedLine("Unknown line type", line)
188
def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
189
self.orig_pos = orig_pos
190
self.orig_range = orig_range
191
self.mod_pos = mod_pos
192
self.mod_range = mod_range
196
def get_header(self):
197
if self.tail is None:
200
tail_str = ' ' + self.tail
201
return "@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
203
self.range_str(self.mod_pos,
207
def range_str(self, pos, range):
208
"""Return a file range, special-casing for 1-line files.
210
:param pos: The position in the file
212
:range: The range in the file
214
:return: a string in the format 1,4 except when range == pos == 1
219
return "%i,%i" % (pos, range)
222
lines = [self.get_header()]
223
for line in self.lines:
224
lines.append(str(line))
225
return "".join(lines)
227
def shift_to_mod(self, pos):
228
if pos < self.orig_pos-1:
230
elif pos > self.orig_pos+self.orig_range:
231
return self.mod_range - self.orig_range
233
return self.shift_to_mod_lines(pos)
235
def shift_to_mod_lines(self, pos):
236
position = self.orig_pos-1
238
for line in self.lines:
239
if isinstance(line, InsertLine):
241
elif isinstance(line, RemoveLine):
246
elif isinstance(line, ContextLine):
253
def iter_hunks(iter_lines):
255
for line in iter_lines:
263
hunk = hunk_from_header(line)
266
while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
267
hunk_line = parse_line(iter_lines.next())
268
hunk.lines.append(hunk_line)
269
if isinstance(hunk_line, (RemoveLine, ContextLine)):
271
if isinstance(hunk_line, (InsertLine, ContextLine)):
277
class BinaryPatch(object):
278
def __init__(self, oldname, newname):
279
self.oldname = oldname
280
self.newname = newname
283
return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
286
class Patch(BinaryPatch):
288
def __init__(self, oldname, newname):
289
BinaryPatch.__init__(self, oldname, newname)
293
ret = self.get_header()
294
ret += "".join([str(h) for h in self.hunks])
297
def get_header(self):
298
return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
300
def stats_values(self):
301
"""Calculate the number of inserts and removes."""
304
for hunk in self.hunks:
305
for line in hunk.lines:
306
if isinstance(line, InsertLine):
308
elif isinstance(line, RemoveLine):
310
return (inserts, removes, len(self.hunks))
313
"""Return a string of patch statistics"""
314
return "%i inserts, %i removes in %i hunks" % \
317
def pos_in_mod(self, position):
319
for hunk in self.hunks:
320
shift = hunk.shift_to_mod(position)
326
def iter_inserted(self):
327
"""Iteraties through inserted lines
329
:return: Pair of line number, line
330
:rtype: iterator of (int, InsertLine)
332
for hunk in self.hunks:
333
pos = hunk.mod_pos - 1;
334
for line in hunk.lines:
335
if isinstance(line, InsertLine):
338
if isinstance(line, ContextLine):
342
def parse_patch(iter_lines):
343
iter_lines = iter_lines_handle_nl(iter_lines)
345
(orig_name, mod_name) = get_patch_names(iter_lines)
346
except BinaryFiles, e:
347
return BinaryPatch(e.orig_name, e.mod_name)
349
patch = Patch(orig_name, mod_name)
350
for hunk in iter_hunks(iter_lines):
351
patch.hunks.append(hunk)
355
def iter_file_patch(iter_lines):
356
regex = re.compile(binary_files_re)
359
for line in iter_lines:
360
if line.startswith('=== ') or line.startswith('*** '):
362
if line.startswith('#'):
365
if line.startswith('-') or line.startswith(' '):
367
elif line.startswith('--- ') or regex.match(line):
368
if len(saved_lines) > 0:
371
elif line.startswith('@@'):
372
hunk = hunk_from_header(line)
373
orig_range = hunk.orig_range
374
saved_lines.append(line)
375
if len(saved_lines) > 0:
379
def iter_lines_handle_nl(iter_lines):
381
Iterates through lines, ensuring that lines that originally had no
382
terminating \n are produced without one. This transformation may be
383
applied at any point up until hunk line parsing, and is safe to apply
387
for line in iter_lines:
389
if not last_line.endswith('\n'):
390
raise AssertionError()
391
last_line = last_line[:-1]
393
if last_line is not None:
396
if last_line is not None:
400
def parse_patches(iter_lines):
401
return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
404
def difference_index(atext, btext):
405
"""Find the indext of the first character that differs between two texts
407
:param atext: The first text
409
:param btext: The second text
411
:return: The index, or None if there are no differences within the range
412
:rtype: int or NoneType
415
if len(btext) < length:
417
for i in range(length):
418
if atext[i] != btext[i]:
423
def iter_patched(orig_lines, patch_lines):
424
"""Iterate through a series of lines with a patch applied.
425
This handles a single file, and does exact, not fuzzy patching.
427
patch_lines = iter_lines_handle_nl(iter(patch_lines))
428
get_patch_names(patch_lines)
429
return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
432
def iter_patched_from_hunks(orig_lines, hunks):
433
"""Iterate through a series of lines with a patch applied.
434
This handles a single file, and does exact, not fuzzy patching.
436
:param orig_lines: The unpatched lines.
437
:param hunks: An iterable of Hunk instances.
441
if orig_lines is not None:
442
orig_lines = iter(orig_lines)
444
while line_no < hunk.orig_pos:
445
orig_line = orig_lines.next()
448
for hunk_line in hunk.lines:
449
seen_patch.append(str(hunk_line))
450
if isinstance(hunk_line, InsertLine):
451
yield hunk_line.contents
452
elif isinstance(hunk_line, (ContextLine, RemoveLine)):
453
orig_line = orig_lines.next()
454
if orig_line != hunk_line.contents:
455
raise PatchConflict(line_no, orig_line, "".join(seen_patch))
456
if isinstance(hunk_line, ContextLine):
459
if not isinstance(hunk_line, RemoveLine):
460
raise AssertionError(hunk_line)
462
if orig_lines is not None:
463
for line in orig_lines: