##// END OF EJS Templates
merge: fix stupid indentation left over from previous refactorings
Mads Kiilerich -
r21551:bde505f4 default
parent child Browse files
Show More
@@ -1,1156 +1,1149
1 1 # merge.py - directory-level update/merge handling for Mercurial
2 2 #
3 3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 import struct
9 9
10 10 from node import nullid, nullrev, hex, bin
11 11 from i18n import _
12 12 from mercurial import obsolete
13 13 import error, util, filemerge, copies, subrepo, worker, dicthelpers
14 14 import errno, os, shutil
15 15
16 16 _pack = struct.pack
17 17 _unpack = struct.unpack
18 18
19 19 def _droponode(data):
20 20 # used for compatibility for v1
21 21 bits = data.split("\0")
22 22 bits = bits[:-2] + bits[-1:]
23 23 return "\0".join(bits)
24 24
25 25 class mergestate(object):
26 26 '''track 3-way merge state of individual files
27 27
28 28 it is stored on disk when needed. Two file are used, one with an old
29 29 format, one with a new format. Both contains similar data, but the new
30 30 format can store new kind of field.
31 31
32 32 Current new format is a list of arbitrary record of the form:
33 33
34 34 [type][length][content]
35 35
36 36 Type is a single character, length is a 4 bytes integer, content is an
37 37 arbitrary suites of bytes of length `length`.
38 38
39 39 Type should be a letter. Capital letter are mandatory record, Mercurial
40 40 should abort if they are unknown. lower case record can be safely ignored.
41 41
42 42 Currently known record:
43 43
44 44 L: the node of the "local" part of the merge (hexified version)
45 45 O: the node of the "other" part of the merge (hexified version)
46 46 F: a file to be merged entry
47 47 '''
48 48 statepathv1 = "merge/state"
49 49 statepathv2 = "merge/state2"
50 50
51 51 def __init__(self, repo):
52 52 self._repo = repo
53 53 self._dirty = False
54 54 self._read()
55 55
56 56 def reset(self, node=None, other=None):
57 57 self._state = {}
58 58 self._local = None
59 59 self._other = None
60 60 if node:
61 61 self._local = node
62 62 self._other = other
63 63 shutil.rmtree(self._repo.join("merge"), True)
64 64 self._dirty = False
65 65
66 66 def _read(self):
67 67 """Analyse each record content to restore a serialized state from disk
68 68
69 69 This function process "record" entry produced by the de-serialization
70 70 of on disk file.
71 71 """
72 72 self._state = {}
73 73 self._local = None
74 74 self._other = None
75 75 records = self._readrecords()
76 76 for rtype, record in records:
77 77 if rtype == 'L':
78 78 self._local = bin(record)
79 79 elif rtype == 'O':
80 80 self._other = bin(record)
81 81 elif rtype == "F":
82 82 bits = record.split("\0")
83 83 self._state[bits[0]] = bits[1:]
84 84 elif not rtype.islower():
85 85 raise util.Abort(_('unsupported merge state record: %s')
86 86 % rtype)
87 87 self._dirty = False
88 88
89 89 def _readrecords(self):
90 90 """Read merge state from disk and return a list of record (TYPE, data)
91 91
92 92 We read data from both v1 and v2 files and decide which one to use.
93 93
94 94 V1 has been used by version prior to 2.9.1 and contains less data than
95 95 v2. We read both versions and check if no data in v2 contradicts
96 96 v1. If there is not contradiction we can safely assume that both v1
97 97 and v2 were written at the same time and use the extract data in v2. If
98 98 there is contradiction we ignore v2 content as we assume an old version
99 99 of Mercurial has overwritten the mergestate file and left an old v2
100 100 file around.
101 101
102 102 returns list of record [(TYPE, data), ...]"""
103 103 v1records = self._readrecordsv1()
104 104 v2records = self._readrecordsv2()
105 105 oldv2 = set() # old format version of v2 record
106 106 for rec in v2records:
107 107 if rec[0] == 'L':
108 108 oldv2.add(rec)
109 109 elif rec[0] == 'F':
110 110 # drop the onode data (not contained in v1)
111 111 oldv2.add(('F', _droponode(rec[1])))
112 112 for rec in v1records:
113 113 if rec not in oldv2:
114 114 # v1 file is newer than v2 file, use it
115 115 # we have to infer the "other" changeset of the merge
116 116 # we cannot do better than that with v1 of the format
117 117 mctx = self._repo[None].parents()[-1]
118 118 v1records.append(('O', mctx.hex()))
119 119 # add place holder "other" file node information
120 120 # nobody is using it yet so we do no need to fetch the data
121 121 # if mctx was wrong `mctx[bits[-2]]` may fails.
122 122 for idx, r in enumerate(v1records):
123 123 if r[0] == 'F':
124 124 bits = r[1].split("\0")
125 125 bits.insert(-2, '')
126 126 v1records[idx] = (r[0], "\0".join(bits))
127 127 return v1records
128 128 else:
129 129 return v2records
130 130
131 131 def _readrecordsv1(self):
132 132 """read on disk merge state for version 1 file
133 133
134 134 returns list of record [(TYPE, data), ...]
135 135
136 136 Note: the "F" data from this file are one entry short
137 137 (no "other file node" entry)
138 138 """
139 139 records = []
140 140 try:
141 141 f = self._repo.opener(self.statepathv1)
142 142 for i, l in enumerate(f):
143 143 if i == 0:
144 144 records.append(('L', l[:-1]))
145 145 else:
146 146 records.append(('F', l[:-1]))
147 147 f.close()
148 148 except IOError, err:
149 149 if err.errno != errno.ENOENT:
150 150 raise
151 151 return records
152 152
153 153 def _readrecordsv2(self):
154 154 """read on disk merge state for version 2 file
155 155
156 156 returns list of record [(TYPE, data), ...]
157 157 """
158 158 records = []
159 159 try:
160 160 f = self._repo.opener(self.statepathv2)
161 161 data = f.read()
162 162 off = 0
163 163 end = len(data)
164 164 while off < end:
165 165 rtype = data[off]
166 166 off += 1
167 167 length = _unpack('>I', data[off:(off + 4)])[0]
168 168 off += 4
169 169 record = data[off:(off + length)]
170 170 off += length
171 171 records.append((rtype, record))
172 172 f.close()
173 173 except IOError, err:
174 174 if err.errno != errno.ENOENT:
175 175 raise
176 176 return records
177 177
178 178 def active(self):
179 179 """Whether mergestate is active.
180 180
181 181 Returns True if there appears to be mergestate. This is a rough proxy
182 182 for "is a merge in progress."
183 183 """
184 184 # Check local variables before looking at filesystem for performance
185 185 # reasons.
186 186 return bool(self._local) or bool(self._state) or \
187 187 self._repo.opener.exists(self.statepathv1) or \
188 188 self._repo.opener.exists(self.statepathv2)
189 189
190 190 def commit(self):
191 191 """Write current state on disk (if necessary)"""
192 192 if self._dirty:
193 193 records = []
194 194 records.append(("L", hex(self._local)))
195 195 records.append(("O", hex(self._other)))
196 196 for d, v in self._state.iteritems():
197 197 records.append(("F", "\0".join([d] + v)))
198 198 self._writerecords(records)
199 199 self._dirty = False
200 200
201 201 def _writerecords(self, records):
202 202 """Write current state on disk (both v1 and v2)"""
203 203 self._writerecordsv1(records)
204 204 self._writerecordsv2(records)
205 205
206 206 def _writerecordsv1(self, records):
207 207 """Write current state on disk in a version 1 file"""
208 208 f = self._repo.opener(self.statepathv1, "w")
209 209 irecords = iter(records)
210 210 lrecords = irecords.next()
211 211 assert lrecords[0] == 'L'
212 212 f.write(hex(self._local) + "\n")
213 213 for rtype, data in irecords:
214 214 if rtype == "F":
215 215 f.write("%s\n" % _droponode(data))
216 216 f.close()
217 217
218 218 def _writerecordsv2(self, records):
219 219 """Write current state on disk in a version 2 file"""
220 220 f = self._repo.opener(self.statepathv2, "w")
221 221 for key, data in records:
222 222 assert len(key) == 1
223 223 format = ">sI%is" % len(data)
224 224 f.write(_pack(format, key, len(data), data))
225 225 f.close()
226 226
227 227 def add(self, fcl, fco, fca, fd):
228 228 """add a new (potentially?) conflicting file the merge state
229 229 fcl: file context for local,
230 230 fco: file context for remote,
231 231 fca: file context for ancestors,
232 232 fd: file path of the resulting merge.
233 233
234 234 note: also write the local version to the `.hg/merge` directory.
235 235 """
236 236 hash = util.sha1(fcl.path()).hexdigest()
237 237 self._repo.opener.write("merge/" + hash, fcl.data())
238 238 self._state[fd] = ['u', hash, fcl.path(),
239 239 fca.path(), hex(fca.filenode()),
240 240 fco.path(), hex(fco.filenode()),
241 241 fcl.flags()]
242 242 self._dirty = True
243 243
244 244 def __contains__(self, dfile):
245 245 return dfile in self._state
246 246
247 247 def __getitem__(self, dfile):
248 248 return self._state[dfile][0]
249 249
250 250 def __iter__(self):
251 251 return iter(sorted(self._state))
252 252
253 253 def files(self):
254 254 return self._state.keys()
255 255
256 256 def mark(self, dfile, state):
257 257 self._state[dfile][0] = state
258 258 self._dirty = True
259 259
260 260 def unresolved(self):
261 261 """Obtain the paths of unresolved files."""
262 262
263 263 for f, entry in self._state.items():
264 264 if entry[0] == 'u':
265 265 yield f
266 266
267 267 def resolve(self, dfile, wctx, labels=None):
268 268 """rerun merge process for file path `dfile`"""
269 269 if self[dfile] == 'r':
270 270 return 0
271 271 stateentry = self._state[dfile]
272 272 state, hash, lfile, afile, anode, ofile, onode, flags = stateentry
273 273 octx = self._repo[self._other]
274 274 fcd = wctx[dfile]
275 275 fco = octx[ofile]
276 276 fca = self._repo.filectx(afile, fileid=anode)
277 277 # "premerge" x flags
278 278 flo = fco.flags()
279 279 fla = fca.flags()
280 280 if 'x' in flags + flo + fla and 'l' not in flags + flo + fla:
281 281 if fca.node() == nullid:
282 282 self._repo.ui.warn(_('warning: cannot merge flags for %s\n') %
283 283 afile)
284 284 elif flags == fla:
285 285 flags = flo
286 286 # restore local
287 287 f = self._repo.opener("merge/" + hash)
288 288 self._repo.wwrite(dfile, f.read(), flags)
289 289 f.close()
290 290 r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca,
291 291 labels=labels)
292 292 if r is None:
293 293 # no real conflict
294 294 del self._state[dfile]
295 295 self._dirty = True
296 296 elif not r:
297 297 self.mark(dfile, 'r')
298 298 return r
299 299
300 300 def _checkunknownfile(repo, wctx, mctx, f):
301 301 return (not repo.dirstate._ignore(f)
302 302 and os.path.isfile(repo.wjoin(f))
303 303 and repo.wopener.audit.check(f)
304 304 and repo.dirstate.normalize(f) not in repo.dirstate
305 305 and mctx[f].cmp(wctx[f]))
306 306
307 307 def _checkunknown(repo, wctx, mctx):
308 308 "check for collisions between unknown files and files in mctx"
309 309
310 310 error = False
311 311 for f in mctx:
312 312 if f not in wctx and _checkunknownfile(repo, wctx, mctx, f):
313 313 error = True
314 314 wctx._repo.ui.warn(_("%s: untracked file differs\n") % f)
315 315 if error:
316 316 raise util.Abort(_("untracked files in working directory differ "
317 317 "from files in requested revision"))
318 318
319 319 def _forgetremoved(wctx, mctx, branchmerge):
320 320 """
321 321 Forget removed files
322 322
323 323 If we're jumping between revisions (as opposed to merging), and if
324 324 neither the working directory nor the target rev has the file,
325 325 then we need to remove it from the dirstate, to prevent the
326 326 dirstate from listing the file when it is no longer in the
327 327 manifest.
328 328
329 329 If we're merging, and the other revision has removed a file
330 330 that is not present in the working directory, we need to mark it
331 331 as removed.
332 332 """
333 333
334 334 ractions = []
335 335 factions = xactions = []
336 336 if branchmerge:
337 337 xactions = ractions
338 338 for f in wctx.deleted():
339 339 if f not in mctx:
340 340 xactions.append((f, None, "forget deleted"))
341 341
342 342 if not branchmerge:
343 343 for f in wctx.removed():
344 344 if f not in mctx:
345 345 factions.append((f, None, "forget removed"))
346 346
347 347 return ractions, factions
348 348
349 349 def _checkcollision(repo, wmf, actions):
350 350 # build provisional merged manifest up
351 351 pmmf = set(wmf)
352 352
353 353 if actions:
354 354 # k, dr, e and rd are no-op
355 355 for m in 'a', 'f', 'g', 'cd', 'dc':
356 356 for f, args, msg in actions[m]:
357 357 pmmf.add(f)
358 358 for f, args, msg in actions['r']:
359 359 pmmf.discard(f)
360 360 for f, args, msg in actions['dm']:
361 361 f2, flags = args
362 362 pmmf.discard(f2)
363 363 pmmf.add(f)
364 364 for f, args, msg in actions['dg']:
365 365 f2, flags = args
366 366 pmmf.add(f)
367 367 for f, args, msg in actions['m']:
368 368 f1, f2, fa, move, anc = args
369 369 if move:
370 370 pmmf.discard(f1)
371 371 pmmf.add(f)
372 372
373 373 # check case-folding collision in provisional merged manifest
374 374 foldmap = {}
375 375 for f in sorted(pmmf):
376 376 fold = util.normcase(f)
377 377 if fold in foldmap:
378 378 raise util.Abort(_("case-folding collision between %s and %s")
379 379 % (f, foldmap[fold]))
380 380 foldmap[fold] = f
381 381
382 382 def manifestmerge(repo, wctx, p2, pa, branchmerge, force, partial,
383 383 acceptremote, followcopies):
384 384 """
385 385 Merge p1 and p2 with ancestor pa and generate merge action list
386 386
387 387 branchmerge and force are as passed in to update
388 388 partial = function to filter file lists
389 389 acceptremote = accept the incoming changes without prompting
390 390 """
391 391
392 392 actions = dict((m, []) for m in 'a f g cd dc r dm dg m dr e rd k'.split())
393 393 copy, movewithdir = {}, {}
394 394
395 395 # manifests fetched in order are going to be faster, so prime the caches
396 396 [x.manifest() for x in
397 397 sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
398 398
399 399 if followcopies:
400 400 ret = copies.mergecopies(repo, wctx, p2, pa)
401 401 copy, movewithdir, diverge, renamedelete = ret
402 402 for of, fl in diverge.iteritems():
403 403 actions['dr'].append((of, (fl,), "divergent renames"))
404 404 for of, fl in renamedelete.iteritems():
405 405 actions['rd'].append((of, (fl,), "rename and delete"))
406 406
407 407 repo.ui.note(_("resolving manifests\n"))
408 408 repo.ui.debug(" branchmerge: %s, force: %s, partial: %s\n"
409 409 % (bool(branchmerge), bool(force), bool(partial)))
410 410 repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, wctx, p2))
411 411
412 412 m1, m2, ma = wctx.manifest(), p2.manifest(), pa.manifest()
413 413 copied = set(copy.values())
414 414 copied.update(movewithdir.values())
415 415
416 416 if '.hgsubstate' in m1:
417 417 # check whether sub state is modified
418 418 for s in sorted(wctx.substate):
419 419 if wctx.sub(s).dirty():
420 420 m1['.hgsubstate'] += "+"
421 421 break
422 422
423 423 aborts = []
424 424 # Compare manifests
425 425 fdiff = dicthelpers.diff(m1, m2)
426 426 flagsdiff = m1.flagsdiff(m2)
427 427 diff12 = dicthelpers.join(fdiff, flagsdiff)
428 428
429 429 for f, (n12, fl12) in diff12.iteritems():
430 430 if n12:
431 431 n1, n2 = n12
432 432 else: # file contents didn't change, but flags did
433 433 n1 = n2 = m1.get(f, None)
434 434 if n1 is None:
435 435 # Since n1 == n2, the file isn't present in m2 either. This
436 436 # means that the file was removed or deleted locally and
437 437 # removed remotely, but that residual entries remain in flags.
438 438 # This can happen in manifests generated by workingctx.
439 439 continue
440 440 if fl12:
441 441 fl1, fl2 = fl12
442 442 else: # flags didn't change, file contents did
443 443 fl1 = fl2 = m1.flags(f)
444 444
445 445 if partial and not partial(f):
446 446 continue
447 447 if n1 and n2:
448 448 fa = f
449 449 a = ma.get(f, nullid)
450 450 if a == nullid:
451 451 fa = copy.get(f, f)
452 452 # Note: f as default is wrong - we can't really make a 3-way
453 453 # merge without an ancestor file.
454 454 fla = ma.flags(fa)
455 455 nol = 'l' not in fl1 + fl2 + fla
456 456 if n2 == a and fl2 == fla:
457 457 actions['k'].append((f, (), "keep")) # remote unchanged
458 458 elif n1 == a and fl1 == fla: # local unchanged - use remote
459 459 if n1 == n2: # optimization: keep local content
460 460 actions['e'].append((f, (fl2,), "update permissions"))
461 461 else:
462 462 actions['g'].append((f, (fl2,), "remote is newer"))
463 463 elif nol and n2 == a: # remote only changed 'x'
464 464 actions['e'].append((f, (fl2,), "update permissions"))
465 465 elif nol and n1 == a: # local only changed 'x'
466 466 actions['g'].append((f, (fl1,), "remote is newer"))
467 467 else: # both changed something
468 468 actions['m'].append((f, (f, f, fa, False, pa.node()),
469 469 "versions differ"))
470 470 elif f in copied: # files we'll deal with on m2 side
471 471 pass
472 472 elif n1 and f in movewithdir: # directory rename, move local
473 473 f2 = movewithdir[f]
474 474 actions['dm'].append((f2, (f, fl1),
475 475 "remote directory rename - move from " + f))
476 476 elif n1 and f in copy:
477 477 f2 = copy[f]
478 478 actions['m'].append((f, (f, f2, f2, False, pa.node()),
479 479 "local copied/moved from " + f2))
480 480 elif n1 and f in ma: # clean, a different, no remote
481 481 if n1 != ma[f]:
482 482 if acceptremote:
483 483 actions['r'].append((f, None, "remote delete"))
484 484 else:
485 485 actions['cd'].append((f, None, "prompt changed/deleted"))
486 486 elif n1[20:] == "a": # added, no remote
487 487 actions['f'].append((f, None, "remote deleted"))
488 488 else:
489 489 actions['r'].append((f, None, "other deleted"))
490 490 elif n2 and f in movewithdir:
491 491 f2 = movewithdir[f]
492 492 actions['dg'].append((f2, (f, fl2),
493 493 "local directory rename - get from " + f))
494 494 elif n2 and f in copy:
495 495 f2 = copy[f]
496 496 if f2 in m2:
497 497 actions['m'].append((f, (f2, f, f2, False, pa.node()),
498 498 "remote copied from " + f2))
499 499 else:
500 500 actions['m'].append((f, (f2, f, f2, True, pa.node()),
501 501 "remote moved from " + f2))
502 502 elif n2 and f not in ma:
503 503 # local unknown, remote created: the logic is described by the
504 504 # following table:
505 505 #
506 506 # force branchmerge different | action
507 507 # n * n | get
508 508 # n * y | abort
509 509 # y n * | get
510 510 # y y n | get
511 511 # y y y | merge
512 512 #
513 513 # Checking whether the files are different is expensive, so we
514 514 # don't do that when we can avoid it.
515 515 if force and not branchmerge:
516 516 actions['g'].append((f, (fl2,), "remote created"))
517 517 else:
518 518 different = _checkunknownfile(repo, wctx, p2, f)
519 519 if force and branchmerge and different:
520 520 # FIXME: This is wrong - f is not in ma ...
521 521 actions['m'].append((f, (f, f, f, False, pa.node()),
522 522 "remote differs from untracked local"))
523 523 elif not force and different:
524 524 aborts.append((f, "ud"))
525 525 else:
526 526 actions['g'].append((f, (fl2,), "remote created"))
527 527 elif n2 and n2 != ma[f]:
528 528 different = _checkunknownfile(repo, wctx, p2, f)
529 529 if not force and different:
530 530 aborts.append((f, "ud"))
531 531 else:
532 532 # if different: old untracked f may be overwritten and lost
533 533 if acceptremote:
534 534 actions['g'].append((f, (m2.flags(f),),
535 535 "remote recreating"))
536 536 else:
537 537 actions['dc'].append((f, (m2.flags(f),),
538 538 "prompt deleted/changed"))
539 539
540 540 for f, m in sorted(aborts):
541 541 if m == "ud":
542 542 repo.ui.warn(_("%s: untracked file differs\n") % f)
543 543 else: assert False, m
544 544 if aborts:
545 545 raise util.Abort(_("untracked files in working directory differ "
546 546 "from files in requested revision"))
547 547
548 548 if not util.checkcase(repo.path):
549 549 # check collision between files only in p2 for clean update
550 550 if (not branchmerge and
551 551 (force or not wctx.dirty(missing=True, branch=False))):
552 552 _checkcollision(repo, m2, None)
553 553 else:
554 554 _checkcollision(repo, m1, actions)
555 555
556 556 return actions
557 557
558 558 def batchremove(repo, actions):
559 559 """apply removes to the working directory
560 560
561 561 yields tuples for progress updates
562 562 """
563 563 verbose = repo.ui.verbose
564 564 unlink = util.unlinkpath
565 565 wjoin = repo.wjoin
566 566 audit = repo.wopener.audit
567 567 i = 0
568 568 for f, args, msg in actions:
569 569 repo.ui.debug(" %s: %s -> r\n" % (f, msg))
570 if True:
571 570 if verbose:
572 571 repo.ui.note(_("removing %s\n") % f)
573 572 audit(f)
574 573 try:
575 574 unlink(wjoin(f), ignoremissing=True)
576 575 except OSError, inst:
577 576 repo.ui.warn(_("update failed to remove %s: %s!\n") %
578 577 (f, inst.strerror))
579 578 if i == 100:
580 579 yield i, f
581 580 i = 0
582 581 i += 1
583 582 if i > 0:
584 583 yield i, f
585 584
586 585 def batchget(repo, mctx, actions):
587 586 """apply gets to the working directory
588 587
589 588 mctx is the context to get from
590 589
591 590 yields tuples for progress updates
592 591 """
593 592 verbose = repo.ui.verbose
594 593 fctx = mctx.filectx
595 594 wwrite = repo.wwrite
596 595 i = 0
597 596 for f, args, msg in actions:
598 597 repo.ui.debug(" %s: %s -> g\n" % (f, msg))
599 if True:
600 598 if verbose:
601 599 repo.ui.note(_("getting %s\n") % f)
602 600 wwrite(f, fctx(f).data(), args[0])
603 601 if i == 100:
604 602 yield i, f
605 603 i = 0
606 604 i += 1
607 605 if i > 0:
608 606 yield i, f
609 607
610 608 def applyupdates(repo, actions, wctx, mctx, overwrite, labels=None):
611 609 """apply the merge action list to the working directory
612 610
613 611 wctx is the working copy context
614 612 mctx is the context to be merged into the working copy
615 613
616 614 Return a tuple of counts (updated, merged, removed, unresolved) that
617 615 describes how many files were affected by the update.
618 616 """
619 617
620 618 updated, merged, removed, unresolved = 0, 0, 0, 0
621 619 ms = mergestate(repo)
622 620 ms.reset(wctx.p1().node(), mctx.node())
623 621 moves = []
624 622 for m, l in actions.items():
625 623 l.sort()
626 624
627 625 # prescan for merges
628 626 for f, args, msg in actions['m']:
629 if True:
630 627 f1, f2, fa, move, anc = args
631 628 if f == '.hgsubstate': # merged internally
632 629 continue
633 630 repo.ui.debug(" preserving %s for resolve of %s\n" % (f1, f))
634 631 fcl = wctx[f1]
635 632 fco = mctx[f2]
636 633 actx = repo[anc]
637 634 if fa in actx:
638 635 fca = actx[fa]
639 636 else:
640 637 fca = repo.filectx(f1, fileid=nullrev)
641 638 ms.add(fcl, fco, fca, f)
642 639 if f1 != f and move:
643 640 moves.append(f1)
644 641
645 642 audit = repo.wopener.audit
646 643 _updating = _('updating')
647 644 _files = _('files')
648 645 progress = repo.ui.progress
649 646
650 647 # remove renamed files after safely stored
651 648 for f in moves:
652 649 if os.path.lexists(repo.wjoin(f)):
653 650 repo.ui.debug("removing %s\n" % f)
654 651 audit(f)
655 652 util.unlinkpath(repo.wjoin(f))
656 653
657 654 numupdates = sum(len(l) for m, l in actions.items() if m != 'k')
658 655
659 656 if [a for a in actions['r'] if a[0] == '.hgsubstate']:
660 657 subrepo.submerge(repo, wctx, mctx, wctx, overwrite)
661 658
662 659 # remove in parallel (must come first)
663 660 z = 0
664 661 prog = worker.worker(repo.ui, 0.001, batchremove, (repo,), actions['r'])
665 662 for i, item in prog:
666 663 z += i
667 664 progress(_updating, z, item=item, total=numupdates, unit=_files)
668 665 removed = len(actions['r'])
669 666
670 667 # get in parallel
671 668 prog = worker.worker(repo.ui, 0.001, batchget, (repo, mctx), actions['g'])
672 669 for i, item in prog:
673 670 z += i
674 671 progress(_updating, z, item=item, total=numupdates, unit=_files)
675 672 updated = len(actions['g'])
676 673
677 674 if [a for a in actions['g'] if a[0] == '.hgsubstate']:
678 675 subrepo.submerge(repo, wctx, mctx, wctx, overwrite)
679 676
680 if True:
681
682 677 # forget (manifest only, just log it) (must come first)
683 678 for f, args, msg in actions['f']:
684 679 repo.ui.debug(" %s: %s -> f\n" % (f, msg))
685 680 z += 1
686 681 progress(_updating, z, item=f, total=numupdates, unit=_files)
687 682
688 683 # re-add (manifest only, just log it)
689 684 for f, args, msg in actions['a']:
690 685 repo.ui.debug(" %s: %s -> a\n" % (f, msg))
691 686 z += 1
692 687 progress(_updating, z, item=f, total=numupdates, unit=_files)
693 688
694 689 # keep (noop, just log it)
695 690 for f, args, msg in actions['k']:
696 691 repo.ui.debug(" %s: %s -> k\n" % (f, msg))
697 692 # no progress
698 693
699 694 # merge
700 695 for f, args, msg in actions['m']:
701 696 repo.ui.debug(" %s: %s -> m\n" % (f, msg))
702 697 z += 1
703 698 progress(_updating, z, item=f, total=numupdates, unit=_files)
704 699 f1, f2, fa, move, anc = args
705 700 if f == '.hgsubstate': # subrepo states need updating
706 701 subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx),
707 702 overwrite)
708 703 continue
709 704 audit(f)
710 705 r = ms.resolve(f, wctx, labels=labels)
711 706 if r is not None and r > 0:
712 707 unresolved += 1
713 708 else:
714 709 if r is None:
715 710 updated += 1
716 711 else:
717 712 merged += 1
718 713
719 714 # directory rename, move local
720 715 for f, args, msg in actions['dm']:
721 716 repo.ui.debug(" %s: %s -> dm\n" % (f, msg))
722 717 z += 1
723 718 progress(_updating, z, item=f, total=numupdates, unit=_files)
724 719 f0, flags = args
725 720 repo.ui.note(_("moving %s to %s\n") % (f0, f))
726 721 audit(f)
727 722 repo.wwrite(f, wctx.filectx(f0).data(), flags)
728 723 util.unlinkpath(repo.wjoin(f0))
729 724 updated += 1
730 725
731 726 # local directory rename, get
732 727 for f, args, msg in actions['dg']:
733 728 repo.ui.debug(" %s: %s -> dg\n" % (f, msg))
734 729 z += 1
735 730 progress(_updating, z, item=f, total=numupdates, unit=_files)
736 731 f0, flags = args
737 732 repo.ui.note(_("getting %s to %s\n") % (f0, f))
738 733 repo.wwrite(f, mctx.filectx(f0).data(), flags)
739 734 updated += 1
740 735
741 736 # divergent renames
742 737 for f, args, msg in actions['dr']:
743 738 repo.ui.debug(" %s: %s -> dr\n" % (f, msg))
744 739 z += 1
745 740 progress(_updating, z, item=f, total=numupdates, unit=_files)
746 741 fl, = args
747 742 repo.ui.warn(_("note: possible conflict - %s was renamed "
748 743 "multiple times to:\n") % f)
749 744 for nf in fl:
750 745 repo.ui.warn(" %s\n" % nf)
751 746
752 747 # rename and delete
753 748 for f, args, msg in actions['rd']:
754 749 repo.ui.debug(" %s: %s -> rd\n" % (f, msg))
755 750 z += 1
756 751 progress(_updating, z, item=f, total=numupdates, unit=_files)
757 752 fl, = args
758 753 repo.ui.warn(_("note: possible conflict - %s was deleted "
759 754 "and renamed to:\n") % f)
760 755 for nf in fl:
761 756 repo.ui.warn(" %s\n" % nf)
762 757
763 758 # exec
764 759 for f, args, msg in actions['e']:
765 760 repo.ui.debug(" %s: %s -> e\n" % (f, msg))
766 761 z += 1
767 762 progress(_updating, z, item=f, total=numupdates, unit=_files)
768 763 flags, = args
769 764 audit(f)
770 765 util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags)
771 766 updated += 1
772 767
773 768 ms.commit()
774 769 progress(_updating, None, total=numupdates, unit=_files)
775 770
776 771 return updated, merged, removed, unresolved
777 772
778 773 def calculateupdates(repo, wctx, mctx, ancestors, branchmerge, force, partial,
779 774 acceptremote, followcopies):
780 775 "Calculate the actions needed to merge mctx into wctx using ancestors"
781 776
782 777 if len(ancestors) == 1: # default
783 778 actions = manifestmerge(repo, wctx, mctx, ancestors[0],
784 779 branchmerge, force,
785 780 partial, acceptremote, followcopies)
786 781
787 782 else: # only when merge.preferancestor=* - experimentalish code
788 783 repo.ui.status(
789 784 _("note: merging %s and %s using bids from ancestors %s\n") %
790 785 (wctx, mctx, _(' and ').join(str(anc) for anc in ancestors)))
791 786
792 787 # Call for bids
793 788 fbids = {} # mapping filename to bids (action method to list af actions)
794 789 for ancestor in ancestors:
795 790 repo.ui.note(_('\ncalculating bids for ancestor %s\n') % ancestor)
796 791 actions = manifestmerge(repo, wctx, mctx, ancestor,
797 792 branchmerge, force,
798 793 partial, acceptremote, followcopies)
799 794 for m, l in sorted(actions.items()):
800 795 for a in l:
801 796 f, args, msg = a
802 797 repo.ui.debug(' %s: %s -> %s\n' % (f, msg, m))
803 798 if f in fbids:
804 799 d = fbids[f]
805 800 if m in d:
806 801 d[m].append(a)
807 802 else:
808 803 d[m] = [a]
809 804 else:
810 805 fbids[f] = {m: [a]}
811 806
812 807 # Pick the best bid for each file
813 808 repo.ui.note(_('\nauction for merging merge bids\n'))
814 809 actions = dict((m, []) for m in actions.keys())
815 810 for f, bids in sorted(fbids.items()):
816 811 # bids is a mapping from action method to list af actions
817 812 # Consensus?
818 813 if len(bids) == 1: # all bids are the same kind of method
819 814 m, l = bids.items()[0]
820 815 if util.all(a == l[0] for a in l[1:]): # len(bids) is > 1
821 816 repo.ui.note(" %s: consensus for %s\n" % (f, m))
822 817 actions[m].append(l[0])
823 818 continue
824 819 # If keep is an option, just do it.
825 820 if "k" in bids:
826 821 repo.ui.note(" %s: picking 'keep' action\n" % f)
827 822 actions['k'].append(bids["k"][0])
828 823 continue
829 824 # If there are gets and they all agree [how could they not?], do it.
830 825 if "g" in bids:
831 826 ga0 = bids["g"][0]
832 827 if util.all(a == ga0 for a in bids["g"][1:]):
833 828 repo.ui.note(" %s: picking 'get' action\n" % f)
834 829 actions['g'].append(ga0)
835 830 continue
836 831 # TODO: Consider other simple actions such as mode changes
837 832 # Handle inefficient democrazy.
838 833 repo.ui.note(_(' %s: multiple bids for merge action:\n') % f)
839 834 for m, l in sorted(bids.items()):
840 835 for _f, args, msg in l:
841 836 repo.ui.note(' %s -> %s\n' % (msg, m))
842 837 # Pick random action. TODO: Instead, prompt user when resolving
843 838 m, l = bids.items()[0]
844 839 repo.ui.warn(_(' %s: ambiguous merge - picked %s action\n') %
845 840 (f, m))
846 841 actions[m].append(l[0])
847 842 continue
848 843 repo.ui.note(_('end of auction\n\n'))
849 844
850 845 # Prompt and create actions. TODO: Move this towards resolve phase.
851 if True:
852 846 for f, args, msg in actions['cd']:
853 847 if repo.ui.promptchoice(
854 848 _("local changed %s which remote deleted\n"
855 849 "use (c)hanged version or (d)elete?"
856 850 "$$ &Changed $$ &Delete") % f, 0):
857 851 actions['r'].append((f, None, "prompt delete"))
858 852 else:
859 853 actions['a'].append((f, None, "prompt keep"))
860 854 del actions['cd'][:]
861 855
862 856 for f, args, msg in actions['dc']:
863 857 flags, = args
864 858 if repo.ui.promptchoice(
865 859 _("remote changed %s which local deleted\n"
866 860 "use (c)hanged version or leave (d)eleted?"
867 861 "$$ &Changed $$ &Deleted") % f, 0) == 0:
868 862 actions['g'].append((f, (flags,), "prompt recreating"))
869 863 del actions['dc'][:]
870 864
871 865 if wctx.rev() is None:
872 866 ractions, factions = _forgetremoved(wctx, mctx, branchmerge)
873 867 actions['r'].extend(ractions)
874 868 actions['f'].extend(factions)
875 869
876 870 return actions
877 871
878 872 def recordupdates(repo, actions, branchmerge):
879 873 "record merge actions to the dirstate"
880 if True:
881 874 # remove (must come first)
882 875 for f, args, msg in actions['r']:
883 876 if branchmerge:
884 877 repo.dirstate.remove(f)
885 878 else:
886 879 repo.dirstate.drop(f)
887 880
888 881 # forget (must come first)
889 882 for f, args, msg in actions['f']:
890 883 repo.dirstate.drop(f)
891 884
892 885 # re-add
893 886 for f, args, msg in actions['a']:
894 887 if not branchmerge:
895 888 repo.dirstate.add(f)
896 889
897 890 # exec change
898 891 for f, args, msg in actions['e']:
899 892 repo.dirstate.normallookup(f)
900 893
901 894 # keep
902 895 for f, args, msg in actions['k']:
903 896 pass
904 897
905 898 # get
906 899 for f, args, msg in actions['g']:
907 900 if branchmerge:
908 901 repo.dirstate.otherparent(f)
909 902 else:
910 903 repo.dirstate.normal(f)
911 904
912 905 # merge
913 906 for f, args, msg in actions['m']:
914 907 f1, f2, fa, move, anc = args
915 908 if branchmerge:
916 909 # We've done a branch merge, mark this file as merged
917 910 # so that we properly record the merger later
918 911 repo.dirstate.merge(f)
919 912 if f1 != f2: # copy/rename
920 913 if move:
921 914 repo.dirstate.remove(f1)
922 915 if f1 != f:
923 916 repo.dirstate.copy(f1, f)
924 917 else:
925 918 repo.dirstate.copy(f2, f)
926 919 else:
927 920 # We've update-merged a locally modified file, so
928 921 # we set the dirstate to emulate a normal checkout
929 922 # of that file some time in the past. Thus our
930 923 # merge will appear as a normal local file
931 924 # modification.
932 925 if f2 == f: # file not locally copied/moved
933 926 repo.dirstate.normallookup(f)
934 927 if move:
935 928 repo.dirstate.drop(f1)
936 929
937 930 # directory rename, move local
938 931 for f, args, msg in actions['dm']:
939 932 f0, flag = args
940 933 if f0 not in repo.dirstate:
941 934 # untracked file moved
942 935 continue
943 936 if branchmerge:
944 937 repo.dirstate.add(f)
945 938 repo.dirstate.remove(f0)
946 939 repo.dirstate.copy(f0, f)
947 940 else:
948 941 repo.dirstate.normal(f)
949 942 repo.dirstate.drop(f0)
950 943
951 944 # directory rename, get
952 945 for f, args, msg in actions['dg']:
953 946 f0, flag = args
954 947 if branchmerge:
955 948 repo.dirstate.add(f)
956 949 repo.dirstate.copy(f0, f)
957 950 else:
958 951 repo.dirstate.normal(f)
959 952
960 953 def update(repo, node, branchmerge, force, partial, ancestor=None,
961 954 mergeancestor=False, labels=None):
962 955 """
963 956 Perform a merge between the working directory and the given node
964 957
965 958 node = the node to update to, or None if unspecified
966 959 branchmerge = whether to merge between branches
967 960 force = whether to force branch merging or file overwriting
968 961 partial = a function to filter file lists (dirstate not updated)
969 962 mergeancestor = whether it is merging with an ancestor. If true,
970 963 we should accept the incoming changes for any prompts that occur.
971 964 If false, merging with an ancestor (fast-forward) is only allowed
972 965 between different named branches. This flag is used by rebase extension
973 966 as a temporary fix and should be avoided in general.
974 967
975 968 The table below shows all the behaviors of the update command
976 969 given the -c and -C or no options, whether the working directory
977 970 is dirty, whether a revision is specified, and the relationship of
978 971 the parent rev to the target rev (linear, on the same named
979 972 branch, or on another named branch).
980 973
981 974 This logic is tested by test-update-branches.t.
982 975
983 976 -c -C dirty rev | linear same cross
984 977 n n n n | ok (1) x
985 978 n n n y | ok ok ok
986 979 n n y n | merge (2) (2)
987 980 n n y y | merge (3) (3)
988 981 n y * * | --- discard ---
989 982 y n y * | --- (4) ---
990 983 y n n * | --- ok ---
991 984 y y * * | --- (5) ---
992 985
993 986 x = can't happen
994 987 * = don't-care
995 988 1 = abort: not a linear update (merge or update --check to force update)
996 989 2 = abort: uncommitted changes (commit and merge, or update --clean to
997 990 discard changes)
998 991 3 = abort: uncommitted changes (commit or update --clean to discard changes)
999 992 4 = abort: uncommitted changes (checked in commands.py)
1000 993 5 = incompatible options (checked in commands.py)
1001 994
1002 995 Return the same tuple as applyupdates().
1003 996 """
1004 997
1005 998 onode = node
1006 999 wlock = repo.wlock()
1007 1000 try:
1008 1001 wc = repo[None]
1009 1002 pl = wc.parents()
1010 1003 p1 = pl[0]
1011 1004 pas = [None]
1012 1005 if ancestor:
1013 1006 pas = [repo[ancestor]]
1014 1007
1015 1008 if node is None:
1016 1009 # Here is where we should consider bookmarks, divergent bookmarks,
1017 1010 # foreground changesets (successors), and tip of current branch;
1018 1011 # but currently we are only checking the branch tips.
1019 1012 try:
1020 1013 node = repo.branchtip(wc.branch())
1021 1014 except error.RepoLookupError:
1022 1015 if wc.branch() == "default": # no default branch!
1023 1016 node = repo.lookup("tip") # update to tip
1024 1017 else:
1025 1018 raise util.Abort(_("branch %s not found") % wc.branch())
1026 1019
1027 1020 if p1.obsolete() and not p1.children():
1028 1021 # allow updating to successors
1029 1022 successors = obsolete.successorssets(repo, p1.node())
1030 1023
1031 1024 # behavior of certain cases is as follows,
1032 1025 #
1033 1026 # divergent changesets: update to highest rev, similar to what
1034 1027 # is currently done when there are more than one head
1035 1028 # (i.e. 'tip')
1036 1029 #
1037 1030 # replaced changesets: same as divergent except we know there
1038 1031 # is no conflict
1039 1032 #
1040 1033 # pruned changeset: no update is done; though, we could
1041 1034 # consider updating to the first non-obsolete parent,
1042 1035 # similar to what is current done for 'hg prune'
1043 1036
1044 1037 if successors:
1045 1038 # flatten the list here handles both divergent (len > 1)
1046 1039 # and the usual case (len = 1)
1047 1040 successors = [n for sub in successors for n in sub]
1048 1041
1049 1042 # get the max revision for the given successors set,
1050 1043 # i.e. the 'tip' of a set
1051 1044 node = repo.revs("max(%ln)", successors)[0]
1052 1045 pas = [p1]
1053 1046
1054 1047 overwrite = force and not branchmerge
1055 1048
1056 1049 p2 = repo[node]
1057 1050 if pas[0] is None:
1058 1051 if repo.ui.config("merge", "preferancestor") == '*':
1059 1052 cahs = repo.changelog.commonancestorsheads(p1.node(), p2.node())
1060 1053 pas = [repo[anc] for anc in (sorted(cahs) or [nullid])]
1061 1054 else:
1062 1055 pas = [p1.ancestor(p2, warn=True)]
1063 1056
1064 1057 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
1065 1058
1066 1059 ### check phase
1067 1060 if not overwrite and len(pl) > 1:
1068 1061 raise util.Abort(_("outstanding uncommitted merges"))
1069 1062 if branchmerge:
1070 1063 if pas == [p2]:
1071 1064 raise util.Abort(_("merging with a working directory ancestor"
1072 1065 " has no effect"))
1073 1066 elif pas == [p1]:
1074 1067 if not mergeancestor and p1.branch() == p2.branch():
1075 1068 raise util.Abort(_("nothing to merge"),
1076 1069 hint=_("use 'hg update' "
1077 1070 "or check 'hg heads'"))
1078 1071 if not force and (wc.files() or wc.deleted()):
1079 1072 raise util.Abort(_("uncommitted changes"),
1080 1073 hint=_("use 'hg status' to list changes"))
1081 1074 for s in sorted(wc.substate):
1082 1075 if wc.sub(s).dirty():
1083 1076 raise util.Abort(_("uncommitted changes in "
1084 1077 "subrepository '%s'") % s)
1085 1078
1086 1079 elif not overwrite:
1087 1080 if p1 == p2: # no-op update
1088 1081 # call the hooks and exit early
1089 1082 repo.hook('preupdate', throw=True, parent1=xp2, parent2='')
1090 1083 repo.hook('update', parent1=xp2, parent2='', error=0)
1091 1084 return 0, 0, 0, 0
1092 1085
1093 1086 if pas not in ([p1], [p2]): # nonlinear
1094 1087 dirty = wc.dirty(missing=True)
1095 1088 if dirty or onode is None:
1096 1089 # Branching is a bit strange to ensure we do the minimal
1097 1090 # amount of call to obsolete.background.
1098 1091 foreground = obsolete.foreground(repo, [p1.node()])
1099 1092 # note: the <node> variable contains a random identifier
1100 1093 if repo[node].node() in foreground:
1101 1094 pas = [p1] # allow updating to successors
1102 1095 elif dirty:
1103 1096 msg = _("uncommitted changes")
1104 1097 if onode is None:
1105 1098 hint = _("commit and merge, or update --clean to"
1106 1099 " discard changes")
1107 1100 else:
1108 1101 hint = _("commit or update --clean to discard"
1109 1102 " changes")
1110 1103 raise util.Abort(msg, hint=hint)
1111 1104 else: # node is none
1112 1105 msg = _("not a linear update")
1113 1106 hint = _("merge or update --check to force update")
1114 1107 raise util.Abort(msg, hint=hint)
1115 1108 else:
1116 1109 # Allow jumping branches if clean and specific rev given
1117 1110 pas = [p1]
1118 1111
1119 1112 followcopies = False
1120 1113 if overwrite:
1121 1114 pas = [wc]
1122 1115 elif pas == [p2]: # backwards
1123 1116 pas = [wc.p1()]
1124 1117 elif not branchmerge and not wc.dirty(missing=True):
1125 1118 pass
1126 1119 elif pas[0] and repo.ui.configbool("merge", "followcopies", True):
1127 1120 followcopies = True
1128 1121
1129 1122 ### calculate phase
1130 1123 actions = calculateupdates(repo, wc, p2, pas, branchmerge, force,
1131 1124 partial, mergeancestor, followcopies)
1132 1125
1133 1126 ### apply phase
1134 1127 if not branchmerge: # just jump to the new rev
1135 1128 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
1136 1129 if not partial:
1137 1130 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
1138 1131 # note that we're in the middle of an update
1139 1132 repo.vfs.write('updatestate', p2.hex())
1140 1133
1141 1134 stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels)
1142 1135
1143 1136 if not partial:
1144 1137 repo.setparents(fp1, fp2)
1145 1138 recordupdates(repo, actions, branchmerge)
1146 1139 # update completed, clear state
1147 1140 util.unlink(repo.join('updatestate'))
1148 1141
1149 1142 if not branchmerge:
1150 1143 repo.dirstate.setbranch(p2.branch())
1151 1144 finally:
1152 1145 wlock.release()
1153 1146
1154 1147 if not partial:
1155 1148 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
1156 1149 return stats
General Comments 0
You need to be logged in to leave comments. Login now