13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
# mbp: "you know that thing where cvs gives you conflict markers?"
19
19
# s: "i hate that."
22
from bzrlib.errors import CantReprocessAndShowBase
23
import bzrlib.patiencediff
24
from bzrlib.textfile import check_text_lines
28
27
def intersect(ra, rb):
67
67
Given BASE, OTHER, THIS, tries to produce a combined text
68
68
incorporating the changes from both BASE->OTHER and BASE->THIS.
69
69
All three will typically be sequences of lines."""
71
def __init__(self, base, a, b, is_cherrypick=False, allow_objects=False):
74
:param base: lines in BASE
77
:param is_cherrypick: flag indicating if this merge is a cherrypick.
78
When cherrypicking b => a, matches with b and base do not conflict.
79
:param allow_objects: if True, do not require that base, a and b are
80
plain Python strs. Also prevents BinaryFile from being raised.
81
Lines can be any sequence of comparable and hashable Python
85
textfile.check_text_lines(base)
86
textfile.check_text_lines(a)
87
textfile.check_text_lines(b)
70
def __init__(self, base, a, b, is_cherrypick=False):
71
check_text_lines(base)
235
221
# section a[0:ia] has been disposed of, etc
238
224
for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
225
#print 'match base [%d:%d]' % (zmatch, zend)
239
227
matchlen = zend - zmatch
242
# matchlen == (aend - amatch)
243
# matchlen == (bend - bmatch)
229
assert matchlen == (aend - amatch)
230
assert matchlen == (bend - bmatch)
244
232
len_a = amatch - ia
245
233
len_b = bmatch - ib
246
234
len_base = zmatch - iz
250
# assert len_base >= 0
252
239
#print 'unmatched a=%d, b=%d' % (len_a, len_b)
299
285
def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
300
286
"""When cherrypicking b => a, ignore matches with b and base."""
301
287
# Do not emit regions which match, only regions which do not match
302
matches = patiencediff.PatienceSequenceMatcher(None,
288
matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
303
289
self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
307
292
yielded_a = False
308
for base_idx, b_idx, match_len in matches:
309
conflict_z_len = base_idx - last_base_idx
310
conflict_b_len = b_idx - last_b_idx
293
for region_iz, region_ib, region_len in matches:
294
conflict_z_len = region_iz - zzcur
295
conflict_b_len = region_ib - bbcur
311
296
if conflict_b_len == 0: # There are no lines in b which conflict,
317
zstart + last_base_idx, zstart + base_idx,
318
aend, aend, bstart + last_b_idx, bstart + b_idx)
301
yield ('conflict', zstart + zzcur, zstart + region_iz,
302
aend, aend, bstart + bbcur, bstart + region_ib)
320
304
# The first conflict gets the a-range
322
yield ('conflict', zstart + last_base_idx, zstart +
324
astart, aend, bstart + last_b_idx, bstart + b_idx)
325
last_base_idx = base_idx + match_len
326
last_b_idx = b_idx + match_len
327
if last_base_idx != zend - zstart or last_b_idx != bend - bstart:
306
yield ('conflict', zstart + zzcur, zstart + region_iz,
307
astart, aend, bstart + bbcur, bstart + region_ib)
308
zzcur = region_iz + region_len
309
bbcur = region_ib + region_len
310
if zzcur != zend - zstart or bbcur != bend - bstart:
329
yield ('conflict', zstart + last_base_idx, zstart + base_idx,
330
aend, aend, bstart + last_b_idx, bstart + b_idx)
312
yield ('conflict', zstart + zzcur, zstart + region_iz,
313
aend, aend, bstart + bbcur, bstart + region_ib)
332
315
# The first conflict gets the a-range
334
yield ('conflict', zstart + last_base_idx, zstart + base_idx,
335
astart, aend, bstart + last_b_idx, bstart + b_idx)
317
yield ('conflict', zstart + zzcur, zstart + region_iz,
318
astart, aend, bstart + bbcur, bstart + region_ib)
336
319
if not yielded_a:
337
320
yield ('conflict', zstart, zend, astart, aend, bstart, bend)
339
322
def reprocess_merge_regions(self, merge_regions):
340
323
"""Where there are conflict regions, remove the agreed lines.
342
Lines where both A and B have made the same changes are
325
Lines where both A and B have made the same changes are
345
328
for region in merge_regions:
404
387
# found a match of base[i[0], i[1]]; this may be less than
405
388
# the region that matches in either one
406
# assert intlen <= alen
407
# assert intlen <= blen
408
# assert abase <= intbase
409
# assert bbase <= intbase
389
assert intlen <= alen
390
assert intlen <= blen
391
assert abase <= intbase
392
assert bbase <= intbase
411
394
asub = amatch + (intbase - abase)
412
395
bsub = bmatch + (intbase - bbase)
413
396
aend = asub + intlen
414
397
bend = bsub + intlen
416
# assert self.base[intbase:intend] == self.a[asub:aend], \
417
# (self.base[intbase:intend], self.a[asub:aend])
418
# assert self.base[intbase:intend] == self.b[bsub:bend]
399
# XXX: How much overhead is involved in slicing all of these
400
# and doing an extra comparison
401
assert self.base[intbase:intend] == self.a[asub:aend], \
402
(self.base[intbase:intend], self.a[asub:aend])
404
assert self.base[intbase:intend] == self.b[bsub:bend]
420
406
sl.append((intbase, intend,
423
410
# advance whichever one ends first in the base text
424
411
if (abase + alen) < (bbase + blen):
429
416
intbase = len(self.base)
430
417
abase = len(self.a)
431
418
bbase = len(self.b)