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