mercurial/simplemerge.py
author Phil Cohen <phillco@fb.com>
Thu, 24 Aug 2017 21:30:51 -0700
changeset 33908 4074de97b512
parent 33907 1ad3085239ad
child 33932 3b2a002ef685
permissions -rw-r--r--
simplemerge: simplify code now that we always write to a context There's no need for an `out` abstraction between files and contexts anymore. Differential Revision: https://phab.mercurial-scm.org/D383
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     1
# Copyright (C) 2004, 2005 Canonical Ltd
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     2
#
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# This program is free software; you can redistribute it and/or modify
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
# it under the terms of the GNU General Public License as published by
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     5
# the Free Software Foundation; either version 2 of the License, or
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     6
# (at your option) any later version.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
#
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     8
# This program is distributed in the hope that it will be useful,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    11
# GNU General Public License for more details.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
#
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
# You should have received a copy of the GNU General Public License
15782
7de7630053cb Remove FSF mailing address from GPL headers
Martin Geisler <mg@aragost.com>
parents: 15381
diff changeset
    14
# along with this program; if not, see <http://www.gnu.org/licenses/>.
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    15
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    16
# mbp: "you know that thing where cvs gives you conflict markers?"
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
# s: "i hate that."
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
25974
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    19
from __future__ import absolute_import
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    20
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    21
from .i18n import _
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    22
from . import (
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26223
diff changeset
    23
    error,
25974
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    24
    mdiff,
33017
c31d45623304 py3: convert kwargs' keys' to str using pycompat.strkwargs()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31238
diff changeset
    25
    pycompat,
25974
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    26
    util,
241a1324a180 simplemerge: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22056
diff changeset
    27
)
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    28
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    29
class CantReprocessAndShowBase(Exception):
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    30
    pass
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
    31
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
def intersect(ra, rb):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
    """Given two ranges return the range where they intersect or None.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
    >>> intersect((0, 10), (0, 6))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
    (0, 6)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    37
    >>> intersect((0, 10), (5, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    38
    (5, 10)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
    >>> intersect((0, 10), (10, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
    >>> intersect((0, 9), (10, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
    >>> intersect((0, 9), (7, 15))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
    (7, 9)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
    """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
    assert ra[0] <= ra[1]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
    assert rb[0] <= rb[1]
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
    46
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
    sa = max(ra[0], rb[0])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
    sb = min(ra[1], rb[1])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
    if sa < sb:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
        return sa, sb
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
    else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
        return None
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    54
def compare_range(a, astart, aend, b, bstart, bend):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    55
    """Compare a[astart:aend] == b[bstart:bend], without slicing.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
    """
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
    57
    if (aend - astart) != (bend - bstart):
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
        return False
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
    for ia, ib in zip(xrange(astart, aend), xrange(bstart, bend)):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
        if a[ia] != b[ib]:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
            return False
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
    else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
        return True
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
    64
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    65
class Merge3Text(object):
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    66
    """3-way merge of texts.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    67
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    68
    Given strings BASE, OTHER, THIS, tries to produce a combined text
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    69
    incorporating the changes from both BASE->OTHER and BASE->THIS."""
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    70
    def __init__(self, basetext, atext, btext, base=None, a=None, b=None):
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    71
        self.basetext = basetext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    72
        self.atext = atext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    73
        self.btext = btext
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    74
        if base is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    75
            base = mdiff.splitnewlines(basetext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    76
        if a is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    77
            a = mdiff.splitnewlines(atext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    78
        if b is None:
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    79
            b = mdiff.splitnewlines(btext)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    80
        self.base = base
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    81
        self.a = a
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    82
        self.b = b
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    83
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    84
    def merge_lines(self,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    85
                    name_a=None,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
                    name_b=None,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
                    name_base=None,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
                    start_marker='<<<<<<<',
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
                    mid_marker='=======',
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
                    end_marker='>>>>>>>',
26223
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
    91
                    base_marker=None,
28072
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
    92
                    localorother=None,
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
    93
                    minimize=False):
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
        """Return merge in cvs-like form.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
        """
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    96
        self.conflicts = False
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    97
        newline = '\n'
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    98
        if len(self.a) > 0:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    99
            if self.a[0].endswith('\r\n'):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   100
                newline = '\r\n'
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   101
            elif self.a[0].endswith('\r'):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   102
                newline = '\r'
26069
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   103
        if name_a and start_marker:
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   104
            start_marker = start_marker + ' ' + name_a
26069
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   105
        if name_b and end_marker:
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   106
            end_marker = end_marker + ' ' + name_b
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   107
        if name_base and base_marker:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   108
            base_marker = base_marker + ' ' + name_base
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   109
        merge_regions = self.merge_regions()
28072
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   110
        if minimize:
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   111
            merge_regions = self.minimize(merge_regions)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   112
        for t in merge_regions:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   113
            what = t[0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   114
            if what == 'unchanged':
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   115
                for i in range(t[1], t[2]):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   116
                    yield self.base[i]
12401
4cdaf1adafc8 backout most of 4f8067c94729
Matt Mackall <mpm@selenic.com>
parents: 12387
diff changeset
   117
            elif what == 'a' or what == 'same':
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   118
                for i in range(t[1], t[2]):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   119
                    yield self.a[i]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   120
            elif what == 'b':
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   121
                for i in range(t[1], t[2]):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   122
                    yield self.b[i]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   123
            elif what == 'conflict':
26223
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   124
                if localorother == 'local':
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   125
                    for i in range(t[3], t[4]):
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   126
                        yield self.a[i]
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   127
                elif localorother == 'other':
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   128
                    for i in range(t[5], t[6]):
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   129
                        yield self.b[i]
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   130
                else:
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   131
                    self.conflicts = True
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   132
                    if start_marker is not None:
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   133
                        yield start_marker + newline
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   134
                    for i in range(t[3], t[4]):
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   135
                        yield self.a[i]
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   136
                    if base_marker is not None:
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   137
                        yield base_marker + newline
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   138
                        for i in range(t[1], t[2]):
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   139
                            yield self.base[i]
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   140
                    if mid_marker is not None:
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   141
                        yield mid_marker + newline
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   142
                    for i in range(t[5], t[6]):
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   143
                        yield self.b[i]
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   144
                    if end_marker is not None:
ed12abab068e simplemerge: enable option to resolve conflicts one way
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 26069
diff changeset
   145
                        yield end_marker + newline
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   146
            else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   147
                raise ValueError(what)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   148
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   149
    def merge_groups(self):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   150
        """Yield sequence of line groups.  Each one is a tuple:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   151
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   152
        'unchanged', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   153
             Lines unchanged from base
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   154
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   155
        'a', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   156
             Lines taken from a
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   157
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   158
        'same', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   159
             Lines taken from a (and equal to b)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   160
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   161
        'b', lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   162
             Lines taken from b
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   163
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   164
        'conflict', base_lines, a_lines, b_lines
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   165
             Lines from base were changed to either a or b and conflict.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   166
        """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   167
        for t in self.merge_regions():
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   168
            what = t[0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   169
            if what == 'unchanged':
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   170
                yield what, self.base[t[1]:t[2]]
12401
4cdaf1adafc8 backout most of 4f8067c94729
Matt Mackall <mpm@selenic.com>
parents: 12387
diff changeset
   171
            elif what == 'a' or what == 'same':
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   172
                yield what, self.a[t[1]:t[2]]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   173
            elif what == 'b':
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   174
                yield what, self.b[t[1]:t[2]]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   175
            elif what == 'conflict':
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   176
                yield (what,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   177
                       self.base[t[1]:t[2]],
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   178
                       self.a[t[3]:t[4]],
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   179
                       self.b[t[5]:t[6]])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   180
            else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   181
                raise ValueError(what)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   182
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   183
    def merge_regions(self):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   184
        """Return sequences of matching and conflicting regions.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   185
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   186
        This returns tuples, where the first value says what kind we
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   187
        have:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   188
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   189
        'unchanged', start, end
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   190
             Take a region of base[start:end]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   191
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   192
        'same', astart, aend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   193
             b and a are different from base but give the same result
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   194
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   195
        'a', start, end
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   196
             Non-clashing insertion from a[start:end]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   197
28070
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
   198
        'conflict', zstart, zend, astart, aend, bstart, bend
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
   199
            Conflict between a and b, with z as common ancestor
a504794cee29 merge: add some useful documentation
Ryan McElroy <rmcelroy@fb.com>
parents: 26614
diff changeset
   200
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   201
        Method is as follows:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   202
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   203
        The two sequences align only on regions which match the base
14549
48ec0763afbb check-code: catch misspellings of descendant
Matt Mackall <mpm@selenic.com>
parents: 14329
diff changeset
   204
        and both descendants.  These are found by doing a two-way diff
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   205
        of each one against the base, and then finding the
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   206
        intersections between those regions.  These "sync regions"
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   207
        are by definition unchanged in both and easily dealt with.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   208
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   209
        The regions in between can be in any of three cases:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   210
        conflicted, or changed on only one side.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   211
        """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   212
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   213
        # section a[0:ia] has been disposed of, etc
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   214
        iz = ia = ib = 0
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   215
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15782
diff changeset
   216
        for region in self.find_sync_regions():
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 15782
diff changeset
   217
            zmatch, zend, amatch, aend, bmatch, bend = region
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   218
            #print 'match base [%d:%d]' % (zmatch, zend)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   219
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   220
            matchlen = zend - zmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   221
            assert matchlen >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   222
            assert matchlen == (aend - amatch)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   223
            assert matchlen == (bend - bmatch)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   224
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   225
            len_a = amatch - ia
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   226
            len_b = bmatch - ib
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   227
            len_base = zmatch - iz
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   228
            assert len_a >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   229
            assert len_b >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   230
            assert len_base >= 0
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   231
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   232
            #print 'unmatched a=%d, b=%d' % (len_a, len_b)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   233
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   234
            if len_a or len_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   235
                # try to avoid actually slicing the lists
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   236
                equal_a = compare_range(self.a, ia, amatch,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   237
                                        self.base, iz, zmatch)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   238
                equal_b = compare_range(self.b, ib, bmatch,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   239
                                        self.base, iz, zmatch)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   240
                same = compare_range(self.a, ia, amatch,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   241
                                     self.b, ib, bmatch)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   242
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   243
                if same:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   244
                    yield 'same', ia, amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   245
                elif equal_a and not equal_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   246
                    yield 'b', ib, bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   247
                elif equal_b and not equal_a:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   248
                    yield 'a', ia, amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   249
                elif not equal_a and not equal_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   250
                    yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   251
                else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   252
                    raise AssertionError("can't handle a=b=base but unmatched")
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   253
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   254
                ia = amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   255
                ib = bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   256
            iz = zmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   257
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   258
            # if the same part of the base was deleted on both sides
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   259
            # that's OK, we can just skip it.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   260
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   261
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   262
            if matchlen > 0:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   263
                assert ia == amatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   264
                assert ib == bmatch
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   265
                assert iz == zmatch
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   266
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   267
                yield 'unchanged', zmatch, zend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   268
                iz = zend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   269
                ia = aend
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   270
                ib = bend
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   271
28071
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   272
    def minimize(self, merge_regions):
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   273
        """Trim conflict regions of lines where A and B sides match.
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   274
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30323
diff changeset
   275
        Lines where both A and B have made the same changes at the beginning
28071
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   276
        or the end of each merge region are eliminated from the conflict
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   277
        region and are instead considered the same.
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   278
        """
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   279
        for region in merge_regions:
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   280
            if region[0] != "conflict":
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   281
                yield region
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   282
                continue
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   283
            issue, z1, z2, a1, a2, b1, b2 = region
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   284
            alen = a2 - a1
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   285
            blen = b2 - b1
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   286
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   287
            # find matches at the front
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   288
            ii = 0
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   289
            while ii < alen and ii < blen and \
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   290
                  self.a[a1 + ii] == self.b[b1 + ii]:
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   291
                ii += 1
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   292
            startmatches = ii
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   293
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   294
            # find matches at the end
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   295
            ii = 0
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   296
            while ii < alen and ii < blen and \
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   297
                  self.a[a2 - ii - 1] == self.b[b2 - ii - 1]:
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   298
                ii += 1
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   299
            endmatches = ii
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   300
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   301
            if startmatches > 0:
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   302
                yield 'same', a1, a1 + startmatches
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   303
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   304
            yield ('conflict', z1, z2,
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   305
                    a1 + startmatches, a2 - endmatches,
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   306
                    b1 + startmatches, b2 - endmatches)
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   307
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   308
            if endmatches > 0:
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   309
                yield 'same', a2 - endmatches, a2
261324dd5f5b merge: introduce method to minimize merge regions
Ryan McElroy <rmcelroy@fb.com>
parents: 28070
diff changeset
   310
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   311
    def find_sync_regions(self):
14549
48ec0763afbb check-code: catch misspellings of descendant
Matt Mackall <mpm@selenic.com>
parents: 14329
diff changeset
   312
        """Return a list of sync regions, where both descendants match the base.
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   313
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   314
        Generates a list of (base1, base2, a1, a2, b1, b2).  There is
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   315
        always a zero-length sync region at the end of all the files.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   316
        """
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   317
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   318
        ia = ib = 0
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
   319
        amatches = mdiff.get_matching_blocks(self.basetext, self.atext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
   320
        bmatches = mdiff.get_matching_blocks(self.basetext, self.btext)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   321
        len_a = len(amatches)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   322
        len_b = len(bmatches)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   323
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   324
        sl = []
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   325
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   326
        while ia < len_a and ib < len_b:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   327
            abase, amatch, alen = amatches[ia]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   328
            bbase, bmatch, blen = bmatches[ib]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   329
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   330
            # there is an unconflicted block at i; how long does it
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   331
            # extend?  until whichever one ends earlier.
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
   332
            i = intersect((abase, abase + alen), (bbase, bbase + blen))
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   333
            if i:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   334
                intbase = i[0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   335
                intend = i[1]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   336
                intlen = intend - intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   337
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   338
                # found a match of base[i[0], i[1]]; this may be less than
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   339
                # the region that matches in either one
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   340
                assert intlen <= alen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   341
                assert intlen <= blen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   342
                assert abase <= intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   343
                assert bbase <= intbase
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   344
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   345
                asub = amatch + (intbase - abase)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   346
                bsub = bmatch + (intbase - bbase)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   347
                aend = asub + intlen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   348
                bend = bsub + intlen
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   349
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   350
                assert self.base[intbase:intend] == self.a[asub:aend], \
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   351
                       (self.base[intbase:intend], self.a[asub:aend])
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   352
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   353
                assert self.base[intbase:intend] == self.b[bsub:bend]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   354
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   355
                sl.append((intbase, intend,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   356
                           asub, aend,
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   357
                           bsub, bend))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   358
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   359
            # advance whichever one ends first in the base text
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   360
            if (abase + alen) < (bbase + blen):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   361
                ia += 1
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   362
            else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   363
                ib += 1
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   364
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   365
        intbase = len(self.base)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   366
        abase = len(self.a)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   367
        bbase = len(self.b)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   368
        sl.append((intbase, intbase, abase, abase, bbase, bbase))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   369
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   370
        return sl
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   371
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   372
    def find_unconflicted(self):
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   373
        """Return a list of ranges in base that are not conflicted."""
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
   374
        am = mdiff.get_matching_blocks(self.basetext, self.atext)
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
   375
        bm = mdiff.get_matching_blocks(self.basetext, self.btext)
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   376
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   377
        unc = []
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   378
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   379
        while am and bm:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   380
            # there is an unconflicted block at i; how long does it
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   381
            # extend?  until whichever one ends earlier.
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   382
            a1 = am[0][0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   383
            a2 = a1 + am[0][2]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   384
            b1 = bm[0][0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   385
            b2 = b1 + bm[0][2]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   386
            i = intersect((a1, a2), (b1, b2))
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   387
            if i:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   388
                unc.append(i)
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   389
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   390
            if a2 < b2:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   391
                del am[0]
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   392
            else:
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   393
                del bm[0]
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
   394
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   395
        return unc
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   396
33825
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   397
def _verifytext(text, path, ui, opts):
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   398
    """verifies that text is non-binary (unless opts[text] is passed,
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   399
    then we just warn)"""
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   400
    if util.binary(text):
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   401
        msg = _("%s looks like a binary file.") % path
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   402
        if not opts.get('quiet'):
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   403
            ui.warn(_('warning: %s\n') % msg)
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   404
        if not opts.get('text'):
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   405
            raise error.Abort(msg)
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   406
    return text
de573184686e simplemerge: extract verifytext as a helper function
Phil Cohen <phillco@fb.com>
parents: 33017
diff changeset
   407
33830
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   408
def _picklabels(defaults, overrides):
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   409
    name_a, name_b, name_base = defaults
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   410
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   411
    if len(overrides) > 0:
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   412
        name_a = overrides[0]
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   413
    if len(overrides) > 1:
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   414
        name_b = overrides[1]
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   415
    if len(overrides) > 2:
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   416
        name_base = overrides[2]
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   417
    if len(overrides) > 3:
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   418
        raise error.Abort(_("can only specify three labels."))
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   419
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   420
    return [name_a, name_b, name_base]
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   421
33907
1ad3085239ad simplemerge: make context parameters non-optional
Phil Cohen <phillco@fb.com>
parents: 33906
diff changeset
   422
def simplemerge(ui, localctx, basectx, otherctx, repo=None, **opts):
33828
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   423
    """Performs the simplemerge algorithm.
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   424
33907
1ad3085239ad simplemerge: make context parameters non-optional
Phil Cohen <phillco@fb.com>
parents: 33906
diff changeset
   425
    The merged result is written into `localctx`.
33905
61b267a99fea simplemerge: stop reading from, and writing to, files
Phil Cohen <phillco@fb.com>
parents: 33904
diff changeset
   426
    """
33828
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   427
    def readctx(ctx):
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   428
        if not ctx:
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   429
            return None
33902
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   430
        # Merges were always run in the working copy before, which means
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   431
        # they used decoded data, if the user defined any repository
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   432
        # filters.
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   433
        #
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   434
        # Maintain that behavior today for BC, though perhaps in the future
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   435
        # it'd be worth considering whether merging encoded data (what the
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   436
        # repository usually sees) might be more useful.
f39ba8237ed6 simplemerge: use `ctx.decoddeddata()` instead of `repo.wreaddata`
Phil Cohen <phillco@fb.com>
parents: 33830
diff changeset
   437
        return _verifytext(ctx.decodeddata(), ctx.path(), ui, opts)
33828
8b91a4ff23ad simplemerge: use contexts to read file data from, if passed
Phil Cohen <phillco@fb.com>
parents: 33826
diff changeset
   438
26069
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   439
    mode = opts.get('mode','merge')
33830
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   440
    name_a, name_b, name_base = None, None, None
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   441
    if mode != 'union':
33904
1915a5e809ca simplemerge: use context paths for default labels instead of file paths
Phil Cohen <phillco@fb.com>
parents: 33902
diff changeset
   442
        name_a, name_b, name_base = _picklabels([localctx.path(),
1915a5e809ca simplemerge: use context paths for default labels instead of file paths
Phil Cohen <phillco@fb.com>
parents: 33902
diff changeset
   443
                                                 otherctx.path(), None],
33830
aa6c290a77fa filemerge: extract `_picklabels` as a helper function
Phil Cohen <phillco@fb.com>
parents: 33829
diff changeset
   444
                                                opts.get('label', []))
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   445
14328
3c65cdcf3ba6 simplemerge: do not allow binary files to abort an entire merge
Steve Borho <steve@borho.org>
parents: 12401
diff changeset
   446
    try:
33905
61b267a99fea simplemerge: stop reading from, and writing to, files
Phil Cohen <phillco@fb.com>
parents: 33904
diff changeset
   447
        localtext = readctx(localctx)
61b267a99fea simplemerge: stop reading from, and writing to, files
Phil Cohen <phillco@fb.com>
parents: 33904
diff changeset
   448
        basetext = readctx(basectx)
61b267a99fea simplemerge: stop reading from, and writing to, files
Phil Cohen <phillco@fb.com>
parents: 33904
diff changeset
   449
        othertext = readctx(otherctx)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26223
diff changeset
   450
    except error.Abort:
14328
3c65cdcf3ba6 simplemerge: do not allow binary files to abort an entire merge
Steve Borho <steve@borho.org>
parents: 12401
diff changeset
   451
        return 1
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   452
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   453
    m3 = Merge3Text(basetext, localtext, othertext)
28072
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   454
    extrakwargs = {
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   455
            "localorother": opts.get("localorother", None),
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   456
            'minimize': True,
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   457
        }
26069
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   458
    if mode == 'union':
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   459
        extrakwargs['start_marker'] = None
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   460
        extrakwargs['mid_marker'] = None
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   461
        extrakwargs['end_marker'] = None
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   462
    elif name_base is not None:
22024
372ae2745acf simplemerge: support three labels when merging
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22023
diff changeset
   463
        extrakwargs['base_marker'] = '|||||||'
372ae2745acf simplemerge: support three labels when merging
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22023
diff changeset
   464
        extrakwargs['name_base'] = name_base
28072
c3e9269d9602 merge: minimize conflicts when common base is not shown (issue4447)
Ryan McElroy <rmcelroy@fb.com>
parents: 28071
diff changeset
   465
        extrakwargs['minimize'] = False
33908
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   466
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   467
    mergedtext = ""
33017
c31d45623304 py3: convert kwargs' keys' to str using pycompat.strkwargs()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31238
diff changeset
   468
    for line in m3.merge_lines(name_a=name_a, name_b=name_b,
c31d45623304 py3: convert kwargs' keys' to str using pycompat.strkwargs()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31238
diff changeset
   469
                               **pycompat.strkwargs(extrakwargs)):
33908
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   470
        if opts.get('print'):
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   471
            ui.fout.write(line)
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   472
        else:
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   473
            mergedtext += line
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   474
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   475
    if not opts.get('print'):
33908
4074de97b512 simplemerge: simplify code now that we always write to a context
Phil Cohen <phillco@fb.com>
parents: 33907
diff changeset
   476
        localctx.write(mergedtext, localctx.flags())
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   477
26069
09d6725cbc60 simplemerge: add 'mode' kwarg which - when 'union' - suppresses markers
Erik Huelsmann <ehuels@gmail.com>
parents: 25974
diff changeset
   478
    if m3.conflicts and not mode == 'union':
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
   479
        return 1