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