/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
# mbp: "you know that thing where cvs gives you conflict markers?"
19
19
# s: "i hate that."
20
20
 
21
 
 
22
 
from bzrlib.errors import CantReprocessAndShowBase
23
 
import bzrlib.patiencediff
24
 
from bzrlib.textfile import check_text_lines
 
21
from bzrlib import (
 
22
    errors,
 
23
    patiencediff,
 
24
    textfile,
 
25
    )
25
26
 
26
27
 
27
28
def intersect(ra, rb):
36
37
    >>> intersect((0, 9), (7, 15))
37
38
    (7, 9)
38
39
    """
39
 
    assert ra[0] <= ra[1]
40
 
    assert rb[0] <= rb[1]
41
 
    
 
40
    # preconditions: (ra[0] <= ra[1]) and (rb[0] <= rb[1])
 
41
 
42
42
    sa = max(ra[0], rb[0])
43
43
    sb = min(ra[1], rb[1])
44
44
    if sa < sb:
57
57
            return False
58
58
    else:
59
59
        return True
60
 
        
 
60
 
61
61
 
62
62
 
63
63
 
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."""
70
 
    def __init__(self, base, a, b, is_cherrypick=False):
71
 
        check_text_lines(base)
72
 
        check_text_lines(a)
73
 
        check_text_lines(b)
 
70
 
 
71
    def __init__(self, base, a, b, is_cherrypick=False, allow_objects=False):
 
72
        """Constructor.
 
73
 
 
74
        :param base: lines in BASE
 
75
        :param a: lines in A
 
76
        :param b: lines in B
 
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
 
82
            objects.
 
83
        """
 
84
        if not allow_objects:
 
85
            textfile.check_text_lines(base)
 
86
            textfile.check_text_lines(a)
 
87
            textfile.check_text_lines(b)
74
88
        self.base = base
75
89
        self.a = a
76
90
        self.b = b
94
108
            elif self.a[0].endswith('\r'):
95
109
                newline = '\r'
96
110
        if base_marker and reprocess:
97
 
            raise CantReprocessAndShowBase()
 
111
            raise errors.CantReprocessAndShowBase()
98
112
        if name_a:
99
113
            start_marker = start_marker + ' ' + name_a
100
114
        if name_b:
133
147
    def merge_annotated(self):
134
148
        """Return merge with conflicts, showing origin of lines.
135
149
 
136
 
        Most useful for debugging merge.        
 
150
        Most useful for debugging merge.
137
151
        """
138
152
        for t in self.merge_regions():
139
153
            what = t[0]
220
234
 
221
235
        # section a[0:ia] has been disposed of, etc
222
236
        iz = ia = ib = 0
223
 
        
 
237
 
224
238
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
225
 
            #print 'match base [%d:%d]' % (zmatch, zend)
226
 
            
227
239
            matchlen = zend - zmatch
228
 
            assert matchlen >= 0
229
 
            assert matchlen == (aend - amatch)
230
 
            assert matchlen == (bend - bmatch)
231
 
            
 
240
            # invariants:
 
241
            #   matchlen >= 0
 
242
            #   matchlen == (aend - amatch)
 
243
            #   matchlen == (bend - bmatch)
232
244
            len_a = amatch - ia
233
245
            len_b = bmatch - ib
234
246
            len_base = zmatch - iz
235
 
            assert len_a >= 0
236
 
            assert len_b >= 0
237
 
            assert len_base >= 0
 
247
            # invariants:
 
248
            # assert len_a >= 0
 
249
            # assert len_b >= 0
 
250
            # assert len_base >= 0
238
251
 
239
252
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
240
253
 
273
286
            # that's OK, we can just skip it.
274
287
 
275
288
            if matchlen > 0:
276
 
                assert ia == amatch
277
 
                assert ib == bmatch
278
 
                assert iz == zmatch
279
 
                
 
289
                # invariants:
 
290
                # assert ia == amatch
 
291
                # assert ib == bmatch
 
292
                # assert iz == zmatch
 
293
 
280
294
                yield 'unchanged', zmatch, zend
281
295
                iz = zend
282
296
                ia = aend
285
299
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
286
300
        """When cherrypicking b => a, ignore matches with b and base."""
287
301
        # Do not emit regions which match, only regions which do not match
288
 
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
 
302
        matches = patiencediff.PatienceSequenceMatcher(None,
289
303
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
290
304
        last_base_idx = 0
291
305
        last_b_idx = 0
325
339
    def reprocess_merge_regions(self, merge_regions):
326
340
        """Where there are conflict regions, remove the agreed lines.
327
341
 
328
 
        Lines where both A and B have made the same changes are 
 
342
        Lines where both A and B have made the same changes are
329
343
        eliminated.
330
344
        """
331
345
        for region in merge_regions:
335
349
            type, iz, zmatch, ia, amatch, ib, bmatch = region
336
350
            a_region = self.a[ia:amatch]
337
351
            b_region = self.b[ib:bmatch]
338
 
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
352
            matches = patiencediff.PatienceSequenceMatcher(
339
353
                    None, a_region, b_region).get_matching_blocks()
340
354
            next_a = ia
341
355
            next_b = ib
366
380
        """
367
381
 
368
382
        ia = ib = 0
369
 
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
383
        amatches = patiencediff.PatienceSequenceMatcher(
370
384
                None, self.base, self.a).get_matching_blocks()
371
 
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
385
        bmatches = patiencediff.PatienceSequenceMatcher(
372
386
                None, self.base, self.b).get_matching_blocks()
373
387
        len_a = len(amatches)
374
388
        len_b = len(bmatches)
389
403
 
390
404
                # found a match of base[i[0], i[1]]; this may be less than
391
405
                # the region that matches in either one
392
 
                assert intlen <= alen
393
 
                assert intlen <= blen
394
 
                assert abase <= intbase
395
 
                assert bbase <= intbase
 
406
                # assert intlen <= alen
 
407
                # assert intlen <= blen
 
408
                # assert abase <= intbase
 
409
                # assert bbase <= intbase
396
410
 
397
411
                asub = amatch + (intbase - abase)
398
412
                bsub = bmatch + (intbase - bbase)
399
413
                aend = asub + intlen
400
414
                bend = bsub + intlen
401
415
 
402
 
                assert self.base[intbase:intend] == self.a[asub:aend], \
403
 
                       (self.base[intbase:intend], self.a[asub:aend])
404
 
 
405
 
                assert self.base[intbase:intend] == self.b[bsub:bend]
 
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]
406
419
 
407
420
                sl.append((intbase, intend,
408
421
                           asub, aend,
409
422
                           bsub, bend))
410
 
 
411
423
            # advance whichever one ends first in the base text
412
424
            if (abase + alen) < (bbase + blen):
413
425
                ia += 1
414
426
            else:
415
427
                ib += 1
416
 
            
 
428
 
417
429
        intbase = len(self.base)
418
430
        abase = len(self.a)
419
431
        bbase = len(self.b)
423
435
 
424
436
    def find_unconflicted(self):
425
437
        """Return a list of ranges in base that are not conflicted."""
426
 
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
 
438
        am = patiencediff.PatienceSequenceMatcher(
427
439
                None, self.base, self.a).get_matching_blocks()
428
 
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
 
440
        bm = patiencediff.PatienceSequenceMatcher(
429
441
                None, self.base, self.b).get_matching_blocks()
430
442
 
431
443
        unc = []
445
457
                del am[0]
446
458
            else:
447
459
                del bm[0]
448
 
                
 
460
 
449
461
        return unc
450
462
 
451
463