/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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