##// END OF EJS Templates
filemerge: use ui out descriptor when calling util.system
Idan Kamara -
r14749:e3be7dc9 stable
parent child Browse files
Show More
@@ -1,269 +1,270 b''
1 1 # filemerge.py - file-level merge handling for Mercurial
2 2 #
3 3 # Copyright 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from node import short
9 9 from i18n import _
10 10 import util, simplemerge, match, error
11 11 import os, tempfile, re, filecmp
12 12
13 13 def _toolstr(ui, tool, part, default=""):
14 14 return ui.config("merge-tools", tool + "." + part, default)
15 15
16 16 def _toolbool(ui, tool, part, default=False):
17 17 return ui.configbool("merge-tools", tool + "." + part, default)
18 18
19 19 def _toollist(ui, tool, part, default=[]):
20 20 return ui.configlist("merge-tools", tool + "." + part, default)
21 21
22 22 _internal = ['internal:' + s
23 23 for s in 'fail local other merge prompt dump'.split()]
24 24
25 25 def _findtool(ui, tool):
26 26 if tool in _internal:
27 27 return tool
28 28 for kn in ("regkey", "regkeyalt"):
29 29 k = _toolstr(ui, tool, kn)
30 30 if not k:
31 31 continue
32 32 p = util.lookupreg(k, _toolstr(ui, tool, "regname"))
33 33 if p:
34 34 p = util.findexe(p + _toolstr(ui, tool, "regappend"))
35 35 if p:
36 36 return p
37 37 return util.findexe(_toolstr(ui, tool, "executable", tool))
38 38
39 39 def _picktool(repo, ui, path, binary, symlink):
40 40 def check(tool, pat, symlink, binary):
41 41 tmsg = tool
42 42 if pat:
43 43 tmsg += " specified for " + pat
44 44 if not _findtool(ui, tool):
45 45 if pat: # explicitly requested tool deserves a warning
46 46 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
47 47 else: # configured but non-existing tools are more silent
48 48 ui.note(_("couldn't find merge tool %s\n") % tmsg)
49 49 elif symlink and not _toolbool(ui, tool, "symlink"):
50 50 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
51 51 elif binary and not _toolbool(ui, tool, "binary"):
52 52 ui.warn(_("tool %s can't handle binary\n") % tmsg)
53 53 elif not util.gui() and _toolbool(ui, tool, "gui"):
54 54 ui.warn(_("tool %s requires a GUI\n") % tmsg)
55 55 else:
56 56 return True
57 57 return False
58 58
59 59 # forcemerge comes from command line arguments, highest priority
60 60 force = ui.config('ui', 'forcemerge')
61 61 if force:
62 62 toolpath = _findtool(ui, force)
63 63 if toolpath:
64 64 return (force, '"' + toolpath + '"')
65 65 else:
66 66 # mimic HGMERGE if given tool not found
67 67 return (force, force)
68 68
69 69 # HGMERGE takes next precedence
70 70 hgmerge = os.environ.get("HGMERGE")
71 71 if hgmerge:
72 72 return (hgmerge, hgmerge)
73 73
74 74 # then patterns
75 75 for pat, tool in ui.configitems("merge-patterns"):
76 76 mf = match.match(repo.root, '', [pat])
77 77 if mf(path) and check(tool, pat, symlink, False):
78 78 toolpath = _findtool(ui, tool)
79 79 return (tool, '"' + toolpath + '"')
80 80
81 81 # then merge tools
82 82 tools = {}
83 83 for k, v in ui.configitems("merge-tools"):
84 84 t = k.split('.')[0]
85 85 if t not in tools:
86 86 tools[t] = int(_toolstr(ui, t, "priority", "0"))
87 87 names = tools.keys()
88 88 tools = sorted([(-p, t) for t, p in tools.items()])
89 89 uimerge = ui.config("ui", "merge")
90 90 if uimerge:
91 91 if uimerge not in names:
92 92 return (uimerge, uimerge)
93 93 tools.insert(0, (None, uimerge)) # highest priority
94 94 tools.append((None, "hgmerge")) # the old default, if found
95 95 for p, t in tools:
96 96 if check(t, None, symlink, binary):
97 97 toolpath = _findtool(ui, t)
98 98 return (t, '"' + toolpath + '"')
99 99 # internal merge as last resort
100 100 return (not (symlink or binary) and "internal:merge" or None, None)
101 101
102 102 def _eoltype(data):
103 103 "Guess the EOL type of a file"
104 104 if '\0' in data: # binary
105 105 return None
106 106 if '\r\n' in data: # Windows
107 107 return '\r\n'
108 108 if '\r' in data: # Old Mac
109 109 return '\r'
110 110 if '\n' in data: # UNIX
111 111 return '\n'
112 112 return None # unknown
113 113
114 114 def _matcheol(file, origfile):
115 115 "Convert EOL markers in a file to match origfile"
116 116 tostyle = _eoltype(util.readfile(origfile))
117 117 if tostyle:
118 118 data = util.readfile(file)
119 119 style = _eoltype(data)
120 120 if style:
121 121 newdata = data.replace(style, tostyle)
122 122 if newdata != data:
123 123 util.writefile(file, newdata)
124 124
125 125 def filemerge(repo, mynode, orig, fcd, fco, fca):
126 126 """perform a 3-way merge in the working directory
127 127
128 128 mynode = parent node before merge
129 129 orig = original local filename before merge
130 130 fco = other file context
131 131 fca = ancestor file context
132 132 fcd = local file context for current/destination file
133 133 """
134 134
135 135 def temp(prefix, ctx):
136 136 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
137 137 (fd, name) = tempfile.mkstemp(prefix=pre)
138 138 data = repo.wwritedata(ctx.path(), ctx.data())
139 139 f = os.fdopen(fd, "wb")
140 140 f.write(data)
141 141 f.close()
142 142 return name
143 143
144 144 def isbin(ctx):
145 145 try:
146 146 return util.binary(ctx.data())
147 147 except IOError:
148 148 return False
149 149
150 150 if not fco.cmp(fcd): # files identical?
151 151 return None
152 152
153 153 ui = repo.ui
154 154 fd = fcd.path()
155 155 binary = isbin(fcd) or isbin(fco) or isbin(fca)
156 156 symlink = 'l' in fcd.flags() + fco.flags()
157 157 tool, toolpath = _picktool(repo, ui, fd, binary, symlink)
158 158 ui.debug("picked tool '%s' for %s (binary %s symlink %s)\n" %
159 159 (tool, fd, binary, symlink))
160 160
161 161 if not tool or tool == 'internal:prompt':
162 162 tool = "internal:local"
163 163 if ui.promptchoice(_(" no tool found to merge %s\n"
164 164 "keep (l)ocal or take (o)ther?") % fd,
165 165 (_("&Local"), _("&Other")), 0):
166 166 tool = "internal:other"
167 167 if tool == "internal:local":
168 168 return 0
169 169 if tool == "internal:other":
170 170 repo.wwrite(fd, fco.data(), fco.flags())
171 171 return 0
172 172 if tool == "internal:fail":
173 173 return 1
174 174
175 175 # do the actual merge
176 176 a = repo.wjoin(fd)
177 177 b = temp("base", fca)
178 178 c = temp("other", fco)
179 179 out = ""
180 180 back = a + ".orig"
181 181 util.copyfile(a, back)
182 182
183 183 if orig != fco.path():
184 184 ui.status(_("merging %s and %s to %s\n") % (orig, fco.path(), fd))
185 185 else:
186 186 ui.status(_("merging %s\n") % fd)
187 187
188 188 ui.debug("my %s other %s ancestor %s\n" % (fcd, fco, fca))
189 189
190 190 # do we attempt to simplemerge first?
191 191 try:
192 192 premerge = _toolbool(ui, tool, "premerge", not (binary or symlink))
193 193 except error.ConfigError:
194 194 premerge = _toolstr(ui, tool, "premerge").lower()
195 195 valid = 'keep'.split()
196 196 if premerge not in valid:
197 197 _valid = ', '.join(["'" + v + "'" for v in valid])
198 198 raise error.ConfigError(_("%s.premerge not valid "
199 199 "('%s' is neither boolean nor %s)") %
200 200 (tool, premerge, _valid))
201 201
202 202 if premerge:
203 203 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
204 204 if not r:
205 205 ui.debug(" premerge successful\n")
206 206 os.unlink(back)
207 207 os.unlink(b)
208 208 os.unlink(c)
209 209 return 0
210 210 if premerge != 'keep':
211 211 util.copyfile(back, a) # restore from backup and try again
212 212
213 213 env = dict(HG_FILE=fd,
214 214 HG_MY_NODE=short(mynode),
215 215 HG_OTHER_NODE=str(fco.changectx()),
216 216 HG_BASE_NODE=str(fca.changectx()),
217 217 HG_MY_ISLINK='l' in fcd.flags(),
218 218 HG_OTHER_ISLINK='l' in fco.flags(),
219 219 HG_BASE_ISLINK='l' in fca.flags())
220 220
221 221 if tool == "internal:merge":
222 222 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
223 223 elif tool == 'internal:dump':
224 224 a = repo.wjoin(fd)
225 225 util.copyfile(a, a + ".local")
226 226 repo.wwrite(fd + ".other", fco.data(), fco.flags())
227 227 repo.wwrite(fd + ".base", fca.data(), fca.flags())
228 228 return 1 # unresolved
229 229 else:
230 230 args = _toolstr(ui, tool, "args", '$local $base $other')
231 231 if "$output" in args:
232 232 out, a = a, back # read input from backup, write to original
233 233 replace = dict(local=a, base=b, other=c, output=out)
234 234 args = util.interpolate(r'\$', replace, args,
235 235 lambda s: '"%s"' % util.localpath(s))
236 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
236 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env,
237 out=ui.fout)
237 238
238 239 if not r and (_toolbool(ui, tool, "checkconflicts") or
239 240 'conflicts' in _toollist(ui, tool, "check")):
240 241 if re.search("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcd.data(),
241 242 re.MULTILINE):
242 243 r = 1
243 244
244 245 checked = False
245 246 if 'prompt' in _toollist(ui, tool, "check"):
246 247 checked = True
247 248 if ui.promptchoice(_("was merge of '%s' successful (yn)?") % fd,
248 249 (_("&Yes"), _("&No")), 1):
249 250 r = 1
250 251
251 252 if not r and not checked and (_toolbool(ui, tool, "checkchanged") or
252 253 'changed' in _toollist(ui, tool, "check")):
253 254 if filecmp.cmp(repo.wjoin(fd), back):
254 255 if ui.promptchoice(_(" output file %s appears unchanged\n"
255 256 "was merge successful (yn)?") % fd,
256 257 (_("&Yes"), _("&No")), 1):
257 258 r = 1
258 259
259 260 if _toolbool(ui, tool, "fixeol"):
260 261 _matcheol(repo.wjoin(fd), back)
261 262
262 263 if r:
263 264 ui.warn(_("merging %s failed!\n") % fd)
264 265 else:
265 266 os.unlink(back)
266 267
267 268 os.unlink(b)
268 269 os.unlink(c)
269 270 return r
General Comments 0
You need to be logged in to leave comments. Login now