~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

  • Committer: Alexander Belchenko
  • Date: 2007-01-30 23:05:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070130230535-kx1rd478rtigyc3v
standalone installer: win98 support

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
    Given BASE, OTHER, THIS, tries to produce a combined text
68
68
    incorporating the changes from both BASE->OTHER and BASE->THIS.
69
69
    All three will typically be sequences of lines."""
70
 
    def __init__(self, base, a, b, is_cherrypick=False):
 
70
    def __init__(self, base, a, b):
71
71
        check_text_lines(base)
72
72
        check_text_lines(a)
73
73
        check_text_lines(b)
74
74
        self.base = base
75
75
        self.a = a
76
76
        self.b = b
77
 
        self.is_cherrypick = is_cherrypick
 
77
 
 
78
 
78
79
 
79
80
    def merge_lines(self,
80
81
                    name_a=None,
87
88
                    reprocess=False):
88
89
        """Return merge in cvs-like form.
89
90
        """
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
91
        if base_marker and reprocess:
97
92
            raise CantReprocessAndShowBase()
98
93
        if name_a:
116
111
                for i in range(t[1], t[2]):
117
112
                    yield self.b[i]
118
113
            elif what == 'conflict':
119
 
                yield start_marker + newline
 
114
                yield start_marker + '\n'
120
115
                for i in range(t[3], t[4]):
121
116
                    yield self.a[i]
122
117
                if base_marker is not None:
123
 
                    yield base_marker + newline
 
118
                    yield base_marker + '\n'
124
119
                    for i in range(t[1], t[2]):
125
120
                        yield self.base[i]
126
 
                yield mid_marker + newline
 
121
                yield mid_marker + '\n'
127
122
                for i in range(t[5], t[6]):
128
123
                    yield self.b[i]
129
 
                yield end_marker + newline
 
124
                yield end_marker + '\n'
130
125
            else:
131
126
                raise ValueError(what)
 
127
        
 
128
        
 
129
 
 
130
 
132
131
 
133
132
    def merge_annotated(self):
134
133
        """Return merge with conflicts, showing origin of lines.
156
155
                yield '>>>>\n'
157
156
            else:
158
157
                raise ValueError(what)
 
158
        
 
159
        
 
160
 
 
161
 
159
162
 
160
163
    def merge_groups(self):
161
164
        """Yield sequence of line groups.  Each one is a tuple:
191
194
            else:
192
195
                raise ValueError(what)
193
196
 
 
197
 
194
198
    def merge_regions(self):
195
199
        """Return sequences of matching and conflicting regions.
196
200
 
240
244
 
241
245
            if len_a or len_b:
242
246
                # try to avoid actually slicing the lists
 
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)
243
251
                same = compare_range(self.a, ia, amatch,
244
252
                                     self.b, ib, bmatch)
245
253
 
246
254
                if same:
247
255
                    yield 'same', ia, amatch
 
256
                elif equal_a and not equal_b:
 
257
                    yield 'b', ib, bmatch
 
258
                elif equal_b and not equal_a:
 
259
                    yield 'a', ia, amatch
 
260
                elif not equal_a and not equal_b:
 
261
                    yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
248
262
                else:
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")
 
263
                    raise AssertionError("can't handle a=b=base but unmatched")
267
264
 
268
265
                ia = amatch
269
266
                ib = bmatch
272
269
            # if the same part of the base was deleted on both sides
273
270
            # that's OK, we can just skip it.
274
271
 
 
272
                
275
273
            if matchlen > 0:
276
274
                assert ia == amatch
277
275
                assert ib == bmatch
281
279
                iz = zend
282
280
                ia = aend
283
281
                ib = bend
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)
 
282
    
324
283
 
325
284
    def reprocess_merge_regions(self, merge_regions):
326
285
        """Where there are conflict regions, remove the agreed lines.
353
312
            if reg is not None:
354
313
                yield reg
355
314
 
 
315
 
356
316
    @staticmethod
357
317
    def mismatch_region(next_a, region_ia,  next_b, region_ib):
358
318
        if next_a < region_ia or next_b < region_ib:
359
319
            return 'conflict', None, None, next_a, region_ia, next_b, region_ib
 
320
            
360
321
 
361
322
    def find_sync_regions(self):
362
323
        """Return a list of sync regions, where both descendents match the base.
421
382
 
422
383
        return sl
423
384
 
 
385
 
 
386
 
424
387
    def find_unconflicted(self):
425
388
        """Return a list of ranges in base that are not conflicted."""
426
389
        am = bzrlib.patiencediff.PatienceSequenceMatcher(