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

  • Committer: Aaron Bentley
  • Date: 2006-04-16 17:26:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1673.
  • Revision ID: aaron.bentley@utoronto.ca-20060416172633-4841d41fc9724d63
Added more tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# s: "i hate that."
20
20
 
21
21
 
 
22
from difflib import SequenceMatcher
 
23
from bzrlib.errors import CantReprocessAndShowBase
 
24
from textfile import check_text_lines
22
25
 
23
26
def intersect(ra, rb):
24
27
    """Given two ranges return the range where they intersect or None.
64
67
    incorporating the changes from both BASE->OTHER and BASE->THIS.
65
68
    All three will typically be sequences of lines."""
66
69
    def __init__(self, base, a, b):
 
70
        check_text_lines(base)
 
71
        check_text_lines(a)
 
72
        check_text_lines(b)
67
73
        self.base = base
68
74
        self.a = a
69
75
        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
76
 
74
77
 
75
78
 
76
79
    def merge_lines(self,
77
80
                    name_a=None,
78
81
                    name_b=None,
79
 
                    start_marker='<<<<<<<<',
80
 
                    mid_marker='========',
81
 
                    end_marker='>>>>>>>>',
82
 
                    show_base=False):
 
82
                    name_base=None,
 
83
                    start_marker='<<<<<<<',
 
84
                    mid_marker='=======',
 
85
                    end_marker='>>>>>>>',
 
86
                    base_marker=None,
 
87
                    reprocess=False):
83
88
        """Return merge in cvs-like form.
84
89
        """
 
90
        if base_marker and reprocess:
 
91
            raise CantReprocessAndShowBase()
85
92
        if name_a:
86
93
            start_marker = start_marker + ' ' + name_a
87
94
        if name_b:
88
95
            end_marker = end_marker + ' ' + name_b
89
 
            
90
 
        for t in self.merge_regions():
 
96
        if name_base and base_marker:
 
97
            base_marker = base_marker + ' ' + name_base
 
98
        merge_regions = self.merge_regions()
 
99
        if reprocess is True:
 
100
            merge_regions = self.reprocess_merge_regions(merge_regions)
 
101
        for t in merge_regions:
91
102
            what = t[0]
92
103
            if what == 'unchanged':
93
104
                for i in range(t[1], t[2]):
102
113
                yield start_marker + '\n'
103
114
                for i in range(t[3], t[4]):
104
115
                    yield self.a[i]
 
116
                if base_marker is not None:
 
117
                    yield base_marker + '\n'
 
118
                    for i in range(t[1], t[2]):
 
119
                        yield self.base[i]
105
120
                yield mid_marker + '\n'
106
121
                for i in range(t[5], t[6]):
107
122
                    yield self.b[i]
263
278
                iz = zend
264
279
                ia = aend
265
280
                ib = bend
266
 
        
267
 
 
268
 
        
 
281
    
 
282
 
 
283
    def reprocess_merge_regions(self, merge_regions):
 
284
        """Where there are conflict regions, remove the agreed lines.
 
285
 
 
286
        Lines where both A and B have made the same changes are 
 
287
        eliminated.
 
288
        """
 
289
        for region in merge_regions:
 
290
            if region[0] != "conflict":
 
291
                yield region
 
292
                continue
 
293
            type, iz, zmatch, ia, amatch, ib, bmatch = region
 
294
            a_region = self.a[ia:amatch]
 
295
            b_region = self.b[ib:bmatch]
 
296
            matches = SequenceMatcher(None, a_region, 
 
297
                                      b_region).get_matching_blocks()
 
298
            next_a = ia
 
299
            next_b = ib
 
300
            for region_ia, region_ib, region_len in matches[:-1]:
 
301
                region_ia += ia
 
302
                region_ib += ib
 
303
                reg = self.mismatch_region(next_a, region_ia, next_b,
 
304
                                           region_ib)
 
305
                if reg is not None:
 
306
                    yield reg
 
307
                yield 'same', region_ia, region_len+region_ia
 
308
                next_a = region_ia + region_len
 
309
                next_b = region_ib + region_len
 
310
            reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
 
311
            if reg is not None:
 
312
                yield reg
 
313
 
 
314
 
 
315
    @staticmethod
 
316
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
 
317
        if next_a < region_ia or next_b < region_ib:
 
318
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
 
319
            
 
320
 
269
321
    def find_sync_regions(self):
270
322
        """Return a list of sync regions, where both descendents match the base.
271
323
 
272
324
        Generates a list of (base1, base2, a1, a2, b1, b2).  There is
273
325
        always a zero-length sync region at the end of all the files.
274
326
        """
275
 
        from difflib import SequenceMatcher
276
327
 
277
328
        ia = ib = 0
278
329
        amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
332
383
 
333
384
    def find_unconflicted(self):
334
385
        """Return a list of ranges in base that are not conflicted."""
335
 
        from difflib import SequenceMatcher
336
386
 
337
387
        import re
338
388