##// END OF EJS Templates
merge with mainline
merge with mainline

File last commit:

r3169:9e002614 default
r3171:70627786 merge default
Show More
merge.py
419 lines | 13.4 KiB | text/x-python | PythonLexer
Matt Mackall
Move merge code to its own module...
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
fix errors reported by pychecker...
r3018 demandload(globals(), "errno util os tempfile")
Matt Mackall
Move merge code to its own module...
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
merge: pull manifest checks and updates into separate functions
r3107 def checkunknown(repo, m2, status):
"""
check for collisions between unknown files and files in m2
"""
modified, added, removed, deleted, unknown = status[:5]
for f in unknown:
if f in m2:
if repo.file(f).cmp(m2[f], repo.wread(f)):
raise util.Abort(_("'%s' already exists in the working"
" dir and differs from remote") % f)
def workingmanifest(repo, man, status):
"""
Update manifest to correspond to the working directory
"""
Matt Mackall
findcopies: use dirstate rename information
r3155 copied = repo.dirstate.copies()
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 modified, added, removed, deleted, unknown = status[:5]
for i,l in (("a", added), ("m", modified), ("u", unknown)):
for f in l:
Matt Mackall
findcopies: use dirstate rename information
r3155 man[f] = man.get(copied.get(f, f), nullid) + i
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 man.set(f, util.is_exec(repo.wjoin(f), man.execf(f)))
for f in deleted + removed:
del man[f]
return man
def forgetremoved(m2, status):
"""
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.
"""
modified, added, removed, deleted, unknown = status[:5]
action = []
for f in deleted + removed:
if f not in m2:
action.append((f, "f"))
return action
Matt Mackall
Add core copy detection algorithm...
r3153 def nonoverlap(d1, d2):
"""
Return list of elements in d1 not in d2
"""
l = []
for d in d1:
if d not in d2:
l.append(d)
l.sort()
return l
def findold(fctx, limit):
"""
find files that path was copied from, back to linkrev limit
"""
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
def findcopies(repo, m1, m2, limit):
"""
Find moves and copies between m1 and m2 back to limit linkrev
"""
Matt Mackall
findcopies: shortcut for empty working dir
r3160 # avoid silly behavior for update from empty dir
if not m1:
return {}
Matt Mackall
findcopies: use dirstate rename information
r3155 dcopies = repo.dirstate.copies()
Matt Mackall
Add core copy detection algorithm...
r3153 copy = {}
match = {}
u1 = nonoverlap(m1, m2)
u2 = nonoverlap(m2, m1)
ctx = util.cachefunc(lambda f,n: repo.filectx(f, fileid=n[:20]))
def checkpair(c, f2, man):
''' check if an apparent pair actually matches '''
c2 = ctx(f2, man[f2])
ca = c.ancestor(c2)
if ca:
copy[c.path()] = f2
copy[f2] = c.path()
for f in u1:
Matt Mackall
findcopies: use dirstate rename information
r3155 c = ctx(dcopies.get(f, f), m1[f])
Matt Mackall
Add core copy detection algorithm...
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
Matt Mackall
merge: move partial filtering out of manifest merge
r3162 def filtermanifest(man, partial):
if partial:
for k in man.keys():
if not partial(k): del man[k]
def manifestmerge(ui, m1, m2, ma, overwrite, backwards):
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 """
Merge manifest m1 with m2 using ancestor ma and generate merge action list
"""
Matt Mackall
merge: simplify exec flag handling
r3118 def fmerge(f):
"""merge executable flags"""
a, b, c = ma.execf(f), m1.execf(f), m2.execf(f)
return ((a^b) | (a^c)) ^ a
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 action = []
Matt Mackall
merge: simplify actions with helper function
r3121 def act(msg, f, m, *args):
ui.debug(" %s: %s -> %s\n" % (f, msg, m))
action.append((f, m) + args)
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 # Compare manifests
for f, n in m1.iteritems():
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:
Matt Mackall
merge: simplify actions with helper function
r3121 act("versions differ", f, "m", fmerge(f), n[:20], m2[f])
Matt Mackall
merge: pull manifest comparison out into separate function
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:]):
Matt Mackall
merge: simplify actions with helper function
r3121 act("remote is newer", f, "g", m2.execf(f), m2[f])
Matt Mackall
merge: eliminate confusing queued variable
r3113 # local is newer, not overwrite, check mode bits
Matt Mackall
merge: simplify exec flag handling
r3118 elif fmerge(f) != m1.execf(f):
Matt Mackall
merge: simplify actions with helper function
r3121 act("update permissions", f, "e", m2.execf(f))
Matt Mackall
merge: eliminate confusing queued variable
r3113 # contents same, check mode bits
elif m1.execf(f) != m2.execf(f):
Matt Mackall
merge: simplify local created logic
r3120 if overwrite or fmerge(f) != m1.execf(f):
Matt Mackall
merge: simplify actions with helper function
r3121 act("update permissions", f, "e", m2.execf(f))
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 del m2[f]
elif f in ma:
Matt Mackall
merge: simplify tests for local changed/remote deleted
r3117 if n != ma[f] and not overwrite:
Matt Mackall
merge: simplify prompt code
r3119 if ui.prompt(
Matt Mackall
merge: simplify tests for local changed/remote deleted
r3117 (_(" local changed %s which remote deleted\n") % f) +
Matt Mackall
merge: simplify prompt code
r3119 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("d"):
Matt Mackall
merge: simplify actions with helper function
r3121 act("prompt delete", f, "r")
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 else:
Matt Mackall
merge: simplify actions with helper function
r3121 act("other deleted", f, "r")
Matt Mackall
merge: pull manifest comparison out into separate function
r3105 else:
# file is created on branch or in working directory
Matt Mackall
merge: simplify local created logic
r3120 if (overwrite and n[20:] != "u") or (backwards and not n[20:]):
Matt Mackall
merge: simplify actions with helper function
r3121 act("remote deleted", f, "r")
Matt Mackall
merge: pull manifest comparison out into separate function
r3105
for f, n in m2.iteritems():
Matt Mackall
merge: reorder tests on m2 items in manifestmerge
r3115 if f in ma:
Matt Mackall
merge: more simplification of m2 manifest scanning
r3116 if overwrite or backwards:
Matt Mackall
merge: simplify actions with helper function
r3121 act("recreating", f, "g", m2.execf(f), n)
Matt Mackall
merge: more simplification of m2 manifest scanning
r3116 elif n != ma[f]:
Matt Mackall
merge: simplify prompt code
r3119 if ui.prompt(
Matt Mackall
merge: more simplification of m2 manifest scanning
r3116 (_("remote changed %s which local deleted\n") % f) +
Matt Mackall
merge: simplify prompt code
r3119 _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("k"):
Matt Mackall
merge: simplify actions with helper function
r3121 act("prompt recreating", f, "g", m2.execf(f), n)
Matt Mackall
merge: reorder tests on m2 items in manifestmerge
r3115 else:
Matt Mackall
merge: simplify actions with helper function
r3121 act("remote created", f, "g", m2.execf(f), n)
Matt Mackall
merge: pull manifest comparison out into separate function
r3105
return action
Matt Mackall
merge: move apply and dirstate code into separate functions
r3111 def applyupdates(repo, action, xp1, xp2):
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))
removed +=1
elif m == "m": # merge
flag, my, other = a[2:]
repo.ui.status(_("merging %s\n") % f)
if merge3(repo, f, my, other, xp1, xp2):
unresolved += 1
util.set_exec(repo.wjoin(f), flag)
merged += 1
elif m == "g": # get
flag, node = a[2:]
repo.ui.note(_("getting %s\n") % f)
t = repo.file(f).read(node)
repo.wwrite(f, t)
util.set_exec(repo.wjoin(f), flag)
updated += 1
elif m == "e": # exec
flag = a[2:]
util.set_exec(repo.wjoin(f), flag)
return updated, merged, removed, unresolved
def recordupdates(repo, action, branchmerge):
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
flag, my, other = a[2:]
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
Merge: combine choose and moddirstate to partial
r2811 def update(repo, node, branchmerge=False, force=False, partial=None,
Matt Mackall
Merge: combine force and forcemerge arguments
r2815 wlock=None, show_stats=True, remind=True):
overwrite = force and not branchmerge
forcemerge = force and branchmerge
Matt Mackall
Refactor update locking slightly
r2812
if not wlock:
wlock = repo.wlock()
Matt Mackall
Merge: move most tests to the beginning
r2814 ### check phase
Matt Mackall
Move merge code to its own module...
r2775 pl = repo.dirstate.parents()
Matt Mackall
Merge: combine force and forcemerge arguments
r2815 if not overwrite and pl[1] != nullid:
Matt Mackall
Move merge code to its own module...
r2775 raise util.Abort(_("outstanding uncommitted merges"))
p1, p2 = pl[0], node
pa = repo.changelog.ancestor(p1, p2)
Matt Mackall
Merge: move most tests to the beginning
r2814
Matt Mackall
merge: add backwards variable
r2968 # are we going backwards?
backwards = (pa == p2)
Matt Mackall
Merge: move most tests to the beginning
r2814 # is there a linear path from p1 to p2?
Matt Mackall
merge: remove linear variable
r3110 if pa == p1 or pa == p2:
if branchmerge:
raise util.Abort(_("there is nothing to merge, just use "
"'hg update' or look at 'hg heads'"))
elif not (overwrite or branchmerge):
Matt Mackall
Merge: combine force and forcemerge arguments
r2815 raise util.Abort(_("update spans branches, use 'hg merge' "
Matt Mackall
Merge: move most tests to the beginning
r2814 "or 'hg update -C' to lose changes"))
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 status = repo.status()
modified, added, removed, deleted, unknown = status[:5]
Matt Mackall
Merge: move most tests to the beginning
r2814 if branchmerge and not forcemerge:
if modified or added or removed:
raise util.Abort(_("outstanding uncommitted changes"))
Matt Mackall
merge: use context code to retrieve manifests
r2979 m1 = repo.changectx(p1).manifest().copy()
m2 = repo.changectx(p2).manifest().copy()
ma = repo.changectx(pa).manifest()
Matt Mackall
Move merge code to its own module...
r2775
# resolve the manifest to determine which files
# we care about merging
repo.ui.note(_("resolving manifests\n"))
Matt Mackall
merge: remove linear variable
r3110 repo.ui.debug(_(" overwrite %s branchmerge %s partial %s\n") %
(overwrite, branchmerge, bool(partial)))
Matt Mackall
Move merge code to its own module...
r2775 repo.ui.debug(_(" ancestor %s local %s remote %s\n") %
Matt Mackall
merge: use context code to retrieve manifests
r2979 (short(p1), short(p2), short(pa)))
Matt Mackall
Move merge code to its own module...
r2775
Matt Mackall
merge: convert actions to list
r3100 action = []
Matt Mackall
findcopies: use dirstate rename information
r3155 copy = {}
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 m1 = workingmanifest(repo, m1, status)
Matt Mackall
merge: move partial filtering out of manifest merge
r3162 filtermanifest(m1, partial)
filtermanifest(m2, partial)
Matt Mackall
Move merge code to its own module...
r2775
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 if not force:
checkunknown(repo, m2, status)
Matt Mackall
merge: remove linear variable
r3110 if not branchmerge:
Matt Mackall
merge: pull manifest checks and updates into separate functions
r3107 action += forgetremoved(m2, status)
Matt Mackall
merge: move findcopies after workingmanifest
r3161 if not (backwards or overwrite):
copy = findcopies(repo, m1, m2, repo.changelog.rev(pa))
Matt Mackall
Add core copy detection algorithm...
r3153
Matt Mackall
merge: move partial filtering out of manifest merge
r3162 action += manifestmerge(repo.ui, m1, m2, ma, overwrite, backwards)
Matt Mackall
merge: eliminate mw manifestdict, do everything with m1
r2975 del m1, m2, ma
Matt Mackall
Move merge code to its own module...
r2775
Matt Mackall
merge: move forgets to the apply stage
r2897 ### apply phase
Matt Mackall
merge: remove linear variable
r3110 if not branchmerge:
Matt Mackall
Move merge code to its own module...
r2775 # we don't need to do any magic, just jump to the new rev
p1, p2 = p2, nullid
Matt Mackall
merge: simplify hook code
r3109 xp1, xp2 = hex(p1), hex(p2)
if p2 == nullid: xp2 = ''
Matt Mackall
Move merge code to its own module...
r2775
Matt Mackall
merge: simplify hook code
r3109 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
Matt Mackall
Move merge code to its own module...
r2775
Matt Mackall
merge: move apply and dirstate code into separate functions
r3111 updated, merged, removed, unresolved = applyupdates(repo, action, xp1, xp2)
Matt Mackall
merge: consolidate dirstate updates
r2899
# update dirstate
if not partial:
repo.dirstate.setparents(p1, p2)
Matt Mackall
merge: move apply and dirstate code into separate functions
r3111 recordupdates(repo, action, branchmerge)
Matt Mackall
Move merge code to its own module...
r2775
if show_stats:
Matt Mackall
merge: combine merge and get lists
r2977 stats = ((updated, _("updated")),
Matt Mackall
merge: make unresolved a counter
r2980 (merged - unresolved, _("merged")),
Matt Mackall
merge: add remove to the action hash
r2978 (removed, _("removed")),
Matt Mackall
merge: make unresolved a counter
r2980 (unresolved, _("unresolved")))
Matt Mackall
Move merge code to its own module...
r2775 note = ", ".join([_("%d files %s") % s for s in stats])
repo.ui.status("%s\n" % note)
Matt Mackall
Merge: combine choose and moddirstate to partial
r2811 if not partial:
Matt Mackall
Rename merge.allow -> merge.branchmerge
r2810 if branchmerge:
Matt Mackall
Merge: refactor err and failedmerge -> unresolved
r2813 if unresolved:
Matt Mackall
Move merge code to its own module...
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
Merge with crew
r2803 elif remind:
Matt Mackall
Move merge code to its own module...
r2775 repo.ui.status(_("(branch merge, don't forget to commit)\n"))
Matt Mackall
Merge: refactor err and failedmerge -> unresolved
r2813 elif unresolved:
Matt Mackall
Move merge code to its own module...
r2775 repo.ui.status(_("There are unresolved merges with"
" locally modified files.\n"))
Matt Mackall
merge: simplify hook code
r3109 repo.hook('update', parent1=xp1, parent2=xp2, error=unresolved)
Matt Mackall
merge: make unresolved a counter
r2980 return unresolved
Matt Mackall
Move merge code to its own module...
r2775