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
20
from bzrlib.errors import (
20
32
binary_files_re = 'Binary files (.*) and (.*) differ\n'
23
class BinaryFiles(Exception):
25
def __init__(self, orig_name, mod_name):
26
self.orig_name = orig_name
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)
36
class MalformedPatchHeader(PatchSyntax):
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
class MalformedLine(PatchSyntax):
53
def __init__(self, desc, line):
56
msg = "Malformed line. %s\n%s" % (self.desc, self.line)
57
PatchSyntax.__init__(self, msg)
60
class PatchConflict(Exception):
61
def __init__(self, line_no, orig_line, patch_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)
69
35
def get_patch_names(iter_lines):
36
line = iter_lines.next()
71
line = iter_lines.next()
72
38
match = re.match(binary_files_re, line)
73
39
if match is not None:
74
40
raise BinaryFiles(match.group(1), match.group(2))
404
374
# parse the patch
405
375
beginning = False
406
376
elif len(saved_lines) > 0:
377
if keep_dirty and len(dirty_head) > 0:
378
yield {'saved_lines': saved_lines,
379
'dirty_head': dirty_head}
409
384
elif line.startswith('@@'):
410
385
hunk = hunk_from_header(line)
411
386
orig_range = hunk.orig_range
412
387
saved_lines.append(line)
413
388
if len(saved_lines) > 0:
389
if keep_dirty and len(dirty_head) > 0:
390
yield {'saved_lines': saved_lines,
391
'dirty_head': dirty_head}
417
396
def iter_lines_handle_nl(iter_lines):
438
def parse_patches(iter_lines, allow_dirty=False):
417
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
440
419
:arg iter_lines: iterable of lines to parse for patches
441
420
:kwarg allow_dirty: If True, allow text that's not part of the patch at
442
421
selected places. This includes comments before and after a patch
443
422
for instance. Default False.
423
:kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
445
return [parse_patch(f.__iter__(), allow_dirty) for f in
446
iter_file_patch(iter_lines, allow_dirty)]
427
for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
428
if 'dirty_head' in patch_lines:
429
patches.append({'patch': parse_patch(
430
patch_lines['saved_lines'], allow_dirty),
431
'dirty_head': patch_lines['dirty_head']})
433
patches.append(parse_patch(patch_lines, allow_dirty))
449
437
def difference_index(atext, btext):