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