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