merge.py
642 lines
| 23.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
Matt Mackall
|
r2775 | # merge.py - directory-level update/merge handling for Mercurial | ||
# | ||||
Thomas Arendsen Hein
|
r4635 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> | ||
Matt Mackall
|
r2775 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r6518 | from node import nullid, nullrev, hex, bin | ||
Matt Mackall
|
r3891 | from i18n import _ | ||
Mads Kiilerich
|
r18328 | import error, util, filemerge, copies, subrepo | ||
Simon Heimberg
|
r8312 | import errno, os, shutil | ||
Matt Mackall
|
r6512 | |||
class mergestate(object): | ||||
'''track 3-way merge state of individual files''' | ||||
def __init__(self, repo): | ||||
self._repo = repo | ||||
Peter Arrenbrecht
|
r12369 | self._dirty = False | ||
Matt Mackall
|
r6518 | self._read() | ||
Matt Mackall
|
r7848 | def reset(self, node=None): | ||
Matt Mackall
|
r6512 | self._state = {} | ||
Matt Mackall
|
r7848 | if node: | ||
self._local = node | ||||
Matt Mackall
|
r6512 | shutil.rmtree(self._repo.join("merge"), True) | ||
Peter Arrenbrecht
|
r12369 | self._dirty = False | ||
Matt Mackall
|
r6518 | def _read(self): | ||
self._state = {} | ||||
try: | ||||
f = self._repo.opener("merge/state") | ||||
Patrick Mezard
|
r6530 | for i, l in enumerate(f): | ||
if i == 0: | ||||
Martin Geisler
|
r11451 | self._local = bin(l[:-1]) | ||
Patrick Mezard
|
r6530 | else: | ||
bits = l[:-1].split("\0") | ||||
self._state[bits[0]] = bits[1:] | ||||
Dan Villiom Podlaski Christiansen
|
r13400 | f.close() | ||
Matt Mackall
|
r6518 | except IOError, err: | ||
if err.errno != errno.ENOENT: | ||||
raise | ||||
Peter Arrenbrecht
|
r12369 | self._dirty = False | ||
def commit(self): | ||||
if self._dirty: | ||||
f = self._repo.opener("merge/state", "w") | ||||
f.write(hex(self._local) + "\n") | ||||
for d, v in self._state.iteritems(): | ||||
f.write("\0".join([d] + v) + "\n") | ||||
Dan Villiom Podlaski Christiansen
|
r13400 | f.close() | ||
Peter Arrenbrecht
|
r12369 | self._dirty = False | ||
Mads Kiilerich
|
r18338 | def add(self, fcl, fco, fca, fd): | ||
Dirkjan Ochtman
|
r6517 | hash = util.sha1(fcl.path()).hexdigest() | ||
Dan Villiom Podlaski Christiansen
|
r14168 | self._repo.opener.write("merge/" + hash, fcl.data()) | ||
Matt Mackall
|
r6518 | self._state[fd] = ['u', hash, fcl.path(), fca.path(), | ||
Mads Kiilerich
|
r18338 | hex(fca.filenode()), fco.path(), fcl.flags()] | ||
Peter Arrenbrecht
|
r12369 | self._dirty = True | ||
Matt Mackall
|
r6512 | def __contains__(self, dfile): | ||
return dfile in self._state | ||||
def __getitem__(self, dfile): | ||||
Matt Mackall
|
r6518 | return self._state[dfile][0] | ||
def __iter__(self): | ||||
l = self._state.keys() | ||||
l.sort() | ||||
for f in l: | ||||
yield f | ||||
Matt Mackall
|
r6512 | def mark(self, dfile, state): | ||
Matt Mackall
|
r6518 | self._state[dfile][0] = state | ||
Peter Arrenbrecht
|
r12369 | self._dirty = True | ||
Matt Mackall
|
r6512 | def resolve(self, dfile, wctx, octx): | ||
if self[dfile] == 'r': | ||||
return 0 | ||||
Matt Mackall
|
r6518 | state, hash, lfile, afile, anode, ofile, flags = self._state[dfile] | ||
Mads Kiilerich
|
r18338 | fcd = wctx[dfile] | ||
fco = octx[ofile] | ||||
fca = self._repo.filectx(afile, fileid=anode) | ||||
# "premerge" x flags | ||||
flo = fco.flags() | ||||
fla = fca.flags() | ||||
if 'x' in flags + flo + fla and 'l' not in flags + flo + fla: | ||||
if fca.node() == nullid: | ||||
self._repo.ui.warn(_('warning: cannot merge flags for %s\n') % | ||||
afile) | ||||
elif flags == fla: | ||||
flags = flo | ||||
# restore local | ||||
Matt Mackall
|
r6512 | f = self._repo.opener("merge/" + hash) | ||
self._repo.wwrite(dfile, f.read(), flags) | ||||
Dan Villiom Podlaski Christiansen
|
r13400 | f.close() | ||
Matt Mackall
|
r6512 | r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca) | ||
Matt Mackall
|
r13536 | if r is None: | ||
# no real conflict | ||||
del self._state[dfile] | ||||
elif not r: | ||||
Matt Mackall
|
r6512 | self.mark(dfile, 'r') | ||
return r | ||||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r16093 | def _checkunknownfile(repo, wctx, mctx, f): | ||
return (not repo.dirstate._ignore(f) | ||||
Matt Mackall
|
r16534 | and os.path.isfile(repo.wjoin(f)) | ||
Matt Mackall
|
r16284 | and repo.dirstate.normalize(f) not in repo.dirstate | ||
Matt Mackall
|
r16093 | and mctx[f].cmp(wctx[f])) | ||
def _checkunknown(repo, wctx, mctx): | ||||
Matt Mackall
|
r3315 | "check for collisions between unknown files and files in mctx" | ||
Jordi Gutiérrez Hermoso
|
r15894 | |||
error = False | ||||
Matt Mackall
|
r16093 | for f in mctx: | ||
if f not in wctx and _checkunknownfile(repo, wctx, mctx, f): | ||||
Jordi Gutiérrez Hermoso
|
r15894 | error = True | ||
Matt Mackall
|
r16093 | wctx._repo.ui.warn(_("%s: untracked file differs\n") % f) | ||
Jordi Gutiérrez Hermoso
|
r15894 | if error: | ||
raise util.Abort(_("untracked files in working directory differ " | ||||
"from files in requested revision")) | ||||
Matt Mackall
|
r3107 | |||
FUJIWARA Katsunori
|
r17889 | def _remains(f, m, ma, workingctx=False): | ||
"""check whether specified file remains after merge. | ||||
It is assumed that specified file is not contained in the manifest | ||||
of the other context. | ||||
""" | ||||
if f in ma: | ||||
n = m[f] | ||||
if n != ma[f]: | ||||
return True # because it is changed locally | ||||
# even though it doesn't remain, if "remote deleted" is | ||||
# chosen in manifestmerge() | ||||
elif workingctx and n[20:] == "a": | ||||
return True # because it is added locally (linear merge specific) | ||||
else: | ||||
return False # because it is removed remotely | ||||
else: | ||||
return True # because it is added locally | ||||
def _checkcollision(mctx, extractxs): | ||||
Matt Mackall
|
r3785 | "check for case folding collisions in the destination context" | ||
folded = {} | ||||
Matt Mackall
|
r6272 | for fn in mctx: | ||
FUJIWARA Katsunori
|
r15637 | fold = util.normcase(fn) | ||
Matt Mackall
|
r3785 | if fold in folded: | ||
raise util.Abort(_("case-folding collision between %s and %s") | ||||
% (fn, folded[fold])) | ||||
folded[fold] = fn | ||||
FUJIWARA Katsunori
|
r17889 | if extractxs: | ||
wctx, actx = extractxs | ||||
FUJIWARA Katsunori
|
r16478 | # class to delay looking up copy mapping | ||
class pathcopies(object): | ||||
@util.propertycache | ||||
def map(self): | ||||
# {dst@mctx: src@wctx} copy mapping | ||||
return copies.pathcopies(wctx, mctx) | ||||
pc = pathcopies() | ||||
FUJIWARA Katsunori
|
r15673 | for fn in wctx: | ||
fold = util.normcase(fn) | ||||
mfn = folded.get(fold, None) | ||||
FUJIWARA Katsunori
|
r17889 | if (mfn and mfn != fn and pc.map.get(mfn) != fn and | ||
_remains(fn, wctx.manifest(), actx.manifest(), True) and | ||||
_remains(mfn, mctx.manifest(), actx.manifest())): | ||||
FUJIWARA Katsunori
|
r15673 | raise util.Abort(_("case-folding collision between %s and %s") | ||
% (mfn, fn)) | ||||
Matt Mackall
|
r6269 | def _forgetremoved(wctx, mctx, branchmerge): | ||
Matt Mackall
|
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. | ||||
Alexis S. L. Carvalho
|
r6242 | |||
If we're merging, and the other revision has removed a file | ||||
that is not present in the working directory, we need to mark it | ||||
as removed. | ||||
Matt Mackall
|
r3107 | """ | ||
Mads Kiilerich
|
r18330 | actions = [] | ||
Alexis S. L. Carvalho
|
r6242 | state = branchmerge and 'r' or 'f' | ||
for f in wctx.deleted(): | ||||
Matt Mackall
|
r6272 | if f not in mctx: | ||
Mads Kiilerich
|
r18330 | actions.append((f, state)) | ||
Alexis S. L. Carvalho
|
r6242 | |||
if not branchmerge: | ||||
for f in wctx.removed(): | ||||
Matt Mackall
|
r6272 | if f not in mctx: | ||
Mads Kiilerich
|
r18330 | actions.append((f, "f")) | ||
Matt Mackall
|
r3107 | |||
Mads Kiilerich
|
r18330 | return actions | ||
Matt Mackall
|
r3107 | |||
Matt Mackall
|
r3295 | def manifestmerge(repo, p1, p2, pa, overwrite, partial): | ||
Matt Mackall
|
r3105 | """ | ||
Alecs King
|
r11817 | Merge p1 and p2 with ancestor pa and generate merge action list | ||
Matt Mackall
|
r3315 | |||
overwrite = whether we clobber working files | ||||
partial = function to filter file lists | ||||
Matt Mackall
|
r3105 | """ | ||
Matt Mackall
|
r3307 | def act(msg, m, f, *args): | ||
Matt Mackall
|
r3295 | repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m)) | ||
Mads Kiilerich
|
r18330 | actions.append((f, m) + args) | ||
Matt Mackall
|
r3121 | |||
Mads Kiilerich
|
r18330 | actions, copy, movewithdir = [], {}, {} | ||
Matt Mackall
|
r8753 | |||
Matt Mackall
|
r8749 | if overwrite: | ||
Matt Mackall
|
r8753 | pa = p1 | ||
elif pa == p2: # backwards | ||||
pa = p1.p1() | ||||
elif pa and repo.ui.configbool("merge", "followcopies", True): | ||||
Siddharth Agarwal
|
r18134 | ret = copies.mergecopies(repo, p1, p2, pa) | ||
copy, movewithdir, diverge, renamedelete = ret | ||||
Matt Mackall
|
r8753 | for of, fl in diverge.iteritems(): | ||
act("divergent renames", "dr", of, fl) | ||||
Thomas Arendsen Hein
|
r16794 | for of, fl in renamedelete.iteritems(): | ||
act("rename and delete", "rd", of, fl) | ||||
Matt Mackall
|
r8753 | |||
repo.ui.note(_("resolving manifests\n")) | ||||
Martin Geisler
|
r15625 | repo.ui.debug(" overwrite: %s, partial: %s\n" | ||
% (bool(overwrite), bool(partial))) | ||||
repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, p1, p2)) | ||||
Matt Mackall
|
r8753 | |||
m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest() | ||||
copied = set(copy.values()) | ||||
Siddharth Agarwal
|
r18134 | copied.update(movewithdir.values()) | ||
Matt Mackall
|
r3295 | |||
Matt Mackall
|
r11470 | if '.hgsubstate' in m1: | ||
Matt Mackall
|
r9783 | # check whether sub state is modified | ||
Mads Kiilerich
|
r18364 | for s in sorted(p1.substate): | ||
Matt Mackall
|
r9783 | if p1.sub(s).dirty(): | ||
m1['.hgsubstate'] += "+" | ||||
break | ||||
Matt Mackall
|
r3105 | # Compare manifests | ||
Matt Mackall
|
r18456 | visit = m1.iteritems() | ||
if repo.ui.debugflag: | ||||
visit = sorted(visit) | ||||
for f, n in visit: | ||||
Matt Mackall
|
r3248 | if partial and not partial(f): | ||
continue | ||||
Matt Mackall
|
r3105 | if f in m2: | ||
Mads Kiilerich
|
r18338 | n2 = m2[f] | ||
fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f) | ||||
nol = 'l' not in fl1 + fl2 + fla | ||||
Matt Mackall
|
r8752 | a = ma.get(f, nullid) | ||
Mads Kiilerich
|
r18338 | if n == n2 and fl1 == fl2: | ||
pass # same - keep local | ||||
elif n2 == a and fl2 == fla: | ||||
pass # remote unchanged - keep local | ||||
elif n == a and fl1 == fla: # local unchanged - use remote | ||||
if n == n2: # optimization: keep local content | ||||
act("update permissions", "e", f, fl2) | ||||
else: | ||||
act("remote is newer", "g", f, fl2) | ||||
elif nol and n2 == a: # remote only changed 'x' | ||||
act("update permissions", "e", f, fl2) | ||||
elif nol and n == a: # local only changed 'x' | ||||
act("remote is newer", "g", f, fl) | ||||
else: # both changed something | ||||
act("versions differ", "m", f, f, f, False) | ||||
Matt Mackall
|
r8752 | elif f in copied: # files we'll deal with on m2 side | ||
pass | ||||
Siddharth Agarwal
|
r18134 | elif f in movewithdir: # directory rename | ||
f2 = movewithdir[f] | ||||
act("remote renamed directory to " + f2, "d", f, None, f2, | ||||
m1.flags(f)) | ||||
Mads Kiilerich
|
r18339 | elif f in copy: | ||
Matt Mackall
|
r3249 | f2 = copy[f] | ||
Mads Kiilerich
|
r18338 | act("local copied/moved to " + f2, "m", f, f2, f, False) | ||
Matt Mackall
|
r8744 | elif f in ma: # clean, a different, no remote | ||
Matt Mackall
|
r8739 | if n != ma[f]: | ||
Simon Heimberg
|
r9048 | if repo.ui.promptchoice( | ||
Thomas Arendsen Hein
|
r5670 | _(" local changed %s which remote deleted\n" | ||
"use (c)hanged version or (d)elete?") % f, | ||||
Simon Heimberg
|
r9048 | (_("&Changed"), _("&Delete")), 0): | ||
Matt Mackall
|
r3307 | act("prompt delete", "r", f) | ||
Matt Mackall
|
r8736 | else: | ||
act("prompt keep", "a", f) | ||||
Matt Mackall
|
r8751 | elif n[20:] == "a": # added, no remote | ||
act("remote deleted", "f", f) | ||||
Matt Mackall
|
r16094 | else: | ||
Matt Mackall
|
r3307 | act("other deleted", "r", f) | ||
Matt Mackall
|
r3105 | |||
Matt Mackall
|
r18456 | visit = m2.iteritems() | ||
if repo.ui.debugflag: | ||||
visit = sorted(visit) | ||||
for f, n in visit: | ||||
Matt Mackall
|
r3248 | if partial and not partial(f): | ||
continue | ||||
Matt Mackall
|
r8752 | if f in m1 or f in copied: # files already visited | ||
Matt Mackall
|
r3729 | continue | ||
Siddharth Agarwal
|
r18134 | if f in movewithdir: | ||
f2 = movewithdir[f] | ||||
act("local renamed directory to " + f2, "d", None, f, f2, | ||||
m2.flags(f)) | ||||
elif f in copy: | ||||
Matt Mackall
|
r3249 | f2 = copy[f] | ||
Mads Kiilerich
|
r18339 | if f2 in m2: | ||
Matt Mackall
|
r3730 | act("remote copied to " + f, "m", | ||
Mads Kiilerich
|
r18338 | f2, f, f, False) | ||
Mads Kiilerich
|
r18339 | else: | ||
Matt Mackall
|
r3730 | act("remote moved to " + f, "m", | ||
Mads Kiilerich
|
r18338 | f2, f, f, True) | ||
Matt Mackall
|
r8741 | elif f not in ma: | ||
Matt Mackall
|
r16094 | if (not overwrite | ||
and _checkunknownfile(repo, p1, p2, f)): | ||||
act("remote differs from untracked local", | ||||
Mads Kiilerich
|
r18338 | "m", f, f, f, False) | ||
Matt Mackall
|
r16094 | else: | ||
act("remote created", "g", f, m2.flags(f)) | ||||
Matt Mackall
|
r8741 | elif n != ma[f]: | ||
Simon Heimberg
|
r9048 | if repo.ui.promptchoice( | ||
Matt Mackall
|
r8741 | _("remote changed %s which local deleted\n" | ||
"use (c)hanged version or leave (d)eleted?") % f, | ||||
Simon Heimberg
|
r9048 | (_("&Changed"), _("&Deleted")), 0) == 0: | ||
Matt Mackall
|
r8741 | act("prompt recreating", "g", f, m2.flags(f)) | ||
Matt Mackall
|
r3105 | |||
Mads Kiilerich
|
r18330 | return actions | ||
Matt Mackall
|
r3105 | |||
Dirkjan Ochtman
|
r8366 | def actionkey(a): | ||
Mads Kiilerich
|
r18329 | return a[1] == "r" and -1 or 0, a | ||
Paul Moore
|
r6805 | |||
Mads Kiilerich
|
r18330 | def applyupdates(repo, actions, wctx, mctx, actx, overwrite): | ||
Peter Arrenbrecht
|
r11454 | """apply the merge action list to the working directory | ||
wctx is the working copy context | ||||
mctx is the context to be merged into the working copy | ||||
actx is the context of the common ancestor | ||||
Greg Ward
|
r13162 | |||
Return a tuple of counts (updated, merged, removed, unresolved) that | ||||
describes how many files were affected by the update. | ||||
Peter Arrenbrecht
|
r11454 | """ | ||
Matt Mackall
|
r3315 | |||
Matt Mackall
|
r3111 | updated, merged, removed, unresolved = 0, 0, 0, 0 | ||
Matt Mackall
|
r6512 | ms = mergestate(repo) | ||
Matt Mackall
|
r13878 | ms.reset(wctx.p1().node()) | ||
Matt Mackall
|
r6512 | moves = [] | ||
Mads Kiilerich
|
r18330 | actions.sort(key=actionkey) | ||
Matt Mackall
|
r6512 | |||
# prescan for merges | ||||
Mads Kiilerich
|
r18330 | for a in actions: | ||
Matt Mackall
|
r5042 | f, m = a[:2] | ||
Mads Kiilerich
|
r18329 | if m == "m": # merge | ||
Mads Kiilerich
|
r18338 | f2, fd, move = a[2:] | ||
Mads Kiilerich
|
r18332 | if fd == '.hgsubstate': # merged internally | ||
Matt Mackall
|
r8814 | continue | ||
Martin Geisler
|
r9467 | repo.ui.debug("preserving %s for resolve of %s\n" % (f, fd)) | ||
Matt Mackall
|
r6512 | fcl = wctx[f] | ||
fco = mctx[f2] | ||||
Matt Mackall
|
r12008 | if mctx == actx: # backwards, use working dir parent as ancestor | ||
Matt Mackall
|
r12664 | if fcl.parents(): | ||
Matt Mackall
|
r13878 | fca = fcl.p1() | ||
Matt Mackall
|
r12664 | else: | ||
fca = repo.filectx(f, fileid=nullrev) | ||||
Matt Mackall
|
r12008 | else: | ||
fca = fcl.ancestor(fco, actx) | ||||
if not fca: | ||||
fca = repo.filectx(f, fileid=nullrev) | ||||
Mads Kiilerich
|
r18338 | ms.add(fcl, fco, fca, fd) | ||
Matt Mackall
|
r6512 | if f != fd and move: | ||
moves.append(f) | ||||
Mads Kiilerich
|
r18328 | audit = repo.wopener.audit | ||
Adrian Buehlmann
|
r14398 | |||
Matt Mackall
|
r6512 | # remove renamed files after safely stored | ||
for f in moves: | ||||
Martin Geisler
|
r12032 | if os.path.lexists(repo.wjoin(f)): | ||
Martin Geisler
|
r9467 | repo.ui.debug("removing %s\n" % f) | ||
Adrian Buehlmann
|
r14398 | audit(f) | ||
Mads Kiilerich
|
r18333 | util.unlinkpath(repo.wjoin(f)) | ||
Matt Mackall
|
r5042 | |||
Mads Kiilerich
|
r18330 | numupdates = len(actions) | ||
for i, a in enumerate(actions): | ||||
Matt Mackall
|
r3111 | f, m = a[:2] | ||
Martin Geisler
|
r15041 | repo.ui.progress(_('updating'), i + 1, item=f, total=numupdates, | ||
unit=_('files')) | ||||
Matt Mackall
|
r3111 | if m == "r": # remove | ||
repo.ui.note(_("removing %s\n") % f) | ||||
Adrian Buehlmann
|
r14398 | audit(f) | ||
Matt Mackall
|
r8814 | if f == '.hgsubstate': # subrepo states need updating | ||
Erik Zielke
|
r13322 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
Matt Mackall
|
r3111 | try: | ||
Mads Kiilerich
|
r18143 | util.unlinkpath(repo.wjoin(f), ignoremissing=True) | ||
Matt Mackall
|
r3111 | except OSError, inst: | ||
Mads Kiilerich
|
r18143 | repo.ui.warn(_("update failed to remove %s: %s!\n") % | ||
(f, inst.strerror)) | ||||
Thomas Arendsen Hein
|
r3673 | removed += 1 | ||
Matt Mackall
|
r3308 | elif m == "m": # merge | ||
Mads Kiilerich
|
r18332 | if fd == '.hgsubstate': # subrepo states need updating | ||
Brodie Rao
|
r16683 | subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx), | ||
overwrite) | ||||
Matt Mackall
|
r8814 | continue | ||
Mads Kiilerich
|
r18338 | f2, fd, move = a[2:] | ||
Mads Kiilerich
|
r18328 | audit(fd) | ||
Matt Mackall
|
r6512 | r = ms.resolve(fd, wctx, mctx) | ||
Alejandro Santos
|
r9030 | if r is not None and r > 0: | ||
Matt Mackall
|
r3249 | unresolved += 1 | ||
Matt Mackall
|
r3309 | else: | ||
Matt Mackall
|
r3400 | if r is None: | ||
updated += 1 | ||||
else: | ||||
merged += 1 | ||||
Matt Mackall
|
r3111 | elif m == "g": # get | ||
Matt Mackall
|
r4007 | flags = a[2] | ||
Matt Mackall
|
r3111 | repo.ui.note(_("getting %s\n") % f) | ||
Mads Kiilerich
|
r18335 | repo.wwrite(f, mctx.filectx(f).data(), flags) | ||
Matt Mackall
|
r3111 | updated += 1 | ||
Matt Mackall
|
r8814 | if f == '.hgsubstate': # subrepo states need updating | ||
Erik Zielke
|
r13322 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
Matt Mackall
|
r3733 | elif m == "d": # directory rename | ||
Matt Mackall
|
r4007 | f2, fd, flags = a[2:] | ||
Matt Mackall
|
r3733 | if f: | ||
repo.ui.note(_("moving %s to %s\n") % (f, fd)) | ||||
Adrian Buehlmann
|
r14398 | audit(f) | ||
Mads Kiilerich
|
r18335 | repo.wwrite(fd, wctx.filectx(f).data(), flags) | ||
Adrian Buehlmann
|
r13235 | util.unlinkpath(repo.wjoin(f)) | ||
Matt Mackall
|
r3733 | if f2: | ||
repo.ui.note(_("getting %s to %s\n") % (f2, fd)) | ||||
Mads Kiilerich
|
r18335 | repo.wwrite(fd, mctx.filectx(f2).data(), flags) | ||
Matt Mackall
|
r3733 | updated += 1 | ||
Matt Mackall
|
r4674 | elif m == "dr": # divergent renames | ||
fl = a[2] | ||||
Dan Villiom Podlaski Christiansen
|
r12757 | repo.ui.warn(_("note: possible conflict - %s was renamed " | ||
"multiple times to:\n") % f) | ||||
Matt Mackall
|
r4674 | for nf in fl: | ||
repo.ui.warn(" %s\n" % nf) | ||||
Thomas Arendsen Hein
|
r16794 | elif m == "rd": # rename and delete | ||
fl = a[2] | ||||
repo.ui.warn(_("note: possible conflict - %s was deleted " | ||||
"and renamed to:\n") % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
Matt Mackall
|
r3111 | elif m == "e": # exec | ||
Matt Mackall
|
r4007 | flags = a[2] | ||
Mads Kiilerich
|
r18328 | audit(f) | ||
Adrian Buehlmann
|
r14232 | util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags) | ||
Mads Kiilerich
|
r18334 | updated += 1 | ||
Peter Arrenbrecht
|
r12369 | ms.commit() | ||
Martin Geisler
|
r15041 | repo.ui.progress(_('updating'), None, total=numupdates, unit=_('files')) | ||
Matt Mackall
|
r3111 | |||
return updated, merged, removed, unresolved | ||||
David Schleimer
|
r18035 | def calculateupdates(repo, tctx, mctx, ancestor, branchmerge, force, partial): | ||
"Calculate the actions needed to merge mctx into tctx" | ||||
Mads Kiilerich
|
r18330 | actions = [] | ||
David Schleimer
|
r18035 | folding = not util.checkcase(repo.path) | ||
if folding: | ||||
# collision check is not needed for clean update | ||||
if (not branchmerge and | ||||
(force or not tctx.dirty(missing=True, branch=False))): | ||||
_checkcollision(mctx, None) | ||||
else: | ||||
Kevin Bullock
|
r18042 | _checkcollision(mctx, (tctx, ancestor)) | ||
David Schleimer
|
r18035 | if not force: | ||
_checkunknown(repo, tctx, mctx) | ||||
David Schleimer
|
r18036 | if tctx.rev() is None: | ||
Mads Kiilerich
|
r18330 | actions += _forgetremoved(tctx, mctx, branchmerge) | ||
actions += manifestmerge(repo, tctx, mctx, | ||||
ancestor, | ||||
force and not branchmerge, | ||||
partial) | ||||
return actions | ||||
David Schleimer
|
r18035 | |||
Mads Kiilerich
|
r18330 | def recordupdates(repo, actions, branchmerge): | ||
Matt Mackall
|
r3315 | "record merge actions to the dirstate" | ||
Mads Kiilerich
|
r18330 | for a in actions: | ||
Matt Mackall
|
r3111 | f, m = a[:2] | ||
if m == "r": # remove | ||||
if branchmerge: | ||||
Matt Mackall
|
r4904 | repo.dirstate.remove(f) | ||
Matt Mackall
|
r3111 | else: | ||
Matt Mackall
|
r14434 | repo.dirstate.drop(f) | ||
Matt Mackall
|
r7768 | elif m == "a": # re-add | ||
if not branchmerge: | ||||
repo.dirstate.add(f) | ||||
Matt Mackall
|
r3111 | elif m == "f": # forget | ||
Matt Mackall
|
r14434 | repo.dirstate.drop(f) | ||
Benoit Boissinot
|
r7569 | elif m == "e": # exec change | ||
Patrick Mezard
|
r7630 | repo.dirstate.normallookup(f) | ||
Benoit Boissinot
|
r7569 | elif m == "g": # get | ||
Matt Mackall
|
r3111 | if branchmerge: | ||
Benoit Boissinot
|
r10968 | repo.dirstate.otherparent(f) | ||
Matt Mackall
|
r3111 | else: | ||
Matt Mackall
|
r4904 | repo.dirstate.normal(f) | ||
Matt Mackall
|
r3111 | elif m == "m": # merge | ||
Mads Kiilerich
|
r18338 | f2, fd, move = a[2:] | ||
Matt Mackall
|
r3251 | if branchmerge: | ||
# We've done a branch merge, mark this file as merged | ||||
# so that we properly record the merger later | ||||
Matt Mackall
|
r4904 | repo.dirstate.merge(fd) | ||
Matt Mackall
|
r3372 | if f != f2: # copy/rename | ||
if move: | ||||
Matt Mackall
|
r4904 | repo.dirstate.remove(f) | ||
Matt Mackall
|
r3372 | if f != fd: | ||
repo.dirstate.copy(f, fd) | ||||
else: | ||||
repo.dirstate.copy(f2, fd) | ||||
Matt Mackall
|
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. | ||||
Gilles Moris
|
r11178 | if f2 == fd: # file not locally copied/moved | ||
repo.dirstate.normallookup(fd) | ||||
Matt Mackall
|
r3308 | if move: | ||
Matt Mackall
|
r14434 | repo.dirstate.drop(f) | ||
Matt Mackall
|
r3733 | elif m == "d": # directory rename | ||
f2, fd, flag = a[2:] | ||||
Matt Mackall
|
r4819 | if not f2 and f not in repo.dirstate: | ||
# untracked file moved | ||||
continue | ||||
Matt Mackall
|
r3733 | if branchmerge: | ||
Matt Mackall
|
r4904 | repo.dirstate.add(fd) | ||
Matt Mackall
|
r3733 | if f: | ||
Matt Mackall
|
r4904 | repo.dirstate.remove(f) | ||
Matt Mackall
|
r3733 | repo.dirstate.copy(f, fd) | ||
if f2: | ||||
repo.dirstate.copy(f2, fd) | ||||
else: | ||||
Matt Mackall
|
r4904 | repo.dirstate.normal(fd) | ||
Matt Mackall
|
r3733 | if f: | ||
Matt Mackall
|
r14434 | repo.dirstate.drop(f) | ||
Matt Mackall
|
r3111 | |||
Patrick Mezard
|
r16696 | def update(repo, node, branchmerge, force, partial, ancestor=None, | ||
mergeancestor=False): | ||||
Matt Mackall
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
Stuart W Marks
|
r9716 | node = the node to update to, or None if unspecified | ||
Matt Mackall
|
r3315 | 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) | ||||
Patrick Mezard
|
r16696 | mergeancestor = if false, merging with an ancestor (fast-forward) | ||
is only allowed between different named branches. This flag | ||||
is used by rebase extension as a temporary fix and should be | ||||
avoided in general. | ||||
Stuart W Marks
|
r9716 | |||
The table below shows all the behaviors of the update command | ||||
given the -c and -C or no options, whether the working directory | ||||
is dirty, whether a revision is specified, and the relationship of | ||||
the parent rev to the target rev (linear, on the same named | ||||
branch, or on another named branch). | ||||
Adrian Buehlmann
|
r12279 | This logic is tested by test-update-branches.t. | ||
Stuart W Marks
|
r9716 | |||
-c -C dirty rev | linear same cross | ||||
n n n n | ok (1) x | ||||
Stuart W Marks
|
r9717 | n n n y | ok ok ok | ||
n n y * | merge (2) (2) | ||||
Stuart W Marks
|
r9716 | n y * * | --- discard --- | ||
Stuart W Marks
|
r9717 | y n y * | --- (3) --- | ||
Stuart W Marks
|
r9716 | y n n * | --- ok --- | ||
Stuart W Marks
|
r9717 | y y * * | --- (4) --- | ||
Stuart W Marks
|
r9716 | |||
x = can't happen | ||||
* = don't-care | ||||
Stuart W Marks
|
r9717 | 1 = abort: crosses branches (use 'hg merge' or 'hg update -c') | ||
2 = abort: crosses branches (use 'hg merge' to merge or | ||||
use 'hg update -C' to discard changes) | ||||
3 = abort: uncommitted local changes | ||||
4 = incompatible options (checked in commands.py) | ||||
Greg Ward
|
r13162 | |||
Return the same tuple as applyupdates(). | ||||
Matt Mackall
|
r3315 | """ | ||
Matt Mackall
|
r2815 | |||
Stuart W Marks
|
r9717 | onode = node | ||
Matt Mackall
|
r4917 | wlock = repo.wlock() | ||
Matt Mackall
|
r4915 | try: | ||
Matt Mackall
|
r6747 | wc = repo[None] | ||
Matt Mackall
|
r4915 | if node is None: | ||
# tip of current branch | ||||
try: | ||||
Brodie Rao
|
r16719 | node = repo.branchtip(wc.branch()) | ||
except error.RepoLookupError: | ||||
Matt Mackall
|
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()) | ||||
Matt Mackall
|
r4915 | overwrite = force and not branchmerge | ||
pl = wc.parents() | ||||
Matt Mackall
|
r6747 | p1, p2 = pl[0], repo[node] | ||
Matt Mackall
|
r13874 | if ancestor: | ||
pa = repo[ancestor] | ||||
else: | ||||
pa = p1.ancestor(p2) | ||||
Matt Mackall
|
r4915 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) | ||
Matt Mackall
|
r3314 | |||
Matt Mackall
|
r4915 | ### check phase | ||
if not overwrite and len(pl) > 1: | ||||
raise util.Abort(_("outstanding uncommitted merges")) | ||||
Matt Mackall
|
r6375 | if branchmerge: | ||
if pa == p2: | ||||
Matt Mackall
|
r11417 | raise util.Abort(_("merging with a working directory ancestor" | ||
" has no effect")) | ||||
Matt Mackall
|
r6375 | elif pa == p1: | ||
Patrick Mezard
|
r16696 | if not mergeancestor and p1.branch() == p2.branch(): | ||
Kevin Bullock
|
r15619 | raise util.Abort(_("nothing to merge"), | ||
hint=_("use 'hg update' " | ||||
"or check 'hg heads'")) | ||||
Matt Mackall
|
r6375 | if not force and (wc.files() or wc.deleted()): | ||
Kevin Bullock
|
r15619 | raise util.Abort(_("outstanding uncommitted changes"), | ||
hint=_("use 'hg status' to list changes")) | ||||
Mads Kiilerich
|
r18364 | for s in sorted(wc.substate): | ||
Oleg Stepanov
|
r13437 | if wc.sub(s).dirty(): | ||
raise util.Abort(_("outstanding uncommitted changes in " | ||||
"subrepository '%s'") % s) | ||||
Matt Mackall
|
r6375 | elif not overwrite: | ||
Matt Mackall
|
r12401 | if pa == p1 or pa == p2: # linear | ||
Matt Mackall
|
r6375 | pass # all good | ||
Augie Fackler
|
r14663 | elif wc.dirty(missing=True): | ||
Brodie Rao
|
r12681 | raise util.Abort(_("crosses branches (merge branches or use" | ||
" --clean to discard changes)")) | ||||
Stuart W Marks
|
r9717 | elif onode is None: | ||
Brendan Cully
|
r14485 | raise util.Abort(_("crosses branches (merge branches or update" | ||
Brodie Rao
|
r12681 | " --check to force update)")) | ||
Matt Mackall
|
r6375 | else: | ||
Stuart W Marks
|
r9717 | # Allow jumping branches if clean and specific rev given | ||
Matt Mackall
|
r16092 | pa = p1 | ||
Matt Mackall
|
r2814 | |||
Matt Mackall
|
r4915 | ### calculate phase | ||
Mads Kiilerich
|
r18330 | actions = calculateupdates(repo, wc, p2, pa, | ||
branchmerge, force, partial) | ||||
Matt Mackall
|
r2775 | |||
Matt Mackall
|
r4915 | ### apply phase | ||
Matt Mackall
|
r13550 | if not branchmerge: # just jump to the new rev | ||
Matt Mackall
|
r4915 | fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||
if not partial: | ||||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||||
Matt Mackall
|
r2775 | |||
Mads Kiilerich
|
r18330 | stats = applyupdates(repo, actions, wc, p2, pa, overwrite) | ||
Matt Mackall
|
r2899 | |||
Matt Mackall
|
r4915 | if not partial: | ||
Patrick Mezard
|
r16551 | repo.setparents(fp1, fp2) | ||
Mads Kiilerich
|
r18330 | recordupdates(repo, actions, branchmerge) | ||
Mads Kiilerich
|
r13561 | if not branchmerge: | ||
Matt Mackall
|
r4915 | repo.dirstate.setbranch(p2.branch()) | ||
finally: | ||||
Ronny Pfannschmidt
|
r8109 | wlock.release() | ||
Sune Foldager
|
r10492 | |||
if not partial: | ||||
repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||||
return stats | ||||