merge.py
632 lines
| 21.1 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 | # | ||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r6211 | from node import nullid, nullrev | ||
|
r3891 | from i18n import _ | ||
|
r6003 | import errno, util, os, heapq, filemerge | ||
|
r2775 | |||
|
r3312 | def checkunknown(wctx, mctx): | ||
|
r3315 | "check for collisions between unknown files and files in mctx" | ||
|
r3312 | man = mctx.manifest() | ||
|
r3218 | for f in wctx.unknown(): | ||
|
r3312 | if f in man: | ||
if mctx.filectx(f).cmp(wctx.filectx(f).data()): | ||||
|
r5670 | raise util.Abort(_("untracked file in working directory differs" | ||
" from file in requested revision: '%s'") | ||||
% f) | ||||
|
r3107 | |||
|
r3785 | def checkcollision(mctx): | ||
"check for case folding collisions in the destination context" | ||||
folded = {} | ||||
for fn in mctx.manifest(): | ||||
fold = fn.lower() | ||||
if fold in folded: | ||||
raise util.Abort(_("case-folding collision between %s and %s") | ||||
% (fn, folded[fold])) | ||||
folded[fold] = fn | ||||
|
r3312 | def forgetremoved(wctx, mctx): | ||
|
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. | ||||
""" | ||||
action = [] | ||||
|
r3312 | man = mctx.manifest() | ||
|
r3218 | for f in wctx.deleted() + wctx.removed(): | ||
|
r3312 | if f not in man: | ||
|
r3107 | action.append((f, "f")) | ||
return action | ||||
|
r3371 | def findcopies(repo, m1, m2, ma, limit): | ||
|
r3153 | """ | ||
Find moves and copies between m1 and m2 back to limit linkrev | ||||
""" | ||||
|
r4400 | def nonoverlap(d1, d2, d3): | ||
"Return list of elements in d1 not in d2 or d3" | ||||
l = [d for d in d1 if d not in d3 and d not in d2] | ||||
l.sort() | ||||
return l | ||||
|
r4397 | def dirname(f): | ||
s = f.rfind("/") | ||||
if s == -1: | ||||
return "" | ||||
return f[:s] | ||||
def dirs(files): | ||||
d = {} | ||||
for f in files: | ||||
f = dirname(f) | ||||
while f not in d: | ||||
d[f] = True | ||||
f = dirname(f) | ||||
return d | ||||
|
r4416 | wctx = repo.workingctx() | ||
def makectx(f, n): | ||||
if len(n) == 20: | ||||
return repo.filectx(f, fileid=n) | ||||
return wctx.filectx(f) | ||||
ctx = util.cachefunc(makectx) | ||||
|
r3732 | def findold(fctx): | ||
"find files that path was copied from, back to linkrev limit" | ||||
old = {} | ||||
|
r4350 | seen = {} | ||
|
r3732 | orig = fctx.path() | ||
visit = [fctx] | ||||
while visit: | ||||
fc = visit.pop() | ||||
|
r4350 | s = str(fc) | ||
if s in seen: | ||||
continue | ||||
seen[s] = 1 | ||||
|
r3875 | if fc.path() != orig and fc.path() not in old: | ||
old[fc.path()] = 1 | ||||
|
r3732 | if fc.rev() < limit: | ||
continue | ||||
visit += fc.parents() | ||||
old = old.keys() | ||||
old.sort() | ||||
return old | ||||
|
r4400 | copy = {} | ||
fullcopy = {} | ||||
|
r4674 | diverge = {} | ||
|
r3732 | |||
|
r4884 | def checkcopies(c, man, aman): | ||
|
r3732 | '''check possible copies for filectx c''' | ||
for of in findold(c): | ||||
|
r4674 | fullcopy[c.path()] = of # remember for dir rename detection | ||
|
r4396 | if of not in man: # original file not in other manifest? | ||
|
r4674 | if of in ma: | ||
diverge.setdefault(of, []).append(c.path()) | ||||
|
r4304 | continue | ||
|
r4884 | # if the original file is unchanged on the other branch, | ||
# no merge needed | ||||
if man[of] == aman.get(of): | ||||
continue | ||||
|
r3732 | c2 = ctx(of, man[of]) | ||
ca = c.ancestor(c2) | ||||
|
r4396 | if not ca: # unrelated? | ||
|
r4304 | continue | ||
|
r4396 | # named changed on only one side? | ||
|
r3732 | if ca.path() == c.path() or ca.path() == c2.path(): | ||
|
r5433 | if c == ca and c2 == ca: # no merge needed, ignore copy | ||
|
r4304 | continue | ||
|
r3732 | copy[c.path()] = of | ||
|
r3371 | if not repo.ui.configbool("merge", "followcopies", True): | ||
|
r4674 | return {}, {} | ||
|
r3249 | |||
|
r3160 | # avoid silly behavior for update from empty dir | ||
|
r3731 | if not m1 or not m2 or not ma: | ||
|
r4674 | return {}, {} | ||
|
r3160 | |||
|
r5371 | repo.ui.debug(_(" searching for copies back to rev %d\n") % limit) | ||
|
r3371 | u1 = nonoverlap(m1, m2, ma) | ||
u2 = nonoverlap(m2, m1, ma) | ||||
|
r3153 | |||
|
r5371 | if u1: | ||
repo.ui.debug(_(" unmatched files in local:\n %s\n") | ||||
% "\n ".join(u1)) | ||||
if u2: | ||||
repo.ui.debug(_(" unmatched files in other:\n %s\n") | ||||
% "\n ".join(u2)) | ||||
|
r3153 | for f in u1: | ||
|
r4884 | checkcopies(ctx(f, m1[f]), m2, ma) | ||
|
r3153 | |||
for f in u2: | ||||
|
r4884 | checkcopies(ctx(f, m2[f]), m1, ma) | ||
|
r3153 | |||
|
r5857 | diverge2 = {} | ||
|
r4674 | for of, fl in diverge.items(): | ||
|
r5857 | if len(fl) == 1: | ||
del diverge[of] # not actually divergent | ||||
else: | ||||
diverge2.update(dict.fromkeys(fl)) # reverse map for below | ||||
|
r4674 | |||
|
r5371 | if fullcopy: | ||
repo.ui.debug(_(" all copies found (* = to merge, ! = divergent):\n")) | ||||
for f in fullcopy: | ||||
note = "" | ||||
if f in copy: note += "*" | ||||
|
r5857 | if f in diverge2: note += "!" | ||
|
r5371 | repo.ui.debug(_(" %s -> %s %s\n") % (f, fullcopy[f], note)) | ||
|
r5857 | del diverge2 | ||
|
r3733 | if not fullcopy or not repo.ui.configbool("merge", "followdirs", True): | ||
|
r4674 | return copy, diverge | ||
|
r3733 | |||
|
r5371 | repo.ui.debug(_(" checking for directory renames\n")) | ||
|
r3733 | # generate a directory move map | ||
d1, d2 = dirs(m1), dirs(m2) | ||||
invalid = {} | ||||
dirmove = {} | ||||
|
r4396 | # examine each file copy for a potential directory move, which is | ||
# when all the files in a directory are moved to a new directory | ||||
|
r3733 | for dst, src in fullcopy.items(): | ||
|
r4397 | dsrc, ddst = dirname(src), dirname(dst) | ||
|
r3733 | if dsrc in invalid: | ||
|
r4396 | # already seen to be uninteresting | ||
|
r3733 | continue | ||
|
r4396 | elif dsrc in d1 and ddst in d1: | ||
# directory wasn't entirely moved locally | ||||
invalid[dsrc] = True | ||||
elif dsrc in d2 and ddst in d2: | ||||
# directory wasn't entirely moved remotely | ||||
|
r3733 | invalid[dsrc] = True | ||
elif dsrc in dirmove and dirmove[dsrc] != ddst: | ||||
|
r4396 | # files from the same directory moved to two different places | ||
|
r3733 | invalid[dsrc] = True | ||
else: | ||||
|
r4396 | # looks good so far | ||
|
r4115 | dirmove[dsrc + "/"] = ddst + "/" | ||
|
r3733 | |||
|
r4398 | for i in invalid: | ||
if i in dirmove: | ||||
del dirmove[i] | ||||
|
r3733 | del d1, d2, invalid | ||
if not dirmove: | ||||
|
r4674 | return copy, diverge | ||
|
r3733 | |||
|
r5371 | for d in dirmove: | ||
repo.ui.debug(_(" dir %s -> %s\n") % (d, dirmove[d])) | ||||
|
r4396 | # check unaccounted nonoverlapping files against directory moves | ||
|
r3733 | for f in u1 + u2: | ||
if f not in fullcopy: | ||||
|
r4115 | for d in dirmove: | ||
if f.startswith(d): | ||||
|
r4396 | # new file added in a directory that was moved, move it | ||
|
r4115 | copy[f] = dirmove[d] + f[len(d):] | ||
|
r5371 | repo.ui.debug(_(" file %s -> %s\n") % (f, copy[f])) | ||
|
r4115 | break | ||
|
r3733 | |||
|
r4674 | return copy, diverge | ||
|
r3153 | |||
|
r5096 | def symmetricdifference(repo, rev1, rev2): | ||
"""symmetric difference of the sets of ancestors of rev1 and rev2 | ||||
|
r5433 | |||
|
r5096 | I.e. revisions that are ancestors of rev1 or rev2, but not both. | ||
""" | ||||
# basic idea: | ||||
# - mark rev1 and rev2 with different colors | ||||
# - walk the graph in topological order with the help of a heap; | ||||
# for each revision r: | ||||
# - if r has only one color, we want to return it | ||||
# - add colors[r] to its parents | ||||
# | ||||
# We keep track of the number of revisions in the heap that | ||||
# we may be interested in. We stop walking the graph as soon | ||||
# as this number reaches 0. | ||||
WHITE = 1 | ||||
BLACK = 2 | ||||
ALLCOLORS = WHITE | BLACK | ||||
colors = {rev1: WHITE, rev2: BLACK} | ||||
cl = repo.changelog | ||||
visit = [-rev1, -rev2] | ||||
heapq.heapify(visit) | ||||
n_wanted = len(visit) | ||||
ret = [] | ||||
while n_wanted: | ||||
r = -heapq.heappop(visit) | ||||
wanted = colors[r] != ALLCOLORS | ||||
n_wanted -= wanted | ||||
if wanted: | ||||
ret.append(r) | ||||
for p in cl.parentrevs(r): | ||||
if p == nullrev: | ||||
continue | ||||
if p not in colors: | ||||
# first time we see p; add it to visit | ||||
n_wanted += wanted | ||||
colors[p] = colors[r] | ||||
heapq.heappush(visit, -p) | ||||
elif colors[p] != ALLCOLORS and colors[p] != colors[r]: | ||||
# at first we thought we wanted p, but now | ||||
# we know we don't really want it | ||||
n_wanted -= 1 | ||||
colors[p] |= colors[r] | ||||
del colors[r] | ||||
return ret | ||||
|
r3295 | def manifestmerge(repo, p1, p2, pa, overwrite, partial): | ||
|
r3105 | """ | ||
|
r3315 | Merge p1 and p2 with ancestor ma and generate merge action list | ||
overwrite = whether we clobber working files | ||||
partial = function to filter file lists | ||||
|
r3105 | """ | ||
|
r3314 | repo.ui.note(_("resolving manifests\n")) | ||
repo.ui.debug(_(" overwrite %s partial %s\n") % (overwrite, bool(partial))) | ||||
repo.ui.debug(_(" ancestor %s local %s remote %s\n") % (pa, p1, p2)) | ||||
|
r3295 | m1 = p1.manifest() | ||
m2 = p2.manifest() | ||||
ma = pa.manifest() | ||||
backwards = (pa == p2) | ||||
|
r3314 | action = [] | ||
copy = {} | ||||
|
r4674 | diverge = {} | ||
|
r3295 | |||
|
r3249 | def fmerge(f, f2=None, fa=None): | ||
|
r4007 | """merge flags""" | ||
|
r3249 | if not f2: | ||
f2 = f | ||||
fa = f | ||||
|
r5708 | a, m, n = ma.flags(fa), m1.flags(f), m2.flags(f2) | ||
if m == n: # flags agree | ||||
return m # unchanged | ||||
if m and n: # flags are set but don't agree | ||||
if not a: # both differ from parent | ||||
r = repo.ui.prompt( | ||||
_(" conflicting flags for %s\n" | ||||
"(n)one, e(x)ec or sym(l)ink?") % f, "[nxl]", "n") | ||||
return r != "n" and r or '' | ||||
if m == a: | ||||
return n # changed from m to n | ||||
return m # changed from n to m | ||||
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) | ||
|
r3731 | if not (backwards or overwrite): | ||
|
r5096 | rev1 = p1.rev() | ||
if rev1 is None: | ||||
# p1 is a workingctx | ||||
rev1 = p1.parents()[0].rev() | ||||
limit = min(symmetricdifference(repo, rev1, p2.rev())) | ||||
copy, diverge = findcopies(repo, m1, m2, ma, limit) | ||||
|
r4674 | |||
for of, fl in diverge.items(): | ||||
act("divergent renames", "dr", of, fl) | ||||
|
r3730 | copied = dict.fromkeys(copy.values()) | ||
|
r3295 | |||
|
r3105 | # Compare manifests | ||
for f, n in m1.iteritems(): | ||||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
|
r3105 | if f in m2: | ||
|
r5708 | if overwrite or backwards: | ||
rflags = m2.flags(f) | ||||
else: | ||||
rflags = fmerge(f) | ||||
|
r3105 | # are files different? | ||
if n != m2[f]: | ||||
a = ma.get(f, nullid) | ||||
|
r5700 | # are we clobbering? | ||
if overwrite: | ||||
|
r5708 | act("clobbering", "g", f, rflags) | ||
|
r5700 | # or are we going back in time and clean? | ||
elif backwards and not n[20:]: | ||||
|
r5708 | act("reverting", "g", f, rflags) | ||
|
r3105 | # are both different from the ancestor? | ||
|
r5700 | elif n != a and m2[f] != a: | ||
|
r5708 | act("versions differ", "m", f, f, f, rflags, False) | ||
|
r3105 | # is remote's version newer? | ||
|
r5700 | elif m2[f] != a: | ||
|
r5708 | act("remote is newer", "g", f, rflags) | ||
|
r3113 | # local is newer, not overwrite, check mode bits | ||
|
r5708 | elif m1.flags(f) != rflags: | ||
act("update permissions", "e", f, rflags) | ||||
|
r3113 | # contents same, check mode bits | ||
|
r5708 | elif m1.flags(f) != rflags: | ||
act("update permissions", "e", f, rflags) | ||||
|
r3730 | elif f in copied: | ||
continue | ||||
|
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)) | ||
|
r3733 | elif f2 in m1: # case 2 A,B/B/B | ||
|
r3730 | act("local copied to " + f2, "m", | ||
f, f2, f, fmerge(f, f2, f2), False) | ||||
else: # case 4,21 A/B/B | ||||
act("local moved to " + f2, "m", | ||||
f, f2, f, fmerge(f, f2, f2), False) | ||||
|
r3105 | elif f in ma: | ||
|
r3117 | if n != ma[f] and not overwrite: | ||
|
r3295 | if repo.ui.prompt( | ||
|
r5670 | _(" local changed %s which remote deleted\n" | ||
"use (c)hanged version or (d)elete?") % f, | ||||
_("[cd]"), _("c")) == _("d"): | ||||
|
r3307 | act("prompt delete", "r", f) | ||
|
r3105 | else: | ||
|
r3307 | act("other deleted", "r", f) | ||
|
r3105 | else: | ||
# file is created on branch or in working directory | ||||
|
r3120 | if (overwrite and n[20:] != "u") or (backwards and not n[20:]): | ||
|
r3307 | act("remote deleted", "r", f) | ||
|
r3105 | |||
for f, n in m2.iteritems(): | ||||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
if f in m1: | ||||
continue | ||||
|
r3729 | if f in copied: | ||
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) | ||||
|
r3249 | elif f in ma: | ||
|
r3116 | if overwrite or backwards: | ||
|
r4007 | act("recreating", "g", f, m2.flags(f)) | ||
|
r3116 | elif n != ma[f]: | ||
|
r3295 | if repo.ui.prompt( | ||
|
r5670 | _("remote changed %s which local deleted\n" | ||
"use (c)hanged version or leave (d)eleted?") % f, | ||||
_("[cd]"), _("c")) == _("c"): | ||||
|
r4007 | act("prompt recreating", "g", f, m2.flags(f)) | ||
|
r3115 | else: | ||
|
r4007 | act("remote created", "g", f, m2.flags(f)) | ||
|
r3105 | |||
return action | ||||
|
r3297 | def applyupdates(repo, action, wctx, mctx): | ||
|
r3315 | "apply the merge action list to the working directory" | ||
|
r3111 | updated, merged, removed, unresolved = 0, 0, 0, 0 | ||
action.sort() | ||||
|
r5042 | # prescan for copy/renames | ||
for a in action: | ||||
f, m = a[:2] | ||||
if m == 'm': # merge | ||||
f2, fd, flags, move = a[2:] | ||||
if f != fd: | ||||
repo.ui.debug(_("copying %s to %s\n") % (f, fd)) | ||||
repo.wwrite(fd, repo.wread(f), flags) | ||||
|
r5158 | audit_path = util.path_auditor(repo.root) | ||
|
r3111 | for a in action: | ||
f, m = a[:2] | ||||
|
r3733 | if f and f[0] == "/": | ||
|
r3111 | continue | ||
if m == "r": # remove | ||||
repo.ui.note(_("removing %s\n") % f) | ||||
|
r5158 | audit_path(f) | ||
|
r3111 | try: | ||
util.unlink(repo.wjoin(f)) | ||||
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 | ||
|
r4007 | f2, fd, flags, move = a[2:] | ||
|
r6003 | r = filemerge.filemerge(repo, f, fd, f2, wctx, mctx) | ||
|
r3400 | if r > 0: | ||
|
r3249 | unresolved += 1 | ||
|
r3309 | else: | ||
|
r3400 | if r is None: | ||
updated += 1 | ||||
else: | ||||
merged += 1 | ||||
|
r5704 | util.set_flags(repo.wjoin(fd), flags) | ||
|
r5059 | if f != fd and move and util.lexists(repo.wjoin(f)): | ||
|
r5042 | repo.ui.debug(_("removing %s\n") % f) | ||
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) | ||
|
r3111 | updated += 1 | ||
|
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)) | ||||
t = wctx.filectx(f).data() | ||||
|
r4007 | repo.wwrite(fd, t, flags) | ||
|
r3733 | util.unlink(repo.wjoin(f)) | ||
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] | ||||
repo.ui.warn("warning: detected divergent renames of %s to:\n" % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
|
r3111 | elif m == "e": # exec | ||
|
r4007 | flags = a[2] | ||
|
r5704 | util.set_flags(repo.wjoin(f), flags) | ||
|
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: | ||
|
r4904 | repo.dirstate.forget(f) | ||
|
r3111 | elif m == "f": # forget | ||
|
r4904 | repo.dirstate.forget(f) | ||
|
r4997 | elif m in "ge": # get or exec change | ||
|
r3111 | if branchmerge: | ||
|
r4904 | repo.dirstate.normaldirty(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. | ||||
|
r5210 | repo.dirstate.normallookup(fd) | ||
|
r3308 | if move: | ||
|
r4904 | repo.dirstate.forget(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: | ||
|
r4904 | repo.dirstate.forget(f) | ||
|
r3111 | |||
|
r4917 | def update(repo, node, branchmerge, force, partial): | ||
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
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) | ||||
""" | ||||
|
r2815 | |||
|
r4917 | wlock = repo.wlock() | ||
|
r4915 | try: | ||
wc = repo.workingctx() | ||||
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 | ||
forcemerge = force and branchmerge | ||||
pl = wc.parents() | ||||
p1, p2 = pl[0], repo.changectx(node) | ||||
pa = p1.ancestor(p2) | ||||
fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) | ||||
fastforward = False | ||||
|
r3314 | |||
|
r4915 | ### check phase | ||
if not overwrite and len(pl) > 1: | ||||
raise util.Abort(_("outstanding uncommitted merges")) | ||||
if pa == p1 or pa == p2: # is there a linear path from p1 to p2? | ||||
if branchmerge: | ||||
if p1.branch() != p2.branch() and pa != p2: | ||||
fastforward = True | ||||
else: | ||||
raise util.Abort(_("there is nothing to merge, just use " | ||||
"'hg update' or look at 'hg heads'")) | ||||
elif not (overwrite or branchmerge): | ||||
raise util.Abort(_("update spans branches, use 'hg merge' " | ||||
"or 'hg update -C' to lose changes")) | ||||
if branchmerge and not forcemerge: | ||||
if wc.files(): | ||||
raise util.Abort(_("outstanding uncommitted changes")) | ||||
|
r2814 | |||
|
r4915 | ### calculate phase | ||
action = [] | ||||
if not force: | ||||
checkunknown(wc, p2) | ||||
if not util.checkfolding(repo.path): | ||||
checkcollision(p2) | ||||
if not branchmerge: | ||||
action += forgetremoved(wc, p2) | ||||
action += manifestmerge(repo, wc, p2, pa, overwrite, partial) | ||||
|
r2775 | |||
|
r4915 | ### apply phase | ||
if not branchmerge: # just jump to the new rev | ||||
fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||||
if not partial: | ||||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||||
|
r2775 | |||
|
r4915 | stats = applyupdates(repo, action, wc, p2) | ||
|
r2899 | |||
|
r4915 | if not partial: | ||
recordupdates(repo, action, branchmerge) | ||||
repo.dirstate.setparents(fp1, fp2) | ||||
if not branchmerge and not fastforward: | ||||
repo.dirstate.setbranch(p2.branch()) | ||||
repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||||
|
r3314 | |||
|
r4915 | return stats | ||
finally: | ||||
del wlock | ||||