##// END OF EJS Templates
allow update to switch to a different branch if the repo is clean
Benoit Boissinot -
r3580:41989e55 default
parent child Browse files
Show More
@@ -1,418 +1,422 b''
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(), "errno util os tempfile")
11 demandload(globals(), "errno util os tempfile")
12
12
13 def filemerge(repo, fw, fo, wctx, mctx):
13 def filemerge(repo, fw, fo, wctx, mctx):
14 """perform a 3-way merge in the working directory
14 """perform a 3-way merge in the working directory
15
15
16 fw = filename in the working directory
16 fw = filename in the working directory
17 fo = filename in other parent
17 fo = filename in other parent
18 wctx, mctx = working and merge changecontexts
18 wctx, mctx = working and merge changecontexts
19 """
19 """
20
20
21 def temp(prefix, ctx):
21 def temp(prefix, ctx):
22 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
22 pre = "%s~%s." % (os.path.basename(ctx.path()), 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(ctx.path(), ctx.data(), f)
25 repo.wwrite(ctx.path(), ctx.data(), f)
26 f.close()
26 f.close()
27 return name
27 return name
28
28
29 fcm = wctx.filectx(fw)
29 fcm = wctx.filectx(fw)
30 fco = mctx.filectx(fo)
30 fco = mctx.filectx(fo)
31
31
32 if not fco.cmp(fcm.data()): # files identical?
32 if not fco.cmp(fcm.data()): # files identical?
33 return None
33 return None
34
34
35 fca = fcm.ancestor(fco)
35 fca = fcm.ancestor(fco)
36 if not fca:
36 if not fca:
37 fca = repo.filectx(fw, fileid=nullrev)
37 fca = repo.filectx(fw, fileid=nullrev)
38 a = repo.wjoin(fw)
38 a = repo.wjoin(fw)
39 b = temp("base", fca)
39 b = temp("base", fca)
40 c = temp("other", fco)
40 c = temp("other", fco)
41
41
42 if fw != fo:
42 if fw != fo:
43 repo.ui.status(_("merging %s and %s\n") % (fw, fo))
43 repo.ui.status(_("merging %s and %s\n") % (fw, fo))
44 else:
44 else:
45 repo.ui.status(_("merging %s\n") % fw)
45 repo.ui.status(_("merging %s\n") % fw)
46
46
47 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
47 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
48
48
49 cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge")
49 cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge")
50 or "hgmerge")
50 or "hgmerge")
51 r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root,
51 r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root,
52 environ={'HG_FILE': fw,
52 environ={'HG_FILE': fw,
53 'HG_MY_NODE': str(wctx.parents()[0]),
53 'HG_MY_NODE': str(wctx.parents()[0]),
54 'HG_OTHER_NODE': str(mctx)})
54 'HG_OTHER_NODE': str(mctx)})
55 if r:
55 if r:
56 repo.ui.warn(_("merging %s failed!\n") % fw)
56 repo.ui.warn(_("merging %s failed!\n") % fw)
57
57
58 os.unlink(b)
58 os.unlink(b)
59 os.unlink(c)
59 os.unlink(c)
60 return r
60 return r
61
61
62 def checkunknown(wctx, mctx):
62 def checkunknown(wctx, mctx):
63 "check for collisions between unknown files and files in mctx"
63 "check for collisions between unknown files and files in mctx"
64 man = mctx.manifest()
64 man = mctx.manifest()
65 for f in wctx.unknown():
65 for f in wctx.unknown():
66 if f in man:
66 if f in man:
67 if mctx.filectx(f).cmp(wctx.filectx(f).data()):
67 if mctx.filectx(f).cmp(wctx.filectx(f).data()):
68 raise util.Abort(_("'%s' already exists in the working"
68 raise util.Abort(_("'%s' already exists in the working"
69 " dir and differs from remote") % f)
69 " dir and differs from remote") % f)
70
70
71 def forgetremoved(wctx, mctx):
71 def forgetremoved(wctx, mctx):
72 """
72 """
73 Forget removed files
73 Forget removed files
74
74
75 If we're jumping between revisions (as opposed to merging), and if
75 If we're jumping between revisions (as opposed to merging), and if
76 neither the working directory nor the target rev has the file,
76 neither the working directory nor the target rev has the file,
77 then we need to remove it from the dirstate, to prevent the
77 then we need to remove it from the dirstate, to prevent the
78 dirstate from listing the file when it is no longer in the
78 dirstate from listing the file when it is no longer in the
79 manifest.
79 manifest.
80 """
80 """
81
81
82 action = []
82 action = []
83 man = mctx.manifest()
83 man = mctx.manifest()
84 for f in wctx.deleted() + wctx.removed():
84 for f in wctx.deleted() + wctx.removed():
85 if f not in man:
85 if f not in man:
86 action.append((f, "f"))
86 action.append((f, "f"))
87
87
88 return action
88 return action
89
89
90 def nonoverlap(d1, d2, d3):
90 def nonoverlap(d1, d2, d3):
91 "Return list of elements in d1 not in d2 or d3"
91 "Return list of elements in d1 not in d2 or d3"
92
92
93 l = []
93 l = []
94 for d in d1:
94 for d in d1:
95 if d not in d3 and d not in d2:
95 if d not in d3 and d not in d2:
96 l.append(d)
96 l.append(d)
97
97
98 l.sort()
98 l.sort()
99 return l
99 return l
100
100
101 def findold(fctx, limit):
101 def findold(fctx, limit):
102 "find files that path was copied from, back to linkrev limit"
102 "find files that path was copied from, back to linkrev limit"
103
103
104 old = {}
104 old = {}
105 orig = fctx.path()
105 orig = fctx.path()
106 visit = [fctx]
106 visit = [fctx]
107 while visit:
107 while visit:
108 fc = visit.pop()
108 fc = visit.pop()
109 if fc.rev() < limit:
109 if fc.rev() < limit:
110 continue
110 continue
111 if fc.path() != orig and fc.path() not in old:
111 if fc.path() != orig and fc.path() not in old:
112 old[fc.path()] = 1
112 old[fc.path()] = 1
113 visit += fc.parents()
113 visit += fc.parents()
114
114
115 old = old.keys()
115 old = old.keys()
116 old.sort()
116 old.sort()
117 return old
117 return old
118
118
119 def findcopies(repo, m1, m2, ma, limit):
119 def findcopies(repo, m1, m2, ma, limit):
120 """
120 """
121 Find moves and copies between m1 and m2 back to limit linkrev
121 Find moves and copies between m1 and m2 back to limit linkrev
122 """
122 """
123
123
124 if not repo.ui.configbool("merge", "followcopies", True):
124 if not repo.ui.configbool("merge", "followcopies", True):
125 return {}
125 return {}
126
126
127 # avoid silly behavior for update from empty dir
127 # avoid silly behavior for update from empty dir
128 if not m1:
128 if not m1:
129 return {}
129 return {}
130
130
131 dcopies = repo.dirstate.copies()
131 dcopies = repo.dirstate.copies()
132 copy = {}
132 copy = {}
133 match = {}
133 match = {}
134 u1 = nonoverlap(m1, m2, ma)
134 u1 = nonoverlap(m1, m2, ma)
135 u2 = nonoverlap(m2, m1, ma)
135 u2 = nonoverlap(m2, m1, ma)
136 ctx = util.cachefunc(lambda f,n: repo.filectx(f, fileid=n[:20]))
136 ctx = util.cachefunc(lambda f,n: repo.filectx(f, fileid=n[:20]))
137
137
138 def checkpair(c, f2, man):
138 def checkpair(c, f2, man):
139 ''' check if an apparent pair actually matches '''
139 ''' check if an apparent pair actually matches '''
140 c2 = ctx(f2, man[f2])
140 c2 = ctx(f2, man[f2])
141 ca = c.ancestor(c2)
141 ca = c.ancestor(c2)
142 if ca and ca.path() == c.path() or ca.path() == c2.path():
142 if ca and ca.path() == c.path() or ca.path() == c2.path():
143 copy[c.path()] = f2
143 copy[c.path()] = f2
144 copy[f2] = c.path()
144 copy[f2] = c.path()
145
145
146 for f in u1:
146 for f in u1:
147 c = ctx(dcopies.get(f, f), m1[f])
147 c = ctx(dcopies.get(f, f), m1[f])
148 for of in findold(c, limit):
148 for of in findold(c, limit):
149 if of in m2:
149 if of in m2:
150 checkpair(c, of, m2)
150 checkpair(c, of, m2)
151 else:
151 else:
152 match.setdefault(of, []).append(f)
152 match.setdefault(of, []).append(f)
153
153
154 for f in u2:
154 for f in u2:
155 c = ctx(f, m2[f])
155 c = ctx(f, m2[f])
156 for of in findold(c, limit):
156 for of in findold(c, limit):
157 if of in m1:
157 if of in m1:
158 checkpair(c, of, m1)
158 checkpair(c, of, m1)
159 elif of in match:
159 elif of in match:
160 for mf in match[of]:
160 for mf in match[of]:
161 checkpair(c, mf, m1)
161 checkpair(c, mf, m1)
162
162
163 return copy
163 return copy
164
164
165 def manifestmerge(repo, p1, p2, pa, overwrite, partial):
165 def manifestmerge(repo, p1, p2, pa, overwrite, partial):
166 """
166 """
167 Merge p1 and p2 with ancestor ma and generate merge action list
167 Merge p1 and p2 with ancestor ma and generate merge action list
168
168
169 overwrite = whether we clobber working files
169 overwrite = whether we clobber working files
170 partial = function to filter file lists
170 partial = function to filter file lists
171 """
171 """
172
172
173 repo.ui.note(_("resolving manifests\n"))
173 repo.ui.note(_("resolving manifests\n"))
174 repo.ui.debug(_(" overwrite %s partial %s\n") % (overwrite, bool(partial)))
174 repo.ui.debug(_(" overwrite %s partial %s\n") % (overwrite, bool(partial)))
175 repo.ui.debug(_(" ancestor %s local %s remote %s\n") % (pa, p1, p2))
175 repo.ui.debug(_(" ancestor %s local %s remote %s\n") % (pa, p1, p2))
176
176
177 m1 = p1.manifest()
177 m1 = p1.manifest()
178 m2 = p2.manifest()
178 m2 = p2.manifest()
179 ma = pa.manifest()
179 ma = pa.manifest()
180 backwards = (pa == p2)
180 backwards = (pa == p2)
181 action = []
181 action = []
182 copy = {}
182 copy = {}
183
183
184 def fmerge(f, f2=None, fa=None):
184 def fmerge(f, f2=None, fa=None):
185 """merge executable flags"""
185 """merge executable flags"""
186 if not f2:
186 if not f2:
187 f2 = f
187 f2 = f
188 fa = f
188 fa = f
189 a, b, c = ma.execf(fa), m1.execf(f), m2.execf(f2)
189 a, b, c = ma.execf(fa), m1.execf(f), m2.execf(f2)
190 return ((a^b) | (a^c)) ^ a
190 return ((a^b) | (a^c)) ^ a
191
191
192 def act(msg, m, f, *args):
192 def act(msg, m, f, *args):
193 repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m))
193 repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m))
194 action.append((f, m) + args)
194 action.append((f, m) + args)
195
195
196 if not (backwards or overwrite):
196 if not (backwards or overwrite):
197 copy = findcopies(repo, m1, m2, ma, pa.rev())
197 copy = findcopies(repo, m1, m2, ma, pa.rev())
198
198
199 # Compare manifests
199 # Compare manifests
200 for f, n in m1.iteritems():
200 for f, n in m1.iteritems():
201 if partial and not partial(f):
201 if partial and not partial(f):
202 continue
202 continue
203 if f in m2:
203 if f in m2:
204 # are files different?
204 # are files different?
205 if n != m2[f]:
205 if n != m2[f]:
206 a = ma.get(f, nullid)
206 a = ma.get(f, nullid)
207 # are both different from the ancestor?
207 # are both different from the ancestor?
208 if not overwrite and n != a and m2[f] != a:
208 if not overwrite and n != a and m2[f] != a:
209 act("versions differ", "m", f, f, f, fmerge(f), False)
209 act("versions differ", "m", f, f, f, fmerge(f), False)
210 # are we clobbering?
210 # are we clobbering?
211 # is remote's version newer?
211 # is remote's version newer?
212 # or are we going back in time and clean?
212 # or are we going back in time and clean?
213 elif overwrite or m2[f] != a or (backwards and not n[20:]):
213 elif overwrite or m2[f] != a or (backwards and not n[20:]):
214 act("remote is newer", "g", f, m2.execf(f))
214 act("remote is newer", "g", f, m2.execf(f))
215 # local is newer, not overwrite, check mode bits
215 # local is newer, not overwrite, check mode bits
216 elif fmerge(f) != m1.execf(f):
216 elif fmerge(f) != m1.execf(f):
217 act("update permissions", "e", f, m2.execf(f))
217 act("update permissions", "e", f, m2.execf(f))
218 # contents same, check mode bits
218 # contents same, check mode bits
219 elif m1.execf(f) != m2.execf(f):
219 elif m1.execf(f) != m2.execf(f):
220 if overwrite or fmerge(f) != m1.execf(f):
220 if overwrite or fmerge(f) != m1.execf(f):
221 act("update permissions", "e", f, m2.execf(f))
221 act("update permissions", "e", f, m2.execf(f))
222 elif f in copy:
222 elif f in copy:
223 f2 = copy[f]
223 f2 = copy[f]
224 if f in ma: # case 3,20 A/B/A
224 if f in ma: # case 3,20 A/B/A
225 act("remote moved", "m", f, f2, f2, fmerge(f, f2, f), True)
225 act("remote moved", "m", f, f2, f2, fmerge(f, f2, f), True)
226 else:
226 else:
227 if f2 in m1: # case 2 A,B/B/B
227 if f2 in m1: # case 2 A,B/B/B
228 act("local copied", "m",
228 act("local copied", "m",
229 f, f2, f, fmerge(f, f2, f2), False)
229 f, f2, f, fmerge(f, f2, f2), False)
230 else: # case 4,21 A/B/B
230 else: # case 4,21 A/B/B
231 act("local moved", "m",
231 act("local moved", "m",
232 f, f2, f, fmerge(f, f2, f2), False)
232 f, f2, f, fmerge(f, f2, f2), False)
233 elif f in ma:
233 elif f in ma:
234 if n != ma[f] and not overwrite:
234 if n != ma[f] and not overwrite:
235 if repo.ui.prompt(
235 if repo.ui.prompt(
236 (_(" local changed %s which remote deleted\n") % f) +
236 (_(" local changed %s which remote deleted\n") % f) +
237 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("d"):
237 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("d"):
238 act("prompt delete", "r", f)
238 act("prompt delete", "r", f)
239 else:
239 else:
240 act("other deleted", "r", f)
240 act("other deleted", "r", f)
241 else:
241 else:
242 # file is created on branch or in working directory
242 # file is created on branch or in working directory
243 if (overwrite and n[20:] != "u") or (backwards and not n[20:]):
243 if (overwrite and n[20:] != "u") or (backwards and not n[20:]):
244 act("remote deleted", "r", f)
244 act("remote deleted", "r", f)
245
245
246 for f, n in m2.iteritems():
246 for f, n in m2.iteritems():
247 if partial and not partial(f):
247 if partial and not partial(f):
248 continue
248 continue
249 if f in m1:
249 if f in m1:
250 continue
250 continue
251 if f in copy:
251 if f in copy:
252 f2 = copy[f]
252 f2 = copy[f]
253 if f2 not in m2: # already seen
253 if f2 not in m2: # already seen
254 continue
254 continue
255 # rename case 1, A/A,B/A
255 # rename case 1, A/A,B/A
256 act("remote copied", "m", f2, f, f, fmerge(f2, f, f2), False)
256 act("remote copied", "m", f2, f, f, fmerge(f2, f, f2), False)
257 elif f in ma:
257 elif f in ma:
258 if overwrite or backwards:
258 if overwrite or backwards:
259 act("recreating", "g", f, m2.execf(f))
259 act("recreating", "g", f, m2.execf(f))
260 elif n != ma[f]:
260 elif n != ma[f]:
261 if repo.ui.prompt(
261 if repo.ui.prompt(
262 (_("remote changed %s which local deleted\n") % f) +
262 (_("remote changed %s which local deleted\n") % f) +
263 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("k"):
263 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("k"):
264 act("prompt recreating", "g", f, m2.execf(f))
264 act("prompt recreating", "g", f, m2.execf(f))
265 else:
265 else:
266 act("remote created", "g", f, m2.execf(f))
266 act("remote created", "g", f, m2.execf(f))
267
267
268 return action
268 return action
269
269
270 def applyupdates(repo, action, wctx, mctx):
270 def applyupdates(repo, action, wctx, mctx):
271 "apply the merge action list to the working directory"
271 "apply the merge action list to the working directory"
272
272
273 updated, merged, removed, unresolved = 0, 0, 0, 0
273 updated, merged, removed, unresolved = 0, 0, 0, 0
274 action.sort()
274 action.sort()
275 for a in action:
275 for a in action:
276 f, m = a[:2]
276 f, m = a[:2]
277 if f[0] == "/":
277 if f[0] == "/":
278 continue
278 continue
279 if m == "r": # remove
279 if m == "r": # remove
280 repo.ui.note(_("removing %s\n") % f)
280 repo.ui.note(_("removing %s\n") % f)
281 util.audit_path(f)
281 util.audit_path(f)
282 try:
282 try:
283 util.unlink(repo.wjoin(f))
283 util.unlink(repo.wjoin(f))
284 except OSError, inst:
284 except OSError, inst:
285 if inst.errno != errno.ENOENT:
285 if inst.errno != errno.ENOENT:
286 repo.ui.warn(_("update failed to remove %s: %s!\n") %
286 repo.ui.warn(_("update failed to remove %s: %s!\n") %
287 (f, inst.strerror))
287 (f, inst.strerror))
288 removed +=1
288 removed +=1
289 elif m == "m": # merge
289 elif m == "m": # merge
290 f2, fd, flag, move = a[2:]
290 f2, fd, flag, move = a[2:]
291 r = filemerge(repo, f, f2, wctx, mctx)
291 r = filemerge(repo, f, f2, wctx, mctx)
292 if r > 0:
292 if r > 0:
293 unresolved += 1
293 unresolved += 1
294 else:
294 else:
295 if r is None:
295 if r is None:
296 updated += 1
296 updated += 1
297 else:
297 else:
298 merged += 1
298 merged += 1
299 if f != fd:
299 if f != fd:
300 repo.ui.debug(_("copying %s to %s\n") % (f, fd))
300 repo.ui.debug(_("copying %s to %s\n") % (f, fd))
301 repo.wwrite(fd, repo.wread(f))
301 repo.wwrite(fd, repo.wread(f))
302 if move:
302 if move:
303 repo.ui.debug(_("removing %s\n") % f)
303 repo.ui.debug(_("removing %s\n") % f)
304 os.unlink(repo.wjoin(f))
304 os.unlink(repo.wjoin(f))
305 util.set_exec(repo.wjoin(fd), flag)
305 util.set_exec(repo.wjoin(fd), flag)
306 elif m == "g": # get
306 elif m == "g": # get
307 flag = a[2]
307 flag = a[2]
308 repo.ui.note(_("getting %s\n") % f)
308 repo.ui.note(_("getting %s\n") % f)
309 t = mctx.filectx(f).data()
309 t = mctx.filectx(f).data()
310 repo.wwrite(f, t)
310 repo.wwrite(f, t)
311 util.set_exec(repo.wjoin(f), flag)
311 util.set_exec(repo.wjoin(f), flag)
312 updated += 1
312 updated += 1
313 elif m == "e": # exec
313 elif m == "e": # exec
314 flag = a[2]
314 flag = a[2]
315 util.set_exec(repo.wjoin(f), flag)
315 util.set_exec(repo.wjoin(f), flag)
316
316
317 return updated, merged, removed, unresolved
317 return updated, merged, removed, unresolved
318
318
319 def recordupdates(repo, action, branchmerge):
319 def recordupdates(repo, action, branchmerge):
320 "record merge actions to the dirstate"
320 "record merge actions to the dirstate"
321
321
322 for a in action:
322 for a in action:
323 f, m = a[:2]
323 f, m = a[:2]
324 if m == "r": # remove
324 if m == "r": # remove
325 if branchmerge:
325 if branchmerge:
326 repo.dirstate.update([f], 'r')
326 repo.dirstate.update([f], 'r')
327 else:
327 else:
328 repo.dirstate.forget([f])
328 repo.dirstate.forget([f])
329 elif m == "f": # forget
329 elif m == "f": # forget
330 repo.dirstate.forget([f])
330 repo.dirstate.forget([f])
331 elif m == "g": # get
331 elif m == "g": # get
332 if branchmerge:
332 if branchmerge:
333 repo.dirstate.update([f], 'n', st_mtime=-1)
333 repo.dirstate.update([f], 'n', st_mtime=-1)
334 else:
334 else:
335 repo.dirstate.update([f], 'n')
335 repo.dirstate.update([f], 'n')
336 elif m == "m": # merge
336 elif m == "m": # merge
337 f2, fd, flag, move = a[2:]
337 f2, fd, flag, move = a[2:]
338 if branchmerge:
338 if branchmerge:
339 # We've done a branch merge, mark this file as merged
339 # We've done a branch merge, mark this file as merged
340 # so that we properly record the merger later
340 # so that we properly record the merger later
341 repo.dirstate.update([fd], 'm')
341 repo.dirstate.update([fd], 'm')
342 if f != f2: # copy/rename
342 if f != f2: # copy/rename
343 if move:
343 if move:
344 repo.dirstate.update([f], 'r')
344 repo.dirstate.update([f], 'r')
345 if f != fd:
345 if f != fd:
346 repo.dirstate.copy(f, fd)
346 repo.dirstate.copy(f, fd)
347 else:
347 else:
348 repo.dirstate.copy(f2, fd)
348 repo.dirstate.copy(f2, fd)
349 else:
349 else:
350 # We've update-merged a locally modified file, so
350 # We've update-merged a locally modified file, so
351 # we set the dirstate to emulate a normal checkout
351 # we set the dirstate to emulate a normal checkout
352 # of that file some time in the past. Thus our
352 # of that file some time in the past. Thus our
353 # merge will appear as a normal local file
353 # merge will appear as a normal local file
354 # modification.
354 # modification.
355 repo.dirstate.update([fd], 'n', st_size=-1, st_mtime=-1)
355 repo.dirstate.update([fd], 'n', st_size=-1, st_mtime=-1)
356 if move:
356 if move:
357 repo.dirstate.forget([f])
357 repo.dirstate.forget([f])
358
358
359 def update(repo, node, branchmerge, force, partial, wlock):
359 def update(repo, node, branchmerge, force, partial, wlock):
360 """
360 """
361 Perform a merge between the working directory and the given node
361 Perform a merge between the working directory and the given node
362
362
363 branchmerge = whether to merge between branches
363 branchmerge = whether to merge between branches
364 force = whether to force branch merging or file overwriting
364 force = whether to force branch merging or file overwriting
365 partial = a function to filter file lists (dirstate not updated)
365 partial = a function to filter file lists (dirstate not updated)
366 wlock = working dir lock, if already held
366 wlock = working dir lock, if already held
367 """
367 """
368
368
369 if not wlock:
369 if not wlock:
370 wlock = repo.wlock()
370 wlock = repo.wlock()
371
371
372 overwrite = force and not branchmerge
372 overwrite = force and not branchmerge
373 forcemerge = force and branchmerge
373 forcemerge = force and branchmerge
374 wc = repo.workingctx()
374 wc = repo.workingctx()
375 pl = wc.parents()
375 pl = wc.parents()
376 p1, p2 = pl[0], repo.changectx(node)
376 p1, p2 = pl[0], repo.changectx(node)
377 pa = p1.ancestor(p2)
377 pa = p1.ancestor(p2)
378 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
378 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
379
379
380 ### check phase
380 ### check phase
381 if not overwrite and len(pl) > 1:
381 if not overwrite and len(pl) > 1:
382 raise util.Abort(_("outstanding uncommitted merges"))
382 raise util.Abort(_("outstanding uncommitted merges"))
383 if pa == p1 or pa == p2: # is there a linear path from p1 to p2?
383 if pa == p1 or pa == p2: # is there a linear path from p1 to p2?
384 if branchmerge:
384 if branchmerge:
385 raise util.Abort(_("there is nothing to merge, just use "
385 raise util.Abort(_("there is nothing to merge, just use "
386 "'hg update' or look at 'hg heads'"))
386 "'hg update' or look at 'hg heads'"))
387 elif not (overwrite or branchmerge):
387 elif not branchmerge:
388 raise util.Abort(_("update spans branches, use 'hg merge' "
388 if not overwrite:
389 "or 'hg update -C' to lose changes"))
389 if wc.files():
390 raise util.Abort(_("outstanding uncommited changes, use "
391 "'hg update -C' to lose changes"))
392 else:
393 overwrite = True
390 if branchmerge and not forcemerge:
394 if branchmerge and not forcemerge:
391 if wc.modified() or wc.added() or wc.removed():
395 if wc.modified() or wc.added() or wc.removed():
392 raise util.Abort(_("outstanding uncommitted changes"))
396 raise util.Abort(_("outstanding uncommitted changes"))
393
397
394 ### calculate phase
398 ### calculate phase
395 action = []
399 action = []
396 if not force:
400 if not force:
397 checkunknown(wc, p2)
401 checkunknown(wc, p2)
398 if not branchmerge:
402 if not branchmerge:
399 action += forgetremoved(wc, p2)
403 action += forgetremoved(wc, p2)
400 action += manifestmerge(repo, wc, p2, pa, overwrite, partial)
404 action += manifestmerge(repo, wc, p2, pa, overwrite, partial)
401
405
402 ### apply phase
406 ### apply phase
403 if not branchmerge: # just jump to the new rev
407 if not branchmerge: # just jump to the new rev
404 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
408 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
405 if not partial:
409 if not partial:
406 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
410 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
407
411
408 stats = applyupdates(repo, action, wc, p2)
412 stats = applyupdates(repo, action, wc, p2)
409
413
410 if not partial:
414 if not partial:
411 recordupdates(repo, action, branchmerge)
415 recordupdates(repo, action, branchmerge)
412 repo.dirstate.setparents(fp1, fp2)
416 repo.dirstate.setparents(fp1, fp2)
413 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
417 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
414 if not branchmerge:
418 if not branchmerge:
415 repo.opener("branch", "w").write(p2.branch() + "\n")
419 repo.opener("branch", "w").write(p2.branch() + "\n")
416
420
417 return stats
421 return stats
418
422
@@ -1,20 +1,24 b''
1 #!/bin/sh
1 #!/bin/sh
2
2
3 mkdir t
3 mkdir t
4 cd t
4 cd t
5 hg init
5 hg init
6 echo This is file a1 > a
6 echo This is file a1 > a
7 echo This is file b1 > b
7 echo This is file b1 > b
8 hg add a b
8 hg add a b
9 hg commit -m "commit #0" -d "1000000 0"
9 hg commit -m "commit #0" -d "1000000 0"
10 echo This is file b22 > b
10 echo This is file b22 > b
11 hg commit -m"comment #1" -d "1000000 0"
11 hg commit -m"comment #1" -d "1000000 0"
12 hg update 0
12 hg update 0
13 rm b
13 rm b
14 hg commit -A -m"comment #2" -d "1000000 0"
14 hg commit -A -m"comment #2" -d "1000000 0"
15
16 echo This is file a > a
15 # in theory, we shouldn't need the "-y" below, but it prevents
17 # in theory, we shouldn't need the "-y" below, but it prevents
16 # this test from hanging when "hg update" erroneously prompts the
18 # this test from hanging when "hg update" erroneously prompts the
17 # user for "keep or delete"
19 # user for "keep or delete"
20 hg update -y 1 # should fail (unclean repo)
21 hg revert a
18 hg update -y 1
22 hg update -y 1
19
23
20 exit 0
24 exit 0
@@ -1,3 +1,4 b''
1 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
1 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
2 removing b
2 removing b
3 abort: update spans branches, use 'hg merge' or 'hg update -C' to lose changes
3 abort: outstanding uncommited changes, use 'hg update -C' to lose changes
4 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
@@ -1,136 +1,136 b''
1 adding a
1 adding a
2 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
2 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
3 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
3 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
4 diff -r 33aaa84a386b a
4 diff -r 33aaa84a386b a
5 --- a/a
5 --- a/a
6 +++ b/a
6 +++ b/a
7 @@ -1,1 +1,1 @@ a
7 @@ -1,1 +1,1 @@ a
8 -a
8 -a
9 +abc
9 +abc
10 adding b
10 adding b
11 M a
11 M a
12 changeset: 0:33aaa84a386b
12 changeset: 0:33aaa84a386b
13 user: test
13 user: test
14 date: Mon Jan 12 13:46:40 1970 +0000
14 date: Mon Jan 12 13:46:40 1970 +0000
15 summary: 1
15 summary: 1
16
16
17 resolving manifests
17 resolving manifests
18 overwrite False partial False
18 overwrite False partial False
19 ancestor 33aaa84a386b local 33aaa84a386b+ remote 802f095af299
19 ancestor 33aaa84a386b local 33aaa84a386b+ remote 802f095af299
20 a: versions differ -> m
20 a: versions differ -> m
21 b: remote created -> g
21 b: remote created -> g
22 merging a
22 merging a
23 my a@33aaa84a386b+ other a@802f095af299 ancestor a@33aaa84a386b
23 my a@33aaa84a386b+ other a@802f095af299 ancestor a@33aaa84a386b
24 getting b
24 getting b
25 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
25 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
26 changeset: 1:802f095af299
26 changeset: 1:802f095af299
27 tag: tip
27 tag: tip
28 user: test
28 user: test
29 date: Mon Jan 12 13:46:40 1970 +0000
29 date: Mon Jan 12 13:46:40 1970 +0000
30 summary: 2
30 summary: 2
31
31
32 resolving manifests
32 resolving manifests
33 overwrite False partial False
33 overwrite False partial False
34 ancestor 33aaa84a386b local 802f095af299+ remote 33aaa84a386b
34 ancestor 33aaa84a386b local 802f095af299+ remote 33aaa84a386b
35 b: remote deleted -> r
35 b: remote deleted -> r
36 removing b
36 removing b
37 0 files updated, 0 files merged, 1 files removed, 0 files unresolved
37 0 files updated, 0 files merged, 1 files removed, 0 files unresolved
38 changeset: 0:33aaa84a386b
38 changeset: 0:33aaa84a386b
39 user: test
39 user: test
40 date: Mon Jan 12 13:46:40 1970 +0000
40 date: Mon Jan 12 13:46:40 1970 +0000
41 summary: 1
41 summary: 1
42
42
43 abort: there is nothing to merge - use "hg update" instead
43 abort: there is nothing to merge - use "hg update" instead
44 failed
44 failed
45 changeset: 0:33aaa84a386b
45 changeset: 0:33aaa84a386b
46 user: test
46 user: test
47 date: Mon Jan 12 13:46:40 1970 +0000
47 date: Mon Jan 12 13:46:40 1970 +0000
48 summary: 1
48 summary: 1
49
49
50 resolving manifests
50 resolving manifests
51 overwrite False partial False
51 overwrite False partial False
52 ancestor 33aaa84a386b local 33aaa84a386b+ remote 802f095af299
52 ancestor 33aaa84a386b local 33aaa84a386b+ remote 802f095af299
53 a: versions differ -> m
53 a: versions differ -> m
54 b: remote created -> g
54 b: remote created -> g
55 merging a
55 merging a
56 my a@33aaa84a386b+ other a@802f095af299 ancestor a@33aaa84a386b
56 my a@33aaa84a386b+ other a@802f095af299 ancestor a@33aaa84a386b
57 getting b
57 getting b
58 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
58 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
59 changeset: 1:802f095af299
59 changeset: 1:802f095af299
60 tag: tip
60 tag: tip
61 user: test
61 user: test
62 date: Mon Jan 12 13:46:40 1970 +0000
62 date: Mon Jan 12 13:46:40 1970 +0000
63 summary: 2
63 summary: 2
64
64
65 changeset: 1:802f095af299
65 changeset: 1:802f095af299
66 tag: tip
66 tag: tip
67 user: test
67 user: test
68 date: Mon Jan 12 13:46:40 1970 +0000
68 date: Mon Jan 12 13:46:40 1970 +0000
69 files: a b
69 files: a b
70 description:
70 description:
71 2
71 2
72
72
73
73
74 changeset: 0:33aaa84a386b
74 changeset: 0:33aaa84a386b
75 user: test
75 user: test
76 date: Mon Jan 12 13:46:40 1970 +0000
76 date: Mon Jan 12 13:46:40 1970 +0000
77 files: a
77 files: a
78 description:
78 description:
79 1
79 1
80
80
81
81
82 diff -r 802f095af299 a
82 diff -r 802f095af299 a
83 --- a/a
83 --- a/a
84 +++ b/a
84 +++ b/a
85 @@ -1,1 +1,1 @@ a2
85 @@ -1,1 +1,1 @@ a2
86 -a2
86 -a2
87 +abc
87 +abc
88 1 files updated, 0 files merged, 1 files removed, 0 files unresolved
88 1 files updated, 0 files merged, 1 files removed, 0 files unresolved
89 adding b
89 adding b
90 M a
90 M a
91 changeset: 1:802f095af299
91 changeset: 1:802f095af299
92 user: test
92 user: test
93 date: Mon Jan 12 13:46:40 1970 +0000
93 date: Mon Jan 12 13:46:40 1970 +0000
94 summary: 2
94 summary: 2
95
95
96 abort: update spans branches, use 'hg merge' or 'hg update -C' to lose changes
96 abort: outstanding uncommited changes, use 'hg update -C' to lose changes
97 failed
97 failed
98 abort: outstanding uncommitted changes
98 abort: outstanding uncommitted changes
99 failed
99 failed
100 resolving manifests
100 resolving manifests
101 overwrite False partial False
101 overwrite False partial False
102 ancestor 33aaa84a386b local 802f095af299+ remote 030602aee63d
102 ancestor 33aaa84a386b local 802f095af299+ remote 030602aee63d
103 a: versions differ -> m
103 a: versions differ -> m
104 b: versions differ -> m
104 b: versions differ -> m
105 merging a
105 merging a
106 my a@802f095af299+ other a@030602aee63d ancestor a@33aaa84a386b
106 my a@802f095af299+ other a@030602aee63d ancestor a@33aaa84a386b
107 merging b
107 merging b
108 my b@802f095af299+ other b@030602aee63d ancestor b@000000000000
108 my b@802f095af299+ other b@030602aee63d ancestor b@000000000000
109 0 files updated, 2 files merged, 0 files removed, 0 files unresolved
109 0 files updated, 2 files merged, 0 files removed, 0 files unresolved
110 (branch merge, don't forget to commit)
110 (branch merge, don't forget to commit)
111 changeset: 1:802f095af299
111 changeset: 1:802f095af299
112 user: test
112 user: test
113 date: Mon Jan 12 13:46:40 1970 +0000
113 date: Mon Jan 12 13:46:40 1970 +0000
114 summary: 2
114 summary: 2
115
115
116 changeset: 2:030602aee63d
116 changeset: 2:030602aee63d
117 tag: tip
117 tag: tip
118 parent: 0:33aaa84a386b
118 parent: 0:33aaa84a386b
119 user: test
119 user: test
120 date: Mon Jan 12 13:46:40 1970 +0000
120 date: Mon Jan 12 13:46:40 1970 +0000
121 summary: 3
121 summary: 3
122
122
123 diff -r 802f095af299 a
123 diff -r 802f095af299 a
124 --- a/a
124 --- a/a
125 +++ b/a
125 +++ b/a
126 @@ -1,1 +1,1 @@ a2
126 @@ -1,1 +1,1 @@ a2
127 -a2
127 -a2
128 +abc
128 +abc
129 adding a
129 adding a
130 pulling from ../a
130 pulling from ../a
131 requesting all changes
131 requesting all changes
132 adding changesets
132 adding changesets
133 adding manifests
133 adding manifests
134 adding file changes
134 adding file changes
135 added 1 changesets with 1 changes to 1 files
135 added 1 changesets with 1 changes to 1 files
136 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
136 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
General Comments 0
You need to be logged in to leave comments. Login now