2006-08-04 00:24:41 +04:00
|
|
|
# merge.py - directory-level update/merge handling for Mercurial
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
|
2006-08-04 00:24:41 +04:00
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms
|
|
|
|
# of the GNU General Public License, incorporated herein by reference.
|
|
|
|
|
|
|
|
from node import *
|
2006-12-15 05:25:19 +03:00
|
|
|
from i18n import _
|
2007-08-12 19:15:10 +04:00
|
|
|
import errno, util, os, tempfile, context, heapq
|
2006-08-04 00:24:41 +04:00
|
|
|
|
2007-08-01 21:33:12 +04:00
|
|
|
def filemerge(repo, fw, fd, fo, wctx, mctx):
|
2006-10-02 21:04:48 +04:00
|
|
|
"""perform a 3-way merge in the working directory
|
|
|
|
|
2007-08-01 21:33:12 +04:00
|
|
|
fw = original filename in the working directory
|
|
|
|
fd = destination filename in the working directory
|
2006-10-02 21:04:48 +04:00
|
|
|
fo = filename in other parent
|
2006-10-10 05:32:03 +04:00
|
|
|
wctx, mctx = working and merge changecontexts
|
2006-10-02 21:04:48 +04:00
|
|
|
"""
|
2006-08-04 00:24:41 +04:00
|
|
|
|
2006-10-02 21:04:48 +04:00
|
|
|
def temp(prefix, ctx):
|
|
|
|
pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
|
2006-08-04 00:24:41 +04:00
|
|
|
(fd, name) = tempfile.mkstemp(prefix=pre)
|
2006-12-30 05:04:31 +03:00
|
|
|
data = repo.wwritedata(ctx.path(), ctx.data())
|
2006-08-04 00:24:41 +04:00
|
|
|
f = os.fdopen(fd, "wb")
|
2006-12-30 05:04:31 +03:00
|
|
|
f.write(data)
|
2006-08-04 00:24:41 +04:00
|
|
|
f.close()
|
|
|
|
return name
|
|
|
|
|
2006-10-10 06:27:04 +04:00
|
|
|
fcm = wctx.filectx(fw)
|
2007-08-01 21:33:12 +04:00
|
|
|
fcmdata = wctx.filectx(fd).data()
|
2006-10-10 06:27:04 +04:00
|
|
|
fco = mctx.filectx(fo)
|
2006-10-10 10:16:06 +04:00
|
|
|
|
2007-08-01 21:33:12 +04:00
|
|
|
if not fco.cmp(fcmdata): # files identical?
|
2006-10-16 01:18:09 +04:00
|
|
|
return None
|
2006-10-10 10:16:06 +04:00
|
|
|
|
2006-10-02 21:04:48 +04:00
|
|
|
fca = fcm.ancestor(fco)
|
|
|
|
if not fca:
|
2006-10-29 12:53:51 +03:00
|
|
|
fca = repo.filectx(fw, fileid=nullrev)
|
2007-08-01 21:33:12 +04:00
|
|
|
a = repo.wjoin(fd)
|
2006-10-02 21:04:48 +04:00
|
|
|
b = temp("base", fca)
|
|
|
|
c = temp("other", fco)
|
2006-08-04 00:24:41 +04:00
|
|
|
|
2006-10-10 10:16:06 +04:00
|
|
|
if fw != fo:
|
|
|
|
repo.ui.status(_("merging %s and %s\n") % (fw, fo))
|
|
|
|
else:
|
|
|
|
repo.ui.status(_("merging %s\n") % fw)
|
|
|
|
|
2006-10-02 21:04:48 +04:00
|
|
|
repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
|
2006-08-04 00:24:41 +04:00
|
|
|
|
|
|
|
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,
|
2007-08-01 21:33:12 +04:00
|
|
|
environ={'HG_FILE': fd,
|
2006-10-10 05:32:03 +04:00
|
|
|
'HG_MY_NODE': str(wctx.parents()[0]),
|
2007-10-04 01:09:36 +04:00
|
|
|
'HG_OTHER_NODE': str(mctx),
|
|
|
|
'HG_MY_ISLINK': fcm.islink(),
|
|
|
|
'HG_OTHER_ISLINK': fco.islink(),
|
|
|
|
'HG_BASE_ISLINK': fca.islink(),})
|
2006-08-04 00:24:41 +04:00
|
|
|
if r:
|
2007-08-01 21:33:12 +04:00
|
|
|
repo.ui.warn(_("merging %s failed!\n") % fd)
|
2006-08-04 00:24:41 +04:00
|
|
|
|
|
|
|
os.unlink(b)
|
|
|
|
os.unlink(c)
|
|
|
|
return r
|
|
|
|
|
2006-10-10 10:43:58 +04:00
|
|
|
def checkunknown(wctx, mctx):
|
2006-10-10 11:31:02 +04:00
|
|
|
"check for collisions between unknown files and files in mctx"
|
2006-10-10 10:43:58 +04:00
|
|
|
man = mctx.manifest()
|
2006-10-03 10:21:46 +04:00
|
|
|
for f in wctx.unknown():
|
2006-10-10 10:43:58 +04:00
|
|
|
if f in man:
|
|
|
|
if mctx.filectx(f).cmp(wctx.filectx(f).data()):
|
2007-12-27 00:47:16 +03:00
|
|
|
raise util.Abort(_("untracked file in working directory differs"
|
|
|
|
" from file in requested revision: '%s'")
|
|
|
|
% f)
|
2006-09-18 01:47:33 +04:00
|
|
|
|
2006-12-05 02:10:31 +03:00
|
|
|
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
|
|
|
|
|
2006-10-10 10:43:58 +04:00
|
|
|
def forgetremoved(wctx, mctx):
|
2006-09-18 01:47:33 +04:00
|
|
|
"""
|
|
|
|
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 = []
|
2006-10-10 10:43:58 +04:00
|
|
|
man = mctx.manifest()
|
2006-10-03 10:21:46 +04:00
|
|
|
for f in wctx.deleted() + wctx.removed():
|
2006-10-10 10:43:58 +04:00
|
|
|
if f not in man:
|
2006-09-18 01:47:33 +04:00
|
|
|
action.append((f, "f"))
|
|
|
|
|
|
|
|
return action
|
|
|
|
|
2006-10-12 23:49:19 +04:00
|
|
|
def findcopies(repo, m1, m2, ma, limit):
|
2006-09-26 01:45:31 +04:00
|
|
|
"""
|
|
|
|
Find moves and copies between m1 and m2 back to limit linkrev
|
|
|
|
"""
|
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
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
|
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
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
|
|
|
|
|
2007-05-08 11:59:13 +04:00
|
|
|
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)
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
def findold(fctx):
|
|
|
|
"find files that path was copied from, back to linkrev limit"
|
|
|
|
old = {}
|
2007-04-16 21:24:52 +04:00
|
|
|
seen = {}
|
2006-12-01 02:36:33 +03:00
|
|
|
orig = fctx.path()
|
|
|
|
visit = [fctx]
|
|
|
|
while visit:
|
|
|
|
fc = visit.pop()
|
2007-04-16 21:24:52 +04:00
|
|
|
s = str(fc)
|
|
|
|
if s in seen:
|
|
|
|
continue
|
|
|
|
seen[s] = 1
|
2006-12-01 02:36:33 +03:00
|
|
|
if fc.path() != orig and fc.path() not in old:
|
|
|
|
old[fc.path()] = 1
|
2006-12-13 02:52:33 +03:00
|
|
|
if fc.rev() < limit:
|
|
|
|
continue
|
2006-12-01 02:36:33 +03:00
|
|
|
visit += fc.parents()
|
|
|
|
|
|
|
|
old = old.keys()
|
|
|
|
old.sort()
|
|
|
|
return old
|
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
copy = {}
|
|
|
|
fullcopy = {}
|
2007-06-22 03:02:03 +04:00
|
|
|
diverge = {}
|
2006-12-01 02:36:33 +03:00
|
|
|
|
2007-07-15 23:43:38 +04:00
|
|
|
def checkcopies(c, man, aman):
|
2006-12-01 02:36:33 +03:00
|
|
|
'''check possible copies for filectx c'''
|
|
|
|
for of in findold(c):
|
2007-06-22 03:02:03 +04:00
|
|
|
fullcopy[c.path()] = of # remember for dir rename detection
|
2007-05-04 02:24:43 +04:00
|
|
|
if of not in man: # original file not in other manifest?
|
2007-06-22 03:02:03 +04:00
|
|
|
if of in ma:
|
|
|
|
diverge.setdefault(of, []).append(c.path())
|
2007-04-06 01:25:47 +04:00
|
|
|
continue
|
2007-07-15 23:43:38 +04:00
|
|
|
# if the original file is unchanged on the other branch,
|
|
|
|
# no merge needed
|
|
|
|
if man[of] == aman.get(of):
|
|
|
|
continue
|
2006-12-01 02:36:33 +03:00
|
|
|
c2 = ctx(of, man[of])
|
|
|
|
ca = c.ancestor(c2)
|
2007-05-04 02:24:43 +04:00
|
|
|
if not ca: # unrelated?
|
2007-04-06 01:25:47 +04:00
|
|
|
continue
|
2007-05-04 02:24:43 +04:00
|
|
|
# named changed on only one side?
|
2006-12-01 02:36:33 +03:00
|
|
|
if ca.path() == c.path() or ca.path() == c2.path():
|
2007-10-09 03:47:22 +04:00
|
|
|
if c == ca and c2 == ca: # no merge needed, ignore copy
|
2007-04-06 01:25:47 +04:00
|
|
|
continue
|
2006-12-01 02:36:33 +03:00
|
|
|
copy[c.path()] = of
|
|
|
|
|
2006-10-12 23:49:19 +04:00
|
|
|
if not repo.ui.configbool("merge", "followcopies", True):
|
2007-06-22 03:02:03 +04:00
|
|
|
return {}, {}
|
2006-10-05 00:43:27 +04:00
|
|
|
|
2006-09-27 00:58:51 +04:00
|
|
|
# avoid silly behavior for update from empty dir
|
2006-12-01 02:36:33 +03:00
|
|
|
if not m1 or not m2 or not ma:
|
2007-06-22 03:02:03 +04:00
|
|
|
return {}, {}
|
2006-09-27 00:58:51 +04:00
|
|
|
|
2007-10-05 04:44:37 +04:00
|
|
|
repo.ui.debug(_(" searching for copies back to rev %d\n") % limit)
|
|
|
|
|
2006-10-12 23:49:19 +04:00
|
|
|
u1 = nonoverlap(m1, m2, ma)
|
|
|
|
u2 = nonoverlap(m2, m1, ma)
|
2006-09-26 01:45:31 +04:00
|
|
|
|
2007-10-05 04:44:37 +04:00
|
|
|
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))
|
|
|
|
|
2006-09-26 01:45:31 +04:00
|
|
|
for f in u1:
|
2007-07-15 23:43:38 +04:00
|
|
|
checkcopies(ctx(f, m1[f]), m2, ma)
|
2006-09-26 01:45:31 +04:00
|
|
|
|
|
|
|
for f in u2:
|
2007-07-15 23:43:38 +04:00
|
|
|
checkcopies(ctx(f, m2[f]), m1, ma)
|
2006-09-26 01:45:31 +04:00
|
|
|
|
2007-06-22 03:02:03 +04:00
|
|
|
d2 = {}
|
|
|
|
for of, fl in diverge.items():
|
|
|
|
for f in fl:
|
|
|
|
fo = list(fl)
|
|
|
|
fo.remove(f)
|
|
|
|
d2[f] = (of, fo)
|
|
|
|
|
2007-10-05 04:44:37 +04:00
|
|
|
if fullcopy:
|
|
|
|
repo.ui.debug(_(" all copies found (* = to merge, ! = divergent):\n"))
|
|
|
|
for f in fullcopy:
|
|
|
|
note = ""
|
|
|
|
if f in copy: note += "*"
|
|
|
|
if f in diverge: note += "!"
|
|
|
|
repo.ui.debug(_(" %s -> %s %s\n") % (f, fullcopy[f], note))
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
if not fullcopy or not repo.ui.configbool("merge", "followdirs", True):
|
2007-06-22 03:02:03 +04:00
|
|
|
return copy, diverge
|
2006-12-01 02:36:33 +03:00
|
|
|
|
2007-10-05 04:44:37 +04:00
|
|
|
repo.ui.debug(_(" checking for directory renames\n"))
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
# generate a directory move map
|
|
|
|
d1, d2 = dirs(m1), dirs(m2)
|
|
|
|
invalid = {}
|
|
|
|
dirmove = {}
|
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
# examine each file copy for a potential directory move, which is
|
|
|
|
# when all the files in a directory are moved to a new directory
|
2006-12-01 02:36:33 +03:00
|
|
|
for dst, src in fullcopy.items():
|
2007-05-04 02:24:43 +04:00
|
|
|
dsrc, ddst = dirname(src), dirname(dst)
|
2006-12-01 02:36:33 +03:00
|
|
|
if dsrc in invalid:
|
2007-05-04 02:24:43 +04:00
|
|
|
# already seen to be uninteresting
|
2006-12-01 02:36:33 +03:00
|
|
|
continue
|
2007-05-04 02:24:43 +04:00
|
|
|
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
|
2006-12-01 02:36:33 +03:00
|
|
|
invalid[dsrc] = True
|
|
|
|
elif dsrc in dirmove and dirmove[dsrc] != ddst:
|
2007-05-04 02:24:43 +04:00
|
|
|
# files from the same directory moved to two different places
|
2006-12-01 02:36:33 +03:00
|
|
|
invalid[dsrc] = True
|
|
|
|
else:
|
2007-05-04 02:24:43 +04:00
|
|
|
# looks good so far
|
2007-02-28 01:20:06 +03:00
|
|
|
dirmove[dsrc + "/"] = ddst + "/"
|
2006-12-01 02:36:33 +03:00
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
for i in invalid:
|
|
|
|
if i in dirmove:
|
|
|
|
del dirmove[i]
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
del d1, d2, invalid
|
|
|
|
|
|
|
|
if not dirmove:
|
2007-06-22 03:02:03 +04:00
|
|
|
return copy, diverge
|
2006-12-01 02:36:33 +03:00
|
|
|
|
2007-10-05 04:44:37 +04:00
|
|
|
for d in dirmove:
|
|
|
|
repo.ui.debug(_(" dir %s -> %s\n") % (d, dirmove[d]))
|
|
|
|
|
2007-05-04 02:24:43 +04:00
|
|
|
# check unaccounted nonoverlapping files against directory moves
|
2006-12-01 02:36:33 +03:00
|
|
|
for f in u1 + u2:
|
|
|
|
if f not in fullcopy:
|
2007-02-28 01:20:06 +03:00
|
|
|
for d in dirmove:
|
|
|
|
if f.startswith(d):
|
2007-05-04 02:24:43 +04:00
|
|
|
# new file added in a directory that was moved, move it
|
2007-02-28 01:20:06 +03:00
|
|
|
copy[f] = dirmove[d] + f[len(d):]
|
2007-10-05 04:44:37 +04:00
|
|
|
repo.ui.debug(_(" file %s -> %s\n") % (f, copy[f]))
|
2007-02-28 01:20:06 +03:00
|
|
|
break
|
2006-12-01 02:36:33 +03:00
|
|
|
|
2007-06-22 03:02:03 +04:00
|
|
|
return copy, diverge
|
2006-09-26 01:45:31 +04:00
|
|
|
|
2007-08-12 19:15:10 +04:00
|
|
|
def symmetricdifference(repo, rev1, rev2):
|
|
|
|
"""symmetric difference of the sets of ancestors of rev1 and rev2
|
2007-10-09 03:47:22 +04:00
|
|
|
|
2007-08-12 19:15:10 +04:00
|
|
|
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
|
|
|
|
|
2006-10-10 01:07:19 +04:00
|
|
|
def manifestmerge(repo, p1, p2, pa, overwrite, partial):
|
2006-09-18 01:13:03 +04:00
|
|
|
"""
|
2006-10-10 11:31:02 +04:00
|
|
|
Merge p1 and p2 with ancestor ma and generate merge action list
|
|
|
|
|
|
|
|
overwrite = whether we clobber working files
|
|
|
|
partial = function to filter file lists
|
2006-09-18 01:13:03 +04:00
|
|
|
"""
|
|
|
|
|
2006-10-10 11:15:20 +04:00
|
|
|
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))
|
|
|
|
|
2006-10-10 01:07:19 +04:00
|
|
|
m1 = p1.manifest()
|
|
|
|
m2 = p2.manifest()
|
|
|
|
ma = pa.manifest()
|
|
|
|
backwards = (pa == p2)
|
2006-10-10 11:15:20 +04:00
|
|
|
action = []
|
|
|
|
copy = {}
|
2007-06-22 03:02:03 +04:00
|
|
|
diverge = {}
|
2006-10-10 01:07:19 +04:00
|
|
|
|
2006-10-05 00:43:27 +04:00
|
|
|
def fmerge(f, f2=None, fa=None):
|
2006-12-30 05:04:31 +03:00
|
|
|
"""merge flags"""
|
2006-10-05 00:43:27 +04:00
|
|
|
if not f2:
|
|
|
|
f2 = f
|
|
|
|
fa = f
|
|
|
|
a, b, c = ma.execf(fa), m1.execf(f), m2.execf(f2)
|
2006-12-30 05:04:31 +03:00
|
|
|
if ((a^b) | (a^c)) ^ a:
|
|
|
|
return 'x'
|
|
|
|
a, b, c = ma.linkf(fa), m1.linkf(f), m2.linkf(f2)
|
|
|
|
if ((a^b) | (a^c)) ^ a:
|
|
|
|
return 'l'
|
|
|
|
return ''
|
2006-09-18 05:38:02 +04:00
|
|
|
|
2006-10-10 09:32:30 +04:00
|
|
|
def act(msg, m, f, *args):
|
2006-10-10 01:07:19 +04:00
|
|
|
repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m))
|
2006-09-18 06:27:30 +04:00
|
|
|
action.append((f, m) + args)
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
if not (backwards or overwrite):
|
2007-08-12 19:15:10 +04:00
|
|
|
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)
|
2007-06-22 03:02:03 +04:00
|
|
|
|
|
|
|
for of, fl in diverge.items():
|
|
|
|
act("divergent renames", "dr", of, fl)
|
|
|
|
|
2006-12-01 02:36:33 +03:00
|
|
|
copied = dict.fromkeys(copy.values())
|
2006-10-10 01:07:19 +04:00
|
|
|
|
2006-09-18 01:13:03 +04:00
|
|
|
# Compare manifests
|
|
|
|
for f, n in m1.iteritems():
|
2006-10-04 23:33:22 +04:00
|
|
|
if partial and not partial(f):
|
|
|
|
continue
|
2006-09-18 01:13:03 +04:00
|
|
|
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:
|
2006-10-10 09:41:55 +04:00
|
|
|
act("versions differ", "m", f, f, f, fmerge(f), False)
|
2006-09-18 01:13:03 +04:00
|
|
|
# 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:]):
|
2006-12-30 05:04:31 +03:00
|
|
|
act("remote is newer", "g", f, m2.flags(f))
|
2006-09-18 04:53:19 +04:00
|
|
|
# local is newer, not overwrite, check mode bits
|
2006-12-30 05:04:31 +03:00
|
|
|
elif fmerge(f) != m1.flags(f):
|
|
|
|
act("update permissions", "e", f, m2.flags(f))
|
2006-09-18 04:53:19 +04:00
|
|
|
# contents same, check mode bits
|
2006-12-30 05:04:31 +03:00
|
|
|
elif m1.flags(f) != m2.flags(f):
|
2007-11-05 23:49:35 +03:00
|
|
|
# are we clobbering?
|
|
|
|
# is remote's version newer?
|
|
|
|
# or are we going back?
|
|
|
|
if overwrite or fmerge(f) != m1.flags(f) or backwards:
|
2006-12-30 05:04:31 +03:00
|
|
|
act("update permissions", "e", f, m2.flags(f))
|
2006-12-01 02:36:33 +03:00
|
|
|
elif f in copied:
|
|
|
|
continue
|
2006-10-05 00:43:27 +04:00
|
|
|
elif f in copy:
|
|
|
|
f2 = copy[f]
|
2006-12-01 02:36:33 +03:00
|
|
|
if f2 not in m2: # directory rename
|
|
|
|
act("remote renamed directory to " + f2, "d",
|
2006-12-30 05:04:31 +03:00
|
|
|
f, None, f2, m1.flags(f))
|
2006-12-01 02:36:33 +03:00
|
|
|
elif f2 in m1: # case 2 A,B/B/B
|
2006-12-01 02:36:33 +03:00
|
|
|
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)
|
2006-09-18 01:13:03 +04:00
|
|
|
elif f in ma:
|
2006-09-18 05:18:27 +04:00
|
|
|
if n != ma[f] and not overwrite:
|
2006-10-10 01:07:19 +04:00
|
|
|
if repo.ui.prompt(
|
2007-12-27 00:47:16 +03:00
|
|
|
_(" local changed %s which remote deleted\n"
|
|
|
|
"use (c)hanged version or (d)elete?") % f,
|
|
|
|
_("[cd]"), _("c")) == _("d"):
|
2006-10-10 09:32:30 +04:00
|
|
|
act("prompt delete", "r", f)
|
2006-09-18 01:13:03 +04:00
|
|
|
else:
|
2006-10-10 09:32:30 +04:00
|
|
|
act("other deleted", "r", f)
|
2006-09-18 01:13:03 +04:00
|
|
|
else:
|
|
|
|
# file is created on branch or in working directory
|
2006-09-18 06:11:26 +04:00
|
|
|
if (overwrite and n[20:] != "u") or (backwards and not n[20:]):
|
2006-10-10 09:32:30 +04:00
|
|
|
act("remote deleted", "r", f)
|
2006-09-18 01:13:03 +04:00
|
|
|
|
|
|
|
for f, n in m2.iteritems():
|
2006-10-04 23:33:22 +04:00
|
|
|
if partial and not partial(f):
|
|
|
|
continue
|
|
|
|
if f in m1:
|
|
|
|
continue
|
2006-12-01 02:36:33 +03:00
|
|
|
if f in copied:
|
|
|
|
continue
|
2006-10-05 00:43:27 +04:00
|
|
|
if f in copy:
|
|
|
|
f2 = copy[f]
|
2006-12-01 02:36:33 +03:00
|
|
|
if f2 not in m1: # directory rename
|
|
|
|
act("local renamed directory to " + f2, "d",
|
2006-12-30 05:04:31 +03:00
|
|
|
None, f, f2, m2.flags(f))
|
2006-12-01 02:36:33 +03:00
|
|
|
elif f2 in m2: # rename case 1, A/A,B/A
|
2006-12-01 02:36:33 +03:00
|
|
|
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)
|
2006-10-05 00:43:27 +04:00
|
|
|
elif f in ma:
|
2006-09-18 05:10:54 +04:00
|
|
|
if overwrite or backwards:
|
2006-12-30 05:04:31 +03:00
|
|
|
act("recreating", "g", f, m2.flags(f))
|
2006-09-18 05:10:54 +04:00
|
|
|
elif n != ma[f]:
|
2006-10-10 01:07:19 +04:00
|
|
|
if repo.ui.prompt(
|
2007-12-27 00:47:16 +03:00
|
|
|
_("remote changed %s which local deleted\n"
|
|
|
|
"use (c)hanged version or leave (d)eleted?") % f,
|
|
|
|
_("[cd]"), _("c")) == _("c"):
|
2006-12-30 05:04:31 +03:00
|
|
|
act("prompt recreating", "g", f, m2.flags(f))
|
2006-09-18 05:00:14 +04:00
|
|
|
else:
|
2006-12-30 05:04:31 +03:00
|
|
|
act("remote created", "g", f, m2.flags(f))
|
2006-09-18 01:13:03 +04:00
|
|
|
|
|
|
|
return action
|
|
|
|
|
2006-10-10 05:32:03 +04:00
|
|
|
def applyupdates(repo, action, wctx, mctx):
|
2006-10-10 11:31:02 +04:00
|
|
|
"apply the merge action list to the working directory"
|
|
|
|
|
2006-09-18 02:39:19 +04:00
|
|
|
updated, merged, removed, unresolved = 0, 0, 0, 0
|
|
|
|
action.sort()
|
2007-08-01 21:33:12 +04:00
|
|
|
# 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)
|
|
|
|
|
2007-08-10 21:46:03 +04:00
|
|
|
audit_path = util.path_auditor(repo.root)
|
|
|
|
|
2006-09-18 02:39:19 +04:00
|
|
|
for a in action:
|
|
|
|
f, m = a[:2]
|
2006-12-01 02:36:33 +03:00
|
|
|
if f and f[0] == "/":
|
2006-09-18 02:39:19 +04:00
|
|
|
continue
|
|
|
|
if m == "r": # remove
|
|
|
|
repo.ui.note(_("removing %s\n") % f)
|
2007-08-10 21:46:03 +04:00
|
|
|
audit_path(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
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))
|
2006-11-17 10:06:54 +03:00
|
|
|
removed += 1
|
2006-10-10 09:41:55 +04:00
|
|
|
elif m == "m": # merge
|
2006-12-30 05:04:31 +03:00
|
|
|
f2, fd, flags, move = a[2:]
|
2007-08-01 21:33:12 +04:00
|
|
|
r = filemerge(repo, f, fd, f2, wctx, mctx)
|
2006-10-16 01:18:09 +04:00
|
|
|
if r > 0:
|
2006-10-05 00:43:27 +04:00
|
|
|
unresolved += 1
|
2006-10-10 09:54:00 +04:00
|
|
|
else:
|
2006-10-16 01:18:09 +04:00
|
|
|
if r is None:
|
|
|
|
updated += 1
|
|
|
|
else:
|
|
|
|
merged += 1
|
2007-08-02 21:29:10 +04:00
|
|
|
util.set_exec(repo.wjoin(fd), "x" in flags)
|
|
|
|
if f != fd and move and util.lexists(repo.wjoin(f)):
|
2007-08-01 21:33:12 +04:00
|
|
|
repo.ui.debug(_("removing %s\n") % f)
|
|
|
|
os.unlink(repo.wjoin(f))
|
2006-09-18 02:39:19 +04:00
|
|
|
elif m == "g": # get
|
2006-12-30 05:04:31 +03:00
|
|
|
flags = a[2]
|
2006-09-18 02:39:19 +04:00
|
|
|
repo.ui.note(_("getting %s\n") % f)
|
2006-10-10 08:23:25 +04:00
|
|
|
t = mctx.filectx(f).data()
|
2006-12-30 05:04:31 +03:00
|
|
|
repo.wwrite(f, t, flags)
|
2006-09-18 02:39:19 +04:00
|
|
|
updated += 1
|
2006-12-01 02:36:33 +03:00
|
|
|
elif m == "d": # directory rename
|
2006-12-30 05:04:31 +03:00
|
|
|
f2, fd, flags = a[2:]
|
2006-12-01 02:36:33 +03:00
|
|
|
if f:
|
|
|
|
repo.ui.note(_("moving %s to %s\n") % (f, fd))
|
|
|
|
t = wctx.filectx(f).data()
|
2006-12-30 05:04:31 +03:00
|
|
|
repo.wwrite(fd, t, flags)
|
2006-12-01 02:36:33 +03:00
|
|
|
util.unlink(repo.wjoin(f))
|
|
|
|
if f2:
|
|
|
|
repo.ui.note(_("getting %s to %s\n") % (f2, fd))
|
|
|
|
t = mctx.filectx(f2).data()
|
2006-12-30 05:04:31 +03:00
|
|
|
repo.wwrite(fd, t, flags)
|
2006-12-01 02:36:33 +03:00
|
|
|
updated += 1
|
2007-06-22 03:02:03 +04:00
|
|
|
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)
|
2006-09-18 02:39:19 +04:00
|
|
|
elif m == "e": # exec
|
2006-12-30 05:04:31 +03:00
|
|
|
flags = a[2]
|
|
|
|
util.set_exec(repo.wjoin(f), flags)
|
2006-09-18 02:39:19 +04:00
|
|
|
|
|
|
|
return updated, merged, removed, unresolved
|
|
|
|
|
2006-10-14 02:58:04 +04:00
|
|
|
def recordupdates(repo, action, branchmerge):
|
2006-10-10 11:31:02 +04:00
|
|
|
"record merge actions to the dirstate"
|
|
|
|
|
2006-09-18 02:39:19 +04:00
|
|
|
for a in action:
|
|
|
|
f, m = a[:2]
|
|
|
|
if m == "r": # remove
|
|
|
|
if branchmerge:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.remove(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
else:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.forget(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
elif m == "f": # forget
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.forget(f)
|
2007-07-26 20:19:53 +04:00
|
|
|
elif m in "ge": # get or exec change
|
2006-09-18 02:39:19 +04:00
|
|
|
if branchmerge:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.normaldirty(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
else:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.normal(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
elif m == "m": # merge
|
2006-10-10 08:23:25 +04:00
|
|
|
f2, fd, flag, move = a[2:]
|
2006-10-05 04:08:04 +04:00
|
|
|
if branchmerge:
|
|
|
|
# We've done a branch merge, mark this file as merged
|
|
|
|
# so that we properly record the merger later
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.merge(fd)
|
2006-10-14 02:58:04 +04:00
|
|
|
if f != f2: # copy/rename
|
|
|
|
if move:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.remove(f)
|
2006-10-14 02:58:04 +04:00
|
|
|
if f != fd:
|
|
|
|
repo.dirstate.copy(f, fd)
|
|
|
|
else:
|
|
|
|
repo.dirstate.copy(f2, fd)
|
2006-10-05 04:08:04 +04:00
|
|
|
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.
|
merge: forcefully mark files that we get from the second parent as dirty
After a hg merge, we want to include in the commit all the files that we
got from the second parent, so that we have the correct file-level
history. To make them visible to hg commit, we try to mark them as dirty.
Unfortunately, right now we can't really mark them as dirty[1] - the
best we can do is to mark them as needing a full comparison of their
contents, but they will still be considered clean if they happen to be
identical to the version in the first parent.
This changeset extends the dirstate format in a compatible way, so that
we can mark a file as dirty:
Right now we use a negative file size to indicate we don't have valid
stat data for this entry. In practice, this size is always -1.
This patch uses -2 to indicate that the entry is dirty. Older versions
of hg won't choke on this dirstate, but they may happily mark the file
as clean after a full comparison, destroying all of our hard work.
The patch adds a dirstate.normallookup method with the semantics of the
current normaldirty, and changes normaldirty to forcefully mark the
entry as dirty.
This should fix issue522.
[1] - well, we could put them in state 'm', but that state has a
different meaning.
2007-08-23 08:48:29 +04:00
|
|
|
repo.dirstate.normallookup(fd)
|
2006-10-10 09:41:55 +04:00
|
|
|
if move:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.forget(f)
|
2006-12-01 02:36:33 +03:00
|
|
|
elif m == "d": # directory rename
|
|
|
|
f2, fd, flag = a[2:]
|
2007-07-06 01:01:07 +04:00
|
|
|
if not f2 and f not in repo.dirstate:
|
|
|
|
# untracked file moved
|
|
|
|
continue
|
2006-12-01 02:36:33 +03:00
|
|
|
if branchmerge:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.add(fd)
|
2006-12-01 02:36:33 +03:00
|
|
|
if f:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.remove(f)
|
2006-12-01 02:36:33 +03:00
|
|
|
repo.dirstate.copy(f, fd)
|
|
|
|
if f2:
|
|
|
|
repo.dirstate.copy(f2, fd)
|
|
|
|
else:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.normal(fd)
|
2006-12-01 02:36:33 +03:00
|
|
|
if f:
|
2007-07-22 01:02:09 +04:00
|
|
|
repo.dirstate.forget(f)
|
2006-09-18 02:39:19 +04:00
|
|
|
|
2007-07-22 01:02:10 +04:00
|
|
|
def update(repo, node, branchmerge, force, partial):
|
2006-10-10 11:31:02 +04:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
"""
|
2006-08-08 19:56:48 +04:00
|
|
|
|
2007-07-22 01:02:10 +04:00
|
|
|
wlock = repo.wlock()
|
2007-07-22 01:02:10 +04:00
|
|
|
try:
|
|
|
|
wc = repo.workingctx()
|
|
|
|
if node is None:
|
|
|
|
# tip of current branch
|
|
|
|
try:
|
|
|
|
node = repo.branchtags()[wc.branch()]
|
|
|
|
except KeyError:
|
2007-12-03 00:15:49 +03:00
|
|
|
if wc.branch() == "default": # no default branch!
|
|
|
|
node = repo.lookup("tip") # update to tip
|
|
|
|
else:
|
|
|
|
raise util.Abort(_("branch %s not found") % wc.branch())
|
2007-07-22 01:02:10 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
### 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"))
|
|
|
|
|
|
|
|
### 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)
|
|
|
|
|
|
|
|
### 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)
|
|
|
|
|
|
|
|
stats = applyupdates(repo, action, wc, p2)
|
|
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
return stats
|
|
|
|
finally:
|
|
|
|
del wlock
|