/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 breezy/merge3.py

  • Committer: Jelmer Vernooij
  • Date: 2019-02-04 01:01:24 UTC
  • mto: This revision was merged to the branch mainline in revision 7268.
  • Revision ID: jelmer@jelmer.uk-20190204010124-ni0i4qc6f5tnbvux
Fix source tests.

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