/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: Martin Pool
  • Date: 2008-04-24 07:38:09 UTC
  • mto: This revision was merged to the branch mainline in revision 3415.
  • Revision ID: mbp@sourcefrog.net-20080424073809-ueh0p57961v1q5cs
Treat assert statements in our code as a hard error

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
 
 
42
39
    sa = max(ra[0], rb[0])
43
40
    sb = min(ra[1], rb[1])
44
41
    if sa < sb:
57
54
            return False
58
55
    else:
59
56
        return True
60
 
 
 
57
        
61
58
 
62
59
 
63
60
 
67
64
    Given BASE, OTHER, THIS, tries to produce a combined text
68
65
    incorporating the changes from both BASE->OTHER and BASE->THIS.
69
66
    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)
 
67
    def __init__(self, base, a, b, is_cherrypick=False):
 
68
        check_text_lines(base)
 
69
        check_text_lines(a)
 
70
        check_text_lines(b)
88
71
        self.base = base
89
72
        self.a = a
90
73
        self.b = b
108
91
            elif self.a[0].endswith('\r'):
109
92
                newline = '\r'
110
93
        if base_marker and reprocess:
111
 
            raise errors.CantReprocessAndShowBase()
 
94
            raise CantReprocessAndShowBase()
112
95
        if name_a:
113
96
            start_marker = start_marker + ' ' + name_a
114
97
        if name_b:
147
130
    def merge_annotated(self):
148
131
        """Return merge with conflicts, showing origin of lines.
149
132
 
150
 
        Most useful for debugging merge.
 
133
        Most useful for debugging merge.        
151
134
        """
152
135
        for t in self.merge_regions():
153
136
            what = t[0]
234
217
 
235
218
        # section a[0:ia] has been disposed of, etc
236
219
        iz = ia = ib = 0
237
 
 
 
220
        
238
221
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
239
222
            matchlen = zend - zmatch
240
 
            # invariants:
241
 
            #   matchlen >= 0
242
 
            #   matchlen == (aend - amatch)
243
 
            #   matchlen == (bend - bmatch)
244
223
            len_a = amatch - ia
245
224
            len_b = bmatch - ib
246
225
            len_base = zmatch - iz
247
 
            # invariants:
248
 
            # assert len_a >= 0
249
 
            # assert len_b >= 0
250
 
            # assert len_base >= 0
251
 
 
252
226
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
253
227
 
254
228
            if len_a or len_b:
286
260
            # that's OK, we can just skip it.
287
261
 
288
262
            if matchlen > 0:
289
 
                # invariants:
290
 
                # assert ia == amatch
291
 
                # assert ib == bmatch
292
 
                # assert iz == zmatch
293
 
 
294
263
                yield 'unchanged', zmatch, zend
295
264
                iz = zend
296
265
                ia = aend
299
268
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
300
269
        """When cherrypicking b => a, ignore matches with b and base."""
301
270
        # Do not emit regions which match, only regions which do not match
302
 
        matches = patiencediff.PatienceSequenceMatcher(None,
 
271
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
303
272
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
304
273
        last_base_idx = 0
305
274
        last_b_idx = 0
339
308
    def reprocess_merge_regions(self, merge_regions):
340
309
        """Where there are conflict regions, remove the agreed lines.
341
310
 
342
 
        Lines where both A and B have made the same changes are
 
311
        Lines where both A and B have made the same changes are 
343
312
        eliminated.
344
313
        """
345
314
        for region in merge_regions:
349
318
            type, iz, zmatch, ia, amatch, ib, bmatch = region
350
319
            a_region = self.a[ia:amatch]
351
320
            b_region = self.b[ib:bmatch]
352
 
            matches = patiencediff.PatienceSequenceMatcher(
 
321
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
353
322
                    None, a_region, b_region).get_matching_blocks()
354
323
            next_a = ia
355
324
            next_b = ib
380
349
        """
381
350
 
382
351
        ia = ib = 0
383
 
        amatches = patiencediff.PatienceSequenceMatcher(
 
352
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
384
353
                None, self.base, self.a).get_matching_blocks()
385
 
        bmatches = patiencediff.PatienceSequenceMatcher(
 
354
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
386
355
                None, self.base, self.b).get_matching_blocks()
387
356
        len_a = len(amatches)
388
357
        len_b = len(bmatches)
400
369
                intbase = i[0]
401
370
                intend = i[1]
402
371
                intlen = intend - intbase
403
 
 
404
 
                # found a match of base[i[0], i[1]]; this may be less than
405
 
                # the region that matches in either one
406
 
                # assert intlen <= alen
407
 
                # assert intlen <= blen
408
 
                # assert abase <= intbase
409
 
                # assert bbase <= intbase
410
 
 
411
372
                asub = amatch + (intbase - abase)
412
373
                bsub = bmatch + (intbase - bbase)
413
374
                aend = asub + intlen
414
375
                bend = bsub + intlen
415
 
 
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]
419
 
 
420
376
                sl.append((intbase, intend,
421
377
                           asub, aend,
422
378
                           bsub, bend))
425
381
                ia += 1
426
382
            else:
427
383
                ib += 1
428
 
 
 
384
            
429
385
        intbase = len(self.base)
430
386
        abase = len(self.a)
431
387
        bbase = len(self.b)
435
391
 
436
392
    def find_unconflicted(self):
437
393
        """Return a list of ranges in base that are not conflicted."""
438
 
        am = patiencediff.PatienceSequenceMatcher(
 
394
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
439
395
                None, self.base, self.a).get_matching_blocks()
440
 
        bm = patiencediff.PatienceSequenceMatcher(
 
396
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
441
397
                None, self.base, self.b).get_matching_blocks()
442
398
 
443
399
        unc = []
457
413
                del am[0]
458
414
            else:
459
415
                del bm[0]
460
 
 
 
416
                
461
417
        return unc
462
418
 
463
419