Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/filemerge.py @ 6004:5af5f0f9d724
merge: allow smarter tool configuration
Add [merge-tool] hgrc section with:
<tool>.executable = name or path (<tool>)
<tool>.args = args with $local/base/other/output ($local $base $other)
<tool>.priority = priority (default 0)
<tool>.binary = handles binary (False)
<tool>.symlink = handles symlinks (False)
<tool>.checkconflict = check for conflict markers (False)
<tool>.premerge = try internal simplemerge (True if not binary or symlink)
Four built-in tools: internal:{merge,local,other,fail}
Add [merge-patterns] section of the form:
<pattern> = <tool>
Priority of settings is:
HGMERGE
merge-patterns
ui:merge
merge-tools by priority
hgmerge, if it can be found
Changes:
unsuccessful merges leave .orig files
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Sun, 03 Feb 2008 19:29:05 -0600 |
parents | 7855b88ba838 |
children | 3c33032d8906 |
comparison
equal
deleted
inserted
replaced
6003:7855b88ba838 | 6004:5af5f0f9d724 |
---|---|
5 # This software may be used and distributed according to the terms | 5 # This software may be used and distributed according to the terms |
6 # of the GNU General Public License, incorporated herein by reference. | 6 # of the GNU General Public License, incorporated herein by reference. |
7 | 7 |
8 from node import * | 8 from node import * |
9 from i18n import _ | 9 from i18n import _ |
10 import util, os, tempfile, context | 10 import util, os, tempfile, context, simplemerge, re |
11 | |
12 def _toolstr(ui, tool, part, default=None): | |
13 return ui.config("merge-tools", tool + "." + part, default) | |
14 | |
15 def _toolbool(ui, tool, part, default=False): | |
16 return ui.configbool("merge-tools", tool + "." + part, default) | |
17 | |
18 def _findtool(ui, tool): | |
19 return util.find_exe(_toolstr(ui, tool, "executable", tool)) | |
20 | |
21 def _picktool(repo, ui, path, binary, symlink): | |
22 def check(tool, pat, symlink, binary): | |
23 tmsg = tool | |
24 if pat: | |
25 tmsg += " specified for " + pat | |
26 if pat and not _findtool(ui, tool): # skip search if not matching | |
27 ui.warn(_("couldn't find merge tool %s\n") % tmsg) | |
28 elif symlink and not _toolbool(ui, tool, "symlink"): | |
29 ui.warn(_("tool %s can't handle symlinks\n") % tmsg) | |
30 elif binary and not _toolbool(ui, tool, "binary"): | |
31 ui.warn(_("tool %s can't handle binary\n") % tmsg) | |
32 else: | |
33 return True | |
34 return False | |
35 | |
36 # HGMERGE takes precedence | |
37 if os.environ.get("HGMERGE"): | |
38 return os.environ.get("HGMERGE") | |
39 | |
40 # then patterns | |
41 for pattern, tool in ui.configitems("merge-patterns"): | |
42 mf = util.matcher(repo.root, "", [pat], [], [])[1] | |
43 if mf(path) and check(tool, pat, symlink, False): | |
44 return tool | |
45 | |
46 # then merge tools | |
47 tools = {} | |
48 for k,v in ui.configitems("merge-tools"): | |
49 t = k.split('.')[0] | |
50 if t not in tools: | |
51 tools[t] = int(_toolstr(ui, t, "priority", "0")) | |
52 tools = [(-p,t) for t,p in tools.items()] | |
53 tools.sort() | |
54 if ui.config("ui", "merge"): | |
55 tools.insert(0, (None, ui.config("ui", "merge"))) # highest priority | |
56 tools.append((None, "hgmerge")) # the old default, if found | |
57 tools.append((None, "internal:merge")) # internal merge as last resort | |
58 for p,t in tools: | |
59 if _findtool(ui, t) and check(t, None, symlink, binary): | |
60 return t | |
11 | 61 |
12 def filemerge(repo, fw, fd, fo, wctx, mctx): | 62 def filemerge(repo, fw, fd, fo, wctx, mctx): |
13 """perform a 3-way merge in the working directory | 63 """perform a 3-way merge in the working directory |
14 | 64 |
15 fw = original filename in the working directory | 65 fw = original filename in the working directory |
25 f = os.fdopen(fd, "wb") | 75 f = os.fdopen(fd, "wb") |
26 f.write(data) | 76 f.write(data) |
27 f.close() | 77 f.close() |
28 return name | 78 return name |
29 | 79 |
30 fcm = wctx.filectx(fw) | 80 def isbin(ctx): |
31 fcmdata = wctx.filectx(fd).data() | 81 try: |
82 return util.binary(ctx.data()) | |
83 except IOError: | |
84 return False | |
85 | |
32 fco = mctx.filectx(fo) | 86 fco = mctx.filectx(fo) |
33 | 87 if not fco.cmp(wctx.filectx(fd).data()): # files identical? |
34 if not fco.cmp(fcmdata): # files identical? | |
35 return None | 88 return None |
36 | 89 |
37 fca = fcm.ancestor(fco) | 90 ui = repo.ui |
38 if not fca: | 91 fcm = wctx.filectx(fw) |
39 fca = repo.filectx(fw, fileid=nullrev) | 92 fca = fcm.ancestor(fco) or repo.filectx(fw, fileid=nullrev) |
93 binary = isbin(fcm) or isbin(fco) or isbin(fca) | |
94 symlink = fcm.islink() or fco.islink() | |
95 tool = _picktool(repo, ui, fw, binary, symlink) | |
96 ui.debug(_("picked tool '%s' for %s (binary %s symlink %s)\n") % | |
97 (tool, fw, binary, symlink)) | |
98 | |
99 if not tool: | |
100 tool = "internal:local" | |
101 if ui.prompt(_(" no tool found to merge %s\n" | |
102 "keep (l)ocal or take (o)ther?") % fw, | |
103 _("[lo]"), _("l")) != _("l"): | |
104 tool = "internal:other" | |
105 if tool == "internal:local": | |
106 return 0 | |
107 if tool == "internal:other": | |
108 repo.wwrite(fd, fco.data(), fco.fileflags()) | |
109 return 0 | |
110 if tool == "internal:fail": | |
111 return 1 | |
112 | |
113 # do the actual merge | |
40 a = repo.wjoin(fd) | 114 a = repo.wjoin(fd) |
41 b = temp("base", fca) | 115 b = temp("base", fca) |
42 c = temp("other", fco) | 116 c = temp("other", fco) |
117 out = "" | |
118 back = a + ".orig" | |
119 util.copyfile(a, back) | |
43 | 120 |
44 if fw != fo: | 121 if fw != fo: |
45 repo.ui.status(_("merging %s and %s\n") % (fw, fo)) | 122 repo.ui.status(_("merging %s and %s\n") % (fw, fo)) |
46 else: | 123 else: |
47 repo.ui.status(_("merging %s\n") % fw) | 124 repo.ui.status(_("merging %s\n") % fw) |
48 | |
49 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca)) | 125 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca)) |
50 | 126 |
51 cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge") | 127 # do we attempt to simplemerge first? |
52 or "hgmerge") | 128 if _toolbool(ui, tool, "premerge", not (binary or symlink)): |
53 r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root, | 129 r = simplemerge.simplemerge(a, b, c, quiet=True) |
54 environ={'HG_FILE': fd, | 130 if not r: |
55 'HG_MY_NODE': str(wctx.parents()[0]), | 131 ui.debug(_(" premerge successful\n")) |
56 'HG_OTHER_NODE': str(mctx), | 132 os.unlink(back) |
57 'HG_MY_ISLINK': fcm.islink(), | 133 os.unlink(b) |
58 'HG_OTHER_ISLINK': fco.islink(), | 134 os.unlink(c) |
59 'HG_BASE_ISLINK': fca.islink(),}) | 135 return 0 |
136 util.copyfile(back, a) # restore from backup and try again | |
137 | |
138 env = dict(HG_FILE=fd, | |
139 HG_MY_NODE=str(wctx.parents()[0]), | |
140 HG_OTHER_NODE=str(mctx), | |
141 HG_MY_ISLINK=fcm.islink(), | |
142 HG_OTHER_ISLINK=fco.islink(), | |
143 HG_BASE_ISLINK=fca.islink()) | |
144 | |
145 if tool == "internal:merge": | |
146 r = simplemerge.simplemerge(a, b, c, label=['local', 'other']) | |
147 else: | |
148 toolpath = _findtool(ui, tool) | |
149 args = _toolstr(ui, tool, "args", '$local $base $other') | |
150 if "$output" in args: | |
151 out, a = a, back # read input from backup, write to original | |
152 replace = dict(local=a, base=b, other=c, output=out) | |
153 args = re.sub("\$(local|base|other|output)", | |
154 lambda x: '"%s"' % replace[x.group()[1:]], args) | |
155 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env) | |
156 | |
157 if not r and _toolbool(ui, tool, "checkconflicts"): | |
158 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcm.data()): | |
159 r = 1 | |
160 | |
60 if r: | 161 if r: |
61 repo.ui.warn(_("merging %s failed!\n") % fd) | 162 repo.ui.warn(_("merging %s failed!\n") % fd) |
163 else: | |
164 os.unlink(back) | |
62 | 165 |
63 os.unlink(b) | 166 os.unlink(b) |
64 os.unlink(c) | 167 os.unlink(c) |
65 return r | 168 return r |