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