merge.py
328 lines
| 11.9 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
Matt Mackall
|
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 * | ||||
Benoit Boissinot
|
r3018 | demandload(globals(), "errno util os tempfile") | ||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r2892 | def fmerge(f, local, other, ancestor): | ||
"""merge executable flags""" | ||||
a, b, c = ancestor.execf(f), local.execf(f), other.execf(f) | ||||
return ((a^b) | (a^c)) ^ a | ||||
Matt Mackall
|
r2775 | def merge3(repo, fn, my, other, p1, p2): | ||
"""perform a 3-way merge in the working directory""" | ||||
def temp(prefix, node): | ||||
pre = "%s~%s." % (os.path.basename(fn), prefix) | ||||
(fd, name) = tempfile.mkstemp(prefix=pre) | ||||
f = os.fdopen(fd, "wb") | ||||
repo.wwrite(fn, fl.read(node), f) | ||||
f.close() | ||||
return name | ||||
fl = repo.file(fn) | ||||
base = fl.ancestor(my, other) | ||||
a = repo.wjoin(fn) | ||||
b = temp("base", base) | ||||
c = temp("other", other) | ||||
repo.ui.note(_("resolving %s\n") % fn) | ||||
repo.ui.debug(_("file %s: my %s other %s ancestor %s\n") % | ||||
(fn, short(my), short(other), short(base))) | ||||
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, | ||||
environ={'HG_FILE': fn, | ||||
'HG_MY_NODE': p1, | ||||
'HG_OTHER_NODE': p2, | ||||
'HG_FILE_MY_NODE': hex(my), | ||||
'HG_FILE_OTHER_NODE': hex(other), | ||||
'HG_FILE_BASE_NODE': hex(base)}) | ||||
if r: | ||||
repo.ui.warn(_("merging %s failed!\n") % fn) | ||||
os.unlink(b) | ||||
os.unlink(c) | ||||
return r | ||||
Matt Mackall
|
r2811 | def update(repo, node, branchmerge=False, force=False, partial=None, | ||
Matt Mackall
|
r2815 | wlock=None, show_stats=True, remind=True): | ||
overwrite = force and not branchmerge | ||||
forcemerge = force and branchmerge | ||||
Matt Mackall
|
r2812 | |||
if not wlock: | ||||
wlock = repo.wlock() | ||||
Matt Mackall
|
r2814 | ### check phase | ||
Matt Mackall
|
r2775 | pl = repo.dirstate.parents() | ||
Matt Mackall
|
r2815 | if not overwrite and pl[1] != nullid: | ||
Matt Mackall
|
r2775 | raise util.Abort(_("outstanding uncommitted merges")) | ||
p1, p2 = pl[0], node | ||||
pa = repo.changelog.ancestor(p1, p2) | ||||
Matt Mackall
|
r2814 | |||
Matt Mackall
|
r2968 | # are we going backwards? | ||
backwards = (pa == p2) | ||||
Matt Mackall
|
r2814 | # is there a linear path from p1 to p2? | ||
linear_path = (pa == p1 or pa == p2) | ||||
if branchmerge and linear_path: | ||||
raise util.Abort(_("there is nothing to merge, just use " | ||||
"'hg update' or look at 'hg heads'")) | ||||
Matt Mackall
|
r2971 | if not linear_path and not (overwrite or branchmerge): | ||
Matt Mackall
|
r2815 | raise util.Abort(_("update spans branches, use 'hg merge' " | ||
Matt Mackall
|
r2814 | "or 'hg update -C' to lose changes")) | ||
Vadim Gelfer
|
r2875 | modified, added, removed, deleted, unknown = repo.status()[:5] | ||
Matt Mackall
|
r2814 | if branchmerge and not forcemerge: | ||
if modified or added or removed: | ||||
raise util.Abort(_("outstanding uncommitted changes")) | ||||
Matt Mackall
|
r2775 | m1n = repo.changelog.read(p1)[0] | ||
m2n = repo.changelog.read(p2)[0] | ||||
man = repo.manifest.ancestor(m1n, m2n) | ||||
Matt Mackall
|
r2975 | m1 = repo.manifest.read(m1n).copy() | ||
Matt Mackall
|
r2775 | m2 = repo.manifest.read(m2n).copy() | ||
ma = repo.manifest.read(man) | ||||
Matt Mackall
|
r2842 | if not force: | ||
Matt Mackall
|
r2775 | for f in unknown: | ||
if f in m2: | ||||
Matt Mackall
|
r2891 | if repo.file(f).cmp(m2[f], repo.wread(f)): | ||
Matt Mackall
|
r2775 | raise util.Abort(_("'%s' already exists in the working" | ||
" dir and differs from remote") % f) | ||||
# resolve the manifest to determine which files | ||||
# we care about merging | ||||
repo.ui.note(_("resolving manifests\n")) | ||||
Matt Mackall
|
r2815 | repo.ui.debug(_(" overwrite %s branchmerge %s partial %s linear %s\n") % | ||
Matt Mackall
|
r2896 | (overwrite, branchmerge, bool(partial), linear_path)) | ||
Matt Mackall
|
r2775 | repo.ui.debug(_(" ancestor %s local %s remote %s\n") % | ||
(short(man), short(m1n), short(m2n))) | ||||
Matt Mackall
|
r2977 | action = {} | ||
Matt Mackall
|
r2897 | forget = [] | ||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r2975 | # update m1 from working dir | ||
Matt Mackall
|
r2775 | umap = dict.fromkeys(unknown) | ||
for f in added + modified + unknown: | ||||
Matt Mackall
|
r2975 | m1[f] = m1.get(f, nullid) + "+" | ||
m1.set(f, util.is_exec(repo.wjoin(f), m1.execf(f))) | ||||
Matt Mackall
|
r2775 | |||
for f in deleted + removed: | ||||
Matt Mackall
|
r2976 | del m1[f] | ||
Matt Mackall
|
r2775 | |||
# 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. | ||||
Matt Mackall
|
r2897 | if linear_path and f not in m2: | ||
forget.append(f) | ||||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r2971 | if partial: | ||
Matt Mackall
|
r2975 | for f in m1.keys(): | ||
if not partial(f): del m1[f] | ||||
Matt Mackall
|
r2971 | for f in m2.keys(): | ||
if not partial(f): del m2[f] | ||||
Matt Mackall
|
r2775 | # Compare manifests | ||
Matt Mackall
|
r2975 | for f, n in m1.iteritems(): | ||
Matt Mackall
|
r2775 | if f in m2: | ||
Matt Mackall
|
r2969 | queued = 0 | ||
Matt Mackall
|
r2775 | |||
# are files different? | ||||
if n != m2[f]: | ||||
a = ma.get(f, nullid) | ||||
# are both different from the ancestor? | ||||
Matt Mackall
|
r2972 | if not overwrite and n != a and m2[f] != a: | ||
Matt Mackall
|
r2775 | repo.ui.debug(_(" %s versions differ, resolve\n") % f) | ||
Matt Mackall
|
r2977 | action[f] = (fmerge(f, m1, m2, ma), n[:20], m2[f]) | ||
Matt Mackall
|
r2969 | queued = 1 | ||
Matt Mackall
|
r2775 | # are we clobbering? | ||
# is remote's version newer? | ||||
Matt Mackall
|
r2968 | # or are we going back in time and clean? | ||
Matt Mackall
|
r2974 | elif overwrite or m2[f] != a or (backwards and not n[20:]): | ||
Matt Mackall
|
r2775 | repo.ui.debug(_(" remote %s is newer, get\n") % f) | ||
Matt Mackall
|
r2977 | action[f] = (m2.execf(f), m2[f], None) | ||
Matt Mackall
|
r2969 | queued = 1 | ||
Matt Mackall
|
r2775 | elif f in umap or f in added: | ||
# this unknown file is the same as the checkout | ||||
# we need to reset the dirstate if the file was added | ||||
Matt Mackall
|
r2977 | action[f] = (m2.execf(f), m2[f], None) | ||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r2969 | # do we still need to look at mode bits? | ||
Matt Mackall
|
r2975 | if not queued and m1.execf(f) != m2.execf(f): | ||
Matt Mackall
|
r2815 | if overwrite: | ||
Matt Mackall
|
r2775 | repo.ui.debug(_(" updating permissions for %s\n") % f) | ||
Matt Mackall
|
r2838 | util.set_exec(repo.wjoin(f), m2.execf(f)) | ||
Matt Mackall
|
r2775 | else: | ||
Benoit Boissinot
|
r3018 | mode = fmerge(f, m1, m2, ma) | ||
if mode != m1.execf(f): | ||||
Matt Mackall
|
r2775 | repo.ui.debug(_(" updating permissions for %s\n") | ||
% f) | ||||
util.set_exec(repo.wjoin(f), mode) | ||||
del m2[f] | ||||
elif f in ma: | ||||
if n != ma[f]: | ||||
r = _("d") | ||||
Matt Mackall
|
r2970 | if not overwrite: | ||
Matt Mackall
|
r2775 | r = repo.ui.prompt( | ||
(_(" local changed %s which remote deleted\n") % f) + | ||||
_("(k)eep or (d)elete?"), _("[kd]"), _("k")) | ||||
if r == _("d"): | ||||
Matt Mackall
|
r2978 | action[f] = (None, None, None) | ||
Matt Mackall
|
r2775 | else: | ||
repo.ui.debug(_("other deleted %s\n") % f) | ||||
Matt Mackall
|
r2978 | action[f] = (None, None, None) | ||
Matt Mackall
|
r2775 | else: | ||
# file is created on branch or in working directory | ||||
Matt Mackall
|
r2815 | if overwrite and f not in umap: | ||
Matt Mackall
|
r2775 | repo.ui.debug(_("remote deleted %s, clobbering\n") % f) | ||
Matt Mackall
|
r2978 | action[f] = (None, None, None) | ||
Matt Mackall
|
r2974 | elif not n[20:]: # same as parent | ||
Matt Mackall
|
r2968 | if backwards: | ||
Matt Mackall
|
r2775 | repo.ui.debug(_("remote deleted %s\n") % f) | ||
Matt Mackall
|
r2978 | action[f] = (None, None, None) | ||
Matt Mackall
|
r2775 | else: | ||
repo.ui.debug(_("local modified %s, keeping\n") % f) | ||||
else: | ||||
repo.ui.debug(_("working dir created %s, keeping\n") % f) | ||||
for f, n in m2.iteritems(): | ||||
if f[0] == "/": | ||||
continue | ||||
if f in ma and n != ma[f]: | ||||
r = _("k") | ||||
Matt Mackall
|
r2970 | if not overwrite: | ||
Matt Mackall
|
r2775 | r = repo.ui.prompt( | ||
(_("remote changed %s which local deleted\n") % f) + | ||||
_("(k)eep or (d)elete?"), _("[kd]"), _("k")) | ||||
if r == _("k"): | ||||
Matt Mackall
|
r2977 | action[f] = (m2.execf(f), n, None) | ||
Matt Mackall
|
r2775 | elif f not in ma: | ||
repo.ui.debug(_("remote created %s\n") % f) | ||||
Matt Mackall
|
r2977 | action[f] = (m2.execf(f), n, None) | ||
Matt Mackall
|
r2775 | else: | ||
Matt Mackall
|
r2968 | if overwrite or backwards: | ||
Matt Mackall
|
r2775 | repo.ui.debug(_("local deleted %s, recreating\n") % f) | ||
Matt Mackall
|
r2977 | action[f] = (m2.execf(f), n, None) | ||
Matt Mackall
|
r2775 | else: | ||
repo.ui.debug(_("local deleted %s\n") % f) | ||||
Matt Mackall
|
r2975 | del m1, m2, ma | ||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r2897 | ### apply phase | ||
Matt Mackall
|
r2815 | if linear_path or overwrite: | ||
Matt Mackall
|
r2775 | # we don't need to do any magic, just jump to the new rev | ||
p1, p2 = p2, nullid | ||||
xp1 = hex(p1) | ||||
xp2 = hex(p2) | ||||
if p2 == nullid: xxp2 = '' | ||||
else: xxp2 = xp2 | ||||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xxp2) | ||||
Matt Mackall
|
r2977 | # update files | ||
unresolved = [] | ||||
Matt Mackall
|
r2978 | updated, merged, removed = 0, 0, 0 | ||
Matt Mackall
|
r2977 | files = action.keys() | ||
Matt Mackall
|
r2775 | files.sort() | ||
for f in files: | ||||
Matt Mackall
|
r2977 | flag, my, other = action[f] | ||
Matt Mackall
|
r2775 | if f[0] == "/": | ||
continue | ||||
Matt Mackall
|
r2978 | if not my: | ||
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)) | ||||
removed +=1 | ||||
elif other: | ||||
Matt Mackall
|
r2977 | repo.ui.status(_("merging %s\n") % f) | ||
if merge3(repo, f, my, other, xp1, xp2): | ||||
unresolved.append(f) | ||||
Matt Mackall
|
r2978 | util.set_exec(repo.wjoin(f), flag) | ||
Matt Mackall
|
r2977 | merged += 1 | ||
else: | ||||
repo.ui.note(_("getting %s\n") % f) | ||||
t = repo.file(f).read(my) | ||||
repo.wwrite(f, t) | ||||
Matt Mackall
|
r2978 | util.set_exec(repo.wjoin(f), flag) | ||
Matt Mackall
|
r2977 | updated += 1 | ||
Matt Mackall
|
r2899 | |||
# update dirstate | ||||
if not partial: | ||||
repo.dirstate.setparents(p1, p2) | ||||
repo.dirstate.forget(forget) | ||||
Matt Mackall
|
r2977 | files = action.keys() | ||
Matt Mackall
|
r2899 | files.sort() | ||
for f in files: | ||||
Matt Mackall
|
r2977 | flag, my, other = action[f] | ||
Matt Mackall
|
r2978 | if not my: | ||
if branchmerge: | ||||
repo.dirstate.update([f], 'r') | ||||
else: | ||||
repo.dirstate.forget([f]) | ||||
elif not other: | ||||
Matt Mackall
|
r2977 | if branchmerge: | ||
repo.dirstate.update([f], 'n', st_mtime=-1) | ||||
else: | ||||
repo.dirstate.update([f], 'n') | ||||
Matt Mackall
|
r2775 | else: | ||
Matt Mackall
|
r2977 | if branchmerge: | ||
# We've done a branch merge, mark this file as merged | ||||
# so that we properly record the merger later | ||||
repo.dirstate.update([f], 'm') | ||||
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. | ||||
fl = repo.file(f) | ||||
f_len = fl.size(fl.rev(other)) | ||||
repo.dirstate.update([f], 'n', st_size=f_len, st_mtime=-1) | ||||
Matt Mackall
|
r2775 | |||
if show_stats: | ||||
Matt Mackall
|
r2977 | stats = ((updated, _("updated")), | ||
(merged - len(unresolved), _("merged")), | ||||
Matt Mackall
|
r2978 | (removed, _("removed")), | ||
Matt Mackall
|
r2813 | (len(unresolved), _("unresolved"))) | ||
Matt Mackall
|
r2775 | note = ", ".join([_("%d files %s") % s for s in stats]) | ||
repo.ui.status("%s\n" % note) | ||||
Matt Mackall
|
r2811 | if not partial: | ||
Matt Mackall
|
r2810 | if branchmerge: | ||
Matt Mackall
|
r2813 | if unresolved: | ||
Matt Mackall
|
r2775 | repo.ui.status(_("There are unresolved merges," | ||
" you can redo the full merge using:\n" | ||||
" hg update -C %s\n" | ||||
" hg merge %s\n" | ||||
% (repo.changelog.rev(p1), | ||||
repo.changelog.rev(p2)))) | ||||
Matt Mackall
|
r2803 | elif remind: | ||
Matt Mackall
|
r2775 | repo.ui.status(_("(branch merge, don't forget to commit)\n")) | ||
Matt Mackall
|
r2813 | elif unresolved: | ||
Matt Mackall
|
r2775 | repo.ui.status(_("There are unresolved merges with" | ||
" locally modified files.\n")) | ||||
Matt Mackall
|
r2813 | repo.hook('update', parent1=xp1, parent2=xxp2, error=len(unresolved)) | ||
return len(unresolved) | ||||
Matt Mackall
|
r2775 | |||