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