128
  C   ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ^o,Ü¡p¬ˆ$‘¦�üÿÂ„e¶‚çÁ_ŠI|¥�+Wé3ž¯ÁÀ  6   ˆl—ß=¿J	Å2ÛR‚ ›P-\mË€}LZ7ÿ�þDÛ‹8ËÎŒŒ€´Ð?ŸÄ598ÃÂÁ ?÷     # Mercurial bookmark support code
#
# Copyright 2008 David Soria Parra <dsp@php.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import struct

from .i18n import _
from .node import (
    bin,
    hex,
    short,
)
from .pycompat import getattr
from . import (
    encoding,
    error,
    obsutil,
    pycompat,
    requirements,
    scmutil,
    txnutil,
    util,
)
from .utils import (
    stringutil,
    urlutil,
)

# label constants
# until 3.5, bookmarks.current was the advertised name, not
# bookmarks.active, so we must use both to avoid breaking old
# custom styles
activebookmarklabel = b'bookmarks.active bookmarks.current'


def bookmarksinstore(repo):
    return requirements.BOOKMARKS_IN_STORE_REQUIREMENT in repo.requirements


def bookmarksvfs(repo):
    return repo.svfs if bookmarksinstore(repo) else repo.vfs


def _getbkfile(repo):
    """Hook so that extensions that mess with the store can hook bm storage.

    For core, this just handles wether we should see pending
    bookmarks or the committed ones. Other extensions (like share)
    may need to tweak this behavior further.
    """
    fp, pending = txnutil.trypending(
        repo.root, bookmarksvfs(repo), b'bookmarks'
    )
    return fp


class bmstore:
    r"""Storage for bookmarks.

    This object should do all bookmark-related reads and writes, so
    that it's fairly simple to replace the storage underlying
    bookmarks without having to clone the logic surrounding
    bookmarks. This type also should manage the active bookmark, if
    any.

    This particular bmstore implementation stores bookmarks as
    {hash}\s{name}\n (the same format as localtags) in
    .hg/bookmarks. The mapping is stored as {name: nodeid}.
    """

    def __init__(self, repo):
        self._repo = repo
        self._refmap = refmap = {}  # refspec: node
        self._nodemap = nodemap = {}  # node: sorted([refspec, ...])
        self._clean = True
        self._aclean = True
        has_node = repo.changelog.index.has_node
        tonode = bin  # force local lookup
        try:
            with _getbkfile(repo) as bkfile:
                for line in bkfile:
                    line = line.strip()
                    if not line:
                        continue
                    try:
                        sha, refspec = line.split(b' ', 1)
                        node = tonode(sha)
                        if has_node(node):
                            refspec = encoding.tolocal(refspec)
                            refmap[refspec] = node
                            nrefs = nodemap.get(node)
                            if nrefs is None:
                                nodemap[node] = [refspec]
                            else:
                                nrefs.append(refspec)
                                if nrefs[-2] > refspec:
                                    # bookmarks weren't sorted before 4.5
                                    nrefs.sort()
                    except ValueError:
                        # binascii.Error (ValueError subclass):
                        # - bin(...)
                        # ValueError:
                        # - node in nm, for non-20-bytes entry
                        # - split(...), for string without ' '
                        bookmarkspath = b'.hg/bookmarks'
                        if bookmarksinstore(repo):
                            bookmarkspath = b'.hg/store/bookmarks'
                        repo.ui.warn(
                            _(b'malformed line in %s: %r\n')
                            % (bookmarkspath, pycompat.bytestr(line))
                        )
        except FileNotFoundError:
            pass
        self._active = _readactive(repo, self)

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, mark):
  