~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

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