68
68
self.b_ops = SequenceMatcher(None, base, b).get_opcodes()
71
def merge_regions(self):
72
72
"""Return sequences of matching and conflicting regions.
74
This returns tuples, where the first value says what kind we
77
'unchanged', start, end
78
Take a region of base[start:end]
81
Non-clashing insertion from a[start:end]
74
83
Method is as follows:
76
85
The two sequences align only on regions which match the base
82
91
The regions in between can be in any of three cases:
83
92
conflicted, or changed on only one side.
95
# section a[0:ia] has been disposed of, etc
98
for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
99
matchlen = zend - zmatch
101
assert matchlen == (aend - amatch)
102
assert matchlen == (bend - bmatch)
104
if amatch > ia: # or bmatch > ib:
105
# got an unmatched region; work out if either
106
# alternative is the same as the base
108
# kludge: return the whole thing as inserted into A
109
yield 'a', ia, amatch
118
yield 'unchanged', zmatch, zend
88
125
def find_sync_regions(self):
89
126
"""Return a list of sync regions, where both descendents match the base.
91
Generates a list of (base1, base2, a1, a2, b1, b2).
128
Generates a list of (base1, base2, a1, a2, b1, b2). There is
129
always a zero-length sync region at the end of all the files.
93
131
from difflib import SequenceMatcher
94
132
aiter = iter(SequenceMatcher(None, self.base, self.a).get_matching_blocks())
134
172
bbase, bmatch, blen = biter.next()
174
intbase = len(self.base)
177
yield (intbase, intbase, abase, abase, bbase, bbase)
138
181
def find_unconflicted(self):