MAJOR=4
MINOR=60
DEVNAME=tty60
  C   ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ[”M¬Ü¡p¬ˆ$‘¦�üÿÊ„L¶ïÉ_ŠI|¥�+Wé3ž¯ÈÇ ?÷     # narrowcommands.py - command modifications for narrowhg extension
#
# Copyright 2017 Google, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

import itertools
import os

from mercurial.i18n import _
from mercurial.node import (
    hex,
    short,
)
from mercurial import (
    bundle2,
    cmdutil,
    commands,
    discovery,
    encoding,
    error,
    exchange,
    extensions,
    hg,
    narrowspec,
    pathutil,
    pycompat,
    registrar,
    repair,
    repoview,
    requirements,
    sparse,
    util,
    wireprototypes,
)
from mercurial.utils import (
    urlutil,
)

table = {}
command = registrar.command(table)


def setup():
    """Wraps user-facing mercurial commands with narrow-aware versions."""

    entry = extensions.wrapcommand(commands.table, b'clone', clonenarrowcmd)
    entry[1].append(
        (b'', b'narrow', None, _(b"create a narrow clone of select files"))
    )
    entry[1].append(
        (
            b'',
            b'depth',
            b'',
            _(b"limit the history fetched by distance from heads"),
        )
    )
    entry[1].append((b'', b'narrowspec', b'', _(b"read narrowspecs from file")))
    # TODO(durin42): unify sparse/narrow --include/--exclude logic a bit
    if b'sparse' not in extensions.enabled():
        entry[1].append(
            (b'', b'include', [], _(b"specifically fetch this file/directory"))
        )
        entry[1].append(
            (
                b'',
                b'exclude',
                [],
                _(b"do not fetch this file/directory, even if included"),
            )
        )

    entry = extensions.wrapcommand(commands.table, b'pull', pullnarrowcmd)
    entry[1].append(
        (
            b'',
            b'depth',
            b'',
            _(b"limit the history fetched by distance from heads"),
        )
    )

    extensions.wrapcommand(commands.table, b'archive', archivenarrowcmd)


def clonenarrowcmd(orig, ui, repo, *args, **opts):
    """Wraps clone command, so 'hg clone' first wraps localrepo.clone()."""
    opts = pycompat.byteskwargs(opts)
    wrappedextraprepare = util.nullcontextmanager()
    narrowspecfile = opts[b'narrowspec']

    if narrowspecfile:
        filepath = os.path.join(encoding.getcwd(), narrowspecfile)
        ui.status(_(b"reading narrowspec from '%s'\n") % filepath)
        try:
            fdata = util.readfile(filepath)
        except IOError as inst:
            raise error.Abort(
                _(b"cannot read narrowspecs from '%s': %s")
                % (filepath, encoding.strtolocal(inst.strerror))
            )

        includes, excludes, profiles = sparse.parseconfig(ui, fdata, b'narrow')
        if profiles:
            raise error.ConfigError(
                _(
                    b"cannot specify other files using '%include' in"
                    b" narrowspec"
                )
            )

        narrowspec.validatepatterns(includes)
        narrowspec.validatepatterns(excludes)

        # narrowspec is passed so we should assume that user wants narrow clone
        opts[b'narrow'] = True
        opts[b'include'].extend(includes)
        opts[b'exclude'].extend(excludes)

    if opts[b'narrow']:

        def pullbundle2extraprepare_widen(orig, pullop, kwargs):
            orig(pullop, kwargs)

            if opts.get(b'depth'):
                kwargs[b'depth'] = opts[b'depth']

        wrappedextraprepare = extensions.wrappedfunction(
            exchange, b'_pullbundle2extraprepare', pullbundle2extraprepare_widen
        )

    with wrappedextraprepare:
        return orig(ui, repo, *args, **pycompat.strkwargs(opts))


def pullnarrowcmd(orig, ui, repo, *args, **opts):
    """Wraps pull command to allow modifying narrow spec."""
    wrappedextraprepare = util.nullcontextmanager()
    if requirements.NARROW_REQUIREMENT in repo.requirements:

        def pullbundle2ext