4:27
  8   'ˆl–äY>”ÊeJeA3p\eÅ1hß�þBÜ¯´³r‰ðÈV^‘Ðù×„| _ÖÕÇÓ ?÷     '# pack.py -- For dealing with packed git objects.
# Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
# Copyright (C) 2008-2013 Jelmer Vernooij <jelmer@jelmer.uk>
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as public by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Classes for dealing with packed git objects.

A pack is a compact representation of a bunch of objects, stored
using deltas where possible.

They have two parts, the pack file, which stores the data, and an index
that tells you where the data is.

To find an object you look in all of the index files 'til you find a
match for the object name. You then use the pointer got from this as
a pointer in to the corresponding packfile.
"""

from collections import defaultdict

import binascii
from contextlib import suppress
from io import BytesIO, UnsupportedOperation
from collections import (
    deque,
)
try:
    from cdifflib import CSequenceMatcher as SequenceMatcher
except ModuleNotFoundError:
    from difflib import SequenceMatcher
import struct

from itertools import chain

import os
import sys
from typing import Optional, Callable, Tuple, List, Deque, Union, Iterable, Iterator, Dict, TypeVar, Generic, Sequence, Set, BinaryIO

try:
    from typing import Protocol
except ImportError:  # python << 3.8
    from typing_extensions import Protocol   # type: ignore

import warnings

from hashlib import sha1
from os import (
    SEEK_CUR,
    SEEK_END,
)
from struct import unpack_from
import zlib

try:
    import mmap
except ImportError:
    has_mmap = False
else:
    has_mmap = True

# For some reason the above try, except fails to set has_mmap = False for plan9
if sys.platform == "Plan9":
    has_mmap = False

from dulwich.errors import (
    ApplyDeltaError,
    ChecksumMismatch,
)
from dulwich.file import GitFile
from dulwich.lru_cache import (
    LRUSizeCache,
)
from dulwich.objects import (
    ShaFile,
    ObjectID,
    hex_to_sha,
    sha_to_hex,
    object_header,
)


OFS_DELTA = 6
REF_DELTA = 7

DELTA_TYPES = (OFS_DELTA, REF_DELTA)


DEFAULT_PACK_DELTA_WINDOW_SIZE = 10

# Keep pack files under 16Mb in memory, otherwise write them out to disk
PACK_SPOOL_FILE_MAX_SIZE = 16 * 1024 * 1024


OldUnpackedObject = Union[Tuple[Union[bytes, int], List[bytes]], List[bytes]]
ResolveExtRefFn = Callable[[bytes], Tuple[int, OldUnpackedObject]]
ProgressFn = Callable[[int, str], None]
PackHint = Tuple[int, Optional[bytes]]


class ObjectContainer(Protocol):

    def add_object(self, obj: ShaFile) -> None:
        """Add a single object to this object store."""

    def add_objects(
            self, objects: Sequence[Tuple[ShaFile, Optional[str]]],
            progress: Optional[Callable[[str], None]] = None) -> None:
        """Add a set of objects to this object store.

        Args:
          objects: Iterable over a list of (object, path) tuples
        """

    def __contains__(self, sha1: bytes) -> bool:
        """Check if a hex sha is present."""

    def __getitem__(self, sha1: bytes) -> ShaFile:
        """Retrieve an object."""


class PackedObjectContainer(ObjectContainer):

    def get_unpacked_object(self, sha1: bytes, *, include_comp: bool = False) -> "UnpackedObject":
        """Get a raw unresolved object."""
        raise NotImple