1
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
1
# Copyright (C) 2004 - 2006 Aaron Bentley
2
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.')
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31
19
class PatchSyntax(Exception):
69
57
def get_patch_names(iter_lines):
71
59
line = iter_lines.next()
72
match = re.match(binary_files_re, line)
74
raise BinaryFiles(match.group(1), match.group(2))
75
60
if not line.startswith("--- "):
76
61
raise MalformedPatchHeader("No orig name", line)
107
92
range = int(range)
108
93
return (pos, range)
111
96
def hunk_from_header(line):
113
matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
115
raise MalformedHunkHeader("Does not match format.", line)
97
if not line.startswith("@@") or not line.endswith("@@\n") \
99
raise MalformedHunkHeader("Does not start and end with @@.", line)
117
(orig, mod) = matches.group(1).split(" ")
118
except (ValueError, IndexError), e:
101
(orig, mod) = line[3:-4].split(" ")
119
103
raise MalformedHunkHeader(str(e), line)
120
104
if not orig.startswith('-') or not mod.startswith('+'):
121
105
raise MalformedHunkHeader("Positions don't start with + or -.", line)
123
107
(orig_pos, orig_range) = parse_range(orig[1:])
124
108
(mod_pos, mod_range) = parse_range(mod[1:])
125
except (ValueError, IndexError), e:
126
110
raise MalformedHunkHeader(str(e), line)
127
111
if mod_range < 0 or orig_range < 0:
128
112
raise MalformedHunkHeader("Hunk range is negative", line)
129
tail = matches.group(3)
130
return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
113
return Hunk(orig_pos, orig_range, mod_pos, mod_range)
179
162
return InsertLine(line[1:])
180
163
elif line.startswith("-"):
181
164
return RemoveLine(line[1:])
183
168
raise MalformedLine("Unknown line type", line)
188
def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
173
def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
189
174
self.orig_pos = orig_pos
190
175
self.orig_range = orig_range
191
176
self.mod_pos = mod_pos
192
177
self.mod_range = mod_range
196
180
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,
181
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
183
self.range_str(self.mod_pos,
207
186
def range_str(self, pos, range):
208
187
"""Return a file range, special-casing for 1-line files.
277
class BinaryPatch(object):
278
258
def __init__(self, oldname, newname):
279
259
self.oldname = oldname
280
260
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)
292
263
def __str__(self):
293
ret = self.get_header()
264
ret = self.get_header()
294
265
ret += "".join([str(h) for h in self.hunks])
297
268
def get_header(self):
298
269
return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
300
def stats_values(self):
301
"""Calculate the number of inserts and removes."""
272
"""Return a string of patch statistics"""
304
275
for hunk in self.hunks:
308
279
elif isinstance(line, RemoveLine):
310
return (inserts, removes, len(self.hunks))
313
"""Return a string of patch statistics"""
314
281
return "%i inserts, %i removes in %i hunks" % \
282
(inserts, removes, len(self.hunks))
317
284
def pos_in_mod(self, position):
318
285
newpos = position
342
309
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)
310
(orig_name, mod_name) = get_patch_names(iter_lines)
311
patch = Patch(orig_name, mod_name)
312
for hunk in iter_hunks(iter_lines):
313
patch.hunks.append(hunk)
355
317
def iter_file_patch(iter_lines):
356
regex = re.compile(binary_files_re)
359
319
for line in iter_lines:
360
320
if line.startswith('=== ') or line.startswith('*** '):
362
322
if line.startswith('#'):
365
if line.startswith('-') or line.startswith(' '):
367
elif line.startswith('--- ') or regex.match(line):
324
elif line.startswith('--- '):
368
325
if len(saved_lines) > 0:
369
326
yield saved_lines
371
elif line.startswith('@@'):
372
hunk = hunk_from_header(line)
373
orig_range = hunk.orig_range
374
328
saved_lines.append(line)
375
329
if len(saved_lines) > 0:
376
330
yield saved_lines
424
378
"""Iterate through a series of lines with a patch applied.
425
379
This handles a single file, and does exact, not fuzzy patching.
427
patch_lines = iter_lines_handle_nl(iter(patch_lines))
381
if orig_lines is not None:
382
orig_lines = orig_lines.__iter__()
384
patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
428
385
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)
387
for hunk in iter_hunks(patch_lines):
444
388
while line_no < hunk.orig_pos:
445
389
orig_line = orig_lines.next()
456
400
if isinstance(hunk_line, ContextLine):
459
if not isinstance(hunk_line, RemoveLine):
460
raise AssertionError(hunk_line)
403
assert isinstance(hunk_line, RemoveLine)
462
405
if orig_lines is not None:
463
406
for line in orig_lines: