Show More
@@ -1,337 +1,334 b'' | |||
|
1 | 1 | # merge.py - directory-level update/merge handling for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 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 gettext as _ |
|
10 | 10 | from demandload import * |
|
11 | 11 | demandload(globals(), "util os tempfile") |
|
12 | 12 | |
|
13 | 13 | def fmerge(f, local, other, ancestor): |
|
14 | 14 | """merge executable flags""" |
|
15 | 15 | a, b, c = ancestor.execf(f), local.execf(f), other.execf(f) |
|
16 | 16 | return ((a^b) | (a^c)) ^ a |
|
17 | 17 | |
|
18 | 18 | def merge3(repo, fn, my, other, p1, p2): |
|
19 | 19 | """perform a 3-way merge in the working directory""" |
|
20 | 20 | |
|
21 | 21 | def temp(prefix, node): |
|
22 | 22 | pre = "%s~%s." % (os.path.basename(fn), prefix) |
|
23 | 23 | (fd, name) = tempfile.mkstemp(prefix=pre) |
|
24 | 24 | f = os.fdopen(fd, "wb") |
|
25 | 25 | repo.wwrite(fn, fl.read(node), f) |
|
26 | 26 | f.close() |
|
27 | 27 | return name |
|
28 | 28 | |
|
29 | 29 | fl = repo.file(fn) |
|
30 | 30 | base = fl.ancestor(my, other) |
|
31 | 31 | a = repo.wjoin(fn) |
|
32 | 32 | b = temp("base", base) |
|
33 | 33 | c = temp("other", other) |
|
34 | 34 | |
|
35 | 35 | repo.ui.note(_("resolving %s\n") % fn) |
|
36 | 36 | repo.ui.debug(_("file %s: my %s other %s ancestor %s\n") % |
|
37 | 37 | (fn, short(my), short(other), short(base))) |
|
38 | 38 | |
|
39 | 39 | cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge") |
|
40 | 40 | or "hgmerge") |
|
41 | 41 | r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root, |
|
42 | 42 | environ={'HG_FILE': fn, |
|
43 | 43 | 'HG_MY_NODE': p1, |
|
44 | 44 | 'HG_OTHER_NODE': p2, |
|
45 | 45 | 'HG_FILE_MY_NODE': hex(my), |
|
46 | 46 | 'HG_FILE_OTHER_NODE': hex(other), |
|
47 | 47 | 'HG_FILE_BASE_NODE': hex(base)}) |
|
48 | 48 | if r: |
|
49 | 49 | repo.ui.warn(_("merging %s failed!\n") % fn) |
|
50 | 50 | |
|
51 | 51 | os.unlink(b) |
|
52 | 52 | os.unlink(c) |
|
53 | 53 | return r |
|
54 | 54 | |
|
55 | 55 | def update(repo, node, branchmerge=False, force=False, partial=None, |
|
56 | 56 | wlock=None, show_stats=True, remind=True): |
|
57 | 57 | |
|
58 | 58 | overwrite = force and not branchmerge |
|
59 | 59 | forcemerge = force and branchmerge |
|
60 | 60 | |
|
61 | 61 | if not wlock: |
|
62 | 62 | wlock = repo.wlock() |
|
63 | 63 | |
|
64 | 64 | ### check phase |
|
65 | 65 | |
|
66 | 66 | pl = repo.dirstate.parents() |
|
67 | 67 | if not overwrite and pl[1] != nullid: |
|
68 | 68 | raise util.Abort(_("outstanding uncommitted merges")) |
|
69 | 69 | |
|
70 | 70 | p1, p2 = pl[0], node |
|
71 | 71 | pa = repo.changelog.ancestor(p1, p2) |
|
72 | 72 | |
|
73 | 73 | # are we going backwards? |
|
74 | 74 | backwards = (pa == p2) |
|
75 | 75 | |
|
76 | 76 | # is there a linear path from p1 to p2? |
|
77 | 77 | linear_path = (pa == p1 or pa == p2) |
|
78 | 78 | if branchmerge and linear_path: |
|
79 | 79 | raise util.Abort(_("there is nothing to merge, just use " |
|
80 | 80 | "'hg update' or look at 'hg heads'")) |
|
81 | 81 | |
|
82 | 82 | if not linear_path and not (overwrite or branchmerge): |
|
83 | 83 | raise util.Abort(_("update spans branches, use 'hg merge' " |
|
84 | 84 | "or 'hg update -C' to lose changes")) |
|
85 | 85 | |
|
86 | 86 | modified, added, removed, deleted, unknown = repo.status()[:5] |
|
87 | 87 | if branchmerge and not forcemerge: |
|
88 | 88 | if modified or added or removed: |
|
89 | 89 | raise util.Abort(_("outstanding uncommitted changes")) |
|
90 | 90 | |
|
91 | 91 | m1n = repo.changelog.read(p1)[0] |
|
92 | 92 | m2n = repo.changelog.read(p2)[0] |
|
93 | 93 | man = repo.manifest.ancestor(m1n, m2n) |
|
94 | m1 = repo.manifest.read(m1n) | |
|
94 | m1 = repo.manifest.read(m1n).copy() | |
|
95 | 95 | m2 = repo.manifest.read(m2n).copy() |
|
96 | 96 | ma = repo.manifest.read(man) |
|
97 | 97 | |
|
98 | 98 | if not force: |
|
99 | 99 | for f in unknown: |
|
100 | 100 | if f in m2: |
|
101 | 101 | if repo.file(f).cmp(m2[f], repo.wread(f)): |
|
102 | 102 | raise util.Abort(_("'%s' already exists in the working" |
|
103 | 103 | " dir and differs from remote") % f) |
|
104 | 104 | |
|
105 | 105 | # resolve the manifest to determine which files |
|
106 | 106 | # we care about merging |
|
107 | 107 | repo.ui.note(_("resolving manifests\n")) |
|
108 | 108 | repo.ui.debug(_(" overwrite %s branchmerge %s partial %s linear %s\n") % |
|
109 | 109 | (overwrite, branchmerge, bool(partial), linear_path)) |
|
110 | 110 | repo.ui.debug(_(" ancestor %s local %s remote %s\n") % |
|
111 | 111 | (short(man), short(m1n), short(m2n))) |
|
112 | 112 | |
|
113 | 113 | merge = {} |
|
114 | 114 | get = {} |
|
115 | 115 | remove = [] |
|
116 | 116 | forget = [] |
|
117 | 117 | |
|
118 | # construct a working dir manifest | |
|
119 | mw = m1.copy() | |
|
118 | # update m1 from working dir | |
|
120 | 119 | umap = dict.fromkeys(unknown) |
|
121 | 120 | |
|
122 | 121 | for f in added + modified + unknown: |
|
123 |
m |
|
|
124 |
m |
|
|
125 | if f in m1: | |
|
126 | mw[f] = m1[f] + "+" | |
|
122 | m1[f] = m1.get(f, nullid) + "+" | |
|
123 | m1.set(f, util.is_exec(repo.wjoin(f), m1.execf(f))) | |
|
127 | 124 | |
|
128 | 125 | for f in deleted + removed: |
|
129 |
if f in m |
|
|
130 |
del m |
|
|
126 | if f in m1: | |
|
127 | del m1[f] | |
|
131 | 128 | |
|
132 | 129 | # If we're jumping between revisions (as opposed to merging), |
|
133 | 130 | # and if neither the working directory nor the target rev has |
|
134 | 131 | # the file, then we need to remove it from the dirstate, to |
|
135 | 132 | # prevent the dirstate from listing the file when it is no |
|
136 | 133 | # longer in the manifest. |
|
137 | 134 | if linear_path and f not in m2: |
|
138 | 135 | forget.append(f) |
|
139 | 136 | |
|
140 | 137 | if partial: |
|
141 |
for f in m |
|
|
142 |
if not partial(f): del m |
|
|
138 | for f in m1.keys(): | |
|
139 | if not partial(f): del m1[f] | |
|
143 | 140 | for f in m2.keys(): |
|
144 | 141 | if not partial(f): del m2[f] |
|
145 | 142 | |
|
146 | 143 | # Compare manifests |
|
147 |
for f, n in m |
|
|
144 | for f, n in m1.iteritems(): | |
|
148 | 145 | if f in m2: |
|
149 | 146 | queued = 0 |
|
150 | 147 | |
|
151 | 148 | # are files different? |
|
152 | 149 | if n != m2[f]: |
|
153 | 150 | a = ma.get(f, nullid) |
|
154 | 151 | # are both different from the ancestor? |
|
155 | 152 | if not overwrite and n != a and m2[f] != a: |
|
156 | 153 | repo.ui.debug(_(" %s versions differ, resolve\n") % f) |
|
157 |
merge[f] = (fmerge(f, m |
|
|
154 | merge[f] = (fmerge(f, m1, m2, ma), n[:20], m2[f]) | |
|
158 | 155 | queued = 1 |
|
159 | 156 | # are we clobbering? |
|
160 | 157 | # is remote's version newer? |
|
161 | 158 | # or are we going back in time and clean? |
|
162 | 159 | elif overwrite or m2[f] != a or (backwards and not n[20:]): |
|
163 | 160 | repo.ui.debug(_(" remote %s is newer, get\n") % f) |
|
164 | 161 | get[f] = (m2.execf(f), m2[f]) |
|
165 | 162 | queued = 1 |
|
166 | 163 | elif f in umap or f in added: |
|
167 | 164 | # this unknown file is the same as the checkout |
|
168 | 165 | # we need to reset the dirstate if the file was added |
|
169 | 166 | get[f] = (m2.execf(f), m2[f]) |
|
170 | 167 | |
|
171 | 168 | # do we still need to look at mode bits? |
|
172 |
if not queued and m |
|
|
169 | if not queued and m1.execf(f) != m2.execf(f): | |
|
173 | 170 | if overwrite: |
|
174 | 171 | repo.ui.debug(_(" updating permissions for %s\n") % f) |
|
175 | 172 | util.set_exec(repo.wjoin(f), m2.execf(f)) |
|
176 | 173 | else: |
|
177 |
if fmerge(f, m |
|
|
174 | if fmerge(f, m1, m2, ma) != m1.execf(f): | |
|
178 | 175 | repo.ui.debug(_(" updating permissions for %s\n") |
|
179 | 176 | % f) |
|
180 | 177 | util.set_exec(repo.wjoin(f), mode) |
|
181 | 178 | del m2[f] |
|
182 | 179 | elif f in ma: |
|
183 | 180 | if n != ma[f]: |
|
184 | 181 | r = _("d") |
|
185 | 182 | if not overwrite: |
|
186 | 183 | r = repo.ui.prompt( |
|
187 | 184 | (_(" local changed %s which remote deleted\n") % f) + |
|
188 | 185 | _("(k)eep or (d)elete?"), _("[kd]"), _("k")) |
|
189 | 186 | if r == _("d"): |
|
190 | 187 | remove.append(f) |
|
191 | 188 | else: |
|
192 | 189 | repo.ui.debug(_("other deleted %s\n") % f) |
|
193 | 190 | remove.append(f) # other deleted it |
|
194 | 191 | else: |
|
195 | 192 | # file is created on branch or in working directory |
|
196 | 193 | if overwrite and f not in umap: |
|
197 | 194 | repo.ui.debug(_("remote deleted %s, clobbering\n") % f) |
|
198 | 195 | remove.append(f) |
|
199 | 196 | elif not n[20:]: # same as parent |
|
200 | 197 | if backwards: |
|
201 | 198 | repo.ui.debug(_("remote deleted %s\n") % f) |
|
202 | 199 | remove.append(f) |
|
203 | 200 | else: |
|
204 | 201 | repo.ui.debug(_("local modified %s, keeping\n") % f) |
|
205 | 202 | else: |
|
206 | 203 | repo.ui.debug(_("working dir created %s, keeping\n") % f) |
|
207 | 204 | |
|
208 | 205 | for f, n in m2.iteritems(): |
|
209 | 206 | if f[0] == "/": |
|
210 | 207 | continue |
|
211 | 208 | if f in ma and n != ma[f]: |
|
212 | 209 | r = _("k") |
|
213 | 210 | if not overwrite: |
|
214 | 211 | r = repo.ui.prompt( |
|
215 | 212 | (_("remote changed %s which local deleted\n") % f) + |
|
216 | 213 | _("(k)eep or (d)elete?"), _("[kd]"), _("k")) |
|
217 | 214 | if r == _("k"): |
|
218 | 215 | get[f] = (m2.execf(f), n) |
|
219 | 216 | elif f not in ma: |
|
220 | 217 | repo.ui.debug(_("remote created %s\n") % f) |
|
221 | 218 | get[f] = (m2.execf(f), n) |
|
222 | 219 | else: |
|
223 | 220 | if overwrite or backwards: |
|
224 | 221 | repo.ui.debug(_("local deleted %s, recreating\n") % f) |
|
225 | 222 | get[f] = (m2.execf(f), n) |
|
226 | 223 | else: |
|
227 | 224 | repo.ui.debug(_("local deleted %s\n") % f) |
|
228 | 225 | |
|
229 |
del |
|
|
226 | del m1, m2, ma | |
|
230 | 227 | |
|
231 | 228 | ### apply phase |
|
232 | 229 | |
|
233 | 230 | if linear_path or overwrite: |
|
234 | 231 | # we don't need to do any magic, just jump to the new rev |
|
235 | 232 | p1, p2 = p2, nullid |
|
236 | 233 | |
|
237 | 234 | xp1 = hex(p1) |
|
238 | 235 | xp2 = hex(p2) |
|
239 | 236 | if p2 == nullid: xxp2 = '' |
|
240 | 237 | else: xxp2 = xp2 |
|
241 | 238 | |
|
242 | 239 | repo.hook('preupdate', throw=True, parent1=xp1, parent2=xxp2) |
|
243 | 240 | |
|
244 | 241 | # get the files we don't need to change |
|
245 | 242 | files = get.keys() |
|
246 | 243 | files.sort() |
|
247 | 244 | for f in files: |
|
248 | 245 | flag, node = get[f] |
|
249 | 246 | if f[0] == "/": |
|
250 | 247 | continue |
|
251 | 248 | repo.ui.note(_("getting %s\n") % f) |
|
252 | 249 | t = repo.file(f).read(node) |
|
253 | 250 | repo.wwrite(f, t) |
|
254 | 251 | util.set_exec(repo.wjoin(f), flag) |
|
255 | 252 | |
|
256 | 253 | # merge the tricky bits |
|
257 | 254 | unresolved = [] |
|
258 | 255 | files = merge.keys() |
|
259 | 256 | files.sort() |
|
260 | 257 | for f in files: |
|
261 | 258 | repo.ui.status(_("merging %s\n") % f) |
|
262 | 259 | flag, my, other = merge[f] |
|
263 | 260 | ret = merge3(repo, f, my, other, xp1, xp2) |
|
264 | 261 | if ret: |
|
265 | 262 | unresolved.append(f) |
|
266 | 263 | util.set_exec(repo.wjoin(f), flag) |
|
267 | 264 | |
|
268 | 265 | remove.sort() |
|
269 | 266 | for f in remove: |
|
270 | 267 | repo.ui.note(_("removing %s\n") % f) |
|
271 | 268 | util.audit_path(f) |
|
272 | 269 | try: |
|
273 | 270 | util.unlink(repo.wjoin(f)) |
|
274 | 271 | except OSError, inst: |
|
275 | 272 | if inst.errno != errno.ENOENT: |
|
276 | 273 | repo.ui.warn(_("update failed to remove %s: %s!\n") % |
|
277 | 274 | (f, inst.strerror)) |
|
278 | 275 | |
|
279 | 276 | # update dirstate |
|
280 | 277 | if not partial: |
|
281 | 278 | repo.dirstate.setparents(p1, p2) |
|
282 | 279 | repo.dirstate.forget(forget) |
|
283 | 280 | if branchmerge: |
|
284 | 281 | repo.dirstate.update(remove, 'r') |
|
285 | 282 | else: |
|
286 | 283 | repo.dirstate.forget(remove) |
|
287 | 284 | |
|
288 | 285 | files = get.keys() |
|
289 | 286 | files.sort() |
|
290 | 287 | for f in files: |
|
291 | 288 | if branchmerge: |
|
292 | 289 | repo.dirstate.update([f], 'n', st_mtime=-1) |
|
293 | 290 | else: |
|
294 | 291 | repo.dirstate.update([f], 'n') |
|
295 | 292 | |
|
296 | 293 | files = merge.keys() |
|
297 | 294 | files.sort() |
|
298 | 295 | for f in files: |
|
299 | 296 | if branchmerge: |
|
300 | 297 | # We've done a branch merge, mark this file as merged |
|
301 | 298 | # so that we properly record the merger later |
|
302 | 299 | repo.dirstate.update([f], 'm') |
|
303 | 300 | else: |
|
304 | 301 | # We've update-merged a locally modified file, so |
|
305 | 302 | # we set the dirstate to emulate a normal checkout |
|
306 | 303 | # of that file some time in the past. Thus our |
|
307 | 304 | # merge will appear as a normal local file |
|
308 | 305 | # modification. |
|
309 | 306 | fl = repo.file(f) |
|
310 | 307 | f_len = fl.size(fl.rev(other)) |
|
311 | 308 | repo.dirstate.update([f], 'n', st_size=f_len, st_mtime=-1) |
|
312 | 309 | |
|
313 | 310 | if show_stats: |
|
314 | 311 | stats = ((len(get), _("updated")), |
|
315 | 312 | (len(merge) - len(unresolved), _("merged")), |
|
316 | 313 | (len(remove), _("removed")), |
|
317 | 314 | (len(unresolved), _("unresolved"))) |
|
318 | 315 | note = ", ".join([_("%d files %s") % s for s in stats]) |
|
319 | 316 | repo.ui.status("%s\n" % note) |
|
320 | 317 | if not partial: |
|
321 | 318 | if branchmerge: |
|
322 | 319 | if unresolved: |
|
323 | 320 | repo.ui.status(_("There are unresolved merges," |
|
324 | 321 | " you can redo the full merge using:\n" |
|
325 | 322 | " hg update -C %s\n" |
|
326 | 323 | " hg merge %s\n" |
|
327 | 324 | % (repo.changelog.rev(p1), |
|
328 | 325 | repo.changelog.rev(p2)))) |
|
329 | 326 | elif remind: |
|
330 | 327 | repo.ui.status(_("(branch merge, don't forget to commit)\n")) |
|
331 | 328 | elif unresolved: |
|
332 | 329 | repo.ui.status(_("There are unresolved merges with" |
|
333 | 330 | " locally modified files.\n")) |
|
334 | 331 | |
|
335 | 332 | repo.hook('update', parent1=xp1, parent2=xxp2, error=len(unresolved)) |
|
336 | 333 | return len(unresolved) |
|
337 | 334 |
General Comments 0
You need to be logged in to leave comments.
Login now