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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from __future__ import absolute_import
27
20
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.'
23
class BinaryFiles(Exception):
38
25
def __init__(self, orig_name, mod_name):
39
26
self.orig_name = orig_name
40
27
self.mod_name = mod_name
28
Exception.__init__(self, 'Binary files section encountered.')
31
class PatchSyntax(Exception):
32
def __init__(self, msg):
33
Exception.__init__(self, msg)
43
36
class MalformedPatchHeader(PatchSyntax):
45
_fmt = "Malformed patch header. %(desc)s\n%(line)r"
47
def __init__(self, desc, line):
37
def __init__(self, desc, line):
40
msg = "Malformed patch header. %s\n%r" % (self.desc, self.line)
41
PatchSyntax.__init__(self, msg)
44
class MalformedHunkHeader(PatchSyntax):
45
def __init__(self, desc, line):
48
msg = "Malformed hunk header. %s\n%r" % (self.desc, self.line)
49
PatchSyntax.__init__(self, msg)
52
52
class MalformedLine(PatchSyntax):
54
_fmt = "Malformed line. %(desc)s\n%(line)r"
56
53
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"')
56
msg = "Malformed line. %s\n%s" % (self.desc, self.line)
57
PatchSyntax.__init__(self, msg)
60
class PatchConflict(Exception):
66
61
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):
62
orig = orig_line.rstrip('\n')
63
patch = str(patch_line).rstrip('\n')
64
msg = 'Text contents mismatch at line %d. Original has "%s",'\
65
' but patch says it should be "%s"' % (line_no, orig, patch)
66
Exception.__init__(self, msg)
81
69
def get_patch_names(iter_lines):
82
line = next(iter_lines)
71
line = iter_lines.next()
84
72
match = re.match(binary_files_re, line)
85
73
if match is not None:
86
74
raise BinaryFiles(match.group(1), match.group(2))
127
115
raise MalformedHunkHeader("Does not match format.", line)
129
117
(orig, mod) = matches.group(1).split(" ")
130
except (ValueError, IndexError) as e:
118
except (ValueError, IndexError), e:
131
119
raise MalformedHunkHeader(str(e), line)
132
120
if not orig.startswith('-') or not mod.startswith('+'):
133
121
raise MalformedHunkHeader("Positions don't start with + or -.", line)
135
123
(orig_pos, orig_range) = parse_range(orig[1:])
136
124
(mod_pos, mod_range) = parse_range(mod[1:])
137
except (ValueError, IndexError) as e:
125
except (ValueError, IndexError), e:
138
126
raise MalformedHunkHeader(str(e), line)
139
127
if mod_range < 0 or orig_range < 0:
140
128
raise MalformedHunkHeader("Hunk range is negative", line)
428
404
# parse the patch
429
405
beginning = False
430
406
elif len(saved_lines) > 0:
431
if keep_dirty and len(dirty_head) > 0:
432
yield {'saved_lines': saved_lines,
433
'dirty_head': dirty_head}
438
409
elif line.startswith('@@'):
439
410
hunk = hunk_from_header(line)
440
411
orig_range = hunk.orig_range
441
412
saved_lines.append(line)
442
413
if len(saved_lines) > 0:
443
if keep_dirty and len(dirty_head) > 0:
444
yield {'saved_lines': saved_lines,
445
'dirty_head': dirty_head}
450
417
def iter_lines_handle_nl(iter_lines):
471
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
438
def parse_patches(iter_lines, allow_dirty=False):
473
440
:arg iter_lines: iterable of lines to parse for patches
474
441
:kwarg allow_dirty: If True, allow text that's not part of the patch at
475
442
selected places. This includes comments before and after a patch
476
443
for instance. Default False.
477
:kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
481
for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
482
if 'dirty_head' in patch_lines:
483
patches.append({'patch': parse_patch(
484
patch_lines['saved_lines'], allow_dirty),
485
'dirty_head': patch_lines['dirty_head']})
487
patches.append(parse_patch(patch_lines, allow_dirty))
445
return [parse_patch(f.__iter__(), allow_dirty) for f in
446
iter_file_patch(iter_lines, allow_dirty)]
491
449
def difference_index(atext, btext):
537
495
if isinstance(hunk_line, InsertLine):
538
496
yield hunk_line.contents
539
497
elif isinstance(hunk_line, (ContextLine, RemoveLine)):
540
orig_line = next(orig_lines)
498
orig_line = orig_lines.next()
541
499
if orig_line != hunk_line.contents:
542
500
raise PatchConflict(line_no, orig_line, "".join(seen_patch))
543
501
if isinstance(hunk_line, ContextLine):