merge.py
589 lines
| 21.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
|
r2775 | # merge.py - directory-level update/merge handling for Mercurial | ||
# | ||||
|
r4635 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> | ||
|
r2775 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2775 | |||
|
r6518 | from node import nullid, nullrev, hex, bin | ||
|
r3891 | from i18n import _ | ||
|
r15637 | import scmutil, util, filemerge, copies, subrepo | ||
|
r8312 | import errno, os, shutil | ||
|
r6512 | |||
class mergestate(object): | ||||
'''track 3-way merge state of individual files''' | ||||
def __init__(self, repo): | ||||
self._repo = repo | ||||
|
r12369 | self._dirty = False | ||
|
r6518 | self._read() | ||
|
r7848 | def reset(self, node=None): | ||
|
r6512 | self._state = {} | ||
|
r7848 | if node: | ||
self._local = node | ||||
|
r6512 | shutil.rmtree(self._repo.join("merge"), True) | ||
|
r12369 | self._dirty = False | ||
|
r6518 | def _read(self): | ||
self._state = {} | ||||
try: | ||||
f = self._repo.opener("merge/state") | ||||
|
r6530 | for i, l in enumerate(f): | ||
if i == 0: | ||||
|
r11451 | self._local = bin(l[:-1]) | ||
|
r6530 | else: | ||
bits = l[:-1].split("\0") | ||||
self._state[bits[0]] = bits[1:] | ||||
|
r13400 | f.close() | ||
|
r6518 | except IOError, err: | ||
if err.errno != errno.ENOENT: | ||||
raise | ||||
|
r12369 | self._dirty = False | ||
def commit(self): | ||||
if self._dirty: | ||||
f = self._repo.opener("merge/state", "w") | ||||
f.write(hex(self._local) + "\n") | ||||
for d, v in self._state.iteritems(): | ||||
f.write("\0".join([d] + v) + "\n") | ||||
|
r13400 | f.close() | ||
|
r12369 | self._dirty = False | ||
|
r6512 | def add(self, fcl, fco, fca, fd, flags): | ||
|
r6517 | hash = util.sha1(fcl.path()).hexdigest() | ||
|
r14168 | self._repo.opener.write("merge/" + hash, fcl.data()) | ||
|
r6518 | self._state[fd] = ['u', hash, fcl.path(), fca.path(), | ||
hex(fca.filenode()), fco.path(), flags] | ||||
|
r12369 | self._dirty = True | ||
|
r6512 | def __contains__(self, dfile): | ||
return dfile in self._state | ||||
def __getitem__(self, dfile): | ||||
|
r6518 | return self._state[dfile][0] | ||
def __iter__(self): | ||||
l = self._state.keys() | ||||
l.sort() | ||||
for f in l: | ||||
yield f | ||||
|
r6512 | def mark(self, dfile, state): | ||
|
r6518 | self._state[dfile][0] = state | ||
|
r12369 | self._dirty = True | ||
|
r6512 | def resolve(self, dfile, wctx, octx): | ||
if self[dfile] == 'r': | ||||
return 0 | ||||
|
r6518 | state, hash, lfile, afile, anode, ofile, flags = self._state[dfile] | ||
|
r6512 | f = self._repo.opener("merge/" + hash) | ||
self._repo.wwrite(dfile, f.read(), flags) | ||||
|
r13400 | f.close() | ||
|
r6512 | fcd = wctx[dfile] | ||
fco = octx[ofile] | ||||
fca = self._repo.filectx(afile, fileid=anode) | ||||
r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca) | ||||
|
r13536 | if r is None: | ||
# no real conflict | ||||
del self._state[dfile] | ||||
elif not r: | ||||
|
r6512 | self.mark(dfile, 'r') | ||
return r | ||||
|
r2775 | |||
|
r15538 | def _checkunknown(wctx, mctx, folding): | ||
|
r3315 | "check for collisions between unknown files and files in mctx" | ||
|
r15538 | if folding: | ||
foldf = util.normcase | ||||
else: | ||||
foldf = lambda fn: fn | ||||
folded = {} | ||||
for fn in mctx: | ||||
folded[foldf(fn)] = fn | ||||
|
r15894 | |||
error = False | ||||
|
r15538 | for fn in wctx.unknown(): | ||
f = foldf(fn) | ||||
if f in folded and mctx[folded[f]].cmp(wctx[f]): | ||||
|
r15894 | error = True | ||
wctx._repo.ui.warn(_("%s: untracked file differs\n") % fn) | ||||
if error: | ||||
raise util.Abort(_("untracked files in working directory differ " | ||||
"from files in requested revision")) | ||||
|
r3107 | |||
|
r15673 | def _checkcollision(mctx, wctx): | ||
|
r3785 | "check for case folding collisions in the destination context" | ||
folded = {} | ||||
|
r6272 | for fn in mctx: | ||
|
r15637 | fold = util.normcase(fn) | ||
|
r3785 | if fold in folded: | ||
raise util.Abort(_("case-folding collision between %s and %s") | ||||
% (fn, folded[fold])) | ||||
folded[fold] = fn | ||||
|
r15673 | if wctx: | ||
for fn in wctx: | ||||
fold = util.normcase(fn) | ||||
mfn = folded.get(fold, None) | ||||
if mfn and (mfn != fn): | ||||
raise util.Abort(_("case-folding collision between %s and %s") | ||||
% (mfn, fn)) | ||||
|
r6269 | def _forgetremoved(wctx, mctx, branchmerge): | ||
|
r3107 | """ | ||
Forget removed files | ||||
If we're jumping between revisions (as opposed to merging), and if | ||||
neither the working directory nor the target rev has the file, | ||||
then we need to remove it from the dirstate, to prevent the | ||||
dirstate from listing the file when it is no longer in the | ||||
manifest. | ||||
|
r6242 | |||
If we're merging, and the other revision has removed a file | ||||
that is not present in the working directory, we need to mark it | ||||
as removed. | ||||
|
r3107 | """ | ||
action = [] | ||||
|
r6242 | state = branchmerge and 'r' or 'f' | ||
for f in wctx.deleted(): | ||||
|
r6272 | if f not in mctx: | ||
|
r6242 | action.append((f, state)) | ||
if not branchmerge: | ||||
for f in wctx.removed(): | ||||
|
r6272 | if f not in mctx: | ||
|
r6242 | action.append((f, "f")) | ||
|
r3107 | |||
return action | ||||
|
r3295 | def manifestmerge(repo, p1, p2, pa, overwrite, partial): | ||
|
r3105 | """ | ||
|
r11817 | Merge p1 and p2 with ancestor pa and generate merge action list | ||
|
r3315 | |||
overwrite = whether we clobber working files | ||||
partial = function to filter file lists | ||||
|
r3105 | """ | ||
|
r8733 | def fmerge(f, f2, fa): | ||
|
r4007 | """merge flags""" | ||
|
r5708 | a, m, n = ma.flags(fa), m1.flags(f), m2.flags(f2) | ||
if m == n: # flags agree | ||||
return m # unchanged | ||||
|
r8733 | if m and n and not a: # flags set, don't agree, differ from parent | ||
|
r9048 | r = repo.ui.promptchoice( | ||
|
r8733 | _(" conflicting flags for %s\n" | ||
"(n)one, e(x)ec or sym(l)ink?") % f, | ||||
|
r9048 | (_("&None"), _("E&xec"), _("Sym&link")), 0) | ||
|
r10282 | if r == 1: | ||
return "x" # Exec | ||||
if r == 2: | ||||
return "l" # Symlink | ||||
|
r9048 | return "" | ||
|
r5708 | if m and m != a: # changed from a to m | ||
return m | ||||
if n and n != a: # changed from a to n | ||||
return n | ||||
return '' # flag was cleared | ||||
|
r3118 | |||
|
r3307 | def act(msg, m, f, *args): | ||
|
r3295 | repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m)) | ||
|
r3121 | action.append((f, m) + args) | ||
|
r8753 | action, copy = [], {} | ||
|
r8749 | if overwrite: | ||
|
r8753 | pa = p1 | ||
elif pa == p2: # backwards | ||||
pa = p1.p1() | ||||
elif pa and repo.ui.configbool("merge", "followcopies", True): | ||||
dirs = repo.ui.configbool("merge", "followdirs", True) | ||||
|
r15774 | copy, diverge = copies.mergecopies(repo, p1, p2, pa, dirs) | ||
|
r8753 | for of, fl in diverge.iteritems(): | ||
act("divergent renames", "dr", of, fl) | ||||
repo.ui.note(_("resolving manifests\n")) | ||||
|
r15625 | repo.ui.debug(" overwrite: %s, partial: %s\n" | ||
% (bool(overwrite), bool(partial))) | ||||
repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, p1, p2)) | ||||
|
r8753 | |||
m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest() | ||||
copied = set(copy.values()) | ||||
|
r3295 | |||
|
r11470 | if '.hgsubstate' in m1: | ||
|
r9783 | # check whether sub state is modified | ||
for s in p1.substate: | ||||
if p1.sub(s).dirty(): | ||||
m1['.hgsubstate'] += "+" | ||||
break | ||||
|
r3105 | # Compare manifests | ||
for f, n in m1.iteritems(): | ||||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
|
r3105 | if f in m2: | ||
|
r8735 | rflags = fmerge(f, f, f) | ||
|
r8752 | a = ma.get(f, nullid) | ||
if n == m2[f] or m2[f] == a: # same or local newer | ||||
|
r11466 | # is file locally modified or flags need changing? | ||
# dirstate flags may need to be made current | ||||
if m1.flags(f) != rflags or n[20:]: | ||||
|
r8752 | act("update permissions", "e", f, rflags) | ||
elif n == a: # remote newer | ||||
act("remote is newer", "g", f, rflags) | ||||
else: # both changed | ||||
act("versions differ", "m", f, f, f, rflags, False) | ||||
elif f in copied: # files we'll deal with on m2 side | ||||
pass | ||||
|
r3249 | elif f in copy: | ||
f2 = copy[f] | ||||
|
r3733 | if f2 not in m2: # directory rename | ||
act("remote renamed directory to " + f2, "d", | ||||
|
r4007 | f, None, f2, m1.flags(f)) | ||
|
r8748 | else: # case 2 A,B/B/B or case 4,21 A/B/B | ||
act("local copied/moved to " + f2, "m", | ||||
|
r3730 | f, f2, f, fmerge(f, f2, f2), False) | ||
|
r8744 | elif f in ma: # clean, a different, no remote | ||
|
r8739 | if n != ma[f]: | ||
|
r9048 | if repo.ui.promptchoice( | ||
|
r5670 | _(" local changed %s which remote deleted\n" | ||
"use (c)hanged version or (d)elete?") % f, | ||||
|
r9048 | (_("&Changed"), _("&Delete")), 0): | ||
|
r3307 | act("prompt delete", "r", f) | ||
|
r8736 | else: | ||
act("prompt keep", "a", f) | ||||
|
r8751 | elif n[20:] == "a": # added, no remote | ||
act("remote deleted", "f", f) | ||||
|
r8744 | elif n[20:] != "u": | ||
|
r3307 | act("other deleted", "r", f) | ||
|
r3105 | |||
for f, n in m2.iteritems(): | ||||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
|
r8752 | if f in m1 or f in copied: # files already visited | ||
|
r3729 | continue | ||
|
r3249 | if f in copy: | ||
f2 = copy[f] | ||||
|
r3733 | if f2 not in m1: # directory rename | ||
act("local renamed directory to " + f2, "d", | ||||
|
r4007 | None, f, f2, m2.flags(f)) | ||
|
r3733 | elif f2 in m2: # rename case 1, A/A,B/A | ||
|
r3730 | act("remote copied to " + f, "m", | ||
f2, f, f, fmerge(f2, f, f2), False) | ||||
else: # case 3,20 A/B/A | ||||
act("remote moved to " + f, "m", | ||||
f2, f, f, fmerge(f2, f, f2), True) | ||||
|
r8741 | elif f not in ma: | ||
|
r4007 | act("remote created", "g", f, m2.flags(f)) | ||
|
r8741 | elif n != ma[f]: | ||
|
r9048 | if repo.ui.promptchoice( | ||
|
r8741 | _("remote changed %s which local deleted\n" | ||
"use (c)hanged version or leave (d)eleted?") % f, | ||||
|
r9048 | (_("&Changed"), _("&Deleted")), 0) == 0: | ||
|
r8741 | act("prompt recreating", "g", f, m2.flags(f)) | ||
|
r3105 | |||
return action | ||||
|
r8366 | def actionkey(a): | ||
return a[1] == 'r' and -1 or 0, a | ||||
|
r6805 | |||
|
r13322 | def applyupdates(repo, action, wctx, mctx, actx, overwrite): | ||
|
r11454 | """apply the merge action list to the working directory | ||
wctx is the working copy context | ||||
mctx is the context to be merged into the working copy | ||||
actx is the context of the common ancestor | ||||
|
r13162 | |||
Return a tuple of counts (updated, merged, removed, unresolved) that | ||||
describes how many files were affected by the update. | ||||
|
r11454 | """ | ||
|
r3315 | |||
|
r3111 | updated, merged, removed, unresolved = 0, 0, 0, 0 | ||
|
r6512 | ms = mergestate(repo) | ||
|
r13878 | ms.reset(wctx.p1().node()) | ||
|
r6512 | moves = [] | ||
|
r8366 | action.sort(key=actionkey) | ||
|
r6512 | |||
# prescan for merges | ||||
|
r5042 | for a in action: | ||
f, m = a[:2] | ||||
if m == 'm': # merge | ||||
f2, fd, flags, move = a[2:] | ||||
|
r8814 | if f == '.hgsubstate': # merged internally | ||
continue | ||||
|
r9467 | repo.ui.debug("preserving %s for resolve of %s\n" % (f, fd)) | ||
|
r6512 | fcl = wctx[f] | ||
fco = mctx[f2] | ||||
|
r12008 | if mctx == actx: # backwards, use working dir parent as ancestor | ||
|
r12664 | if fcl.parents(): | ||
|
r13878 | fca = fcl.p1() | ||
|
r12664 | else: | ||
fca = repo.filectx(f, fileid=nullrev) | ||||
|
r12008 | else: | ||
fca = fcl.ancestor(fco, actx) | ||||
if not fca: | ||||
fca = repo.filectx(f, fileid=nullrev) | ||||
|
r6512 | ms.add(fcl, fco, fca, fd, flags) | ||
if f != fd and move: | ||||
moves.append(f) | ||||
|
r14398 | audit = scmutil.pathauditor(repo.root) | ||
|
r6512 | # remove renamed files after safely stored | ||
for f in moves: | ||||
|
r12032 | if os.path.lexists(repo.wjoin(f)): | ||
|
r9467 | repo.ui.debug("removing %s\n" % f) | ||
|
r14398 | audit(f) | ||
|
r6512 | os.unlink(repo.wjoin(f)) | ||
|
r5042 | |||
|
r10431 | numupdates = len(action) | ||
for i, a in enumerate(action): | ||||
|
r3111 | f, m = a[:2] | ||
|
r15041 | repo.ui.progress(_('updating'), i + 1, item=f, total=numupdates, | ||
unit=_('files')) | ||||
|
r3733 | if f and f[0] == "/": | ||
|
r3111 | continue | ||
if m == "r": # remove | ||||
repo.ui.note(_("removing %s\n") % f) | ||||
|
r14398 | audit(f) | ||
|
r8814 | if f == '.hgsubstate': # subrepo states need updating | ||
|
r13322 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
|
r3111 | try: | ||
|
r13235 | util.unlinkpath(repo.wjoin(f)) | ||
|
r3111 | except OSError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
repo.ui.warn(_("update failed to remove %s: %s!\n") % | ||||
(f, inst.strerror)) | ||||
|
r3673 | removed += 1 | ||
|
r3308 | elif m == "m": # merge | ||
|
r8814 | if f == '.hgsubstate': # subrepo states need updating | ||
|
r13322 | subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx), overwrite) | ||
|
r8814 | continue | ||
|
r4007 | f2, fd, flags, move = a[2:] | ||
|
r14406 | repo.wopener.audit(fd) | ||
|
r6512 | r = ms.resolve(fd, wctx, mctx) | ||
|
r9030 | if r is not None and r > 0: | ||
|
r3249 | unresolved += 1 | ||
|
r3309 | else: | ||
|
r3400 | if r is None: | ||
updated += 1 | ||||
else: | ||||
merged += 1 | ||||
|
r14232 | util.setflags(repo.wjoin(fd), 'l' in flags, 'x' in flags) | ||
|
r13718 | if (move and repo.dirstate.normalize(fd) != f | ||
and os.path.lexists(repo.wjoin(f))): | ||||
|
r9467 | repo.ui.debug("removing %s\n" % f) | ||
|
r14398 | audit(f) | ||
|
r5042 | os.unlink(repo.wjoin(f)) | ||
|
r3111 | elif m == "g": # get | ||
|
r4007 | flags = a[2] | ||
|
r3111 | repo.ui.note(_("getting %s\n") % f) | ||
|
r3303 | t = mctx.filectx(f).data() | ||
|
r4007 | repo.wwrite(f, t, flags) | ||
|
r11755 | t = None | ||
|
r3111 | updated += 1 | ||
|
r8814 | if f == '.hgsubstate': # subrepo states need updating | ||
|
r13322 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
|
r3733 | elif m == "d": # directory rename | ||
|
r4007 | f2, fd, flags = a[2:] | ||
|
r3733 | if f: | ||
repo.ui.note(_("moving %s to %s\n") % (f, fd)) | ||||
|
r14398 | audit(f) | ||
|
r3733 | t = wctx.filectx(f).data() | ||
|
r4007 | repo.wwrite(fd, t, flags) | ||
|
r13235 | util.unlinkpath(repo.wjoin(f)) | ||
|
r3733 | if f2: | ||
repo.ui.note(_("getting %s to %s\n") % (f2, fd)) | ||||
t = mctx.filectx(f2).data() | ||||
|
r4007 | repo.wwrite(fd, t, flags) | ||
|
r3733 | updated += 1 | ||
|
r4674 | elif m == "dr": # divergent renames | ||
fl = a[2] | ||||
|
r12757 | repo.ui.warn(_("note: possible conflict - %s was renamed " | ||
"multiple times to:\n") % f) | ||||
|
r4674 | for nf in fl: | ||
repo.ui.warn(" %s\n" % nf) | ||||
|
r3111 | elif m == "e": # exec | ||
|
r4007 | flags = a[2] | ||
|
r14405 | repo.wopener.audit(f) | ||
|
r14232 | util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags) | ||
|
r12369 | ms.commit() | ||
|
r15041 | repo.ui.progress(_('updating'), None, total=numupdates, unit=_('files')) | ||
|
r3111 | |||
return updated, merged, removed, unresolved | ||||
|
r3372 | def recordupdates(repo, action, branchmerge): | ||
|
r3315 | "record merge actions to the dirstate" | ||
|
r3111 | for a in action: | ||
f, m = a[:2] | ||||
if m == "r": # remove | ||||
if branchmerge: | ||||
|
r4904 | repo.dirstate.remove(f) | ||
|
r3111 | else: | ||
|
r14434 | repo.dirstate.drop(f) | ||
|
r7768 | elif m == "a": # re-add | ||
if not branchmerge: | ||||
repo.dirstate.add(f) | ||||
|
r3111 | elif m == "f": # forget | ||
|
r14434 | repo.dirstate.drop(f) | ||
|
r7569 | elif m == "e": # exec change | ||
|
r7630 | repo.dirstate.normallookup(f) | ||
|
r7569 | elif m == "g": # get | ||
|
r3111 | if branchmerge: | ||
|
r10968 | repo.dirstate.otherparent(f) | ||
|
r3111 | else: | ||
|
r4904 | repo.dirstate.normal(f) | ||
|
r3111 | elif m == "m": # merge | ||
|
r3303 | f2, fd, flag, move = a[2:] | ||
|
r3251 | if branchmerge: | ||
# We've done a branch merge, mark this file as merged | ||||
# so that we properly record the merger later | ||||
|
r4904 | repo.dirstate.merge(fd) | ||
|
r3372 | if f != f2: # copy/rename | ||
if move: | ||||
|
r4904 | repo.dirstate.remove(f) | ||
|
r3372 | if f != fd: | ||
repo.dirstate.copy(f, fd) | ||||
else: | ||||
repo.dirstate.copy(f2, fd) | ||||
|
r3251 | else: | ||
# We've update-merged a locally modified file, so | ||||
# we set the dirstate to emulate a normal checkout | ||||
# of that file some time in the past. Thus our | ||||
# merge will appear as a normal local file | ||||
# modification. | ||||
|
r11178 | if f2 == fd: # file not locally copied/moved | ||
repo.dirstate.normallookup(fd) | ||||
|
r3308 | if move: | ||
|
r14434 | repo.dirstate.drop(f) | ||
|
r3733 | elif m == "d": # directory rename | ||
f2, fd, flag = a[2:] | ||||
|
r4819 | if not f2 and f not in repo.dirstate: | ||
# untracked file moved | ||||
continue | ||||
|
r3733 | if branchmerge: | ||
|
r4904 | repo.dirstate.add(fd) | ||
|
r3733 | if f: | ||
|
r4904 | repo.dirstate.remove(f) | ||
|
r3733 | repo.dirstate.copy(f, fd) | ||
if f2: | ||||
repo.dirstate.copy(f2, fd) | ||||
else: | ||||
|
r4904 | repo.dirstate.normal(fd) | ||
|
r3733 | if f: | ||
|
r14434 | repo.dirstate.drop(f) | ||
|
r3111 | |||
|
r13874 | def update(repo, node, branchmerge, force, partial, ancestor=None): | ||
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
|
r9716 | node = the node to update to, or None if unspecified | ||
|
r3315 | branchmerge = whether to merge between branches | ||
force = whether to force branch merging or file overwriting | ||||
partial = a function to filter file lists (dirstate not updated) | ||||
|
r9716 | |||
The table below shows all the behaviors of the update command | ||||
given the -c and -C or no options, whether the working directory | ||||
is dirty, whether a revision is specified, and the relationship of | ||||
the parent rev to the target rev (linear, on the same named | ||||
branch, or on another named branch). | ||||
|
r12279 | This logic is tested by test-update-branches.t. | ||
|
r9716 | |||
-c -C dirty rev | linear same cross | ||||
n n n n | ok (1) x | ||||
|
r9717 | n n n y | ok ok ok | ||
n n y * | merge (2) (2) | ||||
|
r9716 | n y * * | --- discard --- | ||
|
r9717 | y n y * | --- (3) --- | ||
|
r9716 | y n n * | --- ok --- | ||
|
r9717 | y y * * | --- (4) --- | ||
|
r9716 | |||
x = can't happen | ||||
* = don't-care | ||||
|
r9717 | 1 = abort: crosses branches (use 'hg merge' or 'hg update -c') | ||
2 = abort: crosses branches (use 'hg merge' to merge or | ||||
use 'hg update -C' to discard changes) | ||||
3 = abort: uncommitted local changes | ||||
4 = incompatible options (checked in commands.py) | ||||
|
r13162 | |||
Return the same tuple as applyupdates(). | ||||
|
r3315 | """ | ||
|
r2815 | |||
|
r9717 | onode = node | ||
|
r4917 | wlock = repo.wlock() | ||
|
r4915 | try: | ||
|
r6747 | wc = repo[None] | ||
|
r4915 | if node is None: | ||
# tip of current branch | ||||
try: | ||||
node = repo.branchtags()[wc.branch()] | ||||
except KeyError: | ||||
|
r5570 | if wc.branch() == "default": # no default branch! | ||
node = repo.lookup("tip") # update to tip | ||||
else: | ||||
raise util.Abort(_("branch %s not found") % wc.branch()) | ||||
|
r4915 | overwrite = force and not branchmerge | ||
pl = wc.parents() | ||||
|
r6747 | p1, p2 = pl[0], repo[node] | ||
|
r13874 | if ancestor: | ||
pa = repo[ancestor] | ||||
else: | ||||
pa = p1.ancestor(p2) | ||||
|
r4915 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) | ||
|
r3314 | |||
|
r4915 | ### check phase | ||
if not overwrite and len(pl) > 1: | ||||
raise util.Abort(_("outstanding uncommitted merges")) | ||||
|
r6375 | if branchmerge: | ||
if pa == p2: | ||||
|
r11417 | raise util.Abort(_("merging with a working directory ancestor" | ||
" has no effect")) | ||||
|
r6375 | elif pa == p1: | ||
|
r13561 | if p1.branch() == p2.branch(): | ||
|
r15619 | raise util.Abort(_("nothing to merge"), | ||
hint=_("use 'hg update' " | ||||
"or check 'hg heads'")) | ||||
|
r6375 | if not force and (wc.files() or wc.deleted()): | ||
|
r15619 | raise util.Abort(_("outstanding uncommitted changes"), | ||
hint=_("use 'hg status' to list changes")) | ||||
|
r13437 | for s in wc.substate: | ||
if wc.sub(s).dirty(): | ||||
raise util.Abort(_("outstanding uncommitted changes in " | ||||
"subrepository '%s'") % s) | ||||
|
r6375 | elif not overwrite: | ||
|
r12401 | if pa == p1 or pa == p2: # linear | ||
|
r6375 | pass # all good | ||
|
r14663 | elif wc.dirty(missing=True): | ||
|
r12681 | raise util.Abort(_("crosses branches (merge branches or use" | ||
" --clean to discard changes)")) | ||||
|
r9717 | elif onode is None: | ||
|
r14485 | raise util.Abort(_("crosses branches (merge branches or update" | ||
|
r12681 | " --check to force update)")) | ||
|
r6375 | else: | ||
|
r9717 | # Allow jumping branches if clean and specific rev given | ||
|
r6375 | overwrite = True | ||
|
r2814 | |||
|
r4915 | ### calculate phase | ||
action = [] | ||||
|
r11101 | wc.status(unknown=True) # prime cache | ||
|
r15538 | folding = not util.checkcase(repo.path) | ||
|
r4915 | if not force: | ||
|
r15538 | _checkunknown(wc, p2, folding) | ||
if folding: | ||||
|
r15673 | _checkcollision(p2, branchmerge and p1) | ||
|
r6269 | action += _forgetremoved(wc, p2, branchmerge) | ||
|
r4915 | action += manifestmerge(repo, wc, p2, pa, overwrite, partial) | ||
|
r2775 | |||
|
r4915 | ### apply phase | ||
|
r13550 | if not branchmerge: # just jump to the new rev | ||
|
r4915 | fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||
if not partial: | ||||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||||
|
r2775 | |||
|
r13322 | stats = applyupdates(repo, action, wc, p2, pa, overwrite) | ||
|
r2899 | |||
|
r4915 | if not partial: | ||
|
r10968 | repo.dirstate.setparents(fp1, fp2) | ||
|
r13550 | recordupdates(repo, action, branchmerge) | ||
|
r13561 | if not branchmerge: | ||
|
r4915 | repo.dirstate.setbranch(p2.branch()) | ||
finally: | ||||
|
r8109 | wlock.release() | ||
|
r10492 | |||
if not partial: | ||||
repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||||
return stats | ||||