27
32
binary_files_re = 'Binary files (.*) and (.*) differ\n'
30
class PatchSyntax(BzrError):
31
"""Base class for patch syntax errors."""
34
class BinaryFiles(BzrError):
36
_fmt = 'Binary files section encountered.'
38
def __init__(self, orig_name, mod_name):
39
self.orig_name = orig_name
40
self.mod_name = mod_name
43
class MalformedPatchHeader(PatchSyntax):
45
_fmt = "Malformed patch header. %(desc)s\n%(line)r"
47
def __init__(self, desc, line):
52
class MalformedLine(PatchSyntax):
54
_fmt = "Malformed line. %(desc)s\n%(line)r"
56
def __init__(self, desc, line):
61
class PatchConflict(BzrError):
63
_fmt = ('Text contents mismatch at line %(line_no)d. Original has '
64
'"%(orig_line)s", but patch says it should be "%(patch_line)s"')
66
def __init__(self, line_no, orig_line, patch_line):
67
self.line_no = line_no
68
self.orig_line = orig_line.rstrip('\n')
69
self.patch_line = patch_line.rstrip('\n')
72
class MalformedHunkHeader(PatchSyntax):
74
_fmt = "Malformed hunk header. %(desc)s\n%(line)r"
76
def __init__(self, desc, line):
81
35
def get_patch_names(iter_lines):
82
line = next(iter_lines)
36
line = iter_lines.next()
84
38
match = re.match(binary_files_re, line)
85
39
if match is not None:
127
81
raise MalformedHunkHeader("Does not match format.", line)
129
83
(orig, mod) = matches.group(1).split(" ")
130
except (ValueError, IndexError) as e:
84
except (ValueError, IndexError), e:
131
85
raise MalformedHunkHeader(str(e), line)
132
86
if not orig.startswith('-') or not mod.startswith('+'):
133
87
raise MalformedHunkHeader("Positions don't start with + or -.", line)
135
89
(orig_pos, orig_range) = parse_range(orig[1:])
136
90
(mod_pos, mod_range) = parse_range(mod[1:])
137
except (ValueError, IndexError) as e:
91
except (ValueError, IndexError), e:
138
92
raise MalformedHunkHeader(str(e), line)
139
93
if mod_range < 0 or orig_range < 0:
140
94
raise MalformedHunkHeader("Hunk range is negative", line)
292
246
while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
293
hunk_line = parse_line(next(iter_lines))
247
hunk_line = parse_line(iter_lines.next())
294
248
hunk.lines.append(hunk_line)
295
249
if isinstance(hunk_line, (RemoveLine, ContextLine)):
373
327
iter_lines = iter_lines_handle_nl(iter_lines)
375
329
(orig_name, mod_name) = get_patch_names(iter_lines)
376
except BinaryFiles as e:
330
except BinaryFiles, e:
377
331
return BinaryPatch(e.orig_name, e.mod_name)
379
333
patch = Patch(orig_name, mod_name)
529
483
orig_lines = iter(orig_lines)
530
484
for hunk in hunks:
531
485
while line_no < hunk.orig_pos:
532
orig_line = next(orig_lines)
486
orig_line = orig_lines.next()
535
489
for hunk_line in hunk.lines:
537
491
if isinstance(hunk_line, InsertLine):
538
492
yield hunk_line.contents
539
493
elif isinstance(hunk_line, (ContextLine, RemoveLine)):
540
orig_line = next(orig_lines)
494
orig_line = orig_lines.next()
541
495
if orig_line != hunk_line.contents:
542
496
raise PatchConflict(line_no, orig_line, "".join(seen_patch))
543
497
if isinstance(hunk_line, ContextLine):