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