/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: Robert Collins
  • Date: 2010-05-05 00:05:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5206.
  • Revision ID: robertc@robertcollins.net-20100505000529-ltmllyms5watqj5u
Make 'pydoc bzrlib.tests.build_tree_shape' useful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
 
import patiencediff
23
 
 
24
 
 
25
 
from . import (
 
21
from bzrlib import (
26
22
    errors,
 
23
    patiencediff,
27
24
    textfile,
28
25
    )
29
26
 
30
27
 
31
 
class CantReprocessAndShowBase(errors.BzrError):
32
 
 
33
 
    _fmt = ("Can't reprocess and show base, because reprocessing obscures "
34
 
            "the relationship of conflicting lines to the base")
35
 
 
36
 
 
37
28
def intersect(ra, rb):
38
29
    """Given two ranges return the range where they intersect or None.
39
30
 
59
50
def compare_range(a, astart, aend, b, bstart, bend):
60
51
    """Compare a[astart:aend] == b[bstart:bend], without slicing.
61
52
    """
62
 
    if (aend - astart) != (bend - bstart):
 
53
    if (aend-astart) != (bend-bstart):
63
54
        return False
64
 
    for ia, ib in zip(range(astart, aend), range(bstart, bend)):
 
55
    for ia, ib in zip(xrange(astart, aend), xrange(bstart, bend)):
65
56
        if a[ia] != b[ib]:
66
57
            return False
67
58
    else:
68
59
        return True
69
60
 
70
61
 
 
62
 
 
63
 
71
64
class Merge3(object):
72
65
    """3-way merge of texts.
73
66
 
101
94
                    name_a=None,
102
95
                    name_b=None,
103
96
                    name_base=None,
104
 
                    start_marker=b'<<<<<<<',
105
 
                    mid_marker=b'=======',
106
 
                    end_marker=b'>>>>>>>',
 
97
                    start_marker='<<<<<<<',
 
98
                    mid_marker='=======',
 
99
                    end_marker='>>>>>>>',
107
100
                    base_marker=None,
108
101
                    reprocess=False):
109
102
        """Return merge in cvs-like form.
110
103
        """
111
 
        newline = b'\n'
 
104
        newline = '\n'
112
105
        if len(self.a) > 0:
113
 
            if self.a[0].endswith(b'\r\n'):
114
 
                newline = b'\r\n'
115
 
            elif self.a[0].endswith(b'\r'):
116
 
                newline = b'\r'
 
106
            if self.a[0].endswith('\r\n'):
 
107
                newline = '\r\n'
 
108
            elif self.a[0].endswith('\r'):
 
109
                newline = '\r'
117
110
        if base_marker and reprocess:
118
 
            raise CantReprocessAndShowBase()
 
111
            raise errors.CantReprocessAndShowBase()
119
112
        if name_a:
120
 
            start_marker = start_marker + b' ' + name_a
 
113
            start_marker = start_marker + ' ' + name_a
121
114
        if name_b:
122
 
            end_marker = end_marker + b' ' + name_b
 
115
            end_marker = end_marker + ' ' + name_b
123
116
        if name_base and base_marker:
124
 
            base_marker = base_marker + b' ' + name_base
 
117
            base_marker = base_marker + ' ' + name_base
125
118
        merge_regions = self.merge_regions()
126
119
        if reprocess is True:
127
120
            merge_regions = self.reprocess_merge_regions(merge_regions)
250
243
            #   matchlen == (bend - bmatch)
251
244
            len_a = amatch - ia
252
245
            len_b = bmatch - ib
 
246
            len_base = zmatch - iz
253
247
            # invariants:
254
248
            # assert len_a >= 0
255
249
            # assert len_b >= 0
 
250
            # assert len_base >= 0
256
251
 
257
 
            # print 'unmatched a=%d, b=%d' % (len_a, len_b)
 
252
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
258
253
 
259
254
            if len_a or len_b:
260
255
                # try to avoid actually slicing the lists
275
270
                    elif not equal_a and not equal_b:
276
271
                        if self.is_cherrypick:
277
272
                            for node in self._refine_cherrypick_conflict(
278
 
                                    iz, zmatch, ia, amatch,
279
 
                                    ib, bmatch):
 
273
                                                    iz, zmatch, ia, amatch,
 
274
                                                    ib, bmatch):
280
275
                                yield node
281
276
                        else:
282
 
                            yield ('conflict', iz, zmatch, ia, amatch, ib,
283
 
                                   bmatch)
 
277
                            yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
284
278
                    else:
285
 
                        raise AssertionError(
286
 
                            "can't handle a=b=base but unmatched")
 
279
                        raise AssertionError("can't handle a=b=base but unmatched")
287
280
 
288
281
                ia = amatch
289
282
                ib = bmatch
303
296
                ia = aend
304
297
                ib = bend
305
298
 
306
 
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart,
307
 
                                    bend):
 
299
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
308
300
        """When cherrypicking b => a, ignore matches with b and base."""
309
301
        # Do not emit regions which match, only regions which do not match
310
 
        matches = patiencediff.PatienceSequenceMatcher(
311
 
            None, self.base[zstart:zend], self.b[bstart:bend]
312
 
            ).get_matching_blocks()
 
302
        matches = patiencediff.PatienceSequenceMatcher(None,
 
303
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
313
304
        last_base_idx = 0
314
305
        last_b_idx = 0
315
306
        last_b_idx = 0
316
307
        yielded_a = False
317
308
        for base_idx, b_idx, match_len in matches:
 
309
            conflict_z_len = base_idx - last_base_idx
318
310
            conflict_b_len = b_idx - last_b_idx
319
 
            if conflict_b_len == 0:
320
 
                # There are no lines in b which conflict, so skip it
 
311
            if conflict_b_len == 0: # There are no lines in b which conflict,
 
312
                                    # so skip it
321
313
                pass
322
314
            else:
323
315
                if yielded_a:
328
320
                    # The first conflict gets the a-range
329
321
                    yielded_a = True
330
322
                    yield ('conflict', zstart + last_base_idx, zstart +
331
 
                           base_idx,
 
323
                    base_idx,
332
324
                           astart, aend, bstart + last_b_idx, bstart + b_idx)
333
325
            last_base_idx = base_idx + match_len
334
326
            last_b_idx = b_idx + match_len
358
350
            a_region = self.a[ia:amatch]
359
351
            b_region = self.b[ib:bmatch]
360
352
            matches = patiencediff.PatienceSequenceMatcher(
361
 
                None, a_region, b_region).get_matching_blocks()
 
353
                    None, a_region, b_region).get_matching_blocks()
362
354
            next_a = ia
363
355
            next_b = ib
364
356
            for region_ia, region_ib, region_len in matches[:-1]:
368
360
                                           region_ib)
369
361
                if reg is not None:
370
362
                    yield reg
371
 
                yield 'same', region_ia, region_len + region_ia
 
363
                yield 'same', region_ia, region_len+region_ia
372
364
                next_a = region_ia + region_len
373
365
                next_b = region_ib + region_len
374
366
            reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
376
368
                yield reg
377
369
 
378
370
    @staticmethod
379
 
    def mismatch_region(next_a, region_ia, next_b, region_ib):
 
371
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
380
372
        if next_a < region_ia or next_b < region_ib:
381
373
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
382
374
 
389
381
 
390
382
        ia = ib = 0
391
383
        amatches = patiencediff.PatienceSequenceMatcher(
392
 
            None, self.base, self.a).get_matching_blocks()
 
384
                None, self.base, self.a).get_matching_blocks()
393
385
        bmatches = patiencediff.PatienceSequenceMatcher(
394
 
            None, self.base, self.b).get_matching_blocks()
 
386
                None, self.base, self.b).get_matching_blocks()
395
387
        len_a = len(amatches)
396
388
        len_b = len(bmatches)
397
389
 
403
395
 
404
396
            # there is an unconflicted block at i; how long does it
405
397
            # extend?  until whichever one ends earlier.
406
 
            i = intersect((abase, abase + alen), (bbase, bbase + blen))
 
398
            i = intersect((abase, abase+alen), (bbase, bbase+blen))
407
399
            if i:
408
400
                intbase = i[0]
409
401
                intend = i[1]
444
436
    def find_unconflicted(self):
445
437
        """Return a list of ranges in base that are not conflicted."""
446
438
        am = patiencediff.PatienceSequenceMatcher(
447
 
            None, self.base, self.a).get_matching_blocks()
 
439
                None, self.base, self.a).get_matching_blocks()
448
440
        bm = patiencediff.PatienceSequenceMatcher(
449
 
            None, self.base, self.b).get_matching_blocks()
 
441
                None, self.base, self.b).get_matching_blocks()
450
442
 
451
443
        unc = []
452
444
 
471
463
 
472
464
def main(argv):
473
465
    # as for diff3 and meld the syntax is "MINE BASE OTHER"
474
 
    with open(argv[1], 'rt') as f:
475
 
        a = f.readlines()
476
 
    with open(argv[2], 'rt') as f:
477
 
        base = f.readlines()
478
 
    with open(argv[3], 'rt') as f:
479
 
        b = f.readlines()
 
466
    a = file(argv[1], 'rt').readlines()
 
467
    base = file(argv[2], 'rt').readlines()
 
468
    b = file(argv[3], 'rt').readlines()
480
469
 
481
470
    m3 = Merge3(base, a, b)
482
471
 
483
 
    # for sr in m3.find_sync_regions():
 
472
    #for sr in m3.find_sync_regions():
484
473
    #    print sr
485
474
 
486
475
    # sys.stdout.writelines(m3.merge_lines(name_a=argv[1], name_b=argv[3]))