##// END OF EJS Templates
use internal merge tool when specified for a merge-pattern in hgrc...
Dov Feldstern -
r6522:2b181fb3 default
parent child Browse files
Show More
@@ -1,217 +1,219
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
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 nullrev
8 from node import nullrev
9 from i18n import _
9 from i18n import _
10 import util, os, tempfile, simplemerge, re, filecmp
10 import util, os, tempfile, simplemerge, re, filecmp
11
11
12 def _toolstr(ui, tool, part, default=""):
12 def _toolstr(ui, tool, part, default=""):
13 return ui.config("merge-tools", tool + "." + part, default)
13 return ui.config("merge-tools", tool + "." + part, default)
14
14
15 def _toolbool(ui, tool, part, default=False):
15 def _toolbool(ui, tool, part, default=False):
16 return ui.configbool("merge-tools", tool + "." + part, default)
16 return ui.configbool("merge-tools", tool + "." + part, default)
17
17
18 def _findtool(ui, tool):
18 def _findtool(ui, tool):
19 if tool in ("internal:fail", "internal:local", "internal:other"):
20 return tool
19 k = _toolstr(ui, tool, "regkey")
21 k = _toolstr(ui, tool, "regkey")
20 if k:
22 if k:
21 p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
23 p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
22 if p:
24 if p:
23 p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
25 p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
24 if p:
26 if p:
25 return p
27 return p
26 return util.find_exe(_toolstr(ui, tool, "executable", tool))
28 return util.find_exe(_toolstr(ui, tool, "executable", tool))
27
29
28 def _picktool(repo, ui, path, binary, symlink):
30 def _picktool(repo, ui, path, binary, symlink):
29 def check(tool, pat, symlink, binary):
31 def check(tool, pat, symlink, binary):
30 tmsg = tool
32 tmsg = tool
31 if pat:
33 if pat:
32 tmsg += " specified for " + pat
34 tmsg += " specified for " + pat
33 if pat and not _findtool(ui, tool): # skip search if not matching
35 if pat and not _findtool(ui, tool): # skip search if not matching
34 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
36 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
35 elif symlink and not _toolbool(ui, tool, "symlink"):
37 elif symlink and not _toolbool(ui, tool, "symlink"):
36 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
38 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
37 elif binary and not _toolbool(ui, tool, "binary"):
39 elif binary and not _toolbool(ui, tool, "binary"):
38 ui.warn(_("tool %s can't handle binary\n") % tmsg)
40 ui.warn(_("tool %s can't handle binary\n") % tmsg)
39 elif not util.gui() and _toolbool(ui, tool, "gui"):
41 elif not util.gui() and _toolbool(ui, tool, "gui"):
40 ui.warn(_("tool %s requires a GUI\n") % tmsg)
42 ui.warn(_("tool %s requires a GUI\n") % tmsg)
41 else:
43 else:
42 return True
44 return True
43 return False
45 return False
44
46
45 # HGMERGE takes precedence
47 # HGMERGE takes precedence
46 hgmerge = os.environ.get("HGMERGE")
48 hgmerge = os.environ.get("HGMERGE")
47 if hgmerge:
49 if hgmerge:
48 return (hgmerge, hgmerge)
50 return (hgmerge, hgmerge)
49
51
50 # then patterns
52 # then patterns
51 for pat, tool in ui.configitems("merge-patterns"):
53 for pat, tool in ui.configitems("merge-patterns"):
52 mf = util.matcher(repo.root, "", [pat], [], [])[1]
54 mf = util.matcher(repo.root, "", [pat], [], [])[1]
53 if mf(path) and check(tool, pat, symlink, False):
55 if mf(path) and check(tool, pat, symlink, False):
54 toolpath = _findtool(ui, tool)
56 toolpath = _findtool(ui, tool)
55 return (tool, '"' + toolpath + '"')
57 return (tool, '"' + toolpath + '"')
56
58
57 # then merge tools
59 # then merge tools
58 tools = {}
60 tools = {}
59 for k,v in ui.configitems("merge-tools"):
61 for k,v in ui.configitems("merge-tools"):
60 t = k.split('.')[0]
62 t = k.split('.')[0]
61 if t not in tools:
63 if t not in tools:
62 tools[t] = int(_toolstr(ui, t, "priority", "0"))
64 tools[t] = int(_toolstr(ui, t, "priority", "0"))
63 names = tools.keys()
65 names = tools.keys()
64 tools = [(-p,t) for t,p in tools.items()]
66 tools = [(-p,t) for t,p in tools.items()]
65 tools.sort()
67 tools.sort()
66 uimerge = ui.config("ui", "merge")
68 uimerge = ui.config("ui", "merge")
67 if uimerge:
69 if uimerge:
68 if uimerge not in names:
70 if uimerge not in names:
69 return (uimerge, uimerge)
71 return (uimerge, uimerge)
70 tools.insert(0, (None, uimerge)) # highest priority
72 tools.insert(0, (None, uimerge)) # highest priority
71 tools.append((None, "hgmerge")) # the old default, if found
73 tools.append((None, "hgmerge")) # the old default, if found
72 for p,t in tools:
74 for p,t in tools:
73 toolpath = _findtool(ui, t)
75 toolpath = _findtool(ui, t)
74 if toolpath and check(t, None, symlink, binary):
76 if toolpath and check(t, None, symlink, binary):
75 return (t, '"' + toolpath + '"')
77 return (t, '"' + toolpath + '"')
76 # internal merge as last resort
78 # internal merge as last resort
77 return (not (symlink or binary) and "internal:merge" or None, None)
79 return (not (symlink or binary) and "internal:merge" or None, None)
78
80
79 def _eoltype(data):
81 def _eoltype(data):
80 "Guess the EOL type of a file"
82 "Guess the EOL type of a file"
81 if '\0' in data: # binary
83 if '\0' in data: # binary
82 return None
84 return None
83 if '\r\n' in data: # Windows
85 if '\r\n' in data: # Windows
84 return '\r\n'
86 return '\r\n'
85 if '\r' in data: # Old Mac
87 if '\r' in data: # Old Mac
86 return '\r'
88 return '\r'
87 if '\n' in data: # UNIX
89 if '\n' in data: # UNIX
88 return '\n'
90 return '\n'
89 return None # unknown
91 return None # unknown
90
92
91 def _matcheol(file, origfile):
93 def _matcheol(file, origfile):
92 "Convert EOL markers in a file to match origfile"
94 "Convert EOL markers in a file to match origfile"
93 tostyle = _eoltype(open(origfile, "rb").read())
95 tostyle = _eoltype(open(origfile, "rb").read())
94 if tostyle:
96 if tostyle:
95 data = open(file, "rb").read()
97 data = open(file, "rb").read()
96 style = _eoltype(data)
98 style = _eoltype(data)
97 if style:
99 if style:
98 newdata = data.replace(style, tostyle)
100 newdata = data.replace(style, tostyle)
99 if newdata != data:
101 if newdata != data:
100 open(file, "wb").write(newdata)
102 open(file, "wb").write(newdata)
101
103
102 def filemerge(repo, fw, fd, fo, wctx, mctx):
104 def filemerge(repo, fw, fd, fo, wctx, mctx):
103 """perform a 3-way merge in the working directory
105 """perform a 3-way merge in the working directory
104
106
105 fw = original filename in the working directory
107 fw = original filename in the working directory
106 fd = destination filename in the working directory
108 fd = destination filename in the working directory
107 fo = filename in other parent
109 fo = filename in other parent
108 wctx, mctx = working and merge changecontexts
110 wctx, mctx = working and merge changecontexts
109 """
111 """
110
112
111 def temp(prefix, ctx):
113 def temp(prefix, ctx):
112 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
114 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
113 (fd, name) = tempfile.mkstemp(prefix=pre)
115 (fd, name) = tempfile.mkstemp(prefix=pre)
114 data = repo.wwritedata(ctx.path(), ctx.data())
116 data = repo.wwritedata(ctx.path(), ctx.data())
115 f = os.fdopen(fd, "wb")
117 f = os.fdopen(fd, "wb")
116 f.write(data)
118 f.write(data)
117 f.close()
119 f.close()
118 return name
120 return name
119
121
120 def isbin(ctx):
122 def isbin(ctx):
121 try:
123 try:
122 return util.binary(ctx.data())
124 return util.binary(ctx.data())
123 except IOError:
125 except IOError:
124 return False
126 return False
125
127
126 fco = mctx.filectx(fo)
128 fco = mctx.filectx(fo)
127 if not fco.cmp(wctx.filectx(fd).data()): # files identical?
129 if not fco.cmp(wctx.filectx(fd).data()): # files identical?
128 return None
130 return None
129
131
130 ui = repo.ui
132 ui = repo.ui
131 fcm = wctx.filectx(fw)
133 fcm = wctx.filectx(fw)
132 fca = fcm.ancestor(fco) or repo.filectx(fw, fileid=nullrev)
134 fca = fcm.ancestor(fco) or repo.filectx(fw, fileid=nullrev)
133 binary = isbin(fcm) or isbin(fco) or isbin(fca)
135 binary = isbin(fcm) or isbin(fco) or isbin(fca)
134 symlink = fcm.islink() or fco.islink()
136 symlink = fcm.islink() or fco.islink()
135 tool, toolpath = _picktool(repo, ui, fw, binary, symlink)
137 tool, toolpath = _picktool(repo, ui, fw, binary, symlink)
136 ui.debug(_("picked tool '%s' for %s (binary %s symlink %s)\n") %
138 ui.debug(_("picked tool '%s' for %s (binary %s symlink %s)\n") %
137 (tool, fw, binary, symlink))
139 (tool, fw, binary, symlink))
138
140
139 if not tool:
141 if not tool:
140 tool = "internal:local"
142 tool = "internal:local"
141 if ui.prompt(_(" no tool found to merge %s\n"
143 if ui.prompt(_(" no tool found to merge %s\n"
142 "keep (l)ocal or take (o)ther?") % fw,
144 "keep (l)ocal or take (o)ther?") % fw,
143 _("[lo]"), _("l")) != _("l"):
145 _("[lo]"), _("l")) != _("l"):
144 tool = "internal:other"
146 tool = "internal:other"
145 if tool == "internal:local":
147 if tool == "internal:local":
146 return 0
148 return 0
147 if tool == "internal:other":
149 if tool == "internal:other":
148 repo.wwrite(fd, fco.data(), fco.fileflags())
150 repo.wwrite(fd, fco.data(), fco.fileflags())
149 return 0
151 return 0
150 if tool == "internal:fail":
152 if tool == "internal:fail":
151 return 1
153 return 1
152
154
153 # do the actual merge
155 # do the actual merge
154 a = repo.wjoin(fd)
156 a = repo.wjoin(fd)
155 b = temp("base", fca)
157 b = temp("base", fca)
156 c = temp("other", fco)
158 c = temp("other", fco)
157 out = ""
159 out = ""
158 back = a + ".orig"
160 back = a + ".orig"
159 util.copyfile(a, back)
161 util.copyfile(a, back)
160
162
161 if fw != fo:
163 if fw != fo:
162 repo.ui.status(_("merging %s and %s\n") % (fw, fo))
164 repo.ui.status(_("merging %s and %s\n") % (fw, fo))
163 else:
165 else:
164 repo.ui.status(_("merging %s\n") % fw)
166 repo.ui.status(_("merging %s\n") % fw)
165 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
167 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
166
168
167 # do we attempt to simplemerge first?
169 # do we attempt to simplemerge first?
168 if _toolbool(ui, tool, "premerge", not (binary or symlink)):
170 if _toolbool(ui, tool, "premerge", not (binary or symlink)):
169 r = simplemerge.simplemerge(a, b, c, quiet=True)
171 r = simplemerge.simplemerge(a, b, c, quiet=True)
170 if not r:
172 if not r:
171 ui.debug(_(" premerge successful\n"))
173 ui.debug(_(" premerge successful\n"))
172 os.unlink(back)
174 os.unlink(back)
173 os.unlink(b)
175 os.unlink(b)
174 os.unlink(c)
176 os.unlink(c)
175 return 0
177 return 0
176 util.copyfile(back, a) # restore from backup and try again
178 util.copyfile(back, a) # restore from backup and try again
177
179
178 env = dict(HG_FILE=fd,
180 env = dict(HG_FILE=fd,
179 HG_MY_NODE=str(wctx.parents()[0]),
181 HG_MY_NODE=str(wctx.parents()[0]),
180 HG_OTHER_NODE=str(mctx),
182 HG_OTHER_NODE=str(mctx),
181 HG_MY_ISLINK=fcm.islink(),
183 HG_MY_ISLINK=fcm.islink(),
182 HG_OTHER_ISLINK=fco.islink(),
184 HG_OTHER_ISLINK=fco.islink(),
183 HG_BASE_ISLINK=fca.islink())
185 HG_BASE_ISLINK=fca.islink())
184
186
185 if tool == "internal:merge":
187 if tool == "internal:merge":
186 r = simplemerge.simplemerge(a, b, c, label=['local', 'other'])
188 r = simplemerge.simplemerge(a, b, c, label=['local', 'other'])
187 else:
189 else:
188 args = _toolstr(ui, tool, "args", '$local $base $other')
190 args = _toolstr(ui, tool, "args", '$local $base $other')
189 if "$output" in args:
191 if "$output" in args:
190 out, a = a, back # read input from backup, write to original
192 out, a = a, back # read input from backup, write to original
191 replace = dict(local=a, base=b, other=c, output=out)
193 replace = dict(local=a, base=b, other=c, output=out)
192 args = re.sub("\$(local|base|other|output)",
194 args = re.sub("\$(local|base|other|output)",
193 lambda x: '"%s"' % replace[x.group()[1:]], args)
195 lambda x: '"%s"' % replace[x.group()[1:]], args)
194 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
196 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
195
197
196 if not r and _toolbool(ui, tool, "checkconflicts"):
198 if not r and _toolbool(ui, tool, "checkconflicts"):
197 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcm.data()):
199 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcm.data()):
198 r = 1
200 r = 1
199
201
200 if not r and _toolbool(ui, tool, "checkchanged"):
202 if not r and _toolbool(ui, tool, "checkchanged"):
201 if filecmp.cmp(repo.wjoin(fd), back):
203 if filecmp.cmp(repo.wjoin(fd), back):
202 if ui.prompt(_(" output file %s appears unchanged\n"
204 if ui.prompt(_(" output file %s appears unchanged\n"
203 "was merge successful (yn)?") % fd,
205 "was merge successful (yn)?") % fd,
204 _("[yn]"), _("n")) != _("y"):
206 _("[yn]"), _("n")) != _("y"):
205 r = 1
207 r = 1
206
208
207 if _toolbool(ui, tool, "fixeol"):
209 if _toolbool(ui, tool, "fixeol"):
208 _matcheol(repo.wjoin(fd), back)
210 _matcheol(repo.wjoin(fd), back)
209
211
210 if r:
212 if r:
211 repo.ui.warn(_("merging %s failed!\n") % fd)
213 repo.ui.warn(_("merging %s failed!\n") % fd)
212 else:
214 else:
213 os.unlink(back)
215 os.unlink(back)
214
216
215 os.unlink(b)
217 os.unlink(b)
216 os.unlink(c)
218 os.unlink(c)
217 return r
219 return r
General Comments 0
You need to be logged in to leave comments. Login now