/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: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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