1
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
1
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
4
4
# This program is free software; you can redistribute it and/or modify
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
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
19
class PatchSyntax(Exception):
93
93
return (pos, range)
96
96
def hunk_from_header(line):
98
matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
100
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)
102
(orig, mod) = matches.group(1).split(" ")
103
except (ValueError, IndexError), e:
101
(orig, mod) = line[3:-4].split(" ")
104
103
raise MalformedHunkHeader(str(e), line)
105
104
if not orig.startswith('-') or not mod.startswith('+'):
106
105
raise MalformedHunkHeader("Positions don't start with + or -.", line)
108
107
(orig_pos, orig_range) = parse_range(orig[1:])
109
108
(mod_pos, mod_range) = parse_range(mod[1:])
110
except (ValueError, IndexError), e:
111
110
raise MalformedHunkHeader(str(e), line)
112
111
if mod_range < 0 or orig_range < 0:
113
112
raise MalformedHunkHeader("Hunk range is negative", line)
114
tail = matches.group(3)
115
return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
113
return Hunk(orig_pos, orig_range, mod_pos, mod_range)
164
162
return InsertLine(line[1:])
165
163
elif line.startswith("-"):
166
164
return RemoveLine(line[1:])
168
168
raise MalformedLine("Unknown line type", line)
173
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):
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
181
180
def get_header(self):
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,
181
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
183
self.range_str(self.mod_pos,
192
186
def range_str(self, pos, range):
193
187
"""Return a file range, special-casing for 1-line files.
218
212
return self.shift_to_mod_lines(pos)
220
214
def shift_to_mod_lines(self, pos):
215
assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
221
216
position = self.orig_pos-1
223
218
for line in self.lines:
268
263
def __str__(self):
269
ret = self.get_header()
264
ret = self.get_header()
270
265
ret += "".join([str(h) for h in self.hunks])
273
268
def get_header(self):
274
269
return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
276
def stats_values(self):
277
"""Calculate the number of inserts and removes."""
272
"""Return a string of patch statistics"""
280
275
for hunk in self.hunks:
284
279
elif isinstance(line, RemoveLine):
286
return (inserts, removes, len(self.hunks))
289
"""Return a string of patch statistics"""
290
281
return "%i inserts, %i removes in %i hunks" % \
282
(inserts, removes, len(self.hunks))
293
284
def pos_in_mod(self, position):
294
285
newpos = position
318
309
def parse_patch(iter_lines):
319
iter_lines = iter_lines_handle_nl(iter_lines)
320
310
(orig_name, mod_name) = get_patch_names(iter_lines)
321
311
patch = Patch(orig_name, mod_name)
322
312
for hunk in iter_hunks(iter_lines):
395
385
"""Iterate through a series of lines with a patch applied.
396
386
This handles a single file, and does exact, not fuzzy patching.
398
patch_lines = iter_lines_handle_nl(iter(patch_lines))
388
if orig_lines is not None:
389
orig_lines = orig_lines.__iter__()
391
patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
399
392
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.
412
if orig_lines is not None:
413
orig_lines = iter(orig_lines)
394
for hunk in iter_hunks(patch_lines):
415
395
while line_no < hunk.orig_pos:
416
396
orig_line = orig_lines.next()
427
407
if isinstance(hunk_line, ContextLine):
430
if not isinstance(hunk_line, RemoveLine):
431
raise AssertionError(hunk_line)
410
assert isinstance(hunk_line, RemoveLine)
433
412
if orig_lines is not None:
434
413
for line in orig_lines: