1
# Copyright (C) 2004 - 2006 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., 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
19
class PatchSyntax(Exception):
97
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)
175
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):
176
174
self.orig_pos = orig_pos
177
175
self.orig_range = orig_range
178
176
self.mod_pos = mod_pos
179
177
self.mod_range = mod_range
183
180
def get_header(self):
184
if self.tail is None:
187
tail_str = ' ' + self.tail
188
return "@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
190
self.range_str(self.mod_pos,
181
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
183
self.range_str(self.mod_pos,
194
186
def range_str(self, pos, range):
195
187
"""Return a file range, special-casing for 1-line files.
325
317
def iter_file_patch(iter_lines):
328
319
for line in iter_lines:
329
320
if line.startswith('=== ') or line.startswith('*** '):
331
322
if line.startswith('#'):
334
if line.startswith('-') or line.startswith(' '):
336
324
elif line.startswith('--- '):
337
325
if len(saved_lines) > 0:
338
326
yield saved_lines
340
elif line.startswith('@@'):
341
hunk = hunk_from_header(line)
342
orig_range = hunk.orig_range
343
328
saved_lines.append(line)
344
329
if len(saved_lines) > 0:
345
330
yield saved_lines