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()
75
76
def merge_lines(self,
79
79
start_marker='<<<<<<<',
80
80
mid_marker='=======',
81
81
end_marker='>>>>>>>',
84
83
"""Return merge in cvs-like form.
86
if base_marker and reprocess:
87
raise CantReprocessAndShowBase()
89
86
start_marker = start_marker + ' ' + name_a
91
88
end_marker = end_marker + ' ' + name_b
92
if name_base and base_marker:
93
base_marker = base_marker + ' ' + name_base
94
merge_regions = self.merge_regions()
96
merge_regions = self.reprocess_merge_regions(merge_regions)
97
for t in merge_regions:
90
for t in self.merge_regions():
99
92
if what == 'unchanged':
100
93
for i in range(t[1], t[2]):
109
102
yield start_marker + '\n'
110
103
for i in range(t[3], t[4]):
112
if base_marker is not None:
113
yield base_marker + '\n'
114
for i in range(t[1], t[2]):
116
105
yield mid_marker + '\n'
117
106
for i in range(t[5], t[6]):
279
def reprocess_merge_regions(self, merge_regions):
280
"""Where there are conflict regions, remove the agreed lines.
282
Lines where both A and B have made the same changes are
285
for region in merge_regions:
286
if region[0] != "conflict":
289
type, iz, zmatch, ia, amatch, ib, bmatch = region
290
a_region = self.a[ia:amatch]
291
b_region = self.b[ib:bmatch]
292
matches = SequenceMatcher(None, a_region,
293
b_region).get_matching_blocks()
296
for region_ia, region_ib, region_len in matches[:-1]:
299
reg = self.mismatch_region(next_a, region_ia, next_b,
303
yield 'same', region_ia, region_len+region_ia
304
next_a = region_ia + region_len
305
next_b = region_ib + region_len
306
reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
312
def mismatch_region(next_a, region_ia, next_b, region_ib):
313
if next_a < region_ia or next_b < region_ib:
314
return 'conflict', None, None, next_a, region_ia, next_b, region_ib
317
269
def find_sync_regions(self):
318
270
"""Return a list of sync regions, where both descendents match the base.
320
272
Generates a list of (base1, base2, a1, a2, b1, b2). There is
321
273
always a zero-length sync region at the end of all the files.
275
from difflib import SequenceMatcher
325
278
amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()