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