/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: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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