64
68
incorporating the changes from both BASE->OTHER and BASE->THIS.
65
69
All three will typically be sequences of lines."""
66
70
def __init__(self, base, a, b):
71
check_text_lines(base)
70
from difflib import SequenceMatcher
71
self.a_ops = SequenceMatcher(None, base, a).get_opcodes()
72
self.b_ops = SequenceMatcher(None, base, b).get_opcodes()
76
80
def merge_lines(self,
79
start_marker='<<<<<<<<',
80
mid_marker='========',
81
end_marker='>>>>>>>>',
84
start_marker='<<<<<<<',
83
89
"""Return merge in cvs-like form.
91
if base_marker and reprocess:
92
raise CantReprocessAndShowBase()
86
94
start_marker = start_marker + ' ' + name_a
88
96
end_marker = end_marker + ' ' + name_b
90
for t in self.merge_regions():
97
if name_base and base_marker:
98
base_marker = base_marker + ' ' + name_base
99
merge_regions = self.merge_regions()
100
if reprocess is True:
101
merge_regions = self.reprocess_merge_regions(merge_regions)
102
for t in merge_regions:
92
104
if what == 'unchanged':
93
105
for i in range(t[1], t[2]):
102
114
yield start_marker + '\n'
103
115
for i in range(t[3], t[4]):
117
if base_marker is not None:
118
yield base_marker + '\n'
119
for i in range(t[1], t[2]):
105
121
yield mid_marker + '\n'
106
122
for i in range(t[5], t[6]):
284
def reprocess_merge_regions(self, merge_regions):
285
"""Where there are conflict regions, remove the agreed lines.
287
Lines where both A and B have made the same changes are
290
for region in merge_regions:
291
if region[0] != "conflict":
294
type, iz, zmatch, ia, amatch, ib, bmatch = region
295
a_region = self.a[ia:amatch]
296
b_region = self.b[ib:bmatch]
297
matches = SequenceMatcher(None, a_region,
298
b_region).get_matching_blocks()
301
for region_ia, region_ib, region_len in matches[:-1]:
304
reg = self.mismatch_region(next_a, region_ia, next_b,
308
yield 'same', region_ia, region_len+region_ia
309
next_a = region_ia + region_len
310
next_b = region_ib + region_len
311
reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
317
def mismatch_region(next_a, region_ia, next_b, region_ib):
318
if next_a < region_ia or next_b < region_ib:
319
return 'conflict', None, None, next_a, region_ia, next_b, region_ib
269
322
def find_sync_regions(self):
270
323
"""Return a list of sync regions, where both descendents match the base.
272
325
Generates a list of (base1, base2, a1, a2, b1, b2). There is
273
326
always a zero-length sync region at the end of all the files.
275
from difflib import SequenceMatcher
278
330
amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()