merge.py
2429 lines
| 85.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
|
r2775 | # merge.py - directory-level update/merge handling for Mercurial | ||
# | ||||
|
r47575 | # Copyright 2006, 2007 Olivia Mackall <olivia@selenic.com> | ||
|
r2775 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2775 | |||
|
r25959 | from __future__ import absolute_import | ||
|
r45849 | import collections | ||
|
r25959 | import errno | ||
|
r42656 | import stat | ||
|
r20590 | import struct | ||
|
r25959 | from .i18n import _ | ||
|
r47771 | from .node import nullrev | ||
|
r43346 | from .thirdparty import attr | ||
|
r47519 | from .utils import stringutil | ||
|
r25959 | from . import ( | ||
copies, | ||||
|
r39843 | encoding, | ||
|
r26587 | error, | ||
|
r25959 | filemerge, | ||
|
r31257 | match as matchmod, | ||
|
r45383 | mergestate as mergestatemod, | ||
r33147 | obsutil, | |||
|
r44032 | pathutil, | ||
|
r30519 | pycompat, | ||
|
r27656 | scmutil, | ||
|
r36026 | subrepoutil, | ||
|
r25959 | util, | ||
worker, | ||||
) | ||||
|
r6512 | |||
|
r20590 | _pack = struct.pack | ||
_unpack = struct.unpack | ||||
|
r43346 | |||
|
r27740 | def _getcheckunknownconfig(repo, section, name): | ||
|
r34523 | config = repo.ui.config(section, name) | ||
|
r43347 | valid = [b'abort', b'ignore', b'warn'] | ||
|
r27740 | if config not in valid: | ||
|
r43347 | validstr = b', '.join([b"'" + v + b"'" for v in valid]) | ||
|
r43346 | raise error.ConfigError( | ||
|
r43387 | _(b"%s.%s not valid ('%s' is none of %s)") | ||
|
r43346 | % (section, name, config, validstr) | ||
) | ||||
|
r27740 | return config | ||
|
r43346 | |||
|
r23653 | def _checkunknownfile(repo, wctx, mctx, f, f2=None): | ||
|
r35289 | if wctx.isinmemory(): | ||
# Nothing to do in IMM because nothing in the "working copy" can be an | ||||
# unknown file. | ||||
# | ||||
# Note that we should bail out here, not in ``_checkunknownfiles()``, | ||||
# because that function does other useful work. | ||||
return False | ||||
|
r23653 | if f2 is None: | ||
f2 = f | ||||
|
r43346 | return ( | ||
repo.wvfs.audit.check(f) | ||||
|
r28088 | and repo.wvfs.isfileorlink(f) | ||
|
r16284 | and repo.dirstate.normalize(f) not in repo.dirstate | ||
|
r43346 | and mctx[f2].cmp(wctx[f]) | ||
) | ||||
|
r16093 | |||
|
r35181 | class _unknowndirschecker(object): | ||
|
r34551 | """ | ||
Look for any unknown files or directories that may have a path conflict | ||||
with a file. If any path prefix of the file exists as a file or link, | ||||
then it conflicts. If the file itself is a directory that contains any | ||||
file that is not tracked, then it conflicts. | ||||
Returns the shortest path at which a conflict occurs, or None if there is | ||||
no conflict. | ||||
""" | ||||
|
r43346 | |||
|
r35181 | def __init__(self): | ||
# A set of paths known to be good. This prevents repeated checking of | ||||
# dirs. It will be updated with any new dirs that are checked and found | ||||
# to be safe. | ||||
self._unknowndircache = set() | ||||
|
r34551 | |||
|
r35181 | # A set of paths that are known to be absent. This prevents repeated | ||
# checking of subdirectories that are known not to exist. It will be | ||||
# updated with any new dirs that are checked and found to be absent. | ||||
self._missingdircache = set() | ||||
|
r34551 | |||
|
r35289 | def __call__(self, repo, wctx, f): | ||
if wctx.isinmemory(): | ||||
# Nothing to do in IMM for the same reason as ``_checkunknownfile``. | ||||
return False | ||||
|
r35181 | # Check for path prefixes that exist as unknown files. | ||
|
r44032 | for p in reversed(list(pathutil.finddirs(f))): | ||
|
r35181 | if p in self._missingdircache: | ||
return | ||||
if p in self._unknowndircache: | ||||
continue | ||||
if repo.wvfs.audit.check(p): | ||||
|
r43346 | if ( | ||
repo.wvfs.isfileorlink(p) | ||||
and repo.dirstate.normalize(p) not in repo.dirstate | ||||
): | ||||
|
r35181 | return p | ||
if not repo.wvfs.lexists(p): | ||||
self._missingdircache.add(p) | ||||
return | ||||
self._unknowndircache.add(p) | ||||
# Check if the file conflicts with a directory containing unknown files. | ||||
if repo.wvfs.audit.check(f) and repo.wvfs.isdir(f): | ||||
# Does the directory contain any files that are not in the dirstate? | ||||
for p, dirs, files in repo.wvfs.walk(f): | ||||
for fn in files: | ||||
|
r37104 | relf = util.pconvert(repo.wvfs.reljoin(p, fn)) | ||
|
r37105 | relf = repo.dirstate.normalize(relf, isknown=True) | ||
|
r35181 | if relf not in repo.dirstate: | ||
return f | ||||
return None | ||||
|
r34551 | |||
|
r43346 | |||
|
r45844 | def _checkunknownfiles(repo, wctx, mctx, force, mresult, mergeforce): | ||
|
r23655 | """ | ||
Considers any actions that care about the presence of conflicting unknown | ||||
files. For some actions, the result is to abort; for others, it is to | ||||
choose a different action. | ||||
""" | ||||
|
r34552 | fileconflicts = set() | ||
|
r34553 | pathconflicts = set() | ||
|
r28018 | warnconflicts = set() | ||
abortconflicts = set() | ||||
|
r43347 | unknownconfig = _getcheckunknownconfig(repo, b'merge', b'checkunknown') | ||
ignoredconfig = _getcheckunknownconfig(repo, b'merge', b'checkignored') | ||||
pathconfig = repo.ui.configbool( | ||||
b'experimental', b'merge.checkpathconflicts' | ||||
) | ||||
|
r23655 | if not force: | ||
|
r43346 | |||
|
r27741 | def collectconflicts(conflicts, config): | ||
|
r43347 | if config == b'abort': | ||
|
r27741 | abortconflicts.update(conflicts) | ||
|
r43347 | elif config == b'warn': | ||
|
r27741 | warnconflicts.update(conflicts) | ||
|
r35181 | checkunknowndirs = _unknowndirschecker() | ||
|
r45905 | for f in mresult.files( | ||
( | ||||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
|
r45905 | ) | ||
|
r45850 | ): | ||
if _checkunknownfile(repo, wctx, mctx, f): | ||||
fileconflicts.add(f) | ||||
elif pathconfig and f not in wctx: | ||||
path = checkunknowndirs(repo, wctx, f) | ||||
if path is not None: | ||||
pathconflicts.add(path) | ||||
for f, args, msg in mresult.getactions( | ||||
[mergestatemod.ACTION_LOCAL_DIR_RENAME_GET] | ||||
): | ||||
if _checkunknownfile(repo, wctx, mctx, f, args[0]): | ||||
fileconflicts.add(f) | ||||
|
r23655 | |||
|
r34553 | allconflicts = fileconflicts | pathconflicts | ||
|
r43346 | ignoredconflicts = {c for c in allconflicts if repo.dirstate._ignore(c)} | ||
|
r34552 | unknownconflicts = allconflicts - ignoredconflicts | ||
|
r27742 | collectconflicts(ignoredconflicts, ignoredconfig) | ||
collectconflicts(unknownconflicts, unknownconfig) | ||||
|
r28022 | else: | ||
|
r45900 | for f, args, msg in list( | ||
mresult.getactions([mergestatemod.ACTION_CREATED_MERGE]) | ||||
|
r45850 | ): | ||
fl2, anc = args | ||||
different = _checkunknownfile(repo, wctx, mctx, f) | ||||
if repo.dirstate._ignore(f): | ||||
config = ignoredconfig | ||||
else: | ||||
config = unknownconfig | ||||
|
r28022 | |||
|
r45850 | # The behavior when force is True is described by this table: | ||
# config different mergeforce | action backup | ||||
# * n * | get n | ||||
# * y y | merge - | ||||
# abort y n | merge - (1) | ||||
# warn y n | warn + get y | ||||
# ignore y n | get y | ||||
# | ||||
# (1) this is probably the wrong behavior here -- we should | ||||
# probably abort, but some actions like rebases currently | ||||
# don't like an abort happening in the middle of | ||||
# merge.update. | ||||
if not different: | ||||
mresult.addfile( | ||||
f, | ||||
mergestatemod.ACTION_GET, | ||||
(fl2, False), | ||||
b'remote created', | ||||
) | ||||
elif mergeforce or config == b'abort': | ||||
mresult.addfile( | ||||
f, | ||||
mergestatemod.ACTION_MERGE, | ||||
(f, f, None, False, anc), | ||||
b'remote differs from untracked local', | ||||
) | ||||
elif config == b'abort': | ||||
abortconflicts.add(f) | ||||
else: | ||||
if config == b'warn': | ||||
warnconflicts.add(f) | ||||
mresult.addfile( | ||||
|
r46554 | f, | ||
mergestatemod.ACTION_GET, | ||||
(fl2, True), | ||||
b'remote created', | ||||
|
r45850 | ) | ||
|
r27741 | |||
|
r28018 | for f in sorted(abortconflicts): | ||
|
r34554 | warn = repo.ui.warn | ||
if f in pathconflicts: | ||||
if repo.wvfs.isfileorlink(f): | ||||
|
r43347 | warn(_(b"%s: untracked file conflicts with directory\n") % f) | ||
|
r34554 | else: | ||
|
r43347 | warn(_(b"%s: untracked directory conflicts with file\n") % f) | ||
|
r34554 | else: | ||
|
r43347 | warn(_(b"%s: untracked file differs\n") % f) | ||
|
r28018 | if abortconflicts: | ||
|
r47146 | raise error.StateError( | ||
|
r43346 | _( | ||
|
r43347 | b"untracked files in working directory " | ||
b"differ from files in requested revision" | ||||
|
r43346 | ) | ||
) | ||||
|
r28018 | |||
for f in sorted(warnconflicts): | ||||
|
r34554 | if repo.wvfs.isfileorlink(f): | ||
|
r43347 | repo.ui.warn(_(b"%s: replacing untracked file\n") % f) | ||
|
r34554 | else: | ||
|
r43347 | repo.ui.warn(_(b"%s: replacing untracked files in directory\n") % f) | ||
|
r23655 | |||
|
r45900 | for f, args, msg in list( | ||
mresult.getactions([mergestatemod.ACTION_CREATED]) | ||||
): | ||||
|
r45850 | backup = ( | ||
f in fileconflicts | ||||
or f in pathconflicts | ||||
or any(p in pathconflicts for p in pathutil.finddirs(f)) | ||||
) | ||||
(flags,) = args | ||||
mresult.addfile(f, mergestatemod.ACTION_GET, (flags, backup), msg) | ||||
|
r23655 | |||
|
r43346 | |||
|
r45907 | def _forgetremoved(wctx, mctx, branchmerge, mresult): | ||
|
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. | ||||
|
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. | ||||
|
r3107 | """ | ||
|
r45383 | m = mergestatemod.ACTION_FORGET | ||
|
r21545 | if branchmerge: | ||
|
r45383 | m = mergestatemod.ACTION_REMOVE | ||
|
r6242 | for f in wctx.deleted(): | ||
|
r6272 | if f not in mctx: | ||
|
r45907 | mresult.addfile(f, m, None, b"forget deleted") | ||
|
r6242 | |||
if not branchmerge: | ||||
for f in wctx.removed(): | ||||
|
r6272 | if f not in mctx: | ||
|
r45907 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_FORGET, | ||||
None, | ||||
b"forget removed", | ||||
|
r45383 | ) | ||
|
r3107 | |||
|
r43346 | |||
|
r45893 | def _checkcollision(repo, wmf, mresult): | ||
|
r38056 | """ | ||
Check for case-folding collisions. | ||||
""" | ||||
# If the repo is narrowed, filter out files outside the narrowspec. | ||||
narrowmatch = repo.narrowmatch() | ||||
if not narrowmatch.always(): | ||||
|
r44769 | pmmf = set(wmf.walk(narrowmatch)) | ||
|
r45893 | if mresult: | ||
|
r45905 | for f in list(mresult.files()): | ||
|
r45893 | if not narrowmatch(f): | ||
mresult.removefile(f) | ||||
|
r44769 | else: | ||
# build provisional merged manifest up | ||||
pmmf = set(wmf) | ||||
|
r19105 | |||
|
r45893 | if mresult: | ||
|
r37130 | # KEEP and EXEC are no-op | ||
|
r45905 | for f in mresult.files( | ||
|
r45893 | ( | ||
mergestatemod.ACTION_ADD, | ||||
mergestatemod.ACTION_ADD_MODIFIED, | ||||
mergestatemod.ACTION_FORGET, | ||||
mergestatemod.ACTION_GET, | ||||
mergestatemod.ACTION_CHANGED_DELETED, | ||||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
) | ||||
|
r43346 | ): | ||
|
r45893 | pmmf.add(f) | ||
|
r45905 | for f in mresult.files((mergestatemod.ACTION_REMOVE,)): | ||
|
r21545 | pmmf.discard(f) | ||
|
r45893 | for f, args, msg in mresult.getactions( | ||
[mergestatemod.ACTION_DIR_RENAME_MOVE_LOCAL] | ||||
): | ||||
|
r21545 | f2, flags = args | ||
pmmf.discard(f2) | ||||
pmmf.add(f) | ||||
|
r45905 | for f in mresult.files((mergestatemod.ACTION_LOCAL_DIR_RENAME_GET,)): | ||
|
r21545 | pmmf.add(f) | ||
|
r45893 | for f, args, msg in mresult.getactions([mergestatemod.ACTION_MERGE]): | ||
|
r21545 | f1, f2, fa, move, anc = args | ||
if move: | ||||
pmmf.discard(f1) | ||||
pmmf.add(f) | ||||
|
r19105 | |||
# check case-folding collision in provisional merged manifest | ||||
foldmap = {} | ||||
|
r33807 | for f in pmmf: | ||
|
r19105 | fold = util.normcase(f) | ||
if fold in foldmap: | ||||
|
r47146 | raise error.StateError( | ||
|
r43347 | _(b"case-folding collision between %s and %s") | ||
|
r43346 | % (f, foldmap[fold]) | ||
) | ||||
|
r19105 | foldmap[fold] = f | ||
|
r26661 | # check case-folding of directories | ||
|
r43347 | foldprefix = unfoldprefix = lastfull = b'' | ||
|
r26661 | for fold, f in sorted(foldmap.items()): | ||
if fold.startswith(foldprefix) and not f.startswith(unfoldprefix): | ||||
# the folded prefix matches but actual casing is different | ||||
|
r47146 | raise error.StateError( | ||
|
r43387 | _(b"case-folding collision between %s and directory of %s") | ||
|
r43346 | % (lastfull, f) | ||
) | ||||
|
r43347 | foldprefix = fold + b'/' | ||
unfoldprefix = f + b'/' | ||||
|
r26661 | lastfull = f | ||
|
r43346 | |||
|
r34556 | def _filesindirs(repo, manifest, dirs): | ||
""" | ||||
Generator that yields pairs of all the files in the manifest that are found | ||||
inside the directories listed in dirs, and which directory they are found | ||||
in. | ||||
""" | ||||
for f in manifest: | ||||
|
r44032 | for p in pathutil.finddirs(f): | ||
|
r34556 | if p in dirs: | ||
yield f, p | ||||
break | ||||
|
r43346 | |||
|
r45841 | def checkpathconflicts(repo, wctx, mctx, mresult): | ||
|
r34556 | """ | ||
Check if any actions introduce path conflicts in the repository, updating | ||||
actions to record or handle the path conflict accordingly. | ||||
""" | ||||
mf = wctx.manifest() | ||||
# The set of local files that conflict with a remote directory. | ||||
localconflicts = set() | ||||
# The set of directories that conflict with a remote file, and so may cause | ||||
# conflicts if they still contain any files after the merge. | ||||
remoteconflicts = set() | ||||
# The set of directories that appear as both a file and a directory in the | ||||
# remote manifest. These indicate an invalid remote manifest, which | ||||
# can't be updated to cleanly. | ||||
invalidconflicts = set() | ||||
|
r35182 | # The set of directories that contain files that are being created. | ||
createdfiledirs = set() | ||||
|
r34556 | # The set of files deleted by all the actions. | ||
deletedfiles = set() | ||||
|
r45905 | for f in mresult.files( | ||
|
r45902 | ( | ||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
mergestatemod.ACTION_MERGE, | ||||
mergestatemod.ACTION_CREATED_MERGE, | ||||
|
r45902 | ) | ||
): | ||||
# This action may create a new local file. | ||||
createdfiledirs.update(pathutil.finddirs(f)) | ||||
if mf.hasdir(f): | ||||
# The file aliases a local directory. This might be ok if all | ||||
# the files in the local directory are being deleted. This | ||||
# will be checked once we know what all the deleted files are. | ||||
remoteconflicts.add(f) | ||||
# Track the names of all deleted files. | ||||
|
r45905 | for f in mresult.files((mergestatemod.ACTION_REMOVE,)): | ||
|
r45902 | deletedfiles.add(f) | ||
for (f, args, msg) in mresult.getactions((mergestatemod.ACTION_MERGE,)): | ||||
f1, f2, fa, move, anc = args | ||||
if move: | ||||
deletedfiles.add(f1) | ||||
for (f, args, msg) in mresult.getactions( | ||||
(mergestatemod.ACTION_DIR_RENAME_MOVE_LOCAL,) | ||||
): | ||||
f2, flags = args | ||||
deletedfiles.add(f2) | ||||
|
r34556 | |||
|
r35182 | # Check all directories that contain created files for path conflicts. | ||
for p in createdfiledirs: | ||||
if p in mf: | ||||
if p in mctx: | ||||
# A file is in a directory which aliases both a local | ||||
# and a remote file. This is an internal inconsistency | ||||
# within the remote manifest. | ||||
invalidconflicts.add(p) | ||||
else: | ||||
# A file is in a directory which aliases a local file. | ||||
# We will need to rename the local file. | ||||
localconflicts.add(p) | ||||
|
r45904 | pd = mresult.getfile(p) | ||
if pd and pd[0] in ( | ||||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
mergestatemod.ACTION_MERGE, | ||||
mergestatemod.ACTION_CREATED_MERGE, | ||||
|
r43346 | ): | ||
|
r35182 | # The file is in a directory which aliases a remote file. | ||
# This is an internal inconsistency within the remote | ||||
# manifest. | ||||
invalidconflicts.add(p) | ||||
|
r34556 | # Rename all local conflicting files that have not been deleted. | ||
for p in localconflicts: | ||||
if p not in deletedfiles: | ||||
|
r43347 | ctxname = bytes(wctx).rstrip(b'+') | ||
|
r45905 | pnew = util.safename(p, ctxname, wctx, set(mresult.files())) | ||
|
r45465 | porig = wctx[p].copysource() or p | ||
|
r45841 | mresult.addfile( | ||
pnew, | ||||
|
r45383 | mergestatemod.ACTION_PATH_CONFLICT_RESOLVE, | ||
|
r45465 | (p, porig), | ||
|
r43347 | b'local path conflict', | ||
|
r43346 | ) | ||
|
r45841 | mresult.addfile( | ||
p, | ||||
|
r45383 | mergestatemod.ACTION_PATH_CONFLICT, | ||
(pnew, b'l'), | ||||
b'path conflict', | ||||
) | ||||
|
r34556 | |||
if remoteconflicts: | ||||
# Check if all files in the conflicting directories have been removed. | ||||
|
r43347 | ctxname = bytes(mctx).rstrip(b'+') | ||
|
r34556 | for f, p in _filesindirs(repo, mf, remoteconflicts): | ||
if f not in deletedfiles: | ||||
|
r45904 | m, args, msg = mresult.getfile(p) | ||
|
r45905 | pnew = util.safename(p, ctxname, wctx, set(mresult.files())) | ||
|
r45383 | if m in ( | ||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
mergestatemod.ACTION_MERGE, | ||||
): | ||||
|
r34556 | # Action was merge, just update target. | ||
|
r45841 | mresult.addfile(pnew, m, args, msg) | ||
|
r34556 | else: | ||
# Action was create, change to renamed get action. | ||||
fl = args[0] | ||||
|
r45841 | mresult.addfile( | ||
pnew, | ||||
|
r45383 | mergestatemod.ACTION_LOCAL_DIR_RENAME_GET, | ||
|
r43346 | (p, fl), | ||
|
r43347 | b'remote path conflict', | ||
|
r43346 | ) | ||
|
r45841 | mresult.addfile( | ||
p, | ||||
|
r45383 | mergestatemod.ACTION_PATH_CONFLICT, | ||
(pnew, mergestatemod.ACTION_REMOVE), | ||||
|
r43347 | b'path conflict', | ||
|
r43346 | ) | ||
|
r34556 | remoteconflicts.remove(p) | ||
break | ||||
if invalidconflicts: | ||||
for p in invalidconflicts: | ||||
|
r43347 | repo.ui.warn(_(b"%s: is both a file and a directory\n") % p) | ||
|
r47146 | raise error.StateError( | ||
_(b"destination manifest contains path conflicts") | ||||
) | ||||
|
r34556 | |||
|
r43346 | |||
|
r45842 | def _filternarrowactions(narrowmatch, branchmerge, mresult): | ||
|
r38055 | """ | ||
Filters out actions that can ignored because the repo is narrowed. | ||||
Raise an exception if the merge cannot be completed because the repo is | ||||
narrowed. | ||||
""" | ||||
|
r45903 | # TODO: handle with nonconflicttypes | ||
nonconflicttypes = { | ||||
mergestatemod.ACTION_ADD, | ||||
mergestatemod.ACTION_ADD_MODIFIED, | ||||
mergestatemod.ACTION_CREATED, | ||||
mergestatemod.ACTION_CREATED_MERGE, | ||||
mergestatemod.ACTION_FORGET, | ||||
mergestatemod.ACTION_GET, | ||||
mergestatemod.ACTION_REMOVE, | ||||
mergestatemod.ACTION_EXEC, | ||||
} | ||||
|
r38055 | # We mutate the items in the dict during iteration, so iterate | ||
# over a copy. | ||||
|
r45906 | for f, action in mresult.filemap(): | ||
|
r38055 | if narrowmatch(f): | ||
pass | ||||
elif not branchmerge: | ||||
|
r45842 | mresult.removefile(f) # just updating, ignore changes outside clone | ||
|
r46096 | elif action[0] in mergestatemod.NO_OP_ACTIONS: | ||
|
r45842 | mresult.removefile(f) # merge does not affect file | ||
|
r38055 | elif action[0] in nonconflicttypes: | ||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b'merge affects file \'%s\' outside narrow, ' | ||
b'which is not yet supported' | ||||
|
r43346 | ) | ||
% f, | ||||
|
r43387 | hint=_(b'merging in the other direction may work'), | ||
|
r43346 | ) | ||
|
r38055 | else: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'conflict in file \'%s\' is outside narrow clone') % f | ||
|
r43346 | ) | ||
|
r38055 | |||
|
r45831 | class mergeresult(object): | ||
|
r46554 | """An object representing result of merging manifests. | ||
|
r45831 | |||
It has information about what actions need to be performed on dirstate | ||||
|
r46554 | mapping of divergent renames and other such cases.""" | ||
|
r45831 | |||
|
r45838 | def __init__(self): | ||
|
r45831 | """ | ||
|
r45848 | filemapping: dict of filename as keys and action related info as values | ||
|
r45831 | diverge: mapping of source name -> list of dest name for | ||
divergent renames | ||||
renamedelete: mapping of source name -> list of destinations for files | ||||
deleted on one side and renamed on other. | ||||
|
r45832 | commitinfo: dict containing data which should be used on commit | ||
contains a filename -> info mapping | ||||
|
r45856 | actionmapping: dict of action names as keys and values are dict of | ||
filename as key and related data as values | ||||
|
r45831 | """ | ||
|
r45848 | self._filemapping = {} | ||
|
r45838 | self._diverge = {} | ||
self._renamedelete = {} | ||||
|
r45943 | self._commitinfo = collections.defaultdict(dict) | ||
|
r45856 | self._actionmapping = collections.defaultdict(dict) | ||
|
r45831 | |||
|
r45944 | def updatevalues(self, diverge, renamedelete): | ||
|
r45831 | self._diverge = diverge | ||
self._renamedelete = renamedelete | ||||
|
r45839 | def addfile(self, filename, action, data, message): | ||
|
r46554 | """adds a new file to the mergeresult object | ||
|
r45839 | |||
filename: file which we are adding | ||||
action: one of mergestatemod.ACTION_* | ||||
data: a tuple of information like fctx and ctx related to this merge | ||||
message: a message about the merge | ||||
""" | ||||
|
r45849 | # if the file already existed, we need to delete it's old | ||
# entry form _actionmapping too | ||||
if filename in self._filemapping: | ||||
a, d, m = self._filemapping[filename] | ||||
|
r45856 | del self._actionmapping[a][filename] | ||
|
r45849 | |||
|
r45848 | self._filemapping[filename] = (action, data, message) | ||
|
r45856 | self._actionmapping[action][filename] = (data, message) | ||
|
r45839 | |||
|
r45904 | def getfile(self, filename, default_return=None): | ||
|
r46554 | """returns (action, args, msg) about this file | ||
|
r45904 | |||
|
r46554 | returns default_return if the file is not present""" | ||
|
r45904 | if filename in self._filemapping: | ||
return self._filemapping[filename] | ||||
return default_return | ||||
|
r45905 | def files(self, actions=None): | ||
|
r46554 | """returns files on which provided action needs to perfromed | ||
|
r45905 | |||
If actions is None, all files are returned | ||||
""" | ||||
# TODO: think whether we should return renamedelete and | ||||
# diverge filenames also | ||||
if actions is None: | ||||
for f in self._filemapping: | ||||
yield f | ||||
else: | ||||
for a in actions: | ||||
for f in self._actionmapping[a]: | ||||
yield f | ||||
|
r45840 | def removefile(self, filename): | ||
|
r46554 | """removes a file from the mergeresult object as the file might | ||
not merging anymore""" | ||||
|
r45849 | action, data, message = self._filemapping[filename] | ||
|
r45848 | del self._filemapping[filename] | ||
|
r45856 | del self._actionmapping[action][filename] | ||
|
r45849 | |||
|
r45896 | def getactions(self, actions, sort=False): | ||
|
r46554 | """get list of files which are marked with these actions | ||
|
r45896 | if sort is true, files for each action is sorted and then added | ||
|
r45849 | |||
Returns a list of tuple of form (filename, data, message) | ||||
""" | ||||
for a in actions: | ||||
|
r45896 | if sort: | ||
for f in sorted(self._actionmapping[a]): | ||||
args, msg = self._actionmapping[a][f] | ||||
|
r45900 | yield f, args, msg | ||
|
r45896 | else: | ||
for f, (args, msg) in pycompat.iteritems( | ||||
self._actionmapping[a] | ||||
): | ||||
|
r45900 | yield f, args, msg | ||
|
r45840 | |||
|
r45898 | def len(self, actions=None): | ||
|
r46554 | """returns number of files which needs actions | ||
|
r45898 | |||
if actions is passed, total of number of files in that action | ||||
|
r46554 | only is returned""" | ||
|
r45898 | |||
if actions is None: | ||||
return len(self._filemapping) | ||||
return sum(len(self._actionmapping[a]) for a in actions) | ||||
|
r45906 | def filemap(self, sort=False): | ||
if sorted: | ||||
for key, val in sorted(pycompat.iteritems(self._filemapping)): | ||||
yield key, val | ||||
else: | ||||
for key, val in pycompat.iteritems(self._filemapping): | ||||
yield key, val | ||||
|
r45831 | |||
|
r45944 | def addcommitinfo(self, filename, key, value): | ||
|
r46554 | """adds key-value information about filename which will be required | ||
while committing this merge""" | ||||
|
r45944 | self._commitinfo[filename][key] = value | ||
|
r45831 | @property | ||
def diverge(self): | ||||
return self._diverge | ||||
@property | ||||
def renamedelete(self): | ||||
return self._renamedelete | ||||
|
r45832 | @property | ||
def commitinfo(self): | ||||
return self._commitinfo | ||||
|
r45836 | @property | ||
def actionsdict(self): | ||||
|
r46554 | """returns a dictionary of actions to be perfomed with action as key | ||
and a list of files and related arguments as values""" | ||||
|
r45908 | res = collections.defaultdict(list) | ||
|
r45856 | for a, d in pycompat.iteritems(self._actionmapping): | ||
for f, (args, msg) in pycompat.iteritems(d): | ||||
res[a].append((f, args, msg)) | ||||
return res | ||||
|
r45836 | |||
|
r45831 | def setactions(self, actions): | ||
|
r45848 | self._filemapping = actions | ||
|
r45856 | self._actionmapping = collections.defaultdict(dict) | ||
|
r45849 | for f, (act, data, msg) in pycompat.iteritems(self._filemapping): | ||
|
r45856 | self._actionmapping[act][f] = data, msg | ||
|
r45831 | |||
|
r45835 | def hasconflicts(self): | ||
|
r46554 | """tells whether this merge resulted in some actions which can | ||
result in conflicts or not""" | ||||
|
r45850 | for a in self._actionmapping.keys(): | ||
if ( | ||||
a | ||||
not in ( | ||||
mergestatemod.ACTION_GET, | ||||
mergestatemod.ACTION_EXEC, | ||||
mergestatemod.ACTION_REMOVE, | ||||
mergestatemod.ACTION_PATH_CONFLICT_RESOLVE, | ||||
) | ||||
and self._actionmapping[a] | ||||
|
r46096 | and a not in mergestatemod.NO_OP_ACTIONS | ||
|
r45835 | ): | ||
return True | ||||
return False | ||||
|
r45831 | |||
|
r43346 | def manifestmerge( | ||
repo, | ||||
wctx, | ||||
p2, | ||||
pa, | ||||
branchmerge, | ||||
force, | ||||
matcher, | ||||
acceptremote, | ||||
followcopies, | ||||
forcefulldiff=False, | ||||
): | ||||
|
r3105 | """ | ||
|
r30096 | Merge wctx and p2 with ancestor pa and generate merge action list | ||
|
r3315 | |||
|
r18605 | branchmerge and force are as passed in to update | ||
|
r27346 | matcher = matcher to filter file lists | ||
|
r18778 | acceptremote = accept the incoming changes without prompting | ||
|
r45728 | |||
|
r45831 | Returns an object of mergeresult class | ||
|
r3105 | """ | ||
|
r45839 | mresult = mergeresult() | ||
|
r27346 | if matcher is not None and matcher.always(): | ||
matcher = None | ||||
|
r3105 | |||
|
r18651 | # manifests fetched in order are going to be faster, so prime the caches | ||
|
r43346 | [ | ||
x.manifest() | ||||
for x in sorted(wctx.parents() + [p2, pa], key=scmutil.intrev) | ||||
] | ||||
|
r18651 | |||
|
r44682 | branch_copies1 = copies.branch_copies() | ||
branch_copies2 = copies.branch_copies() | ||||
diverge = {} | ||||
|
r45832 | # information from merge which is needed at commit time | ||
# for example choosing filelog of which parent to commit | ||||
# TODO: use specific constants in future for this mapping | ||||
|
r18651 | if followcopies: | ||
|
r44682 | branch_copies1, branch_copies2, diverge = copies.mergecopies( | ||
repo, wctx, p2, pa | ||||
) | ||||
|
r8753 | |||
|
r32641 | boolbm = pycompat.bytestr(bool(branchmerge)) | ||
boolf = pycompat.bytestr(bool(force)) | ||||
boolm = pycompat.bytestr(bool(matcher)) | ||||
|
r43347 | repo.ui.note(_(b"resolving manifests\n")) | ||
|
r43346 | repo.ui.debug( | ||
|
r43347 | b" branchmerge: %s, force: %s, partial: %s\n" % (boolbm, boolf, boolm) | ||
|
r43346 | ) | ||
|
r43347 | repo.ui.debug(b" ancestor: %s, local: %s, remote: %s\n" % (pa, wctx, p2)) | ||
|
r8753 | |||
|
r18611 | m1, m2, ma = wctx.manifest(), p2.manifest(), pa.manifest() | ||
|
r44682 | copied1 = set(branch_copies1.copy.values()) | ||
copied1.update(branch_copies1.movewithdir.values()) | ||||
copied2 = set(branch_copies2.copy.values()) | ||||
copied2.update(branch_copies2.movewithdir.values()) | ||||
|
r3295 | |||
|
r43347 | if b'.hgsubstate' in m1 and wctx.rev() is None: | ||
|
r38444 | # Check whether sub state is modified, and overwrite the manifest | ||
# to flag the change. If wctx is a committed revision, we shouldn't | ||||
# care for the dirty state of the working directory. | ||||
|
r28226 | if any(wctx.sub(s).dirty() for s in wctx.substate): | ||
|
r47771 | m1[b'.hgsubstate'] = repo.nodeconstants.modifiednodeid | ||
|
r9783 | |||
|
r32151 | # Don't use m2-vs-ma optimization if: | ||
# - ma is the same as m1 or m2, which we're just going to diff again later | ||||
# - The caller specifically asks for a full diff, which is useful during bid | ||||
# merge. | ||||
|
r46154 | # - we are tracking salvaged files specifically hence should process all | ||
# files | ||||
if ( | ||||
pa not in ([wctx, p2] + wctx.parents()) | ||||
and not forcefulldiff | ||||
r46253 | and not ( | |||
repo.ui.configbool(b'experimental', b'merge-track-salvaged') | ||||
or repo.filecopiesmode == b'changeset-sidedata' | ||||
) | ||||
|
r46154 | ): | ||
|
r32151 | # Identify which files are relevant to the merge, so we can limit the | ||
# total m1-vs-m2 diff to just those files. This has significant | ||||
# performance benefits in large repositories. | ||||
relevantfiles = set(ma.diff(m2).keys()) | ||||
# For copied and moved files, we need to add the source file too. | ||||
|
r44682 | for copykey, copyvalue in pycompat.iteritems(branch_copies1.copy): | ||
|
r32151 | if copyvalue in relevantfiles: | ||
relevantfiles.add(copykey) | ||||
|
r44682 | for movedirkey in branch_copies1.movewithdir: | ||
|
r32151 | relevantfiles.add(movedirkey) | ||
|
r32498 | filesmatcher = scmutil.matchfiles(repo, relevantfiles) | ||
matcher = matchmod.intersectmatchers(matcher, filesmatcher) | ||||
|
r32151 | |||
|
r31257 | diff = m1.diff(m2, match=matcher) | ||
|
r43376 | for f, ((n1, fl1), (n2, fl2)) in pycompat.iteritems(diff): | ||
|
r43346 | if n1 and n2: # file exists on both local and remote side | ||
|
r23396 | if f not in ma: | ||
|
r44682 | # TODO: what if they're renamed from different sources? | ||
fa = branch_copies1.copy.get( | ||||
f, None | ||||
) or branch_copies2.copy.get(f, None) | ||||
|
r45853 | args, msg = None, None | ||
|
r23397 | if fa is not None: | ||
|
r45853 | args = (f, f, fa, False, pa.node()) | ||
msg = b'both renamed from %s' % fa | ||||
|
r18338 | else: | ||
|
r45853 | args = (f, f, None, False, pa.node()) | ||
msg = b'both created' | ||||
mresult.addfile(f, mergestatemod.ACTION_MERGE, args, msg) | ||||
|
r46421 | elif f in branch_copies1.copy: | ||
fa = branch_copies1.copy[f] | ||||
mresult.addfile( | ||||
f, | ||||
mergestatemod.ACTION_MERGE, | ||||
(f, fa, fa, False, pa.node()), | ||||
b'local replaced from %s' % fa, | ||||
) | ||||
elif f in branch_copies2.copy: | ||||
fa = branch_copies2.copy[f] | ||||
mresult.addfile( | ||||
f, | ||||
mergestatemod.ACTION_MERGE, | ||||
(fa, f, fa, False, pa.node()), | ||||
b'other replaced from %s' % fa, | ||||
) | ||||
|
r16094 | else: | ||
|
r23396 | a = ma[f] | ||
fla = ma.flags(f) | ||||
|
r43347 | nol = b'l' not in fl1 + fl2 + fla | ||
|
r23395 | if n2 == a and fl2 == fla: | ||
|
r45839 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_KEEP, | ||||
(), | ||||
b'remote unchanged', | ||||
|
r45383 | ) | ||
|
r43346 | elif n1 == a and fl1 == fla: # local unchanged - use remote | ||
if n1 == n2: # optimization: keep local content | ||||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_EXEC, | ||
|
r43347 | (fl2,), | ||
b'update permissions', | ||||
) | ||||
|
r23395 | else: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45834 | mergestatemod.ACTION_GET, | ||
|
r43346 | (fl2, False), | ||
|
r43347 | b'remote is newer', | ||
|
r43346 | ) | ||
|
r45832 | if branchmerge: | ||
|
r45944 | mresult.addcommitinfo( | ||
f, b'filenode-source', b'other' | ||||
) | ||||
|
r43346 | elif nol and n2 == a: # remote only changed 'x' | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_EXEC, | ||
(fl2,), | ||||
b'update permissions', | ||||
) | ||||
|
r43346 | elif nol and n1 == a: # local only changed 'x' | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45834 | mergestatemod.ACTION_GET, | ||
|
r45178 | (fl1, False), | ||
b'remote is newer', | ||||
) | ||||
|
r45832 | if branchmerge: | ||
|
r45944 | mresult.addcommitinfo(f, b'filenode-source', b'other') | ||
|
r43346 | else: # both changed something | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_MERGE, | ||
|
r43346 | (f, f, f, False, pa.node()), | ||
|
r43347 | b'versions differ', | ||
|
r43346 | ) | ||
elif n1: # file exists only on local side | ||||
|
r44682 | if f in copied2: | ||
|
r43346 | pass # we'll deal with it on m2 side | ||
|
r44682 | elif ( | ||
f in branch_copies1.movewithdir | ||||
): # directory rename, move local | ||||
f2 = branch_copies1.movewithdir[f] | ||||
|
r31515 | if f2 in m2: | ||
|
r45839 | mresult.addfile( | ||
f2, | ||||
|
r45383 | mergestatemod.ACTION_MERGE, | ||
|
r43346 | (f, f2, None, True, pa.node()), | ||
|
r43347 | b'remote directory rename, both created', | ||
|
r43346 | ) | ||
|
r23475 | else: | ||
|
r45839 | mresult.addfile( | ||
f2, | ||||
|
r45383 | mergestatemod.ACTION_DIR_RENAME_MOVE_LOCAL, | ||
|
r43346 | (f, fl1), | ||
|
r43347 | b'remote directory rename - move from %s' % f, | ||
|
r43346 | ) | ||
|
r44682 | elif f in branch_copies1.copy: | ||
f2 = branch_copies1.copy[f] | ||||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_MERGE, | ||
|
r43346 | (f, f2, f2, False, pa.node()), | ||
|
r43347 | b'local copied/moved from %s' % f2, | ||
|
r43346 | ) | ||
elif f in ma: # clean, a different, no remote | ||||
|
r23473 | if n1 != ma[f]: | ||
if acceptremote: | ||||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_REMOVE, | ||
None, | ||||
b'remote delete', | ||||
) | ||||
|
r23473 | else: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_CHANGED_DELETED, | ||
|
r43346 | (f, None, f, False, pa.node()), | ||
|
r43347 | b'prompt changed/deleted', | ||
|
r43346 | ) | ||
|
r46158 | if branchmerge: | ||
mresult.addcommitinfo( | ||||
f, b'merge-removal-candidate', b'yes' | ||||
) | ||||
|
r47771 | elif n1 == repo.nodeconstants.addednodeid: | ||
|
r45678 | # This file was locally added. We should forget it instead of | ||
|
r23473 | # deleting it. | ||
|
r45839 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_FORGET, | ||||
None, | ||||
b'remote deleted', | ||||
|
r45383 | ) | ||
|
r20639 | else: | ||
|
r45839 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_REMOVE, | ||||
None, | ||||
b'other deleted', | ||||
|
r45383 | ) | ||
|
r46161 | if branchmerge: | ||
# the file must be absent after merging, | ||||
# howeber the user might make | ||||
# the file reappear using revert and if they does, | ||||
# we force create a new node | ||||
mresult.addcommitinfo( | ||||
f, b'merge-removal-candidate', b'yes' | ||||
) | ||||
|
r46041 | else: # file not in ancestor, not in remote | ||
mresult.addfile( | ||||
f, | ||||
|
r46095 | mergestatemod.ACTION_KEEP_NEW, | ||
|
r46041 | None, | ||
b'ancestor missing, remote missing', | ||||
) | ||||
|
r43346 | elif n2: # file exists only on remote side | ||
|
r44682 | if f in copied1: | ||
|
r43346 | pass # we'll deal with it on m1 side | ||
|
r44682 | elif f in branch_copies2.movewithdir: | ||
f2 = branch_copies2.movewithdir[f] | ||||
|
r31515 | if f2 in m1: | ||
|
r45839 | mresult.addfile( | ||
f2, | ||||
|
r45383 | mergestatemod.ACTION_MERGE, | ||
|
r43346 | (f2, f, None, False, pa.node()), | ||
|
r43347 | b'local directory rename, both created', | ||
|
r43346 | ) | ||
|
r23476 | else: | ||
|
r45839 | mresult.addfile( | ||
f2, | ||||
|
r45383 | mergestatemod.ACTION_LOCAL_DIR_RENAME_GET, | ||
|
r43346 | (f, fl2), | ||
|
r43347 | b'local directory rename - get from %s' % f, | ||
|
r43346 | ) | ||
|
r44682 | elif f in branch_copies2.copy: | ||
f2 = branch_copies2.copy[f] | ||||
|
r45853 | msg, args = None, None | ||
|
r31515 | if f2 in m2: | ||
|
r45853 | args = (f2, f, f2, False, pa.node()) | ||
msg = b'remote copied from %s' % f2 | ||||
|
r23473 | else: | ||
|
r45853 | args = (f2, f, f2, True, pa.node()) | ||
msg = b'remote moved from %s' % f2 | ||||
mresult.addfile(f, mergestatemod.ACTION_MERGE, args, msg) | ||||
|
r23473 | elif f not in ma: | ||
# local unknown, remote created: the logic is described by the | ||||
# following table: | ||||
# | ||||
# force branchmerge different | action | ||||
|
r23651 | # n * * | create | ||
|
r23650 | # y n * | create | ||
# y y n | create | ||||
|
r23473 | # y y y | merge | ||
# | ||||
# Checking whether the files are different is expensive, so we | ||||
# don't do that when we can avoid it. | ||||
|
r23649 | if not force: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
(fl2,), | ||||
b'remote created', | ||||
) | ||||
|
r23649 | elif not branchmerge: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
(fl2,), | ||||
b'remote created', | ||||
) | ||||
|
r23473 | else: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_CREATED_MERGE, | ||
|
r43346 | (fl2, pa.node()), | ||
|
r43347 | b'remote created, get or merge', | ||
|
r43346 | ) | ||
|
r23473 | elif n2 != ma[f]: | ||
|
r30581 | df = None | ||
|
r44682 | for d in branch_copies1.dirmove: | ||
|
r30581 | if f.startswith(d): | ||
# new file added in a directory that was moved | ||||
|
r44682 | df = branch_copies1.dirmove[d] + f[len(d) :] | ||
|
r30581 | break | ||
|
r31515 | if df is not None and df in m1: | ||
|
r45839 | mresult.addfile( | ||
df, | ||||
|
r45383 | mergestatemod.ACTION_MERGE, | ||
|
r43346 | (df, f, f, False, pa.node()), | ||
|
r43347 | b'local directory rename - respect move ' | ||
b'from %s' % f, | ||||
|
r43346 | ) | ||
|
r30581 | elif acceptremote: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_CREATED, | ||
(fl2,), | ||||
b'remote recreating', | ||||
) | ||||
|
r18606 | else: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_DELETED_CHANGED, | ||
|
r43346 | (None, f, f, False, pa.node()), | ||
|
r43347 | b'prompt deleted/changed', | ||
|
r43346 | ) | ||
|
r46158 | if branchmerge: | ||
mresult.addcommitinfo( | ||||
f, b'merge-removal-candidate', b'yes' | ||||
) | ||||
|
r46040 | else: | ||
mresult.addfile( | ||||
f, | ||||
mergestatemod.ACTION_KEEP_ABSENT, | ||||
None, | ||||
b'local not present, remote unchanged', | ||||
) | ||||
|
r46161 | if branchmerge: | ||
# the file must be absent after merging | ||||
# however the user might make | ||||
# the file reappear using revert and if they does, | ||||
# we force create a new node | ||||
mresult.addcommitinfo(f, b'merge-removal-candidate', b'yes') | ||||
|
r23651 | |||
|
r43347 | if repo.ui.configbool(b'experimental', b'merge.checkpathconflicts'): | ||
|
r34942 | # If we are merging, look for path conflicts. | ||
|
r45841 | checkpathconflicts(repo, wctx, p2, mresult) | ||
|
r34556 | |||
|
r38055 | narrowmatch = repo.narrowmatch() | ||
if not narrowmatch.always(): | ||||
# Updates "actions" in place | ||||
|
r45842 | _filternarrowactions(narrowmatch, branchmerge, mresult) | ||
|
r38055 | |||
|
r44682 | renamedelete = branch_copies1.renamedelete | ||
renamedelete.update(branch_copies2.renamedelete) | ||||
|
r45944 | mresult.updatevalues(diverge, renamedelete) | ||
|
r45838 | return mresult | ||
|
r3105 | |||
|
r43346 | |||
|
r45843 | def _resolvetrivial(repo, wctx, mctx, ancestor, mresult): | ||
|
r23531 | """Resolves false conflicts where the nodeid changed but the content | ||
|
r46554 | remained the same.""" | ||
|
r36334 | # We force a copy of actions.items() because we're going to mutate | ||
# actions as we resolve trivial conflicts. | ||||
|
r45905 | for f in list(mresult.files((mergestatemod.ACTION_CHANGED_DELETED,))): | ||
|
r45850 | if f in ancestor and not wctx[f].cmp(ancestor[f]): | ||
|
r23531 | # local did change but ended up with same content | ||
|
r45843 | mresult.addfile( | ||
f, mergestatemod.ACTION_REMOVE, None, b'prompt same' | ||||
) | ||||
|
r45850 | |||
|
r45905 | for f in list(mresult.files((mergestatemod.ACTION_DELETED_CHANGED,))): | ||
|
r45850 | if f in ancestor and not mctx[f].cmp(ancestor[f]): | ||
|
r23531 | # remote did change but ended up with same content | ||
|
r45843 | mresult.removefile(f) # don't get = keep local deleted | ||
|
r43346 | |||
|
r23531 | |||
|
r43346 | def calculateupdates( | ||
repo, | ||||
wctx, | ||||
mctx, | ||||
ancestors, | ||||
branchmerge, | ||||
force, | ||||
acceptremote, | ||||
followcopies, | ||||
matcher=None, | ||||
mergeforce=False, | ||||
): | ||||
|
r45728 | """ | ||
Calculate the actions needed to merge mctx into wctx using ancestors | ||||
Uses manifestmerge() to merge manifest and get list of actions required to | ||||
perform for merging two manifests. If there are multiple ancestors, uses bid | ||||
merge if enabled. | ||||
Also filters out actions which are unrequired if repository is sparse. | ||||
|
r45831 | Returns mergeresult object same as manifestmerge(). | ||
|
r45728 | """ | ||
|
r33323 | # Avoid cycle. | ||
from . import sparse | ||||
|
r45838 | mresult = None | ||
|
r43346 | if len(ancestors) == 1: # default | ||
|
r45831 | mresult = manifestmerge( | ||
|
r43346 | repo, | ||
wctx, | ||||
mctx, | ||||
ancestors[0], | ||||
branchmerge, | ||||
force, | ||||
matcher, | ||||
acceptremote, | ||||
followcopies, | ||||
) | ||||
|
r45844 | _checkunknownfiles(repo, wctx, mctx, force, mresult, mergeforce) | ||
|
r23385 | |||
|
r43346 | else: # only when merge.preferancestor=* - the default | ||
|
r23385 | repo.ui.note( | ||
|
r43347 | _(b"note: merging %s and %s using bids from ancestors %s\n") | ||
|
r43346 | % ( | ||
wctx, | ||||
mctx, | ||||
|
r43347 | _(b' and ').join(pycompat.bytestr(anc) for anc in ancestors), | ||
|
r43346 | ) | ||
) | ||||
|
r23385 | |||
|
r45837 | # mapping filename to bids (action method to list af actions) | ||
# {FILENAME1 : BID1, FILENAME2 : BID2} | ||||
# BID is another dictionary which contains | ||||
# mapping of following form: | ||||
# {ACTION_X : [info, ..], ACTION_Y : [info, ..]} | ||||
fbids = {} | ||||
|
r46042 | mresult = mergeresult() | ||
|
r23526 | diverge, renamedelete = None, None | ||
|
r23385 | for ancestor in ancestors: | ||
|
r43347 | repo.ui.note(_(b'\ncalculating bids for ancestor %s\n') % ancestor) | ||
|
r45831 | mresult1 = manifestmerge( | ||
|
r43346 | repo, | ||
wctx, | ||||
mctx, | ||||
ancestor, | ||||
branchmerge, | ||||
force, | ||||
matcher, | ||||
acceptremote, | ||||
followcopies, | ||||
forcefulldiff=True, | ||||
) | ||||
|
r45844 | _checkunknownfiles(repo, wctx, mctx, force, mresult1, mergeforce) | ||
|
r26318 | |||
# Track the shortest set of warning on the theory that bid | ||||
# merge will correctly incorporate more information | ||||
|
r45831 | if diverge is None or len(mresult1.diverge) < len(diverge): | ||
diverge = mresult1.diverge | ||||
if renamedelete is None or len(renamedelete) < len( | ||||
mresult1.renamedelete | ||||
): | ||||
renamedelete = mresult1.renamedelete | ||||
|
r26318 | |||
|
r46042 | # blindly update final mergeresult commitinfo with what we get | ||
# from mergeresult object for each ancestor | ||||
# TODO: some commitinfo depends on what bid merge choose and hence | ||||
# we will need to make commitinfo also depend on bid merge logic | ||||
mresult._commitinfo.update(mresult1._commitinfo) | ||||
|
r45906 | for f, a in mresult1.filemap(sort=True): | ||
|
r23638 | m, args, msg = a | ||
|
r43347 | repo.ui.debug(b' %s: %s -> %s\n' % (f, msg, m)) | ||
|
r23638 | if f in fbids: | ||
d = fbids[f] | ||||
if m in d: | ||||
d[m].append(a) | ||||
|
r23385 | else: | ||
|
r23638 | d[m] = [a] | ||
else: | ||||
fbids[f] = {m: [a]} | ||||
|
r23385 | |||
|
r45837 | # Call for bids | ||
|
r23385 | # Pick the best bid for each file | ||
|
r46018 | repo.ui.note( | ||
_(b'\nauction for merging merge bids (%d ancestors)\n') | ||||
% len(ancestors) | ||||
) | ||||
|
r23385 | for f, bids in sorted(fbids.items()): | ||
|
r46031 | if repo.ui.debugflag: | ||
repo.ui.debug(b" list of bids for %s:\n" % f) | ||||
for m, l in sorted(bids.items()): | ||||
for _f, args, msg in l: | ||||
repo.ui.debug(b' %s -> %s\n' % (msg, m)) | ||||
|
r23385 | # bids is a mapping from action method to list af actions | ||
# Consensus? | ||||
|
r43346 | if len(bids) == 1: # all bids are the same kind of method | ||
|
r34350 | m, l = list(bids.items())[0] | ||
|
r43346 | if all(a == l[0] for a in l[1:]): # len(bids) is > 1 | ||
|
r43347 | repo.ui.note(_(b" %s: consensus for %s\n") % (f, m)) | ||
|
r45839 | mresult.addfile(f, *l[0]) | ||
|
r23385 | continue | ||
# If keep is an option, just do it. | ||||
|
r45383 | if mergestatemod.ACTION_KEEP in bids: | ||
|
r43347 | repo.ui.note(_(b" %s: picking 'keep' action\n") % f) | ||
|
r45839 | mresult.addfile(f, *bids[mergestatemod.ACTION_KEEP][0]) | ||
|
r23385 | continue | ||
|
r46039 | # If keep absent is an option, just do that | ||
if mergestatemod.ACTION_KEEP_ABSENT in bids: | ||||
repo.ui.note(_(b" %s: picking 'keep absent' action\n") % f) | ||||
mresult.addfile(f, *bids[mergestatemod.ACTION_KEEP_ABSENT][0]) | ||||
continue | ||||
|
r46194 | # ACTION_KEEP_NEW and ACTION_CHANGED_DELETED are conflicting actions | ||
# as one say that file is new while other says that file was present | ||||
# earlier too and has a change delete conflict | ||||
# Let's fall back to conflicting ACTION_CHANGED_DELETED and let user | ||||
# do the right thing | ||||
if ( | ||||
mergestatemod.ACTION_CHANGED_DELETED in bids | ||||
and mergestatemod.ACTION_KEEP_NEW in bids | ||||
): | ||||
repo.ui.note(_(b" %s: picking 'changed/deleted' action\n") % f) | ||||
mresult.addfile( | ||||
f, *bids[mergestatemod.ACTION_CHANGED_DELETED][0] | ||||
) | ||||
continue | ||||
|
r46095 | # If keep new is an option, let's just do that | ||
if mergestatemod.ACTION_KEEP_NEW in bids: | ||||
repo.ui.note(_(b" %s: picking 'keep new' action\n") % f) | ||||
mresult.addfile(f, *bids[mergestatemod.ACTION_KEEP_NEW][0]) | ||||
continue | ||||
|
r46192 | # ACTION_GET and ACTION_DELETE_CHANGED are conflicting actions as | ||
# one action states the file is newer/created on remote side and | ||||
# other states that file is deleted locally and changed on remote | ||||
# side. Let's fallback and rely on a conflicting action to let user | ||||
# do the right thing | ||||
if ( | ||||
mergestatemod.ACTION_DELETED_CHANGED in bids | ||||
and mergestatemod.ACTION_GET in bids | ||||
): | ||||
repo.ui.note(_(b" %s: picking 'delete/changed' action\n") % f) | ||||
mresult.addfile( | ||||
f, *bids[mergestatemod.ACTION_DELETED_CHANGED][0] | ||||
) | ||||
continue | ||||
|
r23385 | # If there are gets and they all agree [how could they not?], do it. | ||
|
r45383 | if mergestatemod.ACTION_GET in bids: | ||
ga0 = bids[mergestatemod.ACTION_GET][0] | ||||
if all(a == ga0 for a in bids[mergestatemod.ACTION_GET][1:]): | ||||
|
r43347 | repo.ui.note(_(b" %s: picking 'get' action\n") % f) | ||
|
r45839 | mresult.addfile(f, *ga0) | ||
|
r23385 | continue | ||
# TODO: Consider other simple actions such as mode changes | ||||
# Handle inefficient democrazy. | ||||
|
r43347 | repo.ui.note(_(b' %s: multiple bids for merge action:\n') % f) | ||
|
r23385 | for m, l in sorted(bids.items()): | ||
for _f, args, msg in l: | ||||
|
r43347 | repo.ui.note(b' %s -> %s\n' % (msg, m)) | ||
|
r23385 | # Pick random action. TODO: Instead, prompt user when resolving | ||
|
r34350 | m, l = list(bids.items())[0] | ||
|
r43346 | repo.ui.warn( | ||
|
r43347 | _(b' %s: ambiguous merge - picked %s action\n') % (f, m) | ||
|
r43346 | ) | ||
|
r45839 | mresult.addfile(f, *l[0]) | ||
|
r23385 | continue | ||
|
r43347 | repo.ui.note(_(b'end of auction\n\n')) | ||
|
r45944 | mresult.updatevalues(diverge, renamedelete) | ||
|
r23385 | |||
|
r23640 | if wctx.rev() is None: | ||
|
r45907 | _forgetremoved(wctx, mctx, branchmerge, mresult) | ||
|
r23640 | |||
|
r45847 | sparse.filterupdatesactions(repo, wctx, mctx, branchmerge, mresult) | ||
|
r45843 | _resolvetrivial(repo, wctx, mctx, ancestors[0], mresult) | ||
|
r33323 | |||
|
r45831 | return mresult | ||
|
r23385 | |||
|
r43346 | |||
|
r34144 | def _getcwd(): | ||
try: | ||||
|
r39843 | return encoding.getcwd() | ||
|
r34144 | except OSError as err: | ||
if err.errno == errno.ENOENT: | ||||
return None | ||||
raise | ||||
|
r43346 | |||
|
r33081 | def batchremove(repo, wctx, actions): | ||
|
r21392 | """apply removes to the working directory | ||
|
r18630 | |||
yields tuples for progress updates | ||||
""" | ||||
|
r18640 | verbose = repo.ui.verbose | ||
|
r34144 | cwd = _getcwd() | ||
|
r18633 | i = 0 | ||
|
r21545 | for f, args, msg in actions: | ||
|
r43347 | repo.ui.debug(b" %s: %s -> r\n" % (f, msg)) | ||
|
r21551 | if verbose: | ||
|
r43347 | repo.ui.note(_(b"removing %s\n") % f) | ||
|
r33086 | wctx[f].audit() | ||
|
r21551 | try: | ||
|
r33082 | wctx[f].remove(ignoremissing=True) | ||
|
r25660 | except OSError as inst: | ||
|
r43346 | repo.ui.warn( | ||
|
r46280 | _(b"update failed to remove %s: %s!\n") | ||
|
r47519 | % (f, stringutil.forcebytestr(inst.strerror)) | ||
|
r43346 | ) | ||
|
r21392 | if i == 100: | ||
yield i, f | ||||
i = 0 | ||||
i += 1 | ||||
if i > 0: | ||||
yield i, f | ||||
|
r34144 | |||
if cwd and not _getcwd(): | ||||
# cwd was removed in the course of removing files; print a helpful | ||||
# warning. | ||||
|
r43346 | repo.ui.warn( | ||
_( | ||||
|
r43347 | b"current directory was removed\n" | ||
b"(consider changing to repo root: %s)\n" | ||||
|
r43346 | ) | ||
% repo.root | ||||
) | ||||
|
r21392 | |||
|
r42656 | def batchget(repo, mctx, wctx, wantfiledata, actions): | ||
|
r21392 | """apply gets to the working directory | ||
mctx is the context to get from | ||||
|
r42656 | Yields arbitrarily many (False, tuple) for progress updates, followed by | ||
exactly one (True, filedata). When wantfiledata is false, filedata is an | ||||
|
r42722 | empty dict. When wantfiledata is true, filedata[f] is a triple (mode, size, | ||
mtime) of the file f written for each action. | ||||
|
r21392 | """ | ||
|
r42722 | filedata = {} | ||
|
r21392 | verbose = repo.ui.verbose | ||
fctx = mctx.filectx | ||||
|
r27656 | ui = repo.ui | ||
|
r21392 | i = 0 | ||
|
r28200 | with repo.wvfs.backgroundclosing(ui, expectedcount=len(actions)): | ||
|
r28199 | for f, (flags, backup), msg in actions: | ||
|
r43347 | repo.ui.debug(b" %s: %s -> g\n" % (f, msg)) | ||
|
r28199 | if verbose: | ||
|
r43347 | repo.ui.note(_(b"getting %s\n") % f) | ||
|
r27656 | |||
|
r28199 | if backup: | ||
|
r34550 | # If a file or directory exists with the same name, back that | ||
# up. Otherwise, look to see if there is a file that conflicts | ||||
# with a directory this file is in, and if so, back that up. | ||||
|
r41711 | conflicting = f | ||
|
r34550 | if not repo.wvfs.lexists(f): | ||
|
r44032 | for p in pathutil.finddirs(f): | ||
|
r34550 | if repo.wvfs.isfileorlink(p): | ||
|
r41711 | conflicting = p | ||
|
r34550 | break | ||
|
r41711 | if repo.wvfs.lexists(conflicting): | ||
|
r41750 | orig = scmutil.backuppath(ui, repo, conflicting) | ||
util.rename(repo.wjoin(conflicting), orig) | ||||
|
r42656 | wfctx = wctx[f] | ||
wfctx.clearunknown() | ||||
|
r43347 | atomictemp = ui.configbool(b"experimental", b"update.atomic-file") | ||
|
r43346 | size = wfctx.write( | ||
fctx(f).data(), | ||||
flags, | ||||
backgroundclose=True, | ||||
atomictemp=atomictemp, | ||||
) | ||||
|
r42656 | if wantfiledata: | ||
s = wfctx.lstat() | ||||
mode = s.st_mode | ||||
mtime = s[stat.ST_MTIME] | ||||
|
r43346 | filedata[f] = (mode, size, mtime) # for dirstate.normal | ||
|
r28199 | if i == 100: | ||
|
r42656 | yield False, (i, f) | ||
|
r28199 | i = 0 | ||
i += 1 | ||||
|
r18633 | if i > 0: | ||
|
r42656 | yield False, (i, f) | ||
yield True, filedata | ||||
|
r18630 | |||
|
r43346 | |||
|
r45895 | def _prefetchfiles(repo, ctx, mresult): | ||
|
r37780 | """Invoke ``scmutil.prefetchfiles()`` for the files relevant to the dict | ||
|
r36159 | of merge actions. ``ctx`` is the context being merged in.""" | ||
# Skipping 'a', 'am', 'f', 'r', 'dm', 'e', 'k', 'p' and 'pr', because they | ||||
# don't touch the context to be merged in. 'cd' is skipped, because | ||||
# changed/deleted never resolves to something from the remote side. | ||||
|
r45905 | files = mresult.files( | ||
|
r45895 | [ | ||
|
r45383 | mergestatemod.ACTION_GET, | ||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
mergestatemod.ACTION_LOCAL_DIR_RENAME_GET, | ||||
mergestatemod.ACTION_MERGE, | ||||
|
r45895 | ] | ||
|
r45905 | ) | ||
|
r45895 | |||
|
r37780 | prefetch = scmutil.prefetchfiles | ||
matchfiles = scmutil.matchfiles | ||||
|
r43346 | prefetch( | ||
|
r46554 | repo, | ||
[ | ||||
( | ||||
ctx.rev(), | ||||
matchfiles(repo, files), | ||||
) | ||||
], | ||||
|
r43346 | ) | ||
|
r34126 | |||
|
r37125 | @attr.s(frozen=True) | ||
class updateresult(object): | ||||
updatedcount = attr.ib() | ||||
mergedcount = attr.ib() | ||||
removedcount = attr.ib() | ||||
unresolvedcount = attr.ib() | ||||
|
r37143 | def isempty(self): | ||
|
r43346 | return not ( | ||
self.updatedcount | ||||
or self.mergedcount | ||||
or self.removedcount | ||||
or self.unresolvedcount | ||||
) | ||||
|
r37143 | |||
|
r43346 | def applyupdates( | ||
|
r46554 | repo, | ||
mresult, | ||||
wctx, | ||||
mctx, | ||||
overwrite, | ||||
wantfiledata, | ||||
labels=None, | ||||
|
r43346 | ): | ||
|
r11454 | """apply the merge action list to the working directory | ||
|
r45894 | mresult is a mergeresult object representing result of the merge | ||
|
r11454 | wctx is the working copy context | ||
mctx is the context to be merged into the working copy | ||||
|
r13162 | |||
|
r42656 | Return a tuple of (counts, filedata), where counts is a tuple | ||
(updated, merged, removed, unresolved) that describes how many | ||||
files were affected by the update, and filedata is as described in | ||||
batchget. | ||||
|
r11454 | """ | ||
|
r3315 | |||
|
r45895 | _prefetchfiles(repo, mctx, mresult) | ||
|
r36159 | |||
|
r27078 | updated, merged, removed = 0, 0, 0 | ||
|
r46071 | ms = wctx.mergestate(clean=True) | ||
|
r46065 | ms.start(wctx.p1().node(), mctx.node(), labels) | ||
|
r45178 | |||
|
r45909 | for f, op in pycompat.iteritems(mresult.commitinfo): | ||
|
r45833 | # the other side of filenode was choosen while merging, store this in | ||
# mergestate so that it can be reused on commit | ||||
|
r45945 | ms.addcommitinfo(f, op) | ||
|
r45833 | |||
|
r46096 | numupdates = mresult.len() - mresult.len(mergestatemod.NO_OP_ACTIONS) | ||
|
r43346 | progress = repo.ui.makeprogress( | ||
|
r43347 | _(b'updating'), unit=_(b'files'), total=numupdates | ||
|
r43346 | ) | ||
|
r18630 | |||
|
r45897 | if b'.hgsubstate' in mresult._actionmapping[mergestatemod.ACTION_REMOVE]: | ||
|
r36026 | subrepoutil.submerge(repo, wctx, mctx, wctx, overwrite, labels) | ||
|
r18632 | |||
|
r34548 | # record path conflicts | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
[mergestatemod.ACTION_PATH_CONFLICT], sort=True | ||||
): | ||||
|
r34548 | f1, fo = args | ||
s = repo.ui.status | ||||
|
r43346 | s( | ||
_( | ||||
|
r43347 | b"%s: path conflict - a file or link has the same name as a " | ||
b"directory\n" | ||||
|
r43346 | ) | ||
% f | ||||
) | ||||
|
r43347 | if fo == b'l': | ||
s(_(b"the local file has been renamed to %s\n") % f1) | ||||
|
r34548 | else: | ||
|
r43347 | s(_(b"the remote file has been renamed to %s\n") % f1) | ||
s(_(b"resolve manually then use 'hg resolve --mark %s'\n") % f) | ||||
|
r45719 | ms.addpathconflict(f, f1, fo) | ||
|
r38364 | progress.increment(item=f) | ||
|
r34548 | |||
|
r34787 | # When merging in-memory, we can't support worker processes, so set the | ||
# per-item cost at 0 in that case. | ||||
cost = 0 if wctx.isinmemory() else 0.001 | ||||
|
r34549 | # remove in parallel (must come before resolving path conflicts and getting) | ||
|
r43346 | prog = worker.worker( | ||
|
r45383 | repo.ui, | ||
cost, | ||||
batchremove, | ||||
(repo, wctx), | ||||
|
r45900 | list(mresult.getactions([mergestatemod.ACTION_REMOVE], sort=True)), | ||
|
r43346 | ) | ||
|
r19095 | for i, item in prog: | ||
|
r38364 | progress.increment(step=i, item=item) | ||
|
r45898 | removed = mresult.len((mergestatemod.ACTION_REMOVE,)) | ||
|
r21390 | |||
|
r34549 | # resolve path conflicts (must come before getting) | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
[mergestatemod.ACTION_PATH_CONFLICT_RESOLVE], sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> pr\n" % (f, msg)) | ||
|
r45465 | (f0, origf0) = args | ||
|
r34549 | if wctx[f0].lexists(): | ||
|
r43347 | repo.ui.note(_(b"moving %s to %s\n") % (f0, f)) | ||
|
r34549 | wctx[f].audit() | ||
wctx[f].write(wctx.filectx(f0).data(), wctx.filectx(f0).flags()) | ||||
wctx[f0].remove() | ||||
|
r38364 | progress.increment(item=f) | ||
|
r34549 | |||
|
r38755 | # get in parallel. | ||
|
r43346 | threadsafe = repo.ui.configbool( | ||
|
r43347 | b'experimental', b'worker.wdir-get-thread-safe' | ||
|
r43346 | ) | ||
prog = worker.worker( | ||||
repo.ui, | ||||
cost, | ||||
batchget, | ||||
(repo, mctx, wctx, wantfiledata), | ||||
|
r45900 | list(mresult.getactions([mergestatemod.ACTION_GET], sort=True)), | ||
|
r43346 | threadsafe=threadsafe, | ||
hasretval=True, | ||||
) | ||||
|
r42722 | getfiledata = {} | ||
|
r42656 | for final, res in prog: | ||
if final: | ||||
getfiledata = res | ||||
else: | ||||
i, item = res | ||||
progress.increment(step=i, item=item) | ||||
|
r18630 | |||
|
r45897 | if b'.hgsubstate' in mresult._actionmapping[mergestatemod.ACTION_GET]: | ||
|
r36026 | subrepoutil.submerge(repo, wctx, mctx, wctx, overwrite, labels) | ||
|
r18632 | |||
|
r21551 | # forget (manifest only, just log it) (must come first) | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_FORGET,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> f\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r21391 | |||
|
r21551 | # re-add (manifest only, just log it) | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_ADD,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> a\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r21391 | |||
|
r27131 | # re-add/mark as modified (manifest only, just log it) | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_ADD_MODIFIED,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> am\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r27131 | |||
|
r21551 | # keep (noop, just log it) | ||
|
r46096 | for a in mergestatemod.NO_OP_ACTIONS: | ||
|
r46095 | for f, args, msg in mresult.getactions((a,), sort=True): | ||
repo.ui.debug(b" %s: %s -> %s\n" % (f, msg, a)) | ||||
# no progress | ||||
|
r21391 | |||
|
r21551 | # directory rename, move local | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_DIR_RENAME_MOVE_LOCAL,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> dm\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r21551 | f0, flags = args | ||
|
r43347 | repo.ui.note(_(b"moving %s to %s\n") % (f0, f)) | ||
|
r33086 | wctx[f].audit() | ||
|
r33083 | wctx[f].write(wctx.filectx(f0).data(), flags) | ||
|
r33082 | wctx[f0].remove() | ||
|
r21391 | |||
|
r21551 | # local directory rename, get | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_LOCAL_DIR_RENAME_GET,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> dg\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r21551 | f0, flags = args | ||
|
r43347 | repo.ui.note(_(b"getting %s to %s\n") % (f0, f)) | ||
|
r33083 | wctx[f].write(mctx.filectx(f0).data(), flags) | ||
|
r21391 | |||
|
r21551 | # exec | ||
|
r45897 | for f, args, msg in mresult.getactions( | ||
(mergestatemod.ACTION_EXEC,), sort=True | ||||
): | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> e\n" % (f, msg)) | ||
|
r38364 | progress.increment(item=f) | ||
|
r43346 | (flags,) = args | ||
|
r33086 | wctx[f].audit() | ||
|
r43347 | wctx[f].setflags(b'l' in flags, b'x' in flags) | ||
|
r21391 | |||
|
r46060 | moves = [] | ||
# 'cd' and 'dc' actions are treated like other merge conflicts | ||||
mergeactions = list( | ||||
mresult.getactions( | ||||
[ | ||||
mergestatemod.ACTION_CHANGED_DELETED, | ||||
mergestatemod.ACTION_DELETED_CHANGED, | ||||
mergestatemod.ACTION_MERGE, | ||||
], | ||||
sort=True, | ||||
) | ||||
) | ||||
for f, args, msg in mergeactions: | ||||
f1, f2, fa, move, anc = args | ||||
if f == b'.hgsubstate': # merged internally | ||||
continue | ||||
if f1 is None: | ||||
fcl = filemerge.absentfilectx(wctx, fa) | ||||
else: | ||||
repo.ui.debug(b" preserving %s for resolve of %s\n" % (f1, f)) | ||||
fcl = wctx[f1] | ||||
if f2 is None: | ||||
fco = filemerge.absentfilectx(mctx, fa) | ||||
else: | ||||
fco = mctx[f2] | ||||
actx = repo[anc] | ||||
if fa in actx: | ||||
fca = actx[fa] | ||||
else: | ||||
# TODO: move to absentfilectx | ||||
fca = repo.filectx(f1, fileid=nullrev) | ||||
ms.add(fcl, fco, fca, f) | ||||
if f1 != f and move: | ||||
moves.append(f1) | ||||
# remove renamed files after safely stored | ||||
for f in moves: | ||||
if wctx[f].lexists(): | ||||
repo.ui.debug(b"removing %s\n" % f) | ||||
wctx[f].audit() | ||||
wctx[f].remove() | ||||
|
r45901 | # these actions updates the file | ||
updated = mresult.len( | ||||
( | ||||
mergestatemod.ACTION_GET, | ||||
mergestatemod.ACTION_EXEC, | ||||
mergestatemod.ACTION_LOCAL_DIR_RENAME_GET, | ||||
mergestatemod.ACTION_DIR_RENAME_MOVE_LOCAL, | ||||
) | ||||
) | ||||
|
r26786 | |||
|
r34681 | try: | ||
# premerge | ||||
tocomplete = [] | ||||
for f, args, msg in mergeactions: | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> m (premerge)\n" % (f, msg)) | ||
|
r47567 | ms.addcommitinfo(f, {b'merged': b'yes'}) | ||
|
r38364 | progress.increment(item=f) | ||
|
r43347 | if f == b'.hgsubstate': # subrepo states need updating | ||
|
r43346 | subrepoutil.submerge( | ||
repo, wctx, mctx, wctx.ancestor(mctx), overwrite, labels | ||||
) | ||||
|
r34681 | continue | ||
wctx[f].audit() | ||||
complete, r = ms.preresolve(f, wctx) | ||||
if not complete: | ||||
numupdates += 1 | ||||
tocomplete.append((f, args, msg)) | ||||
|
r26618 | |||
|
r34681 | # merge | ||
for f, args, msg in tocomplete: | ||||
|
r43347 | repo.ui.debug(b" %s: %s -> m (merge)\n" % (f, msg)) | ||
|
r47567 | ms.addcommitinfo(f, {b'merged': b'yes'}) | ||
|
r38364 | progress.increment(item=f, total=numupdates) | ||
|
r34681 | ms.resolve(f, wctx) | ||
|
r26292 | |||
|
r34681 | finally: | ||
ms.commit() | ||||
|
r26787 | |||
|
r27078 | unresolved = ms.unresolvedcount() | ||
msupdated, msmerged, msremoved = ms.counts() | ||||
updated += msupdated | ||||
merged += msmerged | ||||
removed += msremoved | ||||
|
r27080 | |||
extraactions = ms.actions() | ||||
|
r38392 | progress.complete() | ||
|
r48163 | return ( | ||
updateresult(updated, merged, removed, unresolved), | ||||
getfiledata, | ||||
extraactions, | ||||
|
r45383 | ) | ||
|
r3111 | |||
|
r43346 | |||
|
r45727 | def _advertisefsmonitor(repo, num_gets, p1node): | ||
# Advertise fsmonitor when its presence could be useful. | ||||
# | ||||
# We only advertise when performing an update from an empty working | ||||
# directory. This typically only occurs during initial clone. | ||||
# | ||||
# We give users a mechanism to disable the warning in case it is | ||||
# annoying. | ||||
# | ||||
# We only allow on Linux and MacOS because that's where fsmonitor is | ||||
# considered stable. | ||||
fsmonitorwarning = repo.ui.configbool(b'fsmonitor', b'warn_when_unused') | ||||
fsmonitorthreshold = repo.ui.configint( | ||||
b'fsmonitor', b'warn_update_file_count' | ||||
) | ||||
|
r46034 | # avoid cycle dirstate -> sparse -> merge -> dirstate | ||
from . import dirstate | ||||
if dirstate.rustmod is not None: | ||||
# When using rust status, fsmonitor becomes necessary at higher sizes | ||||
fsmonitorthreshold = repo.ui.configint( | ||||
|
r46554 | b'fsmonitor', | ||
b'warn_update_file_count_rust', | ||||
|
r46034 | ) | ||
|
r45727 | try: | ||
# avoid cycle: extensions -> cmdutil -> merge | ||||
from . import extensions | ||||
extensions.find(b'fsmonitor') | ||||
fsmonitorenabled = repo.ui.config(b'fsmonitor', b'mode') != b'off' | ||||
# We intentionally don't look at whether fsmonitor has disabled | ||||
# itself because a) fsmonitor may have already printed a warning | ||||
# b) we only care about the config state here. | ||||
except KeyError: | ||||
fsmonitorenabled = False | ||||
if ( | ||||
fsmonitorwarning | ||||
and not fsmonitorenabled | ||||
|
r47771 | and p1node == repo.nullid | ||
|
r45727 | and num_gets >= fsmonitorthreshold | ||
and pycompat.sysplatform.startswith((b'linux', b'darwin')) | ||||
): | ||||
repo.ui.warn( | ||||
_( | ||||
b'(warning: large working directory being used without ' | ||||
b'fsmonitor enabled; enable fsmonitor to improve performance; ' | ||||
b'see "hg help -e fsmonitor")\n' | ||||
) | ||||
) | ||||
|
r43347 | UPDATECHECK_ABORT = b'abort' # handled at higher layers | ||
UPDATECHECK_NONE = b'none' | ||||
UPDATECHECK_LINEAR = b'linear' | ||||
UPDATECHECK_NO_CONFLICT = b'noconflict' | ||||
|
r43240 | |||
|
r43346 | |||
|
r46134 | def _update( | ||
|
r43346 | repo, | ||
node, | ||||
branchmerge, | ||||
force, | ||||
ancestor=None, | ||||
mergeancestor=False, | ||||
labels=None, | ||||
matcher=None, | ||||
mergeforce=False, | ||||
|
r44744 | updatedirstate=True, | ||
|
r43346 | updatecheck=None, | ||
wc=None, | ||||
): | ||||
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
|
r30902 | node = the node to update to | ||
|
r3315 | branchmerge = whether to merge between branches | ||
force = whether to force branch merging or file overwriting | ||||
|
r27344 | matcher = a matcher to filter file lists (dirstate not updated) | ||
|
r18778 | mergeancestor = whether it is merging with an ancestor. If true, | ||
we should accept the incoming changes for any prompts that occur. | ||||
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. | ||||
|
r28019 | labels = labels to use for base, local and other | ||
|
r28020 | mergeforce = whether the merge was run with 'merge --force' (deprecated): if | ||
this is True, then 'force' should be True as well. | ||||
|
r9716 | |||
|
r34920 | The table below shows all the behaviors of the update command given the | ||
-c/--check and -C/--clean 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 or not). Match from top first. The -n | ||||
option doesn't exist on the command line, but represents the | ||||
|
r31168 | experimental.updatecheck=noconflict option. | ||
|
r9716 | |||
|
r12279 | This logic is tested by test-update-branches.t. | ||
|
r9716 | |||
|
r31168 | -c -C -n -m dirty rev linear | result | ||
y y * * * * * | (1) | ||||
y * y * * * * | (1) | ||||
y * * y * * * | (1) | ||||
* y y * * * * | (1) | ||||
* y * y * * * | (1) | ||||
* * y y * * * | (1) | ||||
* * * * * n n | x | ||||
* * * * n * * | ok | ||||
n n n n y * y | merge | ||||
n n n n y y n | (2) | ||||
n n n y y * * | merge | ||||
n n y n y * * | merge if no conflict | ||||
n y n n y * * | discard | ||||
y n n n y * * | (3) | ||||
|
r9716 | |||
x = can't happen | ||||
* = don't-care | ||||
|
r31161 | 1 = incompatible options (checked in commands.py) | ||
2 = abort: uncommitted changes (commit or update --clean to discard changes) | ||||
3 = abort: uncommitted changes (checked in commands.py) | ||||
|
r13162 | |||
|
r34303 | The merge is performed inside ``wc``, a workingctx-like objects. It defaults | ||
to repo[None] if None is passed. | ||||
|
r13162 | Return the same tuple as applyupdates(). | ||
|
r3315 | """ | ||
|
r33321 | # Avoid cycle. | ||
from . import sparse | ||||
|
r2815 | |||
|
r31166 | # This function used to find the default destination if node was None, but | ||
|
r30902 | # that's now in destutil.py. | ||
assert node is not None | ||||
|
r31166 | if not branchmerge and not force: | ||
# TODO: remove the default once all callers that pass branchmerge=False | ||||
# and force=False pass a value for updatecheck. We may want to allow | ||||
# updatecheck='abort' to better suppport some of these callers. | ||||
if updatecheck is None: | ||||
|
r43240 | updatecheck = UPDATECHECK_LINEAR | ||
|
r43346 | if updatecheck not in ( | ||
UPDATECHECK_NONE, | ||||
UPDATECHECK_LINEAR, | ||||
UPDATECHECK_NO_CONFLICT, | ||||
|
r43242 | ): | ||
|
r43346 | raise ValueError( | ||
r'Invalid updatecheck %r (can accept %r)' | ||||
% ( | ||||
updatecheck, | ||||
( | ||||
UPDATECHECK_NONE, | ||||
UPDATECHECK_LINEAR, | ||||
UPDATECHECK_NO_CONFLICT, | ||||
), | ||||
) | ||||
) | ||||
|
r45536 | if wc is not None and wc.isinmemory(): | ||
maybe_wlock = util.nullcontextmanager() | ||||
else: | ||||
maybe_wlock = repo.wlock() | ||||
with maybe_wlock: | ||||
|
r34303 | if wc is None: | ||
wc = repo[None] | ||||
|
r20279 | pl = wc.parents() | ||
p1 = pl[0] | ||||
|
r42601 | p2 = repo[node] | ||
|
r23405 | if ancestor is not None: | ||
|
r21081 | pas = [repo[ancestor]] | ||
|
r42602 | else: | ||
|
r43347 | if repo.ui.configlist(b'merge', b'preferancestor') == [b'*']: | ||
|
r21128 | cahs = repo.changelog.commonancestorsheads(p1.node(), p2.node()) | ||
|
r47771 | pas = [repo[anc] for anc in (sorted(cahs) or [repo.nullid])] | ||
|
r21128 | else: | ||
|
r22179 | pas = [p1.ancestor(p2, warn=branchmerge)] | ||
|
r13874 | |||
|
r36195 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), bytes(p1), bytes(p2) | ||
|
r3314 | |||
|
r42601 | overwrite = force and not branchmerge | ||
|
r4915 | ### check phase | ||
|
r27316 | if not overwrite: | ||
if len(pl) > 1: | ||||
|
r47146 | raise error.StateError(_(b"outstanding uncommitted merge")) | ||
|
r46071 | ms = wc.mergestate() | ||
|
r47070 | if ms.unresolvedcount(): | ||
|
r47146 | raise error.StateError( | ||
|
r43347 | _(b"outstanding merge conflicts"), | ||
hint=_(b"use 'hg resolve' to resolve"), | ||||
|
r43346 | ) | ||
|
r6375 | if branchmerge: | ||
|
r21081 | if pas == [p2]: | ||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b"merging with a working directory ancestor" | ||
b" has no effect" | ||||
|
r43346 | ) | ||
) | ||||
|
r21081 | elif pas == [p1]: | ||
|
r31379 | if not mergeancestor and wc.branch() == p2.branch(): | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b"nothing to merge"), | ||
|
r43387 | hint=_(b"use 'hg update' or check 'hg heads'"), | ||
|
r43346 | ) | ||
|
r6375 | if not force and (wc.files() or wc.deleted()): | ||
|
r46529 | raise error.StateError( | ||
|
r43347 | _(b"uncommitted changes"), | ||
hint=_(b"use 'hg status' to list changes"), | ||||
|
r43346 | ) | ||
|
r35285 | if not wc.isinmemory(): | ||
for s in sorted(wc.substate): | ||||
wc.sub(s).bailifchanged() | ||||
|
r13437 | |||
|
r6375 | elif not overwrite: | ||
|
r43346 | if p1 == p2: # no-op update | ||
|
r19929 | # call the hooks and exit early | ||
|
r43347 | repo.hook(b'preupdate', throw=True, parent1=xp2, parent2=b'') | ||
repo.hook(b'update', parent1=xp2, parent2=b'', error=0) | ||||
|
r37125 | return updateresult(0, 0, 0, 0) | ||
|
r19929 | |||
|
r43346 | if updatecheck == UPDATECHECK_LINEAR and pas not in ( | ||
[p1], | ||||
[p2], | ||||
): # nonlinear | ||||
|
r18985 | dirty = wc.dirty(missing=True) | ||
|
r30902 | if dirty: | ||
|
r18985 | # Branching is a bit strange to ensure we do the minimal | ||
r33147 | # amount of call to obsutil.foreground. | |||
foreground = obsutil.foreground(repo, [p1.node()]) | ||||
|
r18985 | # note: the <node> variable contains a random identifier | ||
if repo[node].node() in foreground: | ||||
|
r43346 | pass # allow updating to successors | ||
|
r30902 | else: | ||
|
r43347 | msg = _(b"uncommitted changes") | ||
hint = _(b"commit or update --clean to discard changes") | ||||
|
r30961 | raise error.UpdateAbort(msg, hint=hint) | ||
|
r18985 | else: | ||
# Allow jumping branches if clean and specific rev given | ||||
|
r30901 | pass | ||
if overwrite: | ||||
pas = [wc] | ||||
elif not branchmerge: | ||||
pas = [p1] | ||||
|
r2814 | |||
|
r25843 | # deprecated config: merge.followcopies | ||
|
r43347 | followcopies = repo.ui.configbool(b'merge', b'followcopies') | ||
|
r21080 | if overwrite: | ||
|
r30200 | followcopies = False | ||
elif not pas[0]: | ||||
followcopies = False | ||||
if not branchmerge and not wc.dirty(missing=True): | ||||
followcopies = False | ||||
|
r21080 | |||
|
r4915 | ### calculate phase | ||
|
r45831 | mresult = calculateupdates( | ||
|
r43346 | repo, | ||
wc, | ||||
p2, | ||||
pas, | ||||
branchmerge, | ||||
force, | ||||
mergeancestor, | ||||
followcopies, | ||||
matcher=matcher, | ||||
mergeforce=mergeforce, | ||||
) | ||||
|
r27951 | |||
|
r43240 | if updatecheck == UPDATECHECK_NO_CONFLICT: | ||
|
r45835 | if mresult.hasconflicts(): | ||
msg = _(b"conflicting changes") | ||||
hint = _(b"commit or update --clean to discard changes") | ||||
|
r47146 | raise error.StateError(msg, hint=hint) | ||
|
r31168 | |||
|
r27951 | # Prompt and create actions. Most of this is in the resolve phase | ||
# already, but we can't handle .hgsubstate in filemerge or | ||||
|
r36026 | # subrepoutil.submerge yet so we have to keep prompting for it. | ||
|
r45904 | vals = mresult.getfile(b'.hgsubstate') | ||
if vals: | ||||
|
r43347 | f = b'.hgsubstate' | ||
|
r45904 | m, args, msg = vals | ||
|
r29774 | prompts = filemerge.partextras(labels) | ||
|
r43347 | prompts[b'f'] = f | ||
|
r45383 | if m == mergestatemod.ACTION_CHANGED_DELETED: | ||
|
r27951 | if repo.ui.promptchoice( | ||
|
r43346 | _( | ||
|
r43347 | b"local%(l)s changed %(f)s which other%(o)s deleted\n" | ||
b"use (c)hanged version or (d)elete?" | ||||
b"$$ &Changed $$ &Delete" | ||||
|
r43346 | ) | ||
% prompts, | ||||
0, | ||||
): | ||||
|
r45839 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_REMOVE, | ||||
None, | ||||
b'prompt delete', | ||||
|
r45383 | ) | ||
|
r27951 | elif f in p1: | ||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_ADD_MODIFIED, | ||
|
r43347 | None, | ||
b'prompt keep', | ||||
) | ||||
|
r27951 | else: | ||
|
r45839 | mresult.addfile( | ||
|
r46554 | f, | ||
mergestatemod.ACTION_ADD, | ||||
None, | ||||
b'prompt keep', | ||||
|
r45383 | ) | ||
elif m == mergestatemod.ACTION_DELETED_CHANGED: | ||||
|
r27951 | f1, f2, fa, move, anc = args | ||
flags = p2[f2].flags() | ||||
|
r43346 | if ( | ||
repo.ui.promptchoice( | ||||
_( | ||||
|
r43347 | b"other%(o)s changed %(f)s which local%(l)s deleted\n" | ||
b"use (c)hanged version or leave (d)eleted?" | ||||
b"$$ &Changed $$ &Deleted" | ||||
|
r43346 | ) | ||
% prompts, | ||||
0, | ||||
) | ||||
== 0 | ||||
): | ||||
|
r45839 | mresult.addfile( | ||
f, | ||||
|
r45383 | mergestatemod.ACTION_GET, | ||
|
r43346 | (flags, False), | ||
|
r43347 | b'prompt recreating', | ||
|
r43346 | ) | ||
|
r27951 | else: | ||
|
r45840 | mresult.removefile(f) | ||
|
r27951 | |||
|
r29889 | if not util.fscasesensitive(repo.path): | ||
|
r23544 | # check collision between files only in p2 for clean update | ||
|
r43346 | if not branchmerge and ( | ||
force or not wc.dirty(missing=True, branch=False) | ||||
): | ||||
|
r23544 | _checkcollision(repo, p2.manifest(), None) | ||
else: | ||||
|
r45893 | _checkcollision(repo, wc.manifest(), mresult) | ||
|
r23544 | |||
|
r23525 | # divergent renames | ||
|
r45831 | for f, fl in sorted(pycompat.iteritems(mresult.diverge)): | ||
|
r43346 | repo.ui.warn( | ||
_( | ||||
|
r43347 | b"note: possible conflict - %s was renamed " | ||
b"multiple times to:\n" | ||||
|
r43346 | ) | ||
% f | ||||
) | ||||
|
r42279 | for nf in sorted(fl): | ||
|
r43347 | repo.ui.warn(b" %s\n" % nf) | ||
|
r23525 | |||
# rename and delete | ||||
|
r45831 | for f, fl in sorted(pycompat.iteritems(mresult.renamedelete)): | ||
|
r43346 | repo.ui.warn( | ||
_( | ||||
|
r43347 | b"note: possible conflict - %s was deleted " | ||
b"and renamed to:\n" | ||||
|
r43346 | ) | ||
% f | ||||
) | ||||
|
r42279 | for nf in sorted(fl): | ||
|
r43347 | repo.ui.warn(b" %s\n" % nf) | ||
|
r23525 | |||
|
r26957 | ### apply phase | ||
|
r43346 | if not branchmerge: # just jump to the new rev | ||
|
r47771 | fp1, fp2, xp1, xp2 = fp2, repo.nullid, xp2, b'' | ||
|
r44625 | # If we're doing a partial update, we need to skip updating | ||
|
r44626 | # the dirstate. | ||
always = matcher is None or matcher.always() | ||||
|
r44744 | updatedirstate = updatedirstate and always and not wc.isinmemory() | ||
|
r44612 | if updatedirstate: | ||
|
r43347 | repo.hook(b'preupdate', throw=True, parent1=xp1, parent2=xp2) | ||
|
r26957 | # note that we're in the middle of an update | ||
|
r43347 | repo.vfs.write(b'updatestate', p2.hex()) | ||
|
r26957 | |||
|
r45727 | _advertisefsmonitor( | ||
|
r45898 | repo, mresult.len((mergestatemod.ACTION_GET,)), p1.node() | ||
|
r43346 | ) | ||
|
r34886 | |||
|
r42656 | wantfiledata = updatedirstate and not branchmerge | ||
|
r48163 | stats, getfiledata, extraactions = applyupdates( | ||
|
r46554 | repo, | ||
mresult, | ||||
wc, | ||||
p2, | ||||
overwrite, | ||||
wantfiledata, | ||||
labels=labels, | ||||
|
r43346 | ) | ||
|
r26957 | |||
|
r42656 | if updatedirstate: | ||
|
r48163 | if extraactions: | ||
for k, acts in pycompat.iteritems(extraactions): | ||||
for a in acts: | ||||
mresult.addfile(a[0], k, *a[1:]) | ||||
if k == mergestatemod.ACTION_GET and wantfiledata: | ||||
# no filedata until mergestate is updated to provide it | ||||
for a in acts: | ||||
getfiledata[a[0]] = None | ||||
assert len(getfiledata) == ( | ||||
mresult.len((mergestatemod.ACTION_GET,)) if wantfiledata else 0 | ||||
) | ||||
|
r32351 | with repo.dirstate.parentchange(): | ||
repo.setparents(fp1, fp2) | ||||
|
r45383 | mergestatemod.recordupdates( | ||
|
r45894 | repo, mresult.actionsdict, branchmerge, getfiledata | ||
|
r45383 | ) | ||
|
r32351 | # update completed, clear state | ||
|
r43347 | util.unlink(repo.vfs.join(b'updatestate')) | ||
|
r19482 | |||
|
r32351 | if not branchmerge: | ||
repo.dirstate.setbranch(p2.branch()) | ||||
|
r10492 | |||
r48409 | # If we're updating to a location, clean up any stale temporary includes | |||
# (ex: this happens during hg rebase --abort). | ||||
if not branchmerge: | ||||
sparse.prunetemporaryincludes(repo) | ||||
|
r33321 | |||
|
r44611 | if updatedirstate: | ||
|
r43346 | repo.hook( | ||
|
r43347 | b'update', parent1=xp1, parent2=xp2, error=stats.unresolvedcount | ||
|
r43346 | ) | ||
|
r10492 | return stats | ||
|
r22902 | |||
|
r43346 | |||
|
r44883 | def merge(ctx, labels=None, force=False, wc=None): | ||
"""Merge another topological branch into the working copy. | ||||
force = whether the merge was run with 'merge --force' (deprecated) | ||||
""" | ||||
|
r46134 | return _update( | ||
|
r44883 | ctx.repo(), | ||
ctx.rev(), | ||||
labels=labels, | ||||
branchmerge=True, | ||||
force=force, | ||||
mergeforce=force, | ||||
wc=wc, | ||||
) | ||||
|
r46150 | def update(ctx, updatecheck=None, wc=None): | ||
"""Do a regular update to the given commit, aborting if there are conflicts. | ||||
The 'updatecheck' argument can be used to control what to do in case of | ||||
conflicts. | ||||
Note: This is a new, higher-level update() than the one that used to exist | ||||
in this module. That function is now called _update(). You can hopefully | ||||
replace your callers to use this new update(), or clean_update(), merge(), | ||||
revert_to(), or graft(). | ||||
""" | ||||
return _update( | ||||
ctx.repo(), | ||||
ctx.rev(), | ||||
branchmerge=False, | ||||
force=False, | ||||
labels=[b'working copy', b'destination'], | ||||
updatecheck=updatecheck, | ||||
wc=wc, | ||||
) | ||||
|
r44743 | def clean_update(ctx, wc=None): | ||
"""Do a clean update to the given commit. | ||||
This involves updating to the commit and discarding any changes in the | ||||
working copy. | ||||
""" | ||||
|
r46134 | return _update(ctx.repo(), ctx.rev(), branchmerge=False, force=True, wc=wc) | ||
|
r44743 | |||
|
r44744 | def revert_to(ctx, matcher=None, wc=None): | ||
"""Revert the working copy to the given commit. | ||||
The working copy will keep its current parent(s) but its content will | ||||
be the same as in the given commit. | ||||
""" | ||||
|
r46134 | return _update( | ||
|
r44744 | ctx.repo(), | ||
ctx.rev(), | ||||
branchmerge=False, | ||||
force=True, | ||||
updatedirstate=False, | ||||
matcher=matcher, | ||||
wc=wc, | ||||
) | ||||
|
r43346 | def graft( | ||
|
r44692 | repo, | ||
ctx, | ||||
|
r44693 | base=None, | ||
|
r44692 | labels=None, | ||
keepparent=False, | ||||
keepconflictparent=False, | ||||
wctx=None, | ||||
|
r43346 | ): | ||
|
r22902 | """Do a graft-like merge. | ||
This is a merge where the merge ancestor is chosen such that one | ||||
or more changesets are grafted onto the current changeset. In | ||||
addition to the merge, this fixes up the dirstate to include only | ||||
|
r27267 | a single parent (if keepparent is False) and tries to duplicate any | ||
renames/copies appropriately. | ||||
|
r22902 | |||
ctx - changeset to rebase | ||||
|
r44693 | base - merge base, or ctx.p1() if not specified | ||
|
r22902 | labels - merge labels eg ['local', 'graft'] | ||
|
r27267 | keepparent - keep second parent if any | ||
r42604 | keepconflictparent - if unresolved, keep parent used for the merge | |||
|
r22902 | |||
""" | ||||
|
r24643 | # If we're grafting a descendant onto an ancestor, be sure to pass | ||
# mergeancestor=True to update. This does two things: 1) allows the merge if | ||||
# the destination is the same as the parent of the ctx (so we can use graft | ||||
# to copy commits), and 2) informs update that the incoming changes are | ||||
# newer than the destination so it doesn't prompt about "remote changed foo | ||||
# which local deleted". | ||||
|
r44867 | # We also pass mergeancestor=True when base is the same revision as p1. 2) | ||
# doesn't matter as there can't possibly be conflicts, but 1) is necessary. | ||||
|
r44692 | wctx = wctx or repo[None] | ||
|
r44548 | pctx = wctx.p1() | ||
|
r44693 | base = base or ctx.p1() | ||
|
r44867 | mergeancestor = ( | ||
repo.changelog.isancestor(pctx.node(), ctx.node()) | ||||
or pctx.rev() == base.rev() | ||||
) | ||||
|
r22902 | |||
|
r46134 | stats = _update( | ||
|
r43346 | repo, | ||
ctx.node(), | ||||
True, | ||||
True, | ||||
|
r44235 | base.node(), | ||
|
r43346 | mergeancestor=mergeancestor, | ||
labels=labels, | ||||
|
r44692 | wc=wctx, | ||
|
r43346 | ) | ||
|
r38513 | |||
if keepconflictparent and stats.unresolvedcount: | ||||
pother = ctx.node() | ||||
else: | ||||
|
r47771 | pother = repo.nullid | ||
|
r38513 | parents = ctx.parents() | ||
|
r44235 | if keepparent and len(parents) == 2 and base in parents: | ||
parents.remove(base) | ||||
|
r38513 | pother = parents[0].node() | ||
|
r44237 | # Never set both parents equal to each other | ||
if pother == pctx.node(): | ||||
|
r47771 | pother = repo.nullid | ||
|
r27267 | |||
|
r44692 | if wctx.isinmemory(): | ||
wctx.setparents(pctx.node(), pother) | ||||
|
r32351 | # fix up dirstate for copies and renames | ||
|
r44551 | copies.graftcopies(wctx, ctx, base) | ||
|
r44692 | else: | ||
with repo.dirstate.parentchange(): | ||||
repo.setparents(pctx.node(), pother) | ||||
repo.dirstate.write(repo.currenttransaction()) | ||||
# fix up dirstate for copies and renames | ||||
copies.graftcopies(wctx, ctx, base) | ||||
|
r22902 | return stats | ||
|
r39499 | |||
|
r43346 | |||
|
r46117 | def back_out(ctx, parent=None, wc=None): | ||
if parent is None: | ||||
if ctx.p2() is not None: | ||||
raise error.ProgrammingError( | ||||
b"must specify parent of merge commit to back out" | ||||
) | ||||
parent = ctx.p1() | ||||
|
r46134 | return _update( | ||
|
r46117 | ctx.repo(), | ||
parent, | ||||
branchmerge=True, | ||||
force=True, | ||||
ancestor=ctx.node(), | ||||
mergeancestor=False, | ||||
) | ||||
|
r43346 | def purge( | ||
repo, | ||||
matcher, | ||||
|
r44771 | unknown=True, | ||
|
r43346 | ignored=False, | ||
removeemptydirs=True, | ||||
removefiles=True, | ||||
abortonerror=False, | ||||
noop=False, | ||||
r47078 | confirm=False, | |||
|
r43346 | ): | ||
|
r39499 | """Purge the working directory of untracked files. | ||
``matcher`` is a matcher configured to scan the working directory - | ||||
potentially a subset. | ||||
|
r44771 | ``unknown`` controls whether unknown files should be purged. | ||
``ignored`` controls whether ignored files should be purged. | ||||
|
r39499 | |||
``removeemptydirs`` controls whether empty directories should be removed. | ||||
``removefiles`` controls whether files are removed. | ||||
``abortonerror`` causes an exception to be raised if an error occurs | ||||
deleting a file or directory. | ||||
``noop`` controls whether to actually remove files. If not defined, actions | ||||
will be taken. | ||||
r47078 | ``confirm`` ask confirmation before actually removing anything. | |||
|
r39499 | Returns an iterable of relative paths in the working directory that were | ||
or would be removed. | ||||
""" | ||||
def remove(removefn, path): | ||||
try: | ||||
|
r39500 | removefn(path) | ||
|
r39499 | except OSError: | ||
|
r43347 | m = _(b'%s cannot be removed') % path | ||
|
r39499 | if abortonerror: | ||
raise error.Abort(m) | ||||
else: | ||||
|
r43347 | repo.ui.warn(_(b'warning: %s\n') % m) | ||
|
r39499 | |||
# There's no API to copy a matcher. So mutate the passed matcher and | ||||
# restore it when we're done. | ||||
oldtraversedir = matcher.traversedir | ||||
res = [] | ||||
try: | ||||
if removeemptydirs: | ||||
directories = [] | ||||
|
r44112 | matcher.traversedir = directories.append | ||
|
r39499 | |||
|
r44771 | status = repo.status(match=matcher, ignored=ignored, unknown=unknown) | ||
|
r39499 | |||
r47078 | if confirm: | |||
nb_ignored = len(status.ignored) | ||||
nb_unkown = len(status.unknown) | ||||
if nb_unkown and nb_ignored: | ||||
msg = _(b"permanently delete %d unkown and %d ignored files?") | ||||
msg %= (nb_unkown, nb_ignored) | ||||
elif nb_unkown: | ||||
msg = _(b"permanently delete %d unkown files?") | ||||
msg %= nb_unkown | ||||
elif nb_ignored: | ||||
msg = _(b"permanently delete %d ignored files?") | ||||
msg %= nb_ignored | ||||
r47079 | elif removeemptydirs: | |||
dir_count = 0 | ||||
for f in directories: | ||||
if matcher(f) and not repo.wvfs.listdir(f): | ||||
dir_count += 1 | ||||
if dir_count: | ||||
msg = _( | ||||
b"permanently delete at least %d empty directories?" | ||||
) | ||||
msg %= dir_count | ||||
else: | ||||
# XXX we might be missing directory there | ||||
return res | ||||
r47078 | msg += b" (yN)$$ &Yes $$ &No" | |||
if repo.ui.promptchoice(msg, default=1) == 1: | ||||
raise error.CanceledError(_(b'removal cancelled')) | ||||
|
r39499 | if removefiles: | ||
for f in sorted(status.unknown + status.ignored): | ||||
if not noop: | ||||
|
r43347 | repo.ui.note(_(b'removing file %s\n') % f) | ||
|
r39500 | remove(repo.wvfs.unlink, f) | ||
|
r39499 | res.append(f) | ||
if removeemptydirs: | ||||
for f in sorted(directories, reverse=True): | ||||
|
r39500 | if matcher(f) and not repo.wvfs.listdir(f): | ||
|
r39499 | if not noop: | ||
|
r43347 | repo.ui.note(_(b'removing directory %s\n') % f) | ||
|
r39500 | remove(repo.wvfs.rmdir, f) | ||
|
r39499 | res.append(f) | ||
return res | ||||
finally: | ||||
matcher.traversedir = oldtraversedir | ||||