67
64
incorporating the changes from both BASE->OTHER and BASE->THIS.
68
65
All three will typically be sequences of lines."""
69
66
def __init__(self, base, a, b):
70
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()
79
76
def merge_lines(self,
83
start_marker='<<<<<<<',
79
start_marker='<<<<<<<<',
80
mid_marker='========',
81
end_marker='>>>>>>>>',
88
83
"""Return merge in cvs-like form.
90
if base_marker and reprocess:
91
raise CantReprocessAndShowBase()
93
86
start_marker = start_marker + ' ' + name_a
95
88
end_marker = end_marker + ' ' + name_b
96
if name_base and base_marker:
97
base_marker = base_marker + ' ' + name_base
98
merge_regions = self.merge_regions()
100
merge_regions = self.reprocess_merge_regions(merge_regions)
101
for t in merge_regions:
90
for t in self.merge_regions():
103
92
if what == 'unchanged':
104
93
for i in range(t[1], t[2]):
113
102
yield start_marker + '\n'
114
103
for i in range(t[3], t[4]):
116
if base_marker is not None:
117
yield base_marker + '\n'
118
for i in range(t[1], t[2]):
120
105
yield mid_marker + '\n'
121
106
for i in range(t[5], t[6]):
283
def reprocess_merge_regions(self, merge_regions):
284
"""Where there are conflict regions, remove the agreed lines.
286
Lines where both A and B have made the same changes are
289
for region in merge_regions:
290
if region[0] != "conflict":
293
type, iz, zmatch, ia, amatch, ib, bmatch = region
294
a_region = self.a[ia:amatch]
295
b_region = self.b[ib:bmatch]
296
matches = SequenceMatcher(None, a_region,
297
b_region).get_matching_blocks()
300
for region_ia, region_ib, region_len in matches[:-1]:
303
reg = self.mismatch_region(next_a, region_ia, next_b,
307
yield 'same', region_ia, region_len+region_ia
308
next_a = region_ia + region_len
309
next_b = region_ib + region_len
310
reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
316
def mismatch_region(next_a, region_ia, next_b, region_ib):
317
if next_a < region_ia or next_b < region_ib:
318
return 'conflict', None, None, next_a, region_ia, next_b, region_ib
321
269
def find_sync_regions(self):
322
270
"""Return a list of sync regions, where both descendents match the base.
324
272
Generates a list of (base1, base2, a1, a2, b1, b2). There is
325
273
always a zero-length sync region at the end of all the files.
275
from difflib import SequenceMatcher
329
278
amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
384
333
def find_unconflicted(self):
385
334
"""Return a list of ranges in base that are not conflicted."""
386
am = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
387
bm = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
335
from difflib import SequenceMatcher
339
# don't sync-up on lines containing only blanks or pounds
340
junk_re = re.compile(r'^[ \t#]*$')
342
am = SequenceMatcher(junk_re.match, self.base, self.a).get_matching_blocks()
343
bm = SequenceMatcher(junk_re.match, self.base, self.b).get_matching_blocks()