~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: Jelmer Vernooij
  • Date: 2009-02-10 04:10:44 UTC
  • mto: This revision was merged to the branch mainline in revision 3995.
  • Revision ID: jelmer@samba.org-20090210041044-42lmb09hskt9lt9l
Review from Ian.

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