upgrade.py
303 lines
| 10.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / upgrade.py
Pierre-Yves David
|
r31894 | # upgrade.py - functions for in place upgrade of Mercurial repository | ||
Pierre-Yves David
|
r31864 | # | ||
Pierre-Yves David
|
r31895 | # Copyright (c) 2016-present, Gregory Szorc | ||
Pierre-Yves David
|
r31864 | # | ||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
from __future__ import absolute_import | ||||
from .i18n import _ | ||||
from . import ( | ||||
error, | ||||
Boris Feld
|
r35344 | hg, | ||
Pierre-Yves David
|
r31893 | localrepo, | ||
Pulkit Goyal
|
r46852 | lock as lockmod, | ||
Yuya Nishihara
|
r38183 | pycompat, | ||
Pulkit Goyal
|
r46852 | requirements as requirementsmod, | ||
scmutil, | ||||
r46661 | ) | |||
from .upgrade_utils import ( | ||||
r46662 | actions as upgrade_actions, | |||
r46661 | engine as upgrade_engine, | |||
Pierre-Yves David
|
r31864 | ) | ||
Pulkit Goyal
|
r46852 | from .utils import ( | ||
stringutil, | ||||
) | ||||
r46662 | allformatvariant = upgrade_actions.allformatvariant | |||
Augie Fackler
|
r43346 | |||
def upgraderepo( | ||||
ui, | ||||
repo, | ||||
run=False, | ||||
optimize=None, | ||||
backup=True, | ||||
manifest=None, | ||||
changelog=None, | ||||
r46612 | filelogs=None, | |||
Augie Fackler
|
r43346 | ): | ||
Pierre-Yves David
|
r31864 | """Upgrade a repository in place.""" | ||
Boris Feld
|
r41120 | if optimize is None: | ||
Pulkit Goyal
|
r46825 | optimize = {} | ||
Pierre-Yves David
|
r31864 | repo = repo.unfiltered() | ||
r46661 | revlogs = set(upgrade_engine.UPGRADE_ALL_REVLOGS) | |||
r46595 | specentries = ( | |||
r46661 | (upgrade_engine.UPGRADE_CHANGELOG, changelog), | |||
(upgrade_engine.UPGRADE_MANIFEST, manifest), | ||||
(upgrade_engine.UPGRADE_FILELOGS, filelogs), | ||||
r46595 | ) | |||
r43098 | specified = [(y, x) for (y, x) in specentries if x is not None] | |||
if specified: | ||||
# we have some limitation on revlogs to be recloned | ||||
if any(x for y, x in specified): | ||||
revlogs = set() | ||||
r46595 | for upgrade, enabled in specified: | |||
r43098 | if enabled: | |||
r46595 | revlogs.add(upgrade) | |||
r43098 | else: | |||
# none are enabled | ||||
r46595 | for upgrade, __ in specified: | |||
revlogs.discard(upgrade) | ||||
r43098 | ||||
Pierre-Yves David
|
r31864 | # Ensure the repository can be upgraded. | ||
r46663 | upgrade_actions.check_source_requirements(repo) | |||
Pierre-Yves David
|
r31864 | |||
r46666 | default_options = localrepo.defaultcreateopts(repo.ui) | |||
newreqs = localrepo.newreporequirements(repo.ui, default_options) | ||||
r46662 | newreqs.update(upgrade_actions.preservedrequirements(repo)) | |||
Pierre-Yves David
|
r31864 | |||
r46666 | upgrade_actions.check_requirements_changes(repo, newreqs) | |||
Pierre-Yves David
|
r31864 | |||
# Find and validate all improvements that can be made. | ||||
r46662 | alloptimizations = upgrade_actions.findoptimizations(repo) | |||
Pierre-Yves David
|
r31864 | |||
Pierre-Yves David
|
r31899 | # Apply and Validate arguments. | ||
optimizations = [] | ||||
for o in alloptimizations: | ||||
if o.name in optimize: | ||||
optimizations.append(o) | ||||
optimize.discard(o.name) | ||||
Augie Fackler
|
r43346 | if optimize: # anything left is unknown | ||
raise error.Abort( | ||||
Augie Fackler
|
r43347 | _(b'unknown optimization action requested: %s') | ||
% b', '.join(sorted(optimize)), | ||||
Martin von Zweigbergk
|
r43387 | hint=_(b'run without arguments to see valid optimizations'), | ||
Augie Fackler
|
r43346 | ) | ||
Pierre-Yves David
|
r31864 | |||
Pulkit Goyal
|
r46822 | format_upgrades = upgrade_actions.find_format_upgrades(repo) | ||
Pulkit Goyal
|
r46827 | up_actions = upgrade_actions.determine_upgrade_actions( | ||
Pulkit Goyal
|
r46826 | repo, format_upgrades, optimizations, repo.requirements, newreqs | ||
Augie Fackler
|
r43346 | ) | ||
Pulkit Goyal
|
r46830 | removed_actions = upgrade_actions.find_format_downgrades(repo) | ||
Pierre-Yves David
|
r31864 | |||
r43100 | removedreqs = repo.requirements - newreqs | |||
addedreqs = newreqs - repo.requirements | ||||
r46661 | if revlogs != upgrade_engine.UPGRADE_ALL_REVLOGS: | |||
r46662 | incompatible = upgrade_actions.RECLONES_REQUIREMENTS & ( | |||
removedreqs | addedreqs | ||||
) | ||||
r43100 | if incompatible: | |||
Augie Fackler
|
r43346 | msg = _( | ||
Augie Fackler
|
r43347 | b'ignoring revlogs selection flags, format requirements ' | ||
b'change: %s\n' | ||||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r43347 | ui.warn(msg % b', '.join(sorted(incompatible))) | ||
r46661 | revlogs = upgrade_engine.UPGRADE_ALL_REVLOGS | |||
r43100 | ||||
r46673 | upgrade_op = upgrade_actions.UpgradeOperation( | |||
Pulkit Goyal
|
r46804 | ui, | ||
r46673 | newreqs, | |||
Pulkit Goyal
|
r46807 | repo.requirements, | ||
Pulkit Goyal
|
r46827 | up_actions, | ||
Pulkit Goyal
|
r46830 | removed_actions, | ||
r46673 | revlogs, | |||
) | ||||
Pierre-Yves David
|
r31864 | if not run: | ||
fromconfig = [] | ||||
Pierre-Yves David
|
r31904 | onlydefault = [] | ||
Pierre-Yves David
|
r31864 | |||
Pulkit Goyal
|
r46822 | for d in format_upgrades: | ||
Pierre-Yves David
|
r32031 | if d.fromconfig(repo): | ||
Pierre-Yves David
|
r31901 | fromconfig.append(d) | ||
Pierre-Yves David
|
r32031 | elif d.default: | ||
Pierre-Yves David
|
r31904 | onlydefault.append(d) | ||
Pierre-Yves David
|
r31864 | |||
Pierre-Yves David
|
r31904 | if fromconfig or onlydefault: | ||
Pierre-Yves David
|
r31864 | |||
if fromconfig: | ||||
r45302 | ui.status( | |||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b'repository lacks features recommended by ' | ||
b'current config options:\n\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pierre-Yves David
|
r31864 | for i in fromconfig: | ||
r45302 | ui.status(b'%s\n %s\n\n' % (i.name, i.description)) | |||
Pierre-Yves David
|
r31864 | |||
if onlydefault: | ||||
r45302 | ui.status( | |||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b'repository lacks features used by the default ' | ||
b'config options:\n\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pierre-Yves David
|
r31864 | for i in onlydefault: | ||
r45302 | ui.status(b'%s\n %s\n\n' % (i.name, i.description)) | |||
Pierre-Yves David
|
r31864 | |||
r45302 | ui.status(b'\n') | |||
Pierre-Yves David
|
r31864 | else: | ||
Pulkit Goyal
|
r46822 | ui.status(_(b'(no format upgrades found in existing repository)\n')) | ||
Pierre-Yves David
|
r31864 | |||
r45302 | ui.status( | |||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b'performing an upgrade with "--run" will make the following ' | ||
b'changes:\n\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pierre-Yves David
|
r31864 | |||
Pulkit Goyal
|
r46807 | upgrade_op.print_requirements() | ||
Pulkit Goyal
|
r46806 | upgrade_op.print_optimisations() | ||
Pulkit Goyal
|
r46805 | upgrade_op.print_upgrade_actions() | ||
Pulkit Goyal
|
r46804 | upgrade_op.print_affected_revlogs() | ||
Pierre-Yves David
|
r31864 | |||
Pulkit Goyal
|
r46808 | if upgrade_op.unused_optimizations: | ||
r45302 | ui.status( | |||
Augie Fackler
|
r43346 | _( | ||
Augie Fackler
|
r43347 | b'additional optimizations are available by specifying ' | ||
b'"--optimize <name>":\n\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pulkit Goyal
|
r46808 | upgrade_op.print_unused_optimizations() | ||
Pierre-Yves David
|
r31864 | return | ||
Pulkit Goyal
|
r46848 | if not (upgrade_op.upgrade_actions or upgrade_op.removed_actions): | ||
ui.status(_(b'nothing to do\n')) | ||||
return | ||||
Pierre-Yves David
|
r31864 | # Else we're in the run=true case. | ||
Augie Fackler
|
r43347 | ui.write(_(b'upgrade will perform the following actions:\n\n')) | ||
Pulkit Goyal
|
r46807 | upgrade_op.print_requirements() | ||
Pulkit Goyal
|
r46806 | upgrade_op.print_optimisations() | ||
Pulkit Goyal
|
r46805 | upgrade_op.print_upgrade_actions() | ||
Pulkit Goyal
|
r46804 | upgrade_op.print_affected_revlogs() | ||
Pierre-Yves David
|
r31864 | |||
r45302 | ui.status(_(b'beginning upgrade...\n')) | |||
Jun Wu
|
r33438 | with repo.wlock(), repo.lock(): | ||
r45302 | ui.status(_(b'repository locked and read-only\n')) | |||
Jun Wu
|
r33438 | # Our strategy for upgrading the repository is to create a new, | ||
# temporary repository, write data to it, then do a swap of the | ||||
# data. There are less heavyweight ways to do this, but it is easier | ||||
# to create a new repo object than to instantiate all the components | ||||
# (like the store) separately. | ||||
Augie Fackler
|
r43347 | tmppath = pycompat.mkdtemp(prefix=b'upgrade.', dir=repo.path) | ||
Jun Wu
|
r33438 | backuppath = None | ||
try: | ||||
r45302 | ui.status( | |||
Augie Fackler
|
r43346 | _( | ||
Pulkit Goyal
|
r46839 | b'creating temporary repository to stage upgraded ' | ||
Augie Fackler
|
r43347 | b'data: %s\n' | ||
Augie Fackler
|
r43346 | ) | ||
% tmppath | ||||
) | ||||
Boris Feld
|
r35343 | |||
Yuya Nishihara
|
r35380 | # clone ui without using ui.copy because repo.ui is protected | ||
repoui = repo.ui.__class__(repo.ui) | ||||
dstrepo = hg.repository(repoui, path=tmppath, create=True) | ||||
Pierre-Yves David
|
r31864 | |||
Jun Wu
|
r33438 | with dstrepo.wlock(), dstrepo.lock(): | ||
r46661 | backuppath = upgrade_engine.upgrade( | |||
r46673 | ui, repo, dstrepo, upgrade_op | |||
Augie Fackler
|
r43346 | ) | ||
Pulkit Goyal
|
r46838 | if not backup: | ||
r45302 | ui.status( | |||
Pulkit Goyal
|
r46824 | _(b'removing old repository content %s\n') % backuppath | ||
r45302 | ) | |||
Boris Feld
|
r41121 | repo.vfs.rmtree(backuppath, forcibly=True) | ||
backuppath = None | ||||
Pierre-Yves David
|
r31864 | |||
Jun Wu
|
r33438 | finally: | ||
r45302 | ui.status(_(b'removing temporary repository %s\n') % tmppath) | |||
Jun Wu
|
r33438 | repo.vfs.rmtree(tmppath, forcibly=True) | ||
Pierre-Yves David
|
r31864 | |||
r45302 | if backuppath and not ui.quiet: | |||
Augie Fackler
|
r43346 | ui.warn( | ||
Augie Fackler
|
r43347 | _(b'copy of old repository backed up at %s\n') % backuppath | ||
Augie Fackler
|
r43346 | ) | ||
ui.warn( | ||||
_( | ||||
Augie Fackler
|
r43347 | b'the old repository will not be deleted; remove ' | ||
b'it to free up disk space once the upgraded ' | ||||
b'repository is verified\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pulkit Goyal
|
r46617 | |||
Pulkit Goyal
|
r46830 | upgrade_op.print_post_op_messages() | ||
Pulkit Goyal
|
r46852 | |||
def upgrade_share_to_safe(ui, hgvfs, storevfs, current_requirements): | ||||
"""Upgrades a share to use share-safe mechanism""" | ||||
wlock = None | ||||
try: | ||||
wlock = lockmod.trylock(ui, hgvfs, b'wlock', 0, 0) | ||||
store_requirements = localrepo._readrequires(storevfs, False) | ||||
# after upgrade, store requires will be shared, so lets find | ||||
# the requirements which are not present in store and | ||||
# write them to share's .hg/requires | ||||
diffrequires = current_requirements - store_requirements | ||||
# add share-safe requirement as it will mark the share as share-safe | ||||
diffrequires.add(requirementsmod.SHARESAFE_REQUIREMENT) | ||||
scmutil.writerequires(hgvfs, diffrequires) | ||||
current_requirements.add(requirementsmod.SHARESAFE_REQUIREMENT) | ||||
ui.warn(_(b'repository upgraded to use share-safe mode\n')) | ||||
except error.LockError as e: | ||||
Pulkit Goyal
|
r46856 | if ui.configbool(b'experimental', b'sharesafe-auto-upgrade-fail-error'): | ||
raise error.Abort( | ||||
_(b'failed to upgrade share, got error: %s') | ||||
% stringutil.forcebytestr(e.strerror) | ||||
) | ||||
elif ui.configbool(b'experimental', b'sharesafe-warn-outdated-shares'): | ||||
Pulkit Goyal
|
r46855 | ui.warn( | ||
_(b'failed to upgrade share, got error: %s\n') | ||||
% stringutil.forcebytestr(e.strerror) | ||||
) | ||||
Pulkit Goyal
|
r46852 | finally: | ||
if wlock: | ||||
wlock.release() | ||||
Pulkit Goyal
|
r46853 | |||
def downgrade_share_to_non_safe( | ||||
ui, | ||||
hgvfs, | ||||
sharedvfs, | ||||
current_requirements, | ||||
): | ||||
"""Downgrades a share which use share-safe to not use it""" | ||||
wlock = None | ||||
try: | ||||
wlock = lockmod.trylock(ui, hgvfs, b'wlock', 0, 0) | ||||
source_requirements = localrepo._readrequires(sharedvfs, True) | ||||
# we cannot be 100% sure on which requirements were present in store when | ||||
# the source supported share-safe. However, we do know that working | ||||
# directory requirements were not there. Hence we remove them | ||||
source_requirements -= requirementsmod.WORKING_DIR_REQUIREMENTS | ||||
current_requirements |= source_requirements | ||||
current_requirements.remove(requirementsmod.SHARESAFE_REQUIREMENT) | ||||
scmutil.writerequires(hgvfs, current_requirements) | ||||
ui.warn(_(b'repository downgraded to not use share-safe mode\n')) | ||||
except error.LockError as e: | ||||
# raise error right away because if downgrade failed, we cannot load | ||||
# the repository because it does not have complete set of requirements | ||||
raise error.Abort( | ||||
_(b'failed to downgrade share, got error: %s') | ||||
% stringutil.forcebytestr(e.strerror) | ||||
) | ||||
finally: | ||||
if wlock: | ||||
wlock.release() | ||||