/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: 2008-04-15 04:29:44 UTC
  • mto: This revision was merged to the branch mainline in revision 3371.
  • Revision ID: aaron@aaronbentley.com-20080415042944-2o3o664x26507hej
PEP8 fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2004, 2005 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.
63
67
    Given BASE, OTHER, THIS, tries to produce a combined text
64
68
    incorporating the changes from both BASE->OTHER and BASE->THIS.
65
69
    All three will typically be sequences of lines."""
66
 
    def __init__(self, base, a, b):
 
70
    def __init__(self, base, a, b, is_cherrypick=False):
 
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
 
 
74
 
 
 
77
        self.is_cherrypick = is_cherrypick
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
        newline = '\n'
 
91
        if len(self.a) > 0:
 
92
            if self.a[0].endswith('\r\n'):
 
93
                newline = '\r\n'
 
94
            elif self.a[0].endswith('\r'):
 
95
                newline = '\r'
 
96
        if base_marker and reprocess:
 
97
            raise CantReprocessAndShowBase()
85
98
        if name_a:
86
99
            start_marker = start_marker + ' ' + name_a
87
100
        if name_b:
88
101
            end_marker = end_marker + ' ' + name_b
89
 
            
90
 
        for t in self.merge_regions():
 
102
        if name_base and base_marker:
 
103
            base_marker = base_marker + ' ' + name_base
 
104
        merge_regions = self.merge_regions()
 
105
        if reprocess is True:
 
106
            merge_regions = self.reprocess_merge_regions(merge_regions)
 
107
        for t in merge_regions:
91
108
            what = t[0]
92
109
            if what == 'unchanged':
93
110
                for i in range(t[1], t[2]):
99
116
                for i in range(t[1], t[2]):
100
117
                    yield self.b[i]
101
118
            elif what == 'conflict':
102
 
                yield start_marker + '\n'
 
119
                yield start_marker + newline
103
120
                for i in range(t[3], t[4]):
104
121
                    yield self.a[i]
105
 
                yield mid_marker + '\n'
 
122
                if base_marker is not None:
 
123
                    yield base_marker + newline
 
124
                    for i in range(t[1], t[2]):
 
125
                        yield self.base[i]
 
126
                yield mid_marker + newline
106
127
                for i in range(t[5], t[6]):
107
128
                    yield self.b[i]
108
 
                yield end_marker + '\n'
 
129
                yield end_marker + newline
109
130
            else:
110
131
                raise ValueError(what)
111
 
        
112
 
        
113
 
 
114
 
 
115
132
 
116
133
    def merge_annotated(self):
117
134
        """Return merge with conflicts, showing origin of lines.
139
156
                yield '>>>>\n'
140
157
            else:
141
158
                raise ValueError(what)
142
 
        
143
 
        
144
 
 
145
 
 
146
159
 
147
160
    def merge_groups(self):
148
161
        """Yield sequence of line groups.  Each one is a tuple:
178
191
            else:
179
192
                raise ValueError(what)
180
193
 
181
 
 
182
194
    def merge_regions(self):
183
195
        """Return sequences of matching and conflicting regions.
184
196
 
228
240
 
229
241
            if len_a or len_b:
230
242
                # try to avoid actually slicing the lists
231
 
                equal_a = compare_range(self.a, ia, amatch,
232
 
                                        self.base, iz, zmatch)
233
 
                equal_b = compare_range(self.b, ib, bmatch,
234
 
                                        self.base, iz, zmatch)
235
243
                same = compare_range(self.a, ia, amatch,
236
244
                                     self.b, ib, bmatch)
237
245
 
238
246
                if same:
239
247
                    yield 'same', ia, amatch
240
 
                elif equal_a and not equal_b:
241
 
                    yield 'b', ib, bmatch
242
 
                elif equal_b and not equal_a:
243
 
                    yield 'a', ia, amatch
244
 
                elif not equal_a and not equal_b:
245
 
                    yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
246
248
                else:
247
 
                    raise AssertionError("can't handle a=b=base but unmatched")
 
249
                    equal_a = compare_range(self.a, ia, amatch,
 
250
                                            self.base, iz, zmatch)
 
251
                    equal_b = compare_range(self.b, ib, bmatch,
 
252
                                            self.base, iz, zmatch)
 
253
                    if equal_a and not equal_b:
 
254
                        yield 'b', ib, bmatch
 
255
                    elif equal_b and not equal_a:
 
256
                        yield 'a', ia, amatch
 
257
                    elif not equal_a and not equal_b:
 
258
                        if self.is_cherrypick:
 
259
                            for node in self._refine_cherrypick_conflict(
 
260
                                                    iz, zmatch, ia, amatch,
 
261
                                                    ib, bmatch):
 
262
                                yield node
 
263
                        else:
 
264
                            yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
 
265
                    else:
 
266
                        raise AssertionError("can't handle a=b=base but unmatched")
248
267
 
249
268
                ia = amatch
250
269
                ib = bmatch
253
272
            # if the same part of the base was deleted on both sides
254
273
            # that's OK, we can just skip it.
255
274
 
256
 
                
257
275
            if matchlen > 0:
258
276
                assert ia == amatch
259
277
                assert ib == bmatch
263
281
                iz = zend
264
282
                ia = aend
265
283
                ib = bend
266
 
        
267
 
 
268
 
        
 
284
 
 
285
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
 
286
        """When cherrypicking b => a, ignore matches with b and base."""
 
287
        # Do not emit regions which match, only regions which do not match
 
288
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
 
289
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
 
290
        last_base_idx = 0
 
291
        last_b_idx = 0
 
292
        last_b_idx = 0
 
293
        yielded_a = False
 
294
        for base_idx, b_idx, match_len in matches:
 
295
            conflict_z_len = base_idx - last_base_idx
 
296
            conflict_b_len = b_idx - last_b_idx
 
297
            if conflict_b_len == 0: # There are no lines in b which conflict,
 
298
                                    # so skip it
 
299
                pass
 
300
            else:
 
301
                if yielded_a:
 
302
                    yield ('conflict',
 
303
                           zstart + last_base_idx, zstart + base_idx,
 
304
                           aend, aend, bstart + last_b_idx, bstart + b_idx)
 
305
                else:
 
306
                    # The first conflict gets the a-range
 
307
                    yielded_a = True
 
308
                    yield ('conflict', zstart + last_base_idx, zstart +
 
309
                    base_idx,
 
310
                           astart, aend, bstart + last_b_idx, bstart + b_idx)
 
311
            last_base_idx = base_idx + match_len
 
312
            last_b_idx = b_idx + match_len
 
313
        if last_base_idx != zend - zstart or last_b_idx != bend - bstart:
 
314
            if yielded_a:
 
315
                yield ('conflict', zstart + last_base_idx, zstart + base_idx,
 
316
                       aend, aend, bstart + last_b_idx, bstart + b_idx)
 
317
            else:
 
318
                # The first conflict gets the a-range
 
319
                yielded_a = True
 
320
                yield ('conflict', zstart + last_base_idx, zstart + base_idx,
 
321
                       astart, aend, bstart + last_b_idx, bstart + b_idx)
 
322
        if not yielded_a:
 
323
            yield ('conflict', zstart, zend, astart, aend, bstart, bend)
 
324
 
 
325
    def reprocess_merge_regions(self, merge_regions):
 
326
        """Where there are conflict regions, remove the agreed lines.
 
327
 
 
328
        Lines where both A and B have made the same changes are 
 
329
        eliminated.
 
330
        """
 
331
        for region in merge_regions:
 
332
            if region[0] != "conflict":
 
333
                yield region
 
334
                continue
 
335
            type, iz, zmatch, ia, amatch, ib, bmatch = region
 
336
            a_region = self.a[ia:amatch]
 
337
            b_region = self.b[ib:bmatch]
 
338
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
339
                    None, a_region, b_region).get_matching_blocks()
 
340
            next_a = ia
 
341
            next_b = ib
 
342
            for region_ia, region_ib, region_len in matches[:-1]:
 
343
                region_ia += ia
 
344
                region_ib += ib
 
345
                reg = self.mismatch_region(next_a, region_ia, next_b,
 
346
                                           region_ib)
 
347
                if reg is not None:
 
348
                    yield reg
 
349
                yield 'same', region_ia, region_len+region_ia
 
350
                next_a = region_ia + region_len
 
351
                next_b = region_ib + region_len
 
352
            reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
 
353
            if reg is not None:
 
354
                yield reg
 
355
 
 
356
    @staticmethod
 
357
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
 
358
        if next_a < region_ia or next_b < region_ib:
 
359
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
 
360
 
269
361
    def find_sync_regions(self):
270
362
        """Return a list of sync regions, where both descendents match the base.
271
363
 
272
364
        Generates a list of (base1, base2, a1, a2, b1, b2).  There is
273
365
        always a zero-length sync region at the end of all the files.
274
366
        """
275
 
        from difflib import SequenceMatcher
276
367
 
277
368
        ia = ib = 0
278
 
        amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
279
 
        bmatches = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
 
369
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
370
                None, self.base, self.a).get_matching_blocks()
 
371
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
372
                None, self.base, self.b).get_matching_blocks()
280
373
        len_a = len(amatches)
281
374
        len_b = len(bmatches)
282
375
 
328
421
 
329
422
        return sl
330
423
 
331
 
 
332
 
 
333
424
    def find_unconflicted(self):
334
425
        """Return a list of ranges in base that are not conflicted."""
335
 
        from difflib import SequenceMatcher
336
 
 
337
 
        import re
338
 
 
339
 
        # don't sync-up on lines containing only blanks or pounds
340
 
        junk_re = re.compile(r'^[ \t#]*$')
341
 
        
342
 
        am = SequenceMatcher(junk_re.match, self.base, self.a).get_matching_blocks()
343
 
        bm = SequenceMatcher(junk_re.match, self.base, self.b).get_matching_blocks()
 
426
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
 
427
                None, self.base, self.a).get_matching_blocks()
 
428
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
 
429
                None, self.base, self.b).get_matching_blocks()
344
430
 
345
431
        unc = []
346
432