~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: John Arbash Meinel
  • Date: 2008-03-20 15:10:05 UTC
  • mto: This revision was merged to the branch mainline in revision 3299.
  • Revision ID: john@arbash-meinel.com-20080320151005-z9lajjy69m20of17
uncommit --local in an unbound branch raises the same exception as commit --local

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
 
from __future__ import absolute_import
18
17
 
19
18
# mbp: "you know that thing where cvs gives you conflict markers?"
20
19
# s: "i hate that."
21
20
 
22
 
from bzrlib import (
23
 
    errors,
24
 
    patiencediff,
25
 
    textfile,
26
 
    )
 
21
 
 
22
from bzrlib.errors import CantReprocessAndShowBase
 
23
import bzrlib.patiencediff
 
24
from bzrlib.textfile import check_text_lines
27
25
 
28
26
 
29
27
def intersect(ra, rb):
38
36
    >>> intersect((0, 9), (7, 15))
39
37
    (7, 9)
40
38
    """
41
 
    # preconditions: (ra[0] <= ra[1]) and (rb[0] <= rb[1])
42
 
 
 
39
    assert ra[0] <= ra[1]
 
40
    assert rb[0] <= rb[1]
 
41
    
43
42
    sa = max(ra[0], rb[0])
44
43
    sb = min(ra[1], rb[1])
45
44
    if sa < sb:
58
57
            return False
59
58
    else:
60
59
        return True
61
 
 
 
60
        
62
61
 
63
62
 
64
63
 
68
67
    Given BASE, OTHER, THIS, tries to produce a combined text
69
68
    incorporating the changes from both BASE->OTHER and BASE->THIS.
70
69
    All three will typically be sequences of lines."""
71
 
 
72
 
    def __init__(self, base, a, b, is_cherrypick=False, allow_objects=False):
73
 
        """Constructor.
74
 
 
75
 
        :param base: lines in BASE
76
 
        :param a: lines in A
77
 
        :param b: lines in B
78
 
        :param is_cherrypick: flag indicating if this merge is a cherrypick.
79
 
            When cherrypicking b => a, matches with b and base do not conflict.
80
 
        :param allow_objects: if True, do not require that base, a and b are
81
 
            plain Python strs.  Also prevents BinaryFile from being raised.
82
 
            Lines can be any sequence of comparable and hashable Python
83
 
            objects.
84
 
        """
85
 
        if not allow_objects:
86
 
            textfile.check_text_lines(base)
87
 
            textfile.check_text_lines(a)
88
 
            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)
89
74
        self.base = base
90
75
        self.a = a
91
76
        self.b = b
109
94
            elif self.a[0].endswith('\r'):
110
95
                newline = '\r'
111
96
        if base_marker and reprocess:
112
 
            raise errors.CantReprocessAndShowBase()
 
97
            raise CantReprocessAndShowBase()
113
98
        if name_a:
114
99
            start_marker = start_marker + ' ' + name_a
115
100
        if name_b:
148
133
    def merge_annotated(self):
149
134
        """Return merge with conflicts, showing origin of lines.
150
135
 
151
 
        Most useful for debugging merge.
 
136
        Most useful for debugging merge.        
152
137
        """
153
138
        for t in self.merge_regions():
154
139
            what = t[0]
235
220
 
236
221
        # section a[0:ia] has been disposed of, etc
237
222
        iz = ia = ib = 0
238
 
 
 
223
        
239
224
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
 
225
            #print 'match base [%d:%d]' % (zmatch, zend)
 
226
            
240
227
            matchlen = zend - zmatch
241
 
            # invariants:
242
 
            #   matchlen >= 0
243
 
            #   matchlen == (aend - amatch)
244
 
            #   matchlen == (bend - bmatch)
 
228
            assert matchlen >= 0
 
229
            assert matchlen == (aend - amatch)
 
230
            assert matchlen == (bend - bmatch)
 
231
            
245
232
            len_a = amatch - ia
246
233
            len_b = bmatch - ib
247
234
            len_base = zmatch - iz
248
 
            # invariants:
249
 
            # assert len_a >= 0
250
 
            # assert len_b >= 0
251
 
            # assert len_base >= 0
 
235
            assert len_a >= 0
 
236
            assert len_b >= 0
 
237
            assert len_base >= 0
252
238
 
253
239
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
254
240
 
287
273
            # that's OK, we can just skip it.
288
274
 
289
275
            if matchlen > 0:
290
 
                # invariants:
291
 
                # assert ia == amatch
292
 
                # assert ib == bmatch
293
 
                # assert iz == zmatch
294
 
 
 
276
                assert ia == amatch
 
277
                assert ib == bmatch
 
278
                assert iz == zmatch
 
279
                
295
280
                yield 'unchanged', zmatch, zend
296
281
                iz = zend
297
282
                ia = aend
300
285
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
301
286
        """When cherrypicking b => a, ignore matches with b and base."""
302
287
        # Do not emit regions which match, only regions which do not match
303
 
        matches = patiencediff.PatienceSequenceMatcher(None,
 
288
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
304
289
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
305
290
        last_base_idx = 0
306
291
        last_b_idx = 0
340
325
    def reprocess_merge_regions(self, merge_regions):
341
326
        """Where there are conflict regions, remove the agreed lines.
342
327
 
343
 
        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 
344
329
        eliminated.
345
330
        """
346
331
        for region in merge_regions:
350
335
            type, iz, zmatch, ia, amatch, ib, bmatch = region
351
336
            a_region = self.a[ia:amatch]
352
337
            b_region = self.b[ib:bmatch]
353
 
            matches = patiencediff.PatienceSequenceMatcher(
 
338
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
354
339
                    None, a_region, b_region).get_matching_blocks()
355
340
            next_a = ia
356
341
            next_b = ib
381
366
        """
382
367
 
383
368
        ia = ib = 0
384
 
        amatches = patiencediff.PatienceSequenceMatcher(
 
369
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
385
370
                None, self.base, self.a).get_matching_blocks()
386
 
        bmatches = patiencediff.PatienceSequenceMatcher(
 
371
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
387
372
                None, self.base, self.b).get_matching_blocks()
388
373
        len_a = len(amatches)
389
374
        len_b = len(bmatches)
404
389
 
405
390
                # found a match of base[i[0], i[1]]; this may be less than
406
391
                # the region that matches in either one
407
 
                # assert intlen <= alen
408
 
                # assert intlen <= blen
409
 
                # assert abase <= intbase
410
 
                # assert bbase <= intbase
 
392
                assert intlen <= alen
 
393
                assert intlen <= blen
 
394
                assert abase <= intbase
 
395
                assert bbase <= intbase
411
396
 
412
397
                asub = amatch + (intbase - abase)
413
398
                bsub = bmatch + (intbase - bbase)
414
399
                aend = asub + intlen
415
400
                bend = bsub + intlen
416
401
 
417
 
                # assert self.base[intbase:intend] == self.a[asub:aend], \
418
 
                #       (self.base[intbase:intend], self.a[asub:aend])
419
 
                # 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]
420
406
 
421
407
                sl.append((intbase, intend,
422
408
                           asub, aend,
423
409
                           bsub, bend))
 
410
 
424
411
            # advance whichever one ends first in the base text
425
412
            if (abase + alen) < (bbase + blen):
426
413
                ia += 1
427
414
            else:
428
415
                ib += 1
429
 
 
 
416
            
430
417
        intbase = len(self.base)
431
418
        abase = len(self.a)
432
419
        bbase = len(self.b)
436
423
 
437
424
    def find_unconflicted(self):
438
425
        """Return a list of ranges in base that are not conflicted."""
439
 
        am = patiencediff.PatienceSequenceMatcher(
 
426
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
440
427
                None, self.base, self.a).get_matching_blocks()
441
 
        bm = patiencediff.PatienceSequenceMatcher(
 
428
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
442
429
                None, self.base, self.b).get_matching_blocks()
443
430
 
444
431
        unc = []
458
445
                del am[0]
459
446
            else:
460
447
                del bm[0]
461
 
 
 
448
                
462
449
        return unc
463
450
 
464
451