0
  8   'ˆÂ�þ]�±,Ü¡p¬ˆ$‘¦�üÿÕ„d-ºÔÁa–ßi~” T¾R( 	ÅÍÆ^¸ )‹FÿÓ ?÷     '# streamclone.py - producing and consuming streaming repository data
#
# Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import contextlib
import errno
import os
import struct

from .i18n import _
from .pycompat import open
from .interfaces import repository
from . import (
    bookmarks,
    cacheutil,
    error,
    narrowspec,
    phases,
    pycompat,
    requirements as requirementsmod,
    scmutil,
    store,
    util,
)
from .revlogutils import (
    nodemap,
)
from .utils import (
    stringutil,
)


def new_stream_clone_requirements(default_requirements, streamed_requirements):
    """determine the final set of requirement for a new stream clone

    this method combine the "default" requirements that a new repository would
    use with the constaint we get from the stream clone content. We keep local
    configuration choice when possible.
    """
    requirements = set(default_requirements)
    requirements -= requirementsmod.STREAM_FIXED_REQUIREMENTS
    requirements.update(streamed_requirements)
    return requirements


def streamed_requirements(repo):
    """the set of requirement the new clone will have to support

    This is used for advertising the stream options and to generate the actual
    stream content."""
    requiredformats = (
        repo.requirements & requirementsmod.STREAM_FIXED_REQUIREMENTS
    )
    return requiredformats


def canperformstreamclone(pullop, bundle2=False):
    """Whether it is possible to perform a streaming clone as part of pull.

    ``bundle2`` will cause the function to consider stream clone through
    bundle2 and only through bundle2.

    Returns a tuple of (supported, requirements). ``supported`` is True if
    streaming clone is supported and False otherwise. ``requirements`` is
    a set of repo requirements from the remote, or ``None`` if stream clone
    isn't supported.
    """
    repo = pullop.repo
    remote = pullop.remote

    bundle2supported = False
    if pullop.canusebundle2:
        if b'v2' in pullop.remotebundle2caps.get(b'stream', []):
            bundle2supported = True
        # else
        # Server doesn't support bundle2 stream clone or doesn't support
        # the versions we support. Fall back and possibly allow legacy.

    # Ensures legacy code path uses available bundle2.
    if bundle2supported and not bundle2:
        return False, None
    # Ensures bundle2 doesn't try to do a stream clone if it isn't supported.
    elif bundle2 and not bundle2supported:
        return False, None

    # Streaming clone only works on empty repositories.
    if len(repo):
        return False, None

    # Streaming clone only works if all data is being requested.
    if pullop.heads:
        return False, None

    streamrequested = pullop.streamclonerequested

    # If we don't have a preference, let the server decide for us. This
    # likely only comes into play in LANs.
    if streamrequested is None:
        # The server can advertise whether to prefer streaming clone.
        streamrequested = remote.capable(b'stream-preferred')

    if not streamrequested:
        return False, None

    # In order for stream clone to work, the client has to support all the
    # requirements advertised by the server.
    #
    # The server advertises its requirements via the "stream" and "streamreqs"
    # capability. "stream" (a value-less capability) is advertised if and only
    # if the only requirement is "revlogv1." Else, the "streamreqs" capability
    # is advertised and contains a comma-delimited list of requirements.
    requirements = set()
    if remote.capable(b'stream'):
        requirements.add(requirementsmod.REVLOGV1_REQUIREMENT)
    else:
        streamreqs = remote.capable(b'streamreqs')
        # This is weird and shouldn't happen with modern servers.
        if not streamreqs:
            pullop.repo.ui.warn(
          