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