~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

Late bind to PatienceSequenceMatcher in merge3.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# s: "i hate that."
20
20
 
21
21
 
 
22
from bzrlib.errors import CantReprocessAndShowBase
 
23
import bzrlib.patiencediff
 
24
from bzrlib.textfile import check_text_lines
 
25
 
22
26
 
23
27
def intersect(ra, rb):
24
28
    """Given two ranges return the range where they intersect or None.
64
68
    incorporating the changes from both BASE->OTHER and BASE->THIS.
65
69
    All three will typically be sequences of lines."""
66
70
    def __init__(self, base, a, b):
 
71
        check_text_lines(base)
 
72
        check_text_lines(a)
 
73
        check_text_lines(b)
67
74
        self.base = base
68
75
        self.a = a
69
76
        self.b = b
70
 
        from difflib import SequenceMatcher
71
 
        self.a_ops = SequenceMatcher(None, base, a).get_opcodes()
72
 
        self.b_ops = SequenceMatcher(None, base, b).get_opcodes()
73
77
 
74
78
 
75
79
 
80
84
                    start_marker='<<<<<<<',
81
85
                    mid_marker='=======',
82
86
                    end_marker='>>>>>>>',
83
 
                    base_marker=None):
 
87
                    base_marker=None,
 
88
                    reprocess=False):
84
89
        """Return merge in cvs-like form.
85
90
        """
 
91
        if base_marker and reprocess:
 
92
            raise CantReprocessAndShowBase()
86
93
        if name_a:
87
94
            start_marker = start_marker + ' ' + name_a
88
95
        if name_b:
89
96
            end_marker = end_marker + ' ' + name_b
90
97
        if name_base and base_marker:
91
98
            base_marker = base_marker + ' ' + name_base
92
 
            
93
 
        for t in self.merge_regions():
 
99
        merge_regions = self.merge_regions()
 
100
        if reprocess is True:
 
101
            merge_regions = self.reprocess_merge_regions(merge_regions)
 
102
        for t in merge_regions:
94
103
            what = t[0]
95
104
            if what == 'unchanged':
96
105
                for i in range(t[1], t[2]):
270
279
                iz = zend
271
280
                ia = aend
272
281
                ib = bend
273
 
        
274
 
 
275
 
        
 
282
    
 
283
 
 
284
    def reprocess_merge_regions(self, merge_regions):
 
285
        """Where there are conflict regions, remove the agreed lines.
 
286
 
 
287
        Lines where both A and B have made the same changes are 
 
288
        eliminated.
 
289
        """
 
290
        for region in merge_regions:
 
291
            if region[0] != "conflict":
 
292
                yield region
 
293
                continue
 
294
            type, iz, zmatch, ia, amatch, ib, bmatch = region
 
295
            a_region = self.a[ia:amatch]
 
296
            b_region = self.b[ib:bmatch]
 
297
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
298
                    None, a_region, b_region).get_matching_blocks()
 
299
            next_a = ia
 
300
            next_b = ib
 
301
            for region_ia, region_ib, region_len in matches[:-1]:
 
302
                region_ia += ia
 
303
                region_ib += ib
 
304
                reg = self.mismatch_region(next_a, region_ia, next_b,
 
305
                                           region_ib)
 
306
                if reg is not None:
 
307
                    yield reg
 
308
                yield 'same', region_ia, region_len+region_ia
 
309
                next_a = region_ia + region_len
 
310
                next_b = region_ib + region_len
 
311
            reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
 
312
            if reg is not None:
 
313
                yield reg
 
314
 
 
315
 
 
316
    @staticmethod
 
317
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
 
318
        if next_a < region_ia or next_b < region_ib:
 
319
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
 
320
            
 
321
 
276
322
    def find_sync_regions(self):
277
323
        """Return a list of sync regions, where both descendents match the base.
278
324
 
279
325
        Generates a list of (base1, base2, a1, a2, b1, b2).  There is
280
326
        always a zero-length sync region at the end of all the files.
281
327
        """
282
 
        from difflib import SequenceMatcher
283
328
 
284
329
        ia = ib = 0
285
 
        amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
286
 
        bmatches = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
 
330
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
331
                None, self.base, self.a).get_matching_blocks()
 
332
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
333
                None, self.base, self.b).get_matching_blocks()
287
334
        len_a = len(amatches)
288
335
        len_b = len(bmatches)
289
336
 
339
386
 
340
387
    def find_unconflicted(self):
341
388
        """Return a list of ranges in base that are not conflicted."""
342
 
        from difflib import SequenceMatcher
343
 
 
344
 
        import re
345
 
 
346
 
        # don't sync-up on lines containing only blanks or pounds
347
 
        junk_re = re.compile(r'^[ \t#]*$')
348
 
        
349
 
        am = SequenceMatcher(junk_re.match, self.base, self.a).get_matching_blocks()
350
 
        bm = SequenceMatcher(junk_re.match, self.base, self.b).get_matching_blocks()
 
389
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
 
390
                None, self.base, self.a).get_matching_blocks()
 
391
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
 
392
                None, self.base, self.b).get_matching_blocks()
351
393
 
352
394
        unc = []
353
395