~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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):
39
37
    (7, 9)
40
38
    """
41
39
    # preconditions: (ra[0] <= ra[1]) and (rb[0] <= rb[1])
42
 
 
 
40
    
43
41
    sa = max(ra[0], rb[0])
44
42
    sb = min(ra[1], rb[1])
45
43
    if sa < sb:
58
56
            return False
59
57
    else:
60
58
        return True
61
 
 
 
59
        
62
60
 
63
61
 
64
62
 
68
66
    Given BASE, OTHER, THIS, tries to produce a combined text
69
67
    incorporating the changes from both BASE->OTHER and BASE->THIS.
70
68
    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)
 
69
    def __init__(self, base, a, b, is_cherrypick=False):
 
70
        check_text_lines(base)
 
71
        check_text_lines(a)
 
72
        check_text_lines(b)
89
73
        self.base = base
90
74
        self.a = a
91
75
        self.b = b
109
93
            elif self.a[0].endswith('\r'):
110
94
                newline = '\r'
111
95
        if base_marker and reprocess:
112
 
            raise errors.CantReprocessAndShowBase()
 
96
            raise CantReprocessAndShowBase()
113
97
        if name_a:
114
98
            start_marker = start_marker + ' ' + name_a
115
99
        if name_b:
148
132
    def merge_annotated(self):
149
133
        """Return merge with conflicts, showing origin of lines.
150
134
 
151
 
        Most useful for debugging merge.
 
135
        Most useful for debugging merge.        
152
136
        """
153
137
        for t in self.merge_regions():
154
138
            what = t[0]
235
219
 
236
220
        # section a[0:ia] has been disposed of, etc
237
221
        iz = ia = ib = 0
238
 
 
 
222
        
239
223
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
240
224
            matchlen = zend - zmatch
241
225
            # invariants:
291
275
                # assert ia == amatch
292
276
                # assert ib == bmatch
293
277
                # assert iz == zmatch
294
 
 
 
278
                
295
279
                yield 'unchanged', zmatch, zend
296
280
                iz = zend
297
281
                ia = aend
300
284
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
301
285
        """When cherrypicking b => a, ignore matches with b and base."""
302
286
        # Do not emit regions which match, only regions which do not match
303
 
        matches = patiencediff.PatienceSequenceMatcher(None,
 
287
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
304
288
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
305
289
        last_base_idx = 0
306
290
        last_b_idx = 0
340
324
    def reprocess_merge_regions(self, merge_regions):
341
325
        """Where there are conflict regions, remove the agreed lines.
342
326
 
343
 
        Lines where both A and B have made the same changes are
 
327
        Lines where both A and B have made the same changes are 
344
328
        eliminated.
345
329
        """
346
330
        for region in merge_regions:
350
334
            type, iz, zmatch, ia, amatch, ib, bmatch = region
351
335
            a_region = self.a[ia:amatch]
352
336
            b_region = self.b[ib:bmatch]
353
 
            matches = patiencediff.PatienceSequenceMatcher(
 
337
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
354
338
                    None, a_region, b_region).get_matching_blocks()
355
339
            next_a = ia
356
340
            next_b = ib
381
365
        """
382
366
 
383
367
        ia = ib = 0
384
 
        amatches = patiencediff.PatienceSequenceMatcher(
 
368
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
385
369
                None, self.base, self.a).get_matching_blocks()
386
 
        bmatches = patiencediff.PatienceSequenceMatcher(
 
370
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
387
371
                None, self.base, self.b).get_matching_blocks()
388
372
        len_a = len(amatches)
389
373
        len_b = len(bmatches)
426
410
                ia += 1
427
411
            else:
428
412
                ib += 1
429
 
 
 
413
            
430
414
        intbase = len(self.base)
431
415
        abase = len(self.a)
432
416
        bbase = len(self.b)
436
420
 
437
421
    def find_unconflicted(self):
438
422
        """Return a list of ranges in base that are not conflicted."""
439
 
        am = patiencediff.PatienceSequenceMatcher(
 
423
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
440
424
                None, self.base, self.a).get_matching_blocks()
441
 
        bm = patiencediff.PatienceSequenceMatcher(
 
425
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
442
426
                None, self.base, self.b).get_matching_blocks()
443
427
 
444
428
        unc = []
458
442
                del am[0]
459
443
            else:
460
444
                del bm[0]
461
 
 
 
445
                
462
446
        return unc
463
447
 
464
448