##// END OF EJS Templates
filemerge: refactoring of 'filemerge()'...
FUJIWARA Katsunori -
r16125:83925d3a default
parent child Browse files
Show More
@@ -19,8 +19,16 b' def _toolbool(ui, tool, part, default=Fa'
19 19 def _toollist(ui, tool, part, default=[]):
20 20 return ui.configlist("merge-tools", tool + "." + part, default)
21 21
22 _internal = ['internal:' + s
23 for s in 'fail local other merge prompt dump'.split()]
22 _internal = {}
23
24 def internaltool(name, trymerge, onfailure=None):
25 '''return a decorator for populating internal merge tool table'''
26 def decorator(func):
27 _internal[name] = func
28 func.trymerge = trymerge
29 func.onfailure = onfailure
30 return func
31 return decorator
24 32
25 33 def _findtool(ui, tool):
26 34 if tool in _internal:
@@ -123,6 +131,112 b' def _matcheol(file, origfile):'
123 131 if newdata != data:
124 132 util.writefile(file, newdata)
125 133
134 @internaltool('internal:prompt', False)
135 def _iprompt(repo, mynode, orig, fcd, fco, fca, toolconf):
136 ui = repo.ui
137 fd = fcd.path()
138
139 if ui.promptchoice(_(" no tool found to merge %s\n"
140 "keep (l)ocal or take (o)ther?") % fd,
141 (_("&Local"), _("&Other")), 0):
142 return _iother(repo, mynode, orig, fcd, fco, fca, toolconf)
143 else:
144 return _ilocal(repo, mynode, orig, fcd, fco, fca, toolconf)
145
146 @internaltool('internal:local', False)
147 def _ilocal(repo, mynode, orig, fcd, fco, fca, toolconf):
148 return 0
149
150 @internaltool('internal:other', False)
151 def _iother(repo, mynode, orig, fcd, fco, fca, toolconf):
152 repo.wwrite(fcd.path(), fco.data(), fco.flags())
153 return 0
154
155 @internaltool('internal:fail', False)
156 def _ifail(repo, mynode, orig, fcd, fco, fca, toolconf):
157 return 1
158
159 def _premerge(repo, toolconf, files):
160 tool, toolpath, binary, symlink = toolconf
161 a, b, c, back = files
162
163 ui = repo.ui
164
165 # do we attempt to simplemerge first?
166 try:
167 premerge = _toolbool(ui, tool, "premerge", not (binary or symlink))
168 except error.ConfigError:
169 premerge = _toolstr(ui, tool, "premerge").lower()
170 valid = 'keep'.split()
171 if premerge not in valid:
172 _valid = ', '.join(["'" + v + "'" for v in valid])
173 raise error.ConfigError(_("%s.premerge not valid "
174 "('%s' is neither boolean nor %s)") %
175 (tool, premerge, _valid))
176
177 if premerge:
178 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
179 if not r:
180 ui.debug(" premerge successful\n")
181 return 0
182 if premerge != 'keep':
183 util.copyfile(back, a) # restore from backup and try again
184 return 1 # continue merging
185
186 @internaltool('internal:merge', True,
187 _("merging %s incomplete! "
188 "(edit conflicts, then use 'hg resolve --mark')\n"))
189 def _imerge(repo, mynode, orig, fcd, fco, fca, toolconf, files):
190 r = _premerge(repo, toolconf, files)
191 if r:
192 a, b, c, back = files
193
194 ui = repo.ui
195
196 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
197 return True, r
198 return False, 0
199
200 @internaltool('internal:dump', True)
201 def _idump(repo, mynode, orig, fcd, fco, fca, toolconf, files):
202 r = _premerge(repo, toolconf, files)
203 if r:
204 a, b, c, back = files
205
206 fd = fcd.path()
207
208 util.copyfile(a, a + ".local")
209 repo.wwrite(fd + ".other", fco.data(), fco.flags())
210 repo.wwrite(fd + ".base", fca.data(), fca.flags())
211 return False, r
212
213 def _xmerge(repo, mynode, orig, fcd, fco, fca, toolconf, files):
214 r = _premerge(repo, toolconf, files)
215 if r:
216 tool, toolpath, binary, symlink = toolconf
217 a, b, c, back = files
218 out = ""
219 env = dict(HG_FILE=fcd.path(),
220 HG_MY_NODE=short(mynode),
221 HG_OTHER_NODE=str(fco.changectx()),
222 HG_BASE_NODE=str(fca.changectx()),
223 HG_MY_ISLINK='l' in fcd.flags(),
224 HG_OTHER_ISLINK='l' in fco.flags(),
225 HG_BASE_ISLINK='l' in fca.flags())
226
227 ui = repo.ui
228
229 args = _toolstr(ui, tool, "args", '$local $base $other')
230 if "$output" in args:
231 out, a = a, back # read input from backup, write to original
232 replace = dict(local=a, base=b, other=c, output=out)
233 args = util.interpolate(r'\$', replace, args,
234 lambda s: '"%s"' % util.localpath(s))
235 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env,
236 out=ui.fout)
237 return True, r
238 return False, 0
239
126 240 def filemerge(repo, mynode, orig, fcd, fco, fca):
127 241 """perform a 3-way merge in the working directory
128 242
@@ -153,25 +267,23 b' def filemerge(repo, mynode, orig, fcd, f'
153 267 ui.debug("picked tool '%s' for %s (binary %s symlink %s)\n" %
154 268 (tool, fd, binary, symlink))
155 269
156 if not tool or tool == 'internal:prompt':
157 tool = "internal:local"
158 if ui.promptchoice(_(" no tool found to merge %s\n"
159 "keep (l)ocal or take (o)ther?") % fd,
160 (_("&Local"), _("&Other")), 0):
161 tool = "internal:other"
162 if tool == "internal:local":
163 return 0
164 if tool == "internal:other":
165 repo.wwrite(fd, fco.data(), fco.flags())
166 return 0
167 if tool == "internal:fail":
168 return 1
270 if tool in _internal:
271 func = _internal[tool]
272 trymerge = func.trymerge
273 onfailure = func.onfailure
274 else:
275 func = _xmerge
276 trymerge = True
277 onfailure = _("merging %s failed!\n")
169 278
170 # do the actual merge
279 toolconf = tool, toolpath, binary, symlink
280
281 if not trymerge:
282 return func(repo, mynode, orig, fcd, fco, fca, toolconf)
283
171 284 a = repo.wjoin(fd)
172 285 b = temp("base", fca)
173 286 c = temp("other", fco)
174 out = ""
175 287 back = a + ".orig"
176 288 util.copyfile(a, back)
177 289
@@ -182,54 +294,18 b' def filemerge(repo, mynode, orig, fcd, f'
182 294
183 295 ui.debug("my %s other %s ancestor %s\n" % (fcd, fco, fca))
184 296
185 # do we attempt to simplemerge first?
186 try:
187 premerge = _toolbool(ui, tool, "premerge", not (binary or symlink))
188 except error.ConfigError:
189 premerge = _toolstr(ui, tool, "premerge").lower()
190 valid = 'keep'.split()
191 if premerge not in valid:
192 _valid = ', '.join(["'" + v + "'" for v in valid])
193 raise error.ConfigError(_("%s.premerge not valid "
194 "('%s' is neither boolean nor %s)") %
195 (tool, premerge, _valid))
196
197 if premerge:
198 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
199 if not r:
200 ui.debug(" premerge successful\n")
297 needcheck, r = func(repo, mynode, orig, fcd, fco, fca, toolconf,
298 (a, b, c, back))
299 if not needcheck:
300 if r:
301 if onfailure:
302 ui.warn(onfailure % fd)
303 else:
201 304 os.unlink(back)
202 os.unlink(b)
203 os.unlink(c)
204 return 0
205 if premerge != 'keep':
206 util.copyfile(back, a) # restore from backup and try again
207 305
208 env = dict(HG_FILE=fd,
209 HG_MY_NODE=short(mynode),
210 HG_OTHER_NODE=str(fco.changectx()),
211 HG_BASE_NODE=str(fca.changectx()),
212 HG_MY_ISLINK='l' in fcd.flags(),
213 HG_OTHER_ISLINK='l' in fco.flags(),
214 HG_BASE_ISLINK='l' in fca.flags())
215
216 if tool == "internal:merge":
217 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
218 elif tool == 'internal:dump':
219 a = repo.wjoin(fd)
220 util.copyfile(a, a + ".local")
221 repo.wwrite(fd + ".other", fco.data(), fco.flags())
222 repo.wwrite(fd + ".base", fca.data(), fca.flags())
223 return 1 # unresolved
224 else:
225 args = _toolstr(ui, tool, "args", '$local $base $other')
226 if "$output" in args:
227 out, a = a, back # read input from backup, write to original
228 replace = dict(local=a, base=b, other=c, output=out)
229 args = util.interpolate(r'\$', replace, args,
230 lambda s: '"%s"' % util.localpath(s))
231 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env,
232 out=ui.fout)
306 os.unlink(b)
307 os.unlink(c)
308 return r
233 309
234 310 if not r and (_toolbool(ui, tool, "checkconflicts") or
235 311 'conflicts' in _toollist(ui, tool, "check")):
@@ -246,21 +322,18 b' def filemerge(repo, mynode, orig, fcd, f'
246 322
247 323 if not r and not checked and (_toolbool(ui, tool, "checkchanged") or
248 324 'changed' in _toollist(ui, tool, "check")):
249 if filecmp.cmp(repo.wjoin(fd), back):
325 if filecmp.cmp(a, back):
250 326 if ui.promptchoice(_(" output file %s appears unchanged\n"
251 327 "was merge successful (yn)?") % fd,
252 328 (_("&Yes"), _("&No")), 1):
253 329 r = 1
254 330
255 331 if _toolbool(ui, tool, "fixeol"):
256 _matcheol(repo.wjoin(fd), back)
332 _matcheol(a, back)
257 333
258 334 if r:
259 if tool == "internal:merge":
260 ui.warn(_("merging %s incomplete! "
261 "(edit conflicts, then use 'hg resolve --mark')\n") % fd)
262 else:
263 ui.warn(_("merging %s failed!\n") % fd)
335 if onfailure:
336 ui.warn(onfailure % fd)
264 337 else:
265 338 os.unlink(back)
266 339
General Comments 0
You need to be logged in to leave comments. Login now