1
# Copyright (C) 2004 - 2006 Aaron Bentley
1
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
19
19
class PatchSyntax(Exception):
93
93
return (pos, range)
96
96
def hunk_from_header(line):
97
if not line.startswith("@@") or not line.endswith("@@\n") \
99
raise MalformedHunkHeader("Does not start and end with @@.", line)
98
matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
100
raise MalformedHunkHeader("Does not match format.", line)
101
(orig, mod) = line[3:-4].split(" ")
102
(orig, mod) = matches.group(1).split(" ")
103
except (ValueError, IndexError), e:
103
104
raise MalformedHunkHeader(str(e), line)
104
105
if not orig.startswith('-') or not mod.startswith('+'):
105
106
raise MalformedHunkHeader("Positions don't start with + or -.", line)
107
108
(orig_pos, orig_range) = parse_range(orig[1:])
108
109
(mod_pos, mod_range) = parse_range(mod[1:])
110
except (ValueError, IndexError), e:
110
111
raise MalformedHunkHeader(str(e), line)
111
112
if mod_range < 0 or orig_range < 0:
112
113
raise MalformedHunkHeader("Hunk range is negative", line)
113
return Hunk(orig_pos, orig_range, mod_pos, mod_range)
114
tail = matches.group(3)
115
return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
162
164
return InsertLine(line[1:])
163
165
elif line.startswith("-"):
164
166
return RemoveLine(line[1:])
168
168
raise MalformedLine("Unknown line type", line)
173
def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
173
def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
174
174
self.orig_pos = orig_pos
175
175
self.orig_range = orig_range
176
176
self.mod_pos = mod_pos
177
177
self.mod_range = mod_range
180
181
def get_header(self):
181
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
183
self.range_str(self.mod_pos,
182
if self.tail is None:
185
tail_str = ' ' + self.tail
186
return "@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
188
self.range_str(self.mod_pos,
186
192
def range_str(self, pos, range):
187
193
"""Return a file range, special-casing for 1-line files.
212
218
return self.shift_to_mod_lines(pos)
214
220
def shift_to_mod_lines(self, pos):
215
assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
216
221
position = self.orig_pos-1
218
223
for line in self.lines:
263
268
def __str__(self):
264
ret = self.get_header()
269
ret = self.get_header()
265
270
ret += "".join([str(h) for h in self.hunks])
268
273
def get_header(self):
269
274
return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
272
"""Return a string of patch statistics"""
276
def stats_values(self):
277
"""Calculate the number of inserts and removes."""
275
280
for hunk in self.hunks:
279
284
elif isinstance(line, RemoveLine):
286
return (inserts, removes, len(self.hunks))
289
"""Return a string of patch statistics"""
281
290
return "%i inserts, %i removes in %i hunks" % \
282
(inserts, removes, len(self.hunks))
284
293
def pos_in_mod(self, position):
285
294
newpos = position
309
318
def parse_patch(iter_lines):
319
iter_lines = iter_lines_handle_nl(iter_lines)
310
320
(orig_name, mod_name) = get_patch_names(iter_lines)
311
321
patch = Patch(orig_name, mod_name)
312
322
for hunk in iter_hunks(iter_lines):
317
327
def iter_file_patch(iter_lines):
319
330
for line in iter_lines:
320
331
if line.startswith('=== ') or line.startswith('*** '):
322
333
if line.startswith('#'):
336
if line.startswith('-') or line.startswith(' '):
324
338
elif line.startswith('--- '):
325
339
if len(saved_lines) > 0:
326
340
yield saved_lines
342
elif line.startswith('@@'):
343
hunk = hunk_from_header(line)
344
orig_range = hunk.orig_range
328
345
saved_lines.append(line)
329
346
if len(saved_lines) > 0:
330
347
yield saved_lines
378
395
"""Iterate through a series of lines with a patch applied.
379
396
This handles a single file, and does exact, not fuzzy patching.
381
if orig_lines is not None:
382
orig_lines = orig_lines.__iter__()
398
patch_lines = iter_lines_handle_nl(iter(patch_lines))
399
get_patch_names(patch_lines)
400
return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
403
def iter_patched_from_hunks(orig_lines, hunks):
404
"""Iterate through a series of lines with a patch applied.
405
This handles a single file, and does exact, not fuzzy patching.
407
:param orig_lines: The unpatched lines.
408
:param hunks: An iterable of Hunk instances.
384
patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
385
get_patch_names(patch_lines)
387
for hunk in iter_hunks(patch_lines):
412
if orig_lines is not None:
413
orig_lines = iter(orig_lines)
388
415
while line_no < hunk.orig_pos:
389
416
orig_line = orig_lines.next()
400
427
if isinstance(hunk_line, ContextLine):
403
assert isinstance(hunk_line, RemoveLine)
430
if not isinstance(hunk_line, RemoveLine):
431
raise AssertionError(hunk_line)
405
433
if orig_lines is not None:
406
434
for line in orig_lines: