upgrade.py
265 lines
| 8.6 KiB
| text/x-python
|
PythonLexer
/ mercurial / upgrade.py
|
r31894 | # upgrade.py - functions for in place upgrade of Mercurial repository | ||
|
r31864 | # | ||
|
r31895 | # Copyright (c) 2016-present, Gregory Szorc | ||
|
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, | ||||
|
r35344 | hg, | ||
|
r31893 | localrepo, | ||
|
r38183 | pycompat, | ||
r46661 | ) | |||
from .upgrade_utils import ( | ||||
r46662 | actions as upgrade_actions, | |||
r46661 | engine as upgrade_engine, | |||
|
r31864 | ) | ||
r46662 | allformatvariant = upgrade_actions.allformatvariant | |||
|
r43346 | |||
|
r41120 | # search without '-' to support older form on newer client. | ||
# | ||||
# We don't enforce backward compatibility for debug command so this | ||||
# might eventually be dropped. However, having to use two different | ||||
# forms in script when comparing result is anoying enough to add | ||||
# backward compatibility for a while. | ||||
legacy_opts_map = { | ||||
|
r43347 | b'redeltaparent': b're-delta-parent', | ||
b'redeltamultibase': b're-delta-multibase', | ||||
b'redeltaall': b're-delta-all', | ||||
b'redeltafulladd': b're-delta-fulladd', | ||||
|
r41120 | } | ||
|
r43346 | |||
def upgraderepo( | ||||
ui, | ||||
repo, | ||||
run=False, | ||||
optimize=None, | ||||
backup=True, | ||||
manifest=None, | ||||
changelog=None, | ||||
r46612 | filelogs=None, | |||
|
r43346 | ): | ||
|
r31864 | """Upgrade a repository in place.""" | ||
|
r41120 | if optimize is None: | ||
optimize = [] | ||||
|
r44937 | optimize = {legacy_opts_map.get(o, o) for o in optimize} | ||
|
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 | ||||
|
r31864 | # Ensure the repository can be upgraded. | ||
r46663 | upgrade_actions.check_source_requirements(repo) | |||
|
r31864 | |||
r46666 | default_options = localrepo.defaultcreateopts(repo.ui) | |||
newreqs = localrepo.newreporequirements(repo.ui, default_options) | ||||
r46662 | newreqs.update(upgrade_actions.preservedrequirements(repo)) | |||
|
r31864 | |||
r46666 | upgrade_actions.check_requirements_changes(repo, newreqs) | |||
|
r31864 | |||
# Find and validate all improvements that can be made. | ||||
r46662 | alloptimizations = upgrade_actions.findoptimizations(repo) | |||
|
r31864 | |||
|
r31899 | # Apply and Validate arguments. | ||
optimizations = [] | ||||
for o in alloptimizations: | ||||
if o.name in optimize: | ||||
optimizations.append(o) | ||||
optimize.discard(o.name) | ||||
|
r43346 | if optimize: # anything left is unknown | ||
raise error.Abort( | ||||
|
r43347 | _(b'unknown optimization action requested: %s') | ||
% b', '.join(sorted(optimize)), | ||||
|
r43387 | hint=_(b'run without arguments to see valid optimizations'), | ||
|
r43346 | ) | ||
|
r31864 | |||
|
r46822 | format_upgrades = upgrade_actions.find_format_upgrades(repo) | ||
r46662 | actions = upgrade_actions.determineactions( | |||
|
r46822 | repo, format_upgrades, repo.requirements, newreqs | ||
r46662 | ) | |||
|
r43346 | actions.extend( | ||
o | ||||
for o in sorted(optimizations) | ||||
# determineactions could have added optimisation | ||||
if o not in actions | ||||
) | ||||
|
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: | |||
|
r43346 | msg = _( | ||
|
r43347 | b'ignoring revlogs selection flags, format requirements ' | ||
b'change: %s\n' | ||||
|
r43346 | ) | ||
|
r43347 | ui.warn(msg % b', '.join(sorted(incompatible))) | ||
r46661 | revlogs = upgrade_engine.UPGRADE_ALL_REVLOGS | |||
r43100 | ||||
r46673 | upgrade_op = upgrade_actions.UpgradeOperation( | |||
|
r46804 | ui, | ||
r46673 | newreqs, | |||
|
r46807 | repo.requirements, | ||
|
r46805 | actions, | ||
r46673 | revlogs, | |||
) | ||||
|
r31864 | if not run: | ||
fromconfig = [] | ||||
|
r31904 | onlydefault = [] | ||
|
r31864 | |||
|
r46822 | for d in format_upgrades: | ||
|
r32031 | if d.fromconfig(repo): | ||
|
r31901 | fromconfig.append(d) | ||
|
r32031 | elif d.default: | ||
|
r31904 | onlydefault.append(d) | ||
|
r31864 | |||
|
r31904 | if fromconfig or onlydefault: | ||
|
r31864 | |||
if fromconfig: | ||||
r45302 | ui.status( | |||
|
r43346 | _( | ||
|
r43347 | b'repository lacks features recommended by ' | ||
b'current config options:\n\n' | ||||
|
r43346 | ) | ||
) | ||||
|
r31864 | for i in fromconfig: | ||
r45302 | ui.status(b'%s\n %s\n\n' % (i.name, i.description)) | |||
|
r31864 | |||
if onlydefault: | ||||
r45302 | ui.status( | |||
|
r43346 | _( | ||
|
r43347 | b'repository lacks features used by the default ' | ||
b'config options:\n\n' | ||||
|
r43346 | ) | ||
) | ||||
|
r31864 | for i in onlydefault: | ||
r45302 | ui.status(b'%s\n %s\n\n' % (i.name, i.description)) | |||
|
r31864 | |||
r45302 | ui.status(b'\n') | |||
|
r31864 | else: | ||
|
r46822 | ui.status(_(b'(no format upgrades found in existing repository)\n')) | ||
|
r31864 | |||
r45302 | ui.status( | |||
|
r43346 | _( | ||
|
r43347 | b'performing an upgrade with "--run" will make the following ' | ||
b'changes:\n\n' | ||||
|
r43346 | ) | ||
) | ||||
|
r31864 | |||
|
r46807 | upgrade_op.print_requirements() | ||
|
r46806 | upgrade_op.print_optimisations() | ||
|
r46805 | upgrade_op.print_upgrade_actions() | ||
|
r46804 | upgrade_op.print_affected_revlogs() | ||
|
r31864 | |||
|
r46808 | if upgrade_op.unused_optimizations: | ||
r45302 | ui.status( | |||
|
r43346 | _( | ||
|
r43347 | b'additional optimizations are available by specifying ' | ||
b'"--optimize <name>":\n\n' | ||||
|
r43346 | ) | ||
) | ||||
|
r46808 | upgrade_op.print_unused_optimizations() | ||
|
r31864 | return | ||
# Else we're in the run=true case. | ||||
|
r43347 | ui.write(_(b'upgrade will perform the following actions:\n\n')) | ||
|
r46807 | upgrade_op.print_requirements() | ||
|
r46806 | upgrade_op.print_optimisations() | ||
|
r46805 | upgrade_op.print_upgrade_actions() | ||
|
r46804 | upgrade_op.print_affected_revlogs() | ||
|
r31864 | |||
r45302 | ui.status(_(b'beginning upgrade...\n')) | |||
|
r33438 | with repo.wlock(), repo.lock(): | ||
r45302 | ui.status(_(b'repository locked and read-only\n')) | |||
|
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. | ||||
|
r43347 | tmppath = pycompat.mkdtemp(prefix=b'upgrade.', dir=repo.path) | ||
|
r33438 | backuppath = None | ||
try: | ||||
r45302 | ui.status( | |||
|
r43346 | _( | ||
|
r43347 | b'creating temporary repository to stage migrated ' | ||
b'data: %s\n' | ||||
|
r43346 | ) | ||
% tmppath | ||||
) | ||||
|
r35343 | |||
|
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) | ||||
|
r31864 | |||
|
r33438 | with dstrepo.wlock(), dstrepo.lock(): | ||
r46661 | backuppath = upgrade_engine.upgrade( | |||
r46673 | ui, repo, dstrepo, upgrade_op | |||
|
r43346 | ) | ||
|
r41121 | if not (backup or backuppath is None): | ||
r45302 | ui.status( | |||
|
r46824 | _(b'removing old repository content %s\n') % backuppath | ||
r45302 | ) | |||
|
r41121 | repo.vfs.rmtree(backuppath, forcibly=True) | ||
backuppath = None | ||||
|
r31864 | |||
|
r33438 | finally: | ||
r45302 | ui.status(_(b'removing temporary repository %s\n') % tmppath) | |||
|
r33438 | repo.vfs.rmtree(tmppath, forcibly=True) | ||
|
r31864 | |||
r45302 | if backuppath and not ui.quiet: | |||
|
r43346 | ui.warn( | ||
|
r43347 | _(b'copy of old repository backed up at %s\n') % backuppath | ||
|
r43346 | ) | ||
ui.warn( | ||||
_( | ||||
|
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' | ||||
|
r43346 | ) | ||
) | ||||
|
r46617 | |||
r46662 | if upgrade_actions.sharesafe.name in addedreqs: | |||
|
r46617 | ui.warn( | ||
_( | ||||
b'repository upgraded to share safe mode, existing' | ||||
b' shares will still work in old non-safe mode. ' | ||||
b'Re-share existing shares to use them in safe mode' | ||||
b' New shares will be created in safe mode.\n' | ||||
) | ||||
) | ||||
r46662 | if upgrade_actions.sharesafe.name in removedreqs: | |||
|
r46618 | ui.warn( | ||
_( | ||||
b'repository downgraded to not use share safe mode, ' | ||||
b'existing shares will not work and needs to' | ||||
b' be reshared.\n' | ||||
) | ||||
) | ||||