mercurial/upgrade_utils/auto_upgrade.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 05 Apr 2022 05:19:47 +0200
changeset 49192 2ab79873786e
child 49194 e4b31016e194
permissions -rw-r--r--
auto-upgrade: introduce a way to auto-upgrade to/from share-safe This is the first "automatic-upgrade" capability. In the following commits, similar features are coming for other "fast to upgrade" formats. This is different from the `safe-mismatch.source-not-safe` and `safe-mismatch.source-safe` configuration that deal with mismatch between a share and its share-source. Here we are dealing with mismatch between a repository configuration and its actual format. We will need further work for cases were the repository cannot be locked. A basic protection is in place to avoid a infinite loop for now, but it will get proper attention in a later changeset. Differential Revision: https://phab.mercurial-scm.org/D12611
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
# upgrade.py - functions for automatic upgrade of Mercurial repository
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# Copyright (c) 2022-present, Pierre-Yves David
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
#
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
from ..i18n import _
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
from .. import (
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
    error,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
    requirements as requirementsmod,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
    scmutil,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
def get_share_safe_action(repo):
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
    """return an automatic-upgrade action for `share-safe` if applicable
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    If no action is needed, return None, otherwise return a callback to upgrade
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
    or downgrade the repository according the configuration and repository
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
    format.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
    """
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
    ui = repo.ui
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
    requirements = repo.requirements
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
    auto_upgrade_share_source = ui.configbool(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    26
        b'format',
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
        b'use-share-safe.automatic-upgrade-of-mismatching-repositories',
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
    )
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
    action = None
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
    if (
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
        auto_upgrade_share_source
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
        and requirementsmod.SHARED_REQUIREMENT not in requirements
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
    ):
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
        sf_config = ui.configbool(b'format', b'use-share-safe')
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
        sf_local = requirementsmod.SHARESAFE_REQUIREMENT in requirements
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
        if sf_config and not sf_local:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
            msg = _(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
                b"automatically upgrading repository to the `share-safe`"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
                b" feature\n"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
            )
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
            hint = b"(see `hg help config.format.use-share-safe` for details)\n"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    45
            def action():
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
                if not ui.quiet:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
                    ui.write_err(msg)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
                    ui.write_err(hint)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
                requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
                scmutil.writereporequirements(repo, requirements)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
        elif sf_local and not sf_config:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
            msg = _(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
                b"automatically downgrading repository from the `share-safe`"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
                b" feature\n"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
            )
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
            hint = b"(see `hg help config.format.use-share-safe` for details)\n"
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
            def action():
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
                if not ui.quiet:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
                    ui.write_err(msg)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    62
                    ui.write_err(hint)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    63
                requirements.discard(requirementsmod.SHARESAFE_REQUIREMENT)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
                scmutil.writereporequirements(repo, requirements)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    65
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    66
    return action
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    67
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    68
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    69
AUTO_UPGRADE_ACTIONS = [
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    70
    get_share_safe_action,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    71
]
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    73
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    74
def may_auto_upgrade(repo, maker_func):
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
    """potentially perform auto-upgrade and return the final repository to use
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
    Auto-upgrade are "quick" repository upgrade that might automatically be run
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
    by "any" repository access. See `hg help config.format` for automatic
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
    upgrade documentation.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
    note: each relevant upgrades are done one after the other for simplicity.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
    This avoid having repository is partially inconsistent state while
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    83
    upgrading.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
    repo: the current repository instance
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
    maker_func: a factory function that can recreate a repository after an upgrade
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
    """
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
    clear = False
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
    loop = 0
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
    while not clear:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
        loop += 1
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
        if loop > 100:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
            # XXX basic protection against infinite loop, make it better.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    96
            raise error.ProgrammingError("Too many auto upgrade loops")
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    97
        clear = True
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    98
        for get_action in AUTO_UPGRADE_ACTIONS:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    99
            action = get_action(repo)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   100
            if action is not None:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   101
                clear = False
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   102
                with repo.wlock(wait=False), repo.lock(wait=False):
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
                    action = get_action(repo)
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   104
                    if action is not None:
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   105
                        action()
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   106
                    repo = maker_func()
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   107
    return repo