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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20
class PatchSyntax(Exception):
96
97
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)
173
def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
175
def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
174
176
self.orig_pos = orig_pos
175
177
self.orig_range = orig_range
176
178
self.mod_pos = mod_pos
177
179
self.mod_range = mod_range
180
183
def get_header(self):
181
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
183
self.range_str(self.mod_pos,
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,
186
194
def range_str(self, pos, range):
187
195
"""Return a file range, special-casing for 1-line files.