merge.py
420 lines
| 13.7 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
|
r2775 | # merge.py - directory-level update/merge handling for Mercurial | ||
# | ||||
# Copyright 2006 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
from node import * | ||||
from i18n import gettext as _ | ||||
from demandload import * | ||||
|
r3018 | demandload(globals(), "errno util os tempfile") | ||
|
r2775 | |||
|
r3309 | def filemerge(repo, fw, fo, wctx, mctx): | ||
|
r3211 | """perform a 3-way merge in the working directory | ||
|
r2775 | |||
|
r3315 | fw = filename in the working directory | ||
|
r3211 | fo = filename in other parent | ||
|
r3297 | wctx, mctx = working and merge changecontexts | ||
|
r3211 | """ | ||
def temp(prefix, ctx): | ||||
pre = "%s~%s." % (os.path.basename(ctx.path()), prefix) | ||||
|
r2775 | (fd, name) = tempfile.mkstemp(prefix=pre) | ||
f = os.fdopen(fd, "wb") | ||||
|
r3211 | repo.wwrite(ctx.path(), ctx.data(), f) | ||
|
r2775 | f.close() | ||
return name | ||||
|
r3299 | fcm = wctx.filectx(fw) | ||
fco = mctx.filectx(fo) | ||||
|
r3311 | |||
if not fco.cmp(fcm.data()): # files identical? | ||||
|
r3400 | return None | ||
|
r3311 | |||
|
r3211 | fca = fcm.ancestor(fco) | ||
if not fca: | ||||
|
r3578 | fca = repo.filectx(fw, fileid=nullrev) | ||
|
r3211 | a = repo.wjoin(fw) | ||
b = temp("base", fca) | ||||
c = temp("other", fco) | ||||
|
r2775 | |||
|
r3311 | if fw != fo: | ||
repo.ui.status(_("merging %s and %s\n") % (fw, fo)) | ||||
else: | ||||
repo.ui.status(_("merging %s\n") % fw) | ||||
|
r3211 | repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca)) | ||
|
r2775 | |||
cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge") | ||||
or "hgmerge") | ||||
r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root, | ||||
|
r3211 | environ={'HG_FILE': fw, | ||
|
r3297 | 'HG_MY_NODE': str(wctx.parents()[0]), | ||
'HG_OTHER_NODE': str(mctx)}) | ||||
|
r2775 | if r: | ||
|
r3211 | repo.ui.warn(_("merging %s failed!\n") % fw) | ||
|
r2775 | |||
os.unlink(b) | ||||
os.unlink(c) | ||||
return r | ||||
|
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()): | ||||
|
r3618 | raise util.Abort(_("untracked local file '%s' differs"\ | ||
" from remote version") % f) | ||||
|
r3107 | |||
|
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 nonoverlap(d1, d2, d3): | ||
"Return list of elements in d1 not in d2 or d3" | ||||
|
r3153 | |||
l = [] | ||||
for d in d1: | ||||
|
r3371 | if d not in d3 and d not in d2: | ||
|
r3153 | l.append(d) | ||
l.sort() | ||||
return l | ||||
def findold(fctx, limit): | ||||
|
r3315 | "find files that path was copied from, back to linkrev limit" | ||
|
r3153 | |||
old = {} | ||||
orig = fctx.path() | ||||
visit = [fctx] | ||||
while visit: | ||||
fc = visit.pop() | ||||
if fc.rev() < limit: | ||||
continue | ||||
if fc.path() != orig and fc.path() not in old: | ||||
old[fc.path()] = 1 | ||||
visit += fc.parents() | ||||
old = old.keys() | ||||
old.sort() | ||||
return old | ||||
|
r3371 | def findcopies(repo, m1, m2, ma, limit): | ||
|
r3153 | """ | ||
Find moves and copies between m1 and m2 back to limit linkrev | ||||
""" | ||||
|
r3371 | if not repo.ui.configbool("merge", "followcopies", True): | ||
|
r3249 | return {} | ||
|
r3160 | # avoid silly behavior for update from empty dir | ||
if not m1: | ||||
return {} | ||||
|
r3155 | dcopies = repo.dirstate.copies() | ||
|
r3153 | copy = {} | ||
match = {} | ||||
|
r3371 | u1 = nonoverlap(m1, m2, ma) | ||
u2 = nonoverlap(m2, m1, ma) | ||||
|
r3673 | ctx = util.cachefunc(lambda f, n: repo.filectx(f, fileid=n[:20])) | ||
|
r3153 | |||
def checkpair(c, f2, man): | ||||
''' check if an apparent pair actually matches ''' | ||||
c2 = ctx(f2, man[f2]) | ||||
ca = c.ancestor(c2) | ||||
|
r3674 | if c == ca or c2 == ca: | ||
return | ||||
|
r3251 | if ca and ca.path() == c.path() or ca.path() == c2.path(): | ||
|
r3153 | copy[c.path()] = f2 | ||
copy[f2] = c.path() | ||||
for f in u1: | ||||
|
r3155 | c = ctx(dcopies.get(f, f), m1[f]) | ||
|
r3153 | for of in findold(c, limit): | ||
if of in m2: | ||||
checkpair(c, of, m2) | ||||
else: | ||||
match.setdefault(of, []).append(f) | ||||
for f in u2: | ||||
c = ctx(f, m2[f]) | ||||
for of in findold(c, limit): | ||||
if of in m1: | ||||
checkpair(c, of, m1) | ||||
elif of in match: | ||||
for mf in match[of]: | ||||
checkpair(c, mf, m1) | ||||
return copy | ||||
|
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 = {} | ||||
|
r3295 | |||
|
r3249 | def fmerge(f, f2=None, fa=None): | ||
|
r3118 | """merge executable flags""" | ||
|
r3249 | if not f2: | ||
f2 = f | ||||
fa = f | ||||
a, b, c = ma.execf(fa), m1.execf(f), m2.execf(f2) | ||||
|
r3118 | return ((a^b) | (a^c)) ^ a | ||
|
r3307 | def act(msg, m, f, *args): | ||
|
r3295 | repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m)) | ||
|
r3121 | action.append((f, m) + args) | ||
|
r3295 | if not (backwards or overwrite): | ||
|
r3371 | copy = findcopies(repo, m1, m2, ma, pa.rev()) | ||
|
r3295 | |||
|
r3105 | # Compare manifests | ||
for f, n in m1.iteritems(): | ||||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
|
r3105 | if f in m2: | ||
# are files different? | ||||
if n != m2[f]: | ||||
a = ma.get(f, nullid) | ||||
# are both different from the ancestor? | ||||
if not overwrite and n != a and m2[f] != a: | ||||
|
r3308 | act("versions differ", "m", f, f, f, fmerge(f), False) | ||
|
r3105 | # are we clobbering? | ||
# is remote's version newer? | ||||
# or are we going back in time and clean? | ||||
elif overwrite or m2[f] != a or (backwards and not n[20:]): | ||||
|
r3307 | act("remote is newer", "g", f, m2.execf(f)) | ||
|
r3113 | # local is newer, not overwrite, check mode bits | ||
|
r3118 | elif fmerge(f) != m1.execf(f): | ||
|
r3307 | act("update permissions", "e", f, m2.execf(f)) | ||
|
r3113 | # contents same, check mode bits | ||
elif m1.execf(f) != m2.execf(f): | ||||
|
r3120 | if overwrite or fmerge(f) != m1.execf(f): | ||
|
r3307 | act("update permissions", "e", f, m2.execf(f)) | ||
|
r3249 | elif f in copy: | ||
f2 = copy[f] | ||||
if f in ma: # case 3,20 A/B/A | ||||
|
r3308 | act("remote moved", "m", f, f2, f2, fmerge(f, f2, f), True) | ||
|
r3249 | else: | ||
if f2 in m1: # case 2 A,B/B/B | ||||
|
r3308 | act("local copied", "m", | ||
|
r3307 | f, f2, f, fmerge(f, f2, f2), False) | ||
|
r3249 | else: # case 4,21 A/B/B | ||
|
r3308 | act("local moved", "m", | ||
|
r3307 | 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( | ||
|
r3117 | (_(" local changed %s which remote deleted\n") % f) + | ||
|
r3119 | _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("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 | ||||
|
r3249 | if f in copy: | ||
f2 = copy[f] | ||||
|
r3252 | if f2 not in m2: # already seen | ||
|
r3249 | continue | ||
# rename case 1, A/A,B/A | ||||
|
r3308 | act("remote copied", "m", f2, f, f, fmerge(f2, f, f2), False) | ||
|
r3249 | elif f in ma: | ||
|
r3116 | if overwrite or backwards: | ||
|
r3307 | act("recreating", "g", f, m2.execf(f)) | ||
|
r3116 | elif n != ma[f]: | ||
|
r3295 | if repo.ui.prompt( | ||
|
r3116 | (_("remote changed %s which local deleted\n") % f) + | ||
|
r3119 | _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("k"): | ||
|
r3307 | act("prompt recreating", "g", f, m2.execf(f)) | ||
|
r3115 | else: | ||
|
r3307 | act("remote created", "g", f, m2.execf(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() | ||||
for a in action: | ||||
f, m = a[:2] | ||||
if f[0] == "/": | ||||
continue | ||||
if m == "r": # remove | ||||
repo.ui.note(_("removing %s\n") % f) | ||||
util.audit_path(f) | ||||
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 | ||
|
r3303 | f2, fd, flag, move = a[2:] | ||
|
r3400 | r = filemerge(repo, f, f2, wctx, mctx) | ||
if r > 0: | ||||
|
r3249 | unresolved += 1 | ||
|
r3309 | else: | ||
|
r3400 | if r is None: | ||
updated += 1 | ||||
else: | ||||
merged += 1 | ||||
|
r3309 | if f != fd: | ||
repo.ui.debug(_("copying %s to %s\n") % (f, fd)) | ||||
repo.wwrite(fd, repo.wread(f)) | ||||
if move: | ||||
repo.ui.debug(_("removing %s\n") % f) | ||||
os.unlink(repo.wjoin(f)) | ||||
|
r3249 | util.set_exec(repo.wjoin(fd), flag) | ||
|
r3111 | elif m == "g": # get | ||
|
r3306 | flag = a[2] | ||
|
r3111 | repo.ui.note(_("getting %s\n") % f) | ||
|
r3303 | t = mctx.filectx(f).data() | ||
|
r3111 | repo.wwrite(f, t) | ||
util.set_exec(repo.wjoin(f), flag) | ||||
updated += 1 | ||||
elif m == "e": # exec | ||||
|
r3303 | flag = a[2] | ||
|
r3111 | util.set_exec(repo.wjoin(f), flag) | ||
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: | ||||
repo.dirstate.update([f], 'r') | ||||
else: | ||||
repo.dirstate.forget([f]) | ||||
elif m == "f": # forget | ||||
repo.dirstate.forget([f]) | ||||
elif m == "g": # get | ||||
if branchmerge: | ||||
repo.dirstate.update([f], 'n', st_mtime=-1) | ||||
else: | ||||
repo.dirstate.update([f], 'n') | ||||
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 | ||||
repo.dirstate.update([fd], 'm') | ||||
|
r3372 | if f != f2: # copy/rename | ||
if move: | ||||
repo.dirstate.update([f], 'r') | ||||
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. | ||||
|
r3372 | repo.dirstate.update([fd], 'n', st_size=-1, st_mtime=-1) | ||
|
r3308 | if move: | ||
|
r3372 | repo.dirstate.forget([f]) | ||
|
r3111 | |||
|
r3316 | def update(repo, node, branchmerge, force, partial, wlock): | ||
|
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) | ||||
wlock = working dir lock, if already held | ||||
""" | ||||
|
r2815 | |||
|
r2812 | if not wlock: | ||
wlock = repo.wlock() | ||||
|
r3314 | overwrite = force and not branchmerge | ||
forcemerge = force and branchmerge | ||||
|
r3218 | wc = repo.workingctx() | ||
pl = wc.parents() | ||||
|
r3314 | p1, p2 = pl[0], repo.changectx(node) | ||
pa = p1.ancestor(p2) | ||||
fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) | ||||
### check phase | ||||
|
r3167 | if not overwrite and len(pl) > 1: | ||
|
r2775 | raise util.Abort(_("outstanding uncommitted merges")) | ||
|
r3314 | if pa == p1 or pa == p2: # is there a linear path from p1 to p2? | ||
|
r3110 | if branchmerge: | ||
raise util.Abort(_("there is nothing to merge, just use " | ||||
"'hg update' or look at 'hg heads'")) | ||||
|
r3592 | elif not (overwrite or branchmerge): | ||
raise util.Abort(_("update spans branches, use 'hg merge' " | ||||
"or 'hg update -C' to lose changes")) | ||||
|
r2814 | if branchmerge and not forcemerge: | ||
|
r3581 | if wc.files(): | ||
|
r2814 | raise util.Abort(_("outstanding uncommitted changes")) | ||
|
r3314 | ### calculate phase | ||
|
r3100 | action = [] | ||
|
r3107 | if not force: | ||
|
r3312 | checkunknown(wc, p2) | ||
|
r3110 | if not branchmerge: | ||
|
r3312 | action += forgetremoved(wc, p2) | ||
|
r3295 | action += manifestmerge(repo, wc, p2, pa, overwrite, partial) | ||
|
r2775 | |||
|
r2897 | ### apply phase | ||
|
r3314 | if not branchmerge: # just jump to the new rev | ||
fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||||
|
r3296 | if not partial: | ||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||||
|
r2775 | |||
|
r3316 | stats = applyupdates(repo, action, wc, p2) | ||
|
r2899 | |||
|
r2811 | if not partial: | ||
|
r3372 | recordupdates(repo, action, branchmerge) | ||
|
r3314 | repo.dirstate.setparents(fp1, fp2) | ||
|
r3316 | repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||
|
r3441 | if not branchmerge: | ||
|
r3442 | repo.opener("branch", "w").write(p2.branch() + "\n") | ||
|
r3314 | |||
|
r3316 | return stats | ||
|
r2775 | |||