mercurial/filemerge.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Sun, 12 Feb 2012 21:38:12 +0900
changeset 16126 0c4bec9596d8
parent 16125 83925d3a4559
child 16127 14dc2bbba6d2
permissions -rw-r--r--
filemerge: create detail of internal merge tools from documentation string this patch introduces 'internaltoolsmarker' which creates detail of each internal merge tools from documentation string for 'hg help merge-tools'.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# filemerge.py - file-level merge handling for Mercurial
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9709
diff changeset
     6
# GNU General Public License version 2 or any later version.
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
7873
4a4c7f6a5912 cleanup: drop unused imports
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7397
diff changeset
     8
from node import short
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     9
from i18n import _
11146
523330d567cf merge: tool.premerge=keep will leave premerge markers in $local
David Champion <dgc@uchicago.edu>
parents: 10944
diff changeset
    10
import util, simplemerge, match, error
8312
b87a50b7125c separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents: 8269
diff changeset
    11
import os, tempfile, re, filecmp
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    12
6013
bb441d77df99 filemerge: handle missing regappend
Matt Mackall <mpm@selenic.com>
parents: 6007
diff changeset
    13
def _toolstr(ui, tool, part, default=""):
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    14
    return ui.config("merge-tools", tool + "." + part, default)
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    15
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    16
def _toolbool(ui, tool, part, default=False):
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    17
    return ui.configbool("merge-tools", tool + "." + part, default)
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    18
11148
a912f26777d3 merge: introduce tool.check parameter
David Champion <dgc@uchicago.edu>
parents: 11146
diff changeset
    19
def _toollist(ui, tool, part, default=[]):
a912f26777d3 merge: introduce tool.check parameter
David Champion <dgc@uchicago.edu>
parents: 11146
diff changeset
    20
    return ui.configlist("merge-tools", tool + "." + part, default)
a912f26777d3 merge: introduce tool.check parameter
David Champion <dgc@uchicago.edu>
parents: 11146
diff changeset
    21
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
    22
internals = {}
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    23
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    24
def internaltool(name, trymerge, onfailure=None):
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    25
    '''return a decorator for populating internal merge tool table'''
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    26
    def decorator(func):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
    27
        internals[name] = func
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    28
        func.trymerge = trymerge
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    29
        func.onfailure = onfailure
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    30
        return func
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
    31
    return decorator
8830
a9850eda2973 filemerge: add internal:prompt target
Matt Mackall <mpm@selenic.com>
parents: 8615
diff changeset
    32
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    33
def _findtool(ui, tool):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
    34
    if tool in internals:
6522
2b181fb3a70a use internal merge tool when specified for a merge-pattern in hgrc
Dov Feldstern <dfeldstern@fastimap.com>
parents: 6212
diff changeset
    35
        return tool
13565
984175605311 filemerge: introduce a 'regkeyalt' merge tool variable
Steve Borho <steve@borho.org>
parents: 12788
diff changeset
    36
    for kn in ("regkey", "regkeyalt"):
984175605311 filemerge: introduce a 'regkeyalt' merge tool variable
Steve Borho <steve@borho.org>
parents: 12788
diff changeset
    37
        k = _toolstr(ui, tool, kn)
984175605311 filemerge: introduce a 'regkeyalt' merge tool variable
Steve Borho <steve@borho.org>
parents: 12788
diff changeset
    38
        if not k:
984175605311 filemerge: introduce a 'regkeyalt' merge tool variable
Steve Borho <steve@borho.org>
parents: 12788
diff changeset
    39
            continue
14230
d51630301241 rename util.lookup_reg to lookupreg
Adrian Buehlmann <adrian@cadifra.com>
parents: 14168
diff changeset
    40
        p = util.lookupreg(k, _toolstr(ui, tool, "regname"))
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 6005
diff changeset
    41
        if p:
14271
4030630fb59c rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents: 14230
diff changeset
    42
            p = util.findexe(p + _toolstr(ui, tool, "regappend"))
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 6005
diff changeset
    43
            if p:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 6005
diff changeset
    44
                return p
15264
157d93c41c10 merge: expand environment variables and ~/ in tool.executable
Greg Ward <greg@gerg.ca>
parents: 14749
diff changeset
    45
    exe = _toolstr(ui, tool, "executable", tool)
157d93c41c10 merge: expand environment variables and ~/ in tool.executable
Greg Ward <greg@gerg.ca>
parents: 14749
diff changeset
    46
    return util.findexe(util.expandpath(exe))
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    47
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    48
def _picktool(repo, ui, path, binary, symlink):
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    49
    def check(tool, pat, symlink, binary):
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    50
        tmsg = tool
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    51
        if pat:
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    52
            tmsg += " specified for " + pat
7397
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
    53
        if not _findtool(ui, tool):
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
    54
            if pat: # explicitly requested tool deserves a warning
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
    55
                ui.warn(_("couldn't find merge tool %s\n") % tmsg)
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
    56
            else: # configured but non-existing tools are more silent
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
    57
                ui.note(_("couldn't find merge tool %s\n") % tmsg)
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    58
        elif symlink and not _toolbool(ui, tool, "symlink"):
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    59
            ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    60
        elif binary and not _toolbool(ui, tool, "binary"):
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    61
            ui.warn(_("tool %s can't handle binary\n") % tmsg)
6007
090b1a665901 filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents: 6006
diff changeset
    62
        elif not util.gui() and _toolbool(ui, tool, "gui"):
090b1a665901 filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents: 6006
diff changeset
    63
            ui.warn(_("tool %s requires a GUI\n") % tmsg)
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    64
        else:
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    65
            return True
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    66
        return False
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    67
12788
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    68
    # forcemerge comes from command line arguments, highest priority
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    69
    force = ui.config('ui', 'forcemerge')
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    70
    if force:
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    71
        toolpath = _findtool(ui, force)
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    72
        if toolpath:
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    73
            return (force, '"' + toolpath + '"')
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    74
        else:
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    75
            # mimic HGMERGE if given tool not found
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    76
            return (force, force)
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    77
de793925862e merge: implement --tool arguments using new ui.forcemerge configurable
Steve Borho <steve@borho.org>
parents: 12047
diff changeset
    78
    # HGMERGE takes next precedence
6025
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
    79
    hgmerge = os.environ.get("HGMERGE")
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
    80
    if hgmerge:
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
    81
        return (hgmerge, hgmerge)
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    82
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    83
    # then patterns
6016
288ec2f6faa2 filemerge: fix pattern matching
dhruva <dhruvakm@gmail.com>
parents: 6015
diff changeset
    84
    for pat, tool in ui.configitems("merge-patterns"):
8567
fea40a677d43 match: add some default args
Matt Mackall <mpm@selenic.com>
parents: 8566
diff changeset
    85
        mf = match.match(repo.root, '', [pat])
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    86
        if mf(path) and check(tool, pat, symlink, False):
10339
23e608f42f2c fix spaces/identation issues
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    87
            toolpath = _findtool(ui, tool)
23e608f42f2c fix spaces/identation issues
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    88
            return (tool, '"' + toolpath + '"')
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    89
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    90
    # then merge tools
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    91
    tools = {}
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    92
    for k, v in ui.configitems("merge-tools"):
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    93
        t = k.split('.')[0]
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    94
        if t not in tools:
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
    95
            tools[t] = int(_toolstr(ui, t, "priority", "0"))
6076
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
    96
    names = tools.keys()
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    97
    tools = sorted([(-p, t) for t, p in tools.items()])
6076
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
    98
    uimerge = ui.config("ui", "merge")
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
    99
    if uimerge:
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
   100
        if uimerge not in names:
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
   101
            return (uimerge, uimerge)
0ee885fea464 filemerge: more backwards compatible behavior for ui.merge
Steve Borho <steve@borho.org>
parents: 6075
diff changeset
   102
        tools.insert(0, (None, uimerge)) # highest priority
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   103
    tools.append((None, "hgmerge")) # the old default, if found
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   104
    for p, t in tools:
7397
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
   105
        if check(t, None, symlink, binary):
4c92d8971809 More verbose logging when filemerge searches for merge-tool
Mads Kiilerich <mads@kiilerich.com>
parents: 6762
diff changeset
   106
            toolpath = _findtool(ui, t)
6025
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
   107
            return (t, '"' + toolpath + '"')
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
   108
    # internal merge as last resort
f2335246e5c7 filemerge: wrap quotes around tool path
Steve Borho <steve@borho.org>
parents: 6016
diff changeset
   109
    return (not (symlink or binary) and "internal:merge" or None, None)
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   110
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   111
def _eoltype(data):
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   112
    "Guess the EOL type of a file"
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   113
    if '\0' in data: # binary
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   114
        return None
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   115
    if '\r\n' in data: # Windows
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   116
        return '\r\n'
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   117
    if '\r' in data: # Old Mac
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   118
        return '\r'
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   119
    if '\n' in data: # UNIX
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   120
        return '\n'
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   121
    return None # unknown
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   122
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   123
def _matcheol(file, origfile):
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   124
    "Convert EOL markers in a file to match origfile"
14168
135e244776f0 prevent transient leaks of file handle by using new helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13565
diff changeset
   125
    tostyle = _eoltype(util.readfile(origfile))
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   126
    if tostyle:
14168
135e244776f0 prevent transient leaks of file handle by using new helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13565
diff changeset
   127
        data = util.readfile(file)
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   128
        style = _eoltype(data)
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   129
        if style:
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   130
            newdata = data.replace(style, tostyle)
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   131
            if newdata != data:
14168
135e244776f0 prevent transient leaks of file handle by using new helper functions
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13565
diff changeset
   132
                util.writefile(file, newdata)
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   133
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   134
@internaltool('internal:prompt', False)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   135
def _iprompt(repo, mynode, orig, fcd, fco, fca, toolconf):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   136
    """``internal:prompt``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   137
    Asks the user which of the local or the other version to keep as
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   138
    the merged version."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   139
    ui = repo.ui
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   140
    fd = fcd.path()
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   141
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   142
    if ui.promptchoice(_(" no tool found to merge %s\n"
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   143
                         "keep (l)ocal or take (o)ther?") % fd,
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   144
                       (_("&Local"), _("&Other")), 0):
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   145
        return _iother(repo, mynode, orig, fcd, fco, fca, toolconf)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   146
    else:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   147
        return _ilocal(repo, mynode, orig, fcd, fco, fca, toolconf)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   148
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   149
@internaltool('internal:local', False)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   150
def _ilocal(repo, mynode, orig, fcd, fco, fca, toolconf):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   151
    """``internal:local``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   152
    Uses the local version of files as the merged version."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   153
    return 0
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   154
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   155
@internaltool('internal:other', False)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   156
def _iother(repo, mynode, orig, fcd, fco, fca, toolconf):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   157
    """``internal:other``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   158
    Uses the other version of files as the merged version."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   159
    repo.wwrite(fcd.path(), fco.data(), fco.flags())
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   160
    return 0
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   161
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   162
@internaltool('internal:fail', False)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   163
def _ifail(repo, mynode, orig, fcd, fco, fca, toolconf):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   164
    """``internal:fail``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   165
    Rather than attempting to merge files that were modified on both
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   166
    branches, it marks them as unresolved. The resolve command must be
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   167
    used to resolve these conflicts."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   168
    return 1
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   169
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   170
def _premerge(repo, toolconf, files):
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   171
    tool, toolpath, binary, symlink = toolconf
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   172
    a, b, c, back = files
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   173
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   174
    ui = repo.ui
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   175
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   176
    # do we attempt to simplemerge first?
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   177
    try:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   178
        premerge = _toolbool(ui, tool, "premerge", not (binary or symlink))
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   179
    except error.ConfigError:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   180
        premerge = _toolstr(ui, tool, "premerge").lower()
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   181
        valid = 'keep'.split()
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   182
        if premerge not in valid:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   183
            _valid = ', '.join(["'" + v + "'" for v in valid])
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   184
            raise error.ConfigError(_("%s.premerge not valid "
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   185
                                      "('%s' is neither boolean nor %s)") %
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   186
                                    (tool, premerge, _valid))
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   187
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   188
    if premerge:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   189
        r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   190
        if not r:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   191
            ui.debug(" premerge successful\n")
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   192
            return 0
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   193
        if premerge != 'keep':
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   194
            util.copyfile(back, a) # restore from backup and try again
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   195
    return 1 # continue merging
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   196
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   197
@internaltool('internal:merge', True,
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   198
              _("merging %s incomplete! "
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   199
                "(edit conflicts, then use 'hg resolve --mark')\n"))
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   200
def _imerge(repo, mynode, orig, fcd, fco, fca, toolconf, files):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   201
    """``internal:merge``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   202
    Uses the internal non-interactive simple merge algorithm for merging
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   203
    files. It will fail if there are any conflicts and leave markers in
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   204
    the partially merged file."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   205
    r = _premerge(repo, toolconf, files)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   206
    if r:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   207
        a, b, c, back = files
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   208
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   209
        ui = repo.ui
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   210
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   211
        r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   212
        return True, r
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   213
    return False, 0
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   214
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   215
@internaltool('internal:dump', True)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   216
def _idump(repo, mynode, orig, fcd, fco, fca, toolconf, files):
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   217
    """``internal:dump``
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   218
    Creates three versions of the files to merge, containing the
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   219
    contents of local, other and base. These files can then be used to
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   220
    perform a merge manually. If the file to be merged is named
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   221
    ``a.txt``, these files will accordingly be named ``a.txt.local``,
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   222
    ``a.txt.other`` and ``a.txt.base`` and they will be placed in the
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   223
    same directory as ``a.txt``."""
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   224
    r = _premerge(repo, toolconf, files)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   225
    if r:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   226
        a, b, c, back = files
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   227
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   228
        fd = fcd.path()
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   229
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   230
        util.copyfile(a, a + ".local")
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   231
        repo.wwrite(fd + ".other", fco.data(), fco.flags())
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   232
        repo.wwrite(fd + ".base", fca.data(), fca.flags())
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   233
    return False, r
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   234
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   235
def _xmerge(repo, mynode, orig, fcd, fco, fca, toolconf, files):
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   236
    r = _premerge(repo, toolconf, files)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   237
    if r:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   238
        tool, toolpath, binary, symlink = toolconf
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   239
        a, b, c, back = files
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   240
        out = ""
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   241
        env = dict(HG_FILE=fcd.path(),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   242
                   HG_MY_NODE=short(mynode),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   243
                   HG_OTHER_NODE=str(fco.changectx()),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   244
                   HG_BASE_NODE=str(fca.changectx()),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   245
                   HG_MY_ISLINK='l' in fcd.flags(),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   246
                   HG_OTHER_ISLINK='l' in fco.flags(),
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   247
                   HG_BASE_ISLINK='l' in fca.flags())
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   248
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   249
        ui = repo.ui
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   250
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   251
        args = _toolstr(ui, tool, "args", '$local $base $other')
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   252
        if "$output" in args:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   253
            out, a = a, back # read input from backup, write to original
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   254
        replace = dict(local=a, base=b, other=c, output=out)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   255
        args = util.interpolate(r'\$', replace, args,
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   256
                                lambda s: '"%s"' % util.localpath(s))
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   257
        r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env,
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   258
                        out=ui.fout)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   259
        return True, r
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   260
    return False, 0
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   261
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   262
def filemerge(repo, mynode, orig, fcd, fco, fca):
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   263
    """perform a 3-way merge in the working directory
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   264
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   265
    mynode = parent node before merge
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   266
    orig = original local filename before merge
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   267
    fco = other file context
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   268
    fca = ancestor file context
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   269
    fcd = local file context for current/destination file
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   270
    """
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   271
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   272
    def temp(prefix, ctx):
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   273
        pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   274
        (fd, name) = tempfile.mkstemp(prefix=pre)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   275
        data = repo.wwritedata(ctx.path(), ctx.data())
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   276
        f = os.fdopen(fd, "wb")
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   277
        f.write(data)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   278
        f.close()
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   279
        return name
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   280
11702
eb07fbc21e9c filectx: use cmp(self, fctx) instead of cmp(self, text)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11149
diff changeset
   281
    if not fco.cmp(fcd): # files identical?
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   282
        return None
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   283
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   284
    ui = repo.ui
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   285
    fd = fcd.path()
15738
e86dd8dfdea0 context: add isbinary function
Laurens Holst <laurens.hg@grauw.nl>
parents: 15501
diff changeset
   286
    binary = fcd.isbinary() or fco.isbinary() or fca.isbinary()
6744
d3691d31fc9c context: remove islink and isexec methods
Matt Mackall <mpm@selenic.com>
parents: 6743
diff changeset
   287
    symlink = 'l' in fcd.flags() + fco.flags()
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   288
    tool, toolpath = _picktool(repo, ui, fd, binary, symlink)
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9049
diff changeset
   289
    ui.debug("picked tool '%s' for %s (binary %s symlink %s)\n" %
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   290
               (tool, fd, binary, symlink))
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   291
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   292
    if tool in internals:
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   293
        func = internals[tool]
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   294
        trymerge = func.trymerge
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   295
        onfailure = func.onfailure
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   296
    else:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   297
        func = _xmerge
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   298
        trymerge = True
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   299
        onfailure = _("merging %s failed!\n")
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   300
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   301
    toolconf = tool, toolpath, binary, symlink
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   302
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   303
    if not trymerge:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   304
        return func(repo, mynode, orig, fcd, fco, fca, toolconf)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   305
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   306
    a = repo.wjoin(fd)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   307
    b = temp("base", fca)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   308
    c = temp("other", fco)
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   309
    back = a + ".orig"
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   310
    util.copyfile(a, back)
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   311
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   312
    if orig != fco.path():
8615
94ca38e63576 use ui instead of repo.ui when the former is in scope
Martin Geisler <mg@lazybytes.net>
parents: 8567
diff changeset
   313
        ui.status(_("merging %s and %s to %s\n") % (orig, fco.path(), fd))
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   314
    else:
8615
94ca38e63576 use ui instead of repo.ui when the former is in scope
Martin Geisler <mg@lazybytes.net>
parents: 8567
diff changeset
   315
        ui.status(_("merging %s\n") % fd)
6512
368a4ec603cc merge: introduce mergestate
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
   316
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9049
diff changeset
   317
    ui.debug("my %s other %s ancestor %s\n" % (fcd, fco, fca))
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   318
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   319
    needcheck, r = func(repo, mynode, orig, fcd, fco, fca, toolconf,
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   320
                        (a, b, c, back))
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   321
    if not needcheck:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   322
        if r:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   323
            if onfailure:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   324
                ui.warn(onfailure % fd)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   325
        else:
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   326
            os.unlink(back)
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   327
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   328
        os.unlink(b)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   329
        os.unlink(c)
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   330
        return r
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   331
11148
a912f26777d3 merge: introduce tool.check parameter
David Champion <dgc@uchicago.edu>
parents: 11146
diff changeset
   332
    if not r and (_toolbool(ui, tool, "checkconflicts") or
a912f26777d3 merge: introduce tool.check parameter
David Champion <dgc@uchicago.edu>
parents: 11146
diff changeset
   333
                  'conflicts' in _toollist(ui, tool, "check")):
12046
8e7960feb139 Fix merge-tools.checkconflicts
Thomas Arendsen Hein <thomas@intevation.de>
parents: 12008
diff changeset
   334
        if re.search("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcd.data(),
8e7960feb139 Fix merge-tools.checkconflicts
Thomas Arendsen Hein <thomas@intevation.de>
parents: 12008
diff changeset
   335
                     re.MULTILINE):
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   336
            r = 1
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   337
11149
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   338
    checked = False
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   339
    if 'prompt' in _toollist(ui, tool, "check"):
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   340
        checked = True
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   341
        if ui.promptchoice(_("was merge of '%s' successful (yn)?") % fd,
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   342
                           (_("&Yes"), _("&No")), 1):
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   343
            r = 1
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   344
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   345
    if not r and not checked and (_toolbool(ui, tool, "checkchanged") or
d3c1eddfdbcf merge: tool.check = prompt will force an interactive merge check
David Champion <dgc@uchicago.edu>
parents: 11148
diff changeset
   346
                                  'changed' in _toollist(ui, tool, "check")):
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   347
        if filecmp.cmp(a, back):
9048
86b4a9b0ddda ui: extract choice from prompt
Simon Heimberg <simohe@besonet.ch>
parents: 8861
diff changeset
   348
            if ui.promptchoice(_(" output file %s appears unchanged\n"
9049
38b5d5e0efab filemerge, subrepo: correct indention
Martin Geisler <mg@lazybytes.net>
parents: 9048
diff changeset
   349
                                 "was merge successful (yn)?") % fd,
38b5d5e0efab filemerge, subrepo: correct indention
Martin Geisler <mg@lazybytes.net>
parents: 9048
diff changeset
   350
                               (_("&Yes"), _("&No")), 1):
6075
63e0e57ab157 filemerge: add 'checkchanged' merge tool property
Steve Borho <steve@borho.org>
parents: 6025
diff changeset
   351
                r = 1
63e0e57ab157 filemerge: add 'checkchanged' merge tool property
Steve Borho <steve@borho.org>
parents: 6025
diff changeset
   352
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   353
    if _toolbool(ui, tool, "fixeol"):
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   354
        _matcheol(a, back)
6005
3c33032d8906 merge: add support for tool EOL fixups
Matt Mackall <mpm@selenic.com>
parents: 6004
diff changeset
   355
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   356
    if r:
16125
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   357
        if onfailure:
83925d3a4559 filemerge: refactoring of 'filemerge()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15738
diff changeset
   358
            ui.warn(onfailure % fd)
6004
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   359
    else:
5af5f0f9d724 merge: allow smarter tool configuration
Matt Mackall <mpm@selenic.com>
parents: 6003
diff changeset
   360
        os.unlink(back)
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   361
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   362
    os.unlink(b)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   363
    os.unlink(c)
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   364
    return r
16126
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   365
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   366
# tell hggettext to extract docstrings from these functions:
0c4bec9596d8 filemerge: create detail of internal merge tools from documentation string
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16125
diff changeset
   367
i18nfunctions = internals.values()