1843200
  C   9ˆl–äY>”JeJe@†à¸Û*bÑ¿�þY‘ÆÖnP¸VDHÓ@þÛ„n…ßÚ_ŠI|¥�+Wé3ž¯ÂÞ =e    9# remotefilelog.py - filelog implementation where filelog history is stored
#                    remotely
#
# Copyright 2013 Facebook, 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 collections
import os

from mercurial.node import bin
from mercurial.i18n import _
from mercurial import (
    ancestor,
    error,
    mdiff,
    revlog,
)
from mercurial.utils import storageutil
from mercurial.revlogutils import flagutil

from . import (
    constants,
    fileserverclient,
    shallowutil,
)


class remotefilelognodemap:
    def __init__(self, filename, store):
        self._filename = filename
        self._store = store

    def __contains__(self, node):
        missing = self._store.getmissing([(self._filename, node)])
        return not bool(missing)

    def __get__(self, node):
        if node not in self:
            raise KeyError(node)
        return node


class remotefilelog:

    _generaldelta = True
    _flagserrorclass = error.RevlogError

    def __init__(self, opener, path, repo):
        self.opener = opener
        self.filename = path
        self.repo = repo
        self.nodemap = remotefilelognodemap(self.filename, repo.contentstore)

        self.version = 1

        self._flagprocessors = dict(flagutil.flagprocessors)

    def read(self, node):
        """returns the file contents at this node"""
        t = self.revision(node)
        if not t.startswith(b'\1\n'):
            return t
        s = t.index(b'\1\n', 2)
        return t[s + 2 :]

    def add(self, text, meta, transaction, linknode, p1=None, p2=None):
        # hash with the metadata, like in vanilla filelogs
        hashtext = shallowutil.createrevlogtext(
            text, meta.get(b'copy'), meta.get(b'copyrev')
        )
        node = storageutil.hashrevisionsha1(hashtext, p1, p2)
        return self.addrevision(
            hashtext, transaction, linknode, p1, p2, node=node
        )

    def _createfileblob(self, text, meta, flags, p1, p2, node, linknode):
        # text passed to "_createfileblob" does not include filelog metadata
        header = shallowutil.buildfileblobheader(len(text), flags)
        data = b"%s\0%s" % (header, text)

        realp1 = p1
        copyfrom = b""
        if meta and b'copy' in meta:
            copyfrom = meta[b'copy']
            realp1 = bin(meta[b'copyrev'])

        data += b"%s%s%s%s%s\0" % (node, realp1, p2, linknode, copyfrom)

        visited = set()

        pancestors = {}
        queue = []
        if realp1 != self.repo.nullid:
            p1flog = self
            if copyfrom:
                p1flog = remotefilelog(self.opener, copyfrom, self.repo)

            pancestors.update(p1flog.ancestormap(realp1))
            queue.append(realp1)
            visited.add(realp1)
        if p2 != self.repo.nullid:
            pancestors.update(self.ancestormap(p2))
            queue.append(p2)
            visited.add(p2)

        ancestortext = b""

        # add the ancestors in topological order
        while queue:
            c = queue.pop(0)
            pa1, pa2, ancestorlinknode, pacopyfrom = pancestors[c]

            pacopyfrom = pacopyfrom or b''
            ancestortext += b"%s%s%s%s%s\0" % (
                c,
                pa1,
                pa2,
                ancestorlinknode,
                pacopyfrom,
            )

            if pa1 != self.repo.nullid and pa1 not in visited:
                queue.append(pa1)
                visited.add(pa1)
            if pa2 != self.repo.nullid and pa2 not in visited:
                queue.append(pa2)
                visited.add(pa2)

        data += ancestortext

        return data

    def addrevision(
        self,
        text,
        transaction,
        linknode,
        p1,
        p2,
        cachedelta=None,
        node=None,
        flags=revlog.REVIDX_DEFAULT_FLAGS,
        sidedata=None,
    ):
        # text passed to "addrevi