2
  C   ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ[uÈÖnP¸VDHÓ@þÃ„™uÿÈ_ŠI|¥�+Wé3ž¯ÇÆ ?÷     # upgrade.py - functions for in place upgrade of Mercurial repository
#
# Copyright (c) 2016-present, Gregory Szorc
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import stat

from ..i18n import _
from ..pycompat import getattr
from .. import (
    changelog,
    error,
    filelog,
    manifest,
    metadata,
    pycompat,
    requirements,
    scmutil,
    store,
    util,
    vfs as vfsmod,
)
from ..revlogutils import (
    constants as revlogconst,
    flagutil,
    nodemap,
    sidedata as sidedatamod,
)
from . import actions as upgrade_actions


def get_sidedata_helpers(srcrepo, dstrepo):
    use_w = srcrepo.ui.configbool(b'experimental', b'worker.repository-upgrade')
    sequential = pycompat.iswindows or not use_w
    if not sequential:
        srcrepo.register_sidedata_computer(
            revlogconst.KIND_CHANGELOG,
            sidedatamod.SD_FILES,
            (sidedatamod.SD_FILES,),
            metadata._get_worker_sidedata_adder(srcrepo, dstrepo),
            flagutil.REVIDX_HASCOPIESINFO,
            replace=True,
        )
    return sidedatamod.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata)


def _revlogfrompath(repo, rl_type, path):
    """Obtain a revlog from a repo path.

    An instance of the appropriate class is returned.
    """
    if rl_type & store.FILEFLAGS_CHANGELOG:
        return changelog.changelog(repo.svfs)
    elif rl_type & store.FILEFLAGS_MANIFESTLOG:
        mandir = b''
        if b'/' in path:
            mandir = path.rsplit(b'/', 1)[0]
        return manifest.manifestrevlog(
            repo.nodeconstants, repo.svfs, tree=mandir
        )
    else:
        # drop the extension and the `data/` prefix
        path_part = path.rsplit(b'.', 1)[0].split(b'/', 1)
        if len(path_part) < 2:
            msg = _(b'cannot recognize revlog from filename: %s')
            msg %= path
            raise error.Abort(msg)
        path = path_part[1]
        return filelog.filelog(repo.svfs, path)


def _copyrevlog(tr, destrepo, oldrl, rl_type, unencodedname):
    """copy all relevant files for `oldrl` into `destrepo` store

    Files are copied "as is" without any transformation. The copy is performed
    without extra checks. Callers are responsible for making sure the copied
    content is compatible with format of the destination repository.
    """
    oldrl = getattr(oldrl, '_revlog', oldrl)
    newrl = _revlogfrompath(destrepo, rl_type, unencodedname)
    newrl = getattr(newrl, '_revlog', newrl)

    oldvfs = oldrl.opener
    newvfs = newrl.opener
    oldindex = oldvfs.join(oldrl._indexfile)
    newindex = newvfs.join(newrl._indexfile)
    olddata = oldvfs.join(oldrl._datafile)
    newdata = newvfs.join(newrl._datafile)

    with newvfs(newrl._indexfile, b'w'):
        pass  # create all the directories

    util.copyfile(oldindex, newindex)
    copydata = oldrl.opener.exists(oldrl._datafile)
    if copydata:
        util.copyfile(olddata, newdata)

    if rl_type & store.FILEFLAGS_FILELOG:
        destrepo.svfs.fncache.add(unencodedname)
        if copydata:
            destrepo.svfs.fncache.add(unencodedname[:-2] + b'.d')


UPGRADE_CHANGELOG = b"changelog"
UPGRADE_MANIFEST = b"manifest"
UPGRADE_FILELOGS = b"all-filelogs"

UPGRADE_ALL_REVLOGS = frozenset(
    [UPGRADE_CHANGELOG, UPGRADE_MANIFEST, UPGRADE_FILELOGS]
)


def matchrevlog(revlogfilter, rl_type):
    """check if a revlog is selected for cloning.

    In other words, are there any updates which need to be done on revlog
    or it can be blindly copied.

    The store entry is checked against the passed filter"""
    if rl_type & store.FILEFLAGS_CHANGELOG:
        return UPGRADE_CHANGELOG in revlogfilter
    elif rl_type & store.FILEFLAGS_MANIFESTLOG:
        return UPGRADE_MANIFEST in revlogfilter
    assert rl_type & store.FILEFLAGS_FILELOG
    return UPGRADE_FILELOGS in revlogfilter


def _perform_clone(
    ui,
    dstrepo,
    tr,
    