~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-27 01:54:40 UTC
  • mto: (1711.2.26 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1734.
  • Revision ID: john@arbash-meinel.com-20060527015440-1a10495d8e56ed5f
deprecating appendpath, it does exactly what pathjoin does

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2004, 2005 by 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
20
20
 
21
21
 
22
22
from bzrlib.errors import CantReprocessAndShowBase
23
 
import bzrlib.patiencediff
 
23
from bzrlib.patiencediff import SequenceMatcher
24
24
from bzrlib.textfile import check_text_lines
25
25
 
26
 
 
27
26
def intersect(ra, rb):
28
27
    """Given two ranges return the range where they intersect or None.
29
28
 
36
35
    >>> intersect((0, 9), (7, 15))
37
36
    (7, 9)
38
37
    """
39
 
    # preconditions: (ra[0] <= ra[1]) and (rb[0] <= rb[1])
 
38
    assert ra[0] <= ra[1]
 
39
    assert rb[0] <= rb[1]
40
40
    
41
41
    sa = max(ra[0], rb[0])
42
42
    sb = min(ra[1], rb[1])
66
66
    Given BASE, OTHER, THIS, tries to produce a combined text
67
67
    incorporating the changes from both BASE->OTHER and BASE->THIS.
68
68
    All three will typically be sequences of lines."""
69
 
    def __init__(self, base, a, b, is_cherrypick=False):
 
69
    def __init__(self, base, a, b):
70
70
        check_text_lines(base)
71
71
        check_text_lines(a)
72
72
        check_text_lines(b)
73
73
        self.base = base
74
74
        self.a = a
75
75
        self.b = b
76
 
        self.is_cherrypick = is_cherrypick
 
76
 
 
77
 
77
78
 
78
79
    def merge_lines(self,
79
80
                    name_a=None,
86
87
                    reprocess=False):
87
88
        """Return merge in cvs-like form.
88
89
        """
89
 
        newline = '\n'
90
 
        if len(self.a) > 0:
91
 
            if self.a[0].endswith('\r\n'):
92
 
                newline = '\r\n'
93
 
            elif self.a[0].endswith('\r'):
94
 
                newline = '\r'
95
90
        if base_marker and reprocess:
96
91
            raise CantReprocessAndShowBase()
97
92
        if name_a:
115
110
                for i in range(t[1], t[2]):
116
111
                    yield self.b[i]
117
112
            elif what == 'conflict':
118
 
                yield start_marker + newline
 
113
                yield start_marker + '\n'
119
114
                for i in range(t[3], t[4]):
120
115
                    yield self.a[i]
121
116
                if base_marker is not None:
122
 
                    yield base_marker + newline
 
117
                    yield base_marker + '\n'
123
118
                    for i in range(t[1], t[2]):
124
119
                        yield self.base[i]
125
 
                yield mid_marker + newline
 
120
                yield mid_marker + '\n'
126
121
                for i in range(t[5], t[6]):
127
122
                    yield self.b[i]
128
 
                yield end_marker + newline
 
123
                yield end_marker + '\n'
129
124
            else:
130
125
                raise ValueError(what)
 
126
        
 
127
        
 
128
 
 
129
 
131
130
 
132
131
    def merge_annotated(self):
133
132
        """Return merge with conflicts, showing origin of lines.
155
154
                yield '>>>>\n'
156
155
            else:
157
156
                raise ValueError(what)
 
157
        
 
158
        
 
159
 
 
160
 
158
161
 
159
162
    def merge_groups(self):
160
163
        """Yield sequence of line groups.  Each one is a tuple:
190
193
            else:
191
194
                raise ValueError(what)
192
195
 
 
196
 
193
197
    def merge_regions(self):
194
198
        """Return sequences of matching and conflicting regions.
195
199
 
221
225
        iz = ia = ib = 0
222
226
        
223
227
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
 
228
            #print 'match base [%d:%d]' % (zmatch, zend)
 
229
            
224
230
            matchlen = zend - zmatch
225
 
            # invariants:
226
 
            #   matchlen >= 0
227
 
            #   matchlen == (aend - amatch)
228
 
            #   matchlen == (bend - bmatch)
 
231
            assert matchlen >= 0
 
232
            assert matchlen == (aend - amatch)
 
233
            assert matchlen == (bend - bmatch)
 
234
            
229
235
            len_a = amatch - ia
230
236
            len_b = bmatch - ib
231
237
            len_base = zmatch - iz
232
 
            # invariants:
233
 
            # assert len_a >= 0
234
 
            # assert len_b >= 0
235
 
            # assert len_base >= 0
 
238
            assert len_a >= 0
 
239
            assert len_b >= 0
 
240
            assert len_base >= 0
236
241
 
237
242
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
238
243
 
239
244
            if len_a or len_b:
240
245
                # try to avoid actually slicing the lists
 
246
                equal_a = compare_range(self.a, ia, amatch,
 
247
                                        self.base, iz, zmatch)
 
248
                equal_b = compare_range(self.b, ib, bmatch,
 
249
                                        self.base, iz, zmatch)
241
250
                same = compare_range(self.a, ia, amatch,
242
251
                                     self.b, ib, bmatch)
243
252
 
244
253
                if same:
245
254
                    yield 'same', ia, amatch
 
255
                elif equal_a and not equal_b:
 
256
                    yield 'b', ib, bmatch
 
257
                elif equal_b and not equal_a:
 
258
                    yield 'a', ia, amatch
 
259
                elif not equal_a and not equal_b:
 
260
                    yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
246
261
                else:
247
 
                    equal_a = compare_range(self.a, ia, amatch,
248
 
                                            self.base, iz, zmatch)
249
 
                    equal_b = compare_range(self.b, ib, bmatch,
250
 
                                            self.base, iz, zmatch)
251
 
                    if equal_a and not equal_b:
252
 
                        yield 'b', ib, bmatch
253
 
                    elif equal_b and not equal_a:
254
 
                        yield 'a', ia, amatch
255
 
                    elif not equal_a and not equal_b:
256
 
                        if self.is_cherrypick:
257
 
                            for node in self._refine_cherrypick_conflict(
258
 
                                                    iz, zmatch, ia, amatch,
259
 
                                                    ib, bmatch):
260
 
                                yield node
261
 
                        else:
262
 
                            yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
263
 
                    else:
264
 
                        raise AssertionError("can't handle a=b=base but unmatched")
 
262
                    raise AssertionError("can't handle a=b=base but unmatched")
265
263
 
266
264
                ia = amatch
267
265
                ib = bmatch
270
268
            # if the same part of the base was deleted on both sides
271
269
            # that's OK, we can just skip it.
272
270
 
 
271
                
273
272
            if matchlen > 0:
274
 
                # invariants:
275
 
                # assert ia == amatch
276
 
                # assert ib == bmatch
277
 
                # assert iz == zmatch
 
273
                assert ia == amatch
 
274
                assert ib == bmatch
 
275
                assert iz == zmatch
278
276
                
279
277
                yield 'unchanged', zmatch, zend
280
278
                iz = zend
281
279
                ia = aend
282
280
                ib = bend
283
 
 
284
 
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
285
 
        """When cherrypicking b => a, ignore matches with b and base."""
286
 
        # Do not emit regions which match, only regions which do not match
287
 
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
288
 
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
289
 
        last_base_idx = 0
290
 
        last_b_idx = 0
291
 
        last_b_idx = 0
292
 
        yielded_a = False
293
 
        for base_idx, b_idx, match_len in matches:
294
 
            conflict_z_len = base_idx - last_base_idx
295
 
            conflict_b_len = b_idx - last_b_idx
296
 
            if conflict_b_len == 0: # There are no lines in b which conflict,
297
 
                                    # so skip it
298
 
                pass
299
 
            else:
300
 
                if yielded_a:
301
 
                    yield ('conflict',
302
 
                           zstart + last_base_idx, zstart + base_idx,
303
 
                           aend, aend, bstart + last_b_idx, bstart + b_idx)
304
 
                else:
305
 
                    # The first conflict gets the a-range
306
 
                    yielded_a = True
307
 
                    yield ('conflict', zstart + last_base_idx, zstart +
308
 
                    base_idx,
309
 
                           astart, aend, bstart + last_b_idx, bstart + b_idx)
310
 
            last_base_idx = base_idx + match_len
311
 
            last_b_idx = b_idx + match_len
312
 
        if last_base_idx != zend - zstart or last_b_idx != bend - bstart:
313
 
            if yielded_a:
314
 
                yield ('conflict', zstart + last_base_idx, zstart + base_idx,
315
 
                       aend, aend, bstart + last_b_idx, bstart + b_idx)
316
 
            else:
317
 
                # The first conflict gets the a-range
318
 
                yielded_a = True
319
 
                yield ('conflict', zstart + last_base_idx, zstart + base_idx,
320
 
                       astart, aend, bstart + last_b_idx, bstart + b_idx)
321
 
        if not yielded_a:
322
 
            yield ('conflict', zstart, zend, astart, aend, bstart, bend)
 
281
    
323
282
 
324
283
    def reprocess_merge_regions(self, merge_regions):
325
284
        """Where there are conflict regions, remove the agreed lines.
334
293
            type, iz, zmatch, ia, amatch, ib, bmatch = region
335
294
            a_region = self.a[ia:amatch]
336
295
            b_region = self.b[ib:bmatch]
337
 
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
338
 
                    None, a_region, b_region).get_matching_blocks()
 
296
            matches = SequenceMatcher(None, a_region, 
 
297
                                      b_region).get_matching_blocks()
339
298
            next_a = ia
340
299
            next_b = ib
341
300
            for region_ia, region_ib, region_len in matches[:-1]:
352
311
            if reg is not None:
353
312
                yield reg
354
313
 
 
314
 
355
315
    @staticmethod
356
316
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
357
317
        if next_a < region_ia or next_b < region_ib:
358
318
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
 
319
            
359
320
 
360
321
    def find_sync_regions(self):
361
322
        """Return a list of sync regions, where both descendents match the base.
365
326
        """
366
327
 
367
328
        ia = ib = 0
368
 
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
369
 
                None, self.base, self.a).get_matching_blocks()
370
 
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
371
 
                None, self.base, self.b).get_matching_blocks()
 
329
        amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
 
330
        bmatches = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
372
331
        len_a = len(amatches)
373
332
        len_b = len(bmatches)
374
333
 
388
347
 
389
348
                # found a match of base[i[0], i[1]]; this may be less than
390
349
                # the region that matches in either one
391
 
                # assert intlen <= alen
392
 
                # assert intlen <= blen
393
 
                # assert abase <= intbase
394
 
                # assert bbase <= intbase
 
350
                assert intlen <= alen
 
351
                assert intlen <= blen
 
352
                assert abase <= intbase
 
353
                assert bbase <= intbase
395
354
 
396
355
                asub = amatch + (intbase - abase)
397
356
                bsub = bmatch + (intbase - bbase)
398
357
                aend = asub + intlen
399
358
                bend = bsub + intlen
400
359
 
401
 
                # assert self.base[intbase:intend] == self.a[asub:aend], \
402
 
                #       (self.base[intbase:intend], self.a[asub:aend])
403
 
                # assert self.base[intbase:intend] == self.b[bsub:bend]
 
360
                assert self.base[intbase:intend] == self.a[asub:aend], \
 
361
                       (self.base[intbase:intend], self.a[asub:aend])
 
362
 
 
363
                assert self.base[intbase:intend] == self.b[bsub:bend]
404
364
 
405
365
                sl.append((intbase, intend,
406
366
                           asub, aend,
407
367
                           bsub, bend))
 
368
 
408
369
            # advance whichever one ends first in the base text
409
370
            if (abase + alen) < (bbase + blen):
410
371
                ia += 1
418
379
 
419
380
        return sl
420
381
 
 
382
 
 
383
 
421
384
    def find_unconflicted(self):
422
385
        """Return a list of ranges in base that are not conflicted."""
423
 
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
424
 
                None, self.base, self.a).get_matching_blocks()
425
 
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
426
 
                None, self.base, self.b).get_matching_blocks()
 
386
        am = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
 
387
        bm = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
427
388
 
428
389
        unc = []
429
390