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