~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge3.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
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
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
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
# mbp: "you know that thing where cvs gives you conflict markers?"
19
19
# s: "i hate that."
20
20
 
21
 
 
22
 
from bzrlib.errors import CantReprocessAndShowBase
23
 
import bzrlib.patiencediff
24
 
from bzrlib.textfile import check_text_lines
 
21
from bzrlib import (
 
22
    errors,
 
23
    patiencediff,
 
24
    textfile,
 
25
    )
25
26
 
26
27
 
27
28
def intersect(ra, rb):
37
38
    (7, 9)
38
39
    """
39
40
    # preconditions: (ra[0] <= ra[1]) and (rb[0] <= rb[1])
40
 
    
 
41
 
41
42
    sa = max(ra[0], rb[0])
42
43
    sb = min(ra[1], rb[1])
43
44
    if sa < sb:
56
57
            return False
57
58
    else:
58
59
        return True
59
 
        
 
60
 
60
61
 
61
62
 
62
63
 
66
67
    Given BASE, OTHER, THIS, tries to produce a combined text
67
68
    incorporating the changes from both BASE->OTHER and BASE->THIS.
68
69
    All three will typically be sequences of lines."""
69
 
    def __init__(self, base, a, b, is_cherrypick=False):
70
 
        check_text_lines(base)
71
 
        check_text_lines(a)
72
 
        check_text_lines(b)
 
70
 
 
71
    def __init__(self, base, a, b, is_cherrypick=False, allow_objects=False):
 
72
        """Constructor.
 
73
 
 
74
        :param base: lines in BASE
 
75
        :param a: lines in A
 
76
        :param b: lines in B
 
77
        :param is_cherrypick: flag indicating if this merge is a cherrypick.
 
78
            When cherrypicking b => a, matches with b and base do not conflict.
 
79
        :param allow_objects: if True, do not require that base, a and b are
 
80
            plain Python strs.  Also prevents BinaryFile from being raised.
 
81
            Lines can be any sequence of comparable and hashable Python
 
82
            objects.
 
83
        """
 
84
        if not allow_objects:
 
85
            textfile.check_text_lines(base)
 
86
            textfile.check_text_lines(a)
 
87
            textfile.check_text_lines(b)
73
88
        self.base = base
74
89
        self.a = a
75
90
        self.b = b
93
108
            elif self.a[0].endswith('\r'):
94
109
                newline = '\r'
95
110
        if base_marker and reprocess:
96
 
            raise CantReprocessAndShowBase()
 
111
            raise errors.CantReprocessAndShowBase()
97
112
        if name_a:
98
113
            start_marker = start_marker + ' ' + name_a
99
114
        if name_b:
132
147
    def merge_annotated(self):
133
148
        """Return merge with conflicts, showing origin of lines.
134
149
 
135
 
        Most useful for debugging merge.        
 
150
        Most useful for debugging merge.
136
151
        """
137
152
        for t in self.merge_regions():
138
153
            what = t[0]
219
234
 
220
235
        # section a[0:ia] has been disposed of, etc
221
236
        iz = ia = ib = 0
222
 
        
 
237
 
223
238
        for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
224
239
            matchlen = zend - zmatch
225
240
            # invariants:
275
290
                # assert ia == amatch
276
291
                # assert ib == bmatch
277
292
                # assert iz == zmatch
278
 
                
 
293
 
279
294
                yield 'unchanged', zmatch, zend
280
295
                iz = zend
281
296
                ia = aend
284
299
    def _refine_cherrypick_conflict(self, zstart, zend, astart, aend, bstart, bend):
285
300
        """When cherrypicking b => a, ignore matches with b and base."""
286
301
        # Do not emit regions which match, only regions which do not match
287
 
        matches = bzrlib.patiencediff.PatienceSequenceMatcher(None,
 
302
        matches = patiencediff.PatienceSequenceMatcher(None,
288
303
            self.base[zstart:zend], self.b[bstart:bend]).get_matching_blocks()
289
304
        last_base_idx = 0
290
305
        last_b_idx = 0
324
339
    def reprocess_merge_regions(self, merge_regions):
325
340
        """Where there are conflict regions, remove the agreed lines.
326
341
 
327
 
        Lines where both A and B have made the same changes are 
 
342
        Lines where both A and B have made the same changes are
328
343
        eliminated.
329
344
        """
330
345
        for region in merge_regions:
334
349
            type, iz, zmatch, ia, amatch, ib, bmatch = region
335
350
            a_region = self.a[ia:amatch]
336
351
            b_region = self.b[ib:bmatch]
337
 
            matches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
352
            matches = patiencediff.PatienceSequenceMatcher(
338
353
                    None, a_region, b_region).get_matching_blocks()
339
354
            next_a = ia
340
355
            next_b = ib
365
380
        """
366
381
 
367
382
        ia = ib = 0
368
 
        amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
383
        amatches = patiencediff.PatienceSequenceMatcher(
369
384
                None, self.base, self.a).get_matching_blocks()
370
 
        bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
 
385
        bmatches = patiencediff.PatienceSequenceMatcher(
371
386
                None, self.base, self.b).get_matching_blocks()
372
387
        len_a = len(amatches)
373
388
        len_b = len(bmatches)
410
425
                ia += 1
411
426
            else:
412
427
                ib += 1
413
 
            
 
428
 
414
429
        intbase = len(self.base)
415
430
        abase = len(self.a)
416
431
        bbase = len(self.b)
420
435
 
421
436
    def find_unconflicted(self):
422
437
        """Return a list of ranges in base that are not conflicted."""
423
 
        am = bzrlib.patiencediff.PatienceSequenceMatcher(
 
438
        am = patiencediff.PatienceSequenceMatcher(
424
439
                None, self.base, self.a).get_matching_blocks()
425
 
        bm = bzrlib.patiencediff.PatienceSequenceMatcher(
 
440
        bm = patiencediff.PatienceSequenceMatcher(
426
441
                None, self.base, self.b).get_matching_blocks()
427
442
 
428
443
        unc = []
442
457
                del am[0]
443
458
            else:
444
459
                del bm[0]
445
 
                
 
460
 
446
461
        return unc
447
462
 
448
463