Просмотр исходного кода

Update for upstream type changes

Jelmer Vernooij 1 месяц назад
Родитель
Сommit
e909af89ba
2 измененных файлов с 17 добавлено и 13 удалено
  1. 9 8
      dulwich/midx.py
  2. 8 5
      dulwich/object_store.py

+ 9 - 8
dulwich/midx.py

@@ -61,6 +61,7 @@ else:
     has_mmap = True
     has_mmap = True
 
 
 from .file import GitFile, _GitFile
 from .file import GitFile, _GitFile
+from .objects import ObjectID, RawObjectID
 from .pack import SHA1Writer
 from .pack import SHA1Writer
 
 
 # MIDX signature
 # MIDX signature
@@ -304,7 +305,7 @@ class MultiPackIndex:
         """Return the number of objects in this MIDX."""
         """Return the number of objects in this MIDX."""
         return self.object_count
         return self.object_count
 
 
-    def _get_oid(self, index: int) -> bytes:
+    def _get_oid(self, index: int) -> RawObjectID:
         """Get the object ID at the given index.
         """Get the object ID at the given index.
 
 
         Args:
         Args:
@@ -317,7 +318,7 @@ class MultiPackIndex:
             raise IndexError(f"Index {index} out of range")
             raise IndexError(f"Index {index} out of range")
 
 
         offset = self._oidl_offset + index * self.hash_size
         offset = self._oidl_offset + index * self.hash_size
-        return self._contents[offset : offset + self.hash_size]
+        return RawObjectID(self._contents[offset : offset + self.hash_size])
 
 
     def _get_pack_info(self, index: int) -> tuple[int, int]:
     def _get_pack_info(self, index: int) -> tuple[int, int]:
         """Get pack ID and offset for object at the given index.
         """Get pack ID and offset for object at the given index.
@@ -351,7 +352,7 @@ class MultiPackIndex:
 
 
         return pack_id, pack_offset
         return pack_id, pack_offset
 
 
-    def object_offset(self, sha: bytes) -> tuple[str, int] | None:
+    def object_offset(self, sha: ObjectID | RawObjectID) -> tuple[str, int] | None:
         """Return the pack name and offset for the given object.
         """Return the pack name and offset for the given object.
 
 
         Args:
         Args:
@@ -386,7 +387,7 @@ class MultiPackIndex:
 
 
         return None
         return None
 
 
-    def __contains__(self, sha: bytes) -> bool:
+    def __contains__(self, sha: ObjectID | RawObjectID) -> bool:
         """Check if the given object SHA is in this MIDX.
         """Check if the given object SHA is in this MIDX.
 
 
         Args:
         Args:
@@ -397,7 +398,7 @@ class MultiPackIndex:
         """
         """
         return self.object_offset(sha) is not None
         return self.object_offset(sha) is not None
 
 
-    def iterentries(self) -> Iterator[tuple[bytes, str, int]]:
+    def iterentries(self) -> Iterator[tuple[RawObjectID, str, int]]:
         """Iterate over all entries in this MIDX.
         """Iterate over all entries in this MIDX.
 
 
         Yields:
         Yields:
@@ -453,7 +454,7 @@ def load_midx_file(
 
 
 def write_midx(
 def write_midx(
     f: IO[bytes],
     f: IO[bytes],
-    pack_index_entries: list[tuple[str, list[tuple[bytes, int, int | None]]]],
+    pack_index_entries: list[tuple[str, list[tuple[RawObjectID, int, int | None]]]],
     hash_algorithm: int = HASH_ALGORITHM_SHA1,
     hash_algorithm: int = HASH_ALGORITHM_SHA1,
 ) -> bytes:
 ) -> bytes:
     """Write a multi-pack-index file.
     """Write a multi-pack-index file.
@@ -481,7 +482,7 @@ def write_midx(
     pack_index_entries_sorted = sorted(pack_index_entries, key=lambda x: x[0])
     pack_index_entries_sorted = sorted(pack_index_entries, key=lambda x: x[0])
 
 
     # Collect all objects from all packs
     # Collect all objects from all packs
-    all_objects: list[tuple[bytes, int, int]] = []  # (sha, pack_id, offset)
+    all_objects: list[tuple[RawObjectID, int, int]] = []  # (sha, pack_id, offset)
     pack_names: list[str] = []
     pack_names: list[str] = []
 
 
     for pack_id, (pack_name, entries) in enumerate(pack_index_entries_sorted):
     for pack_id, (pack_name, entries) in enumerate(pack_index_entries_sorted):
@@ -621,7 +622,7 @@ def write_midx(
 
 
 def write_midx_file(
 def write_midx_file(
     path: str | os.PathLike[str],
     path: str | os.PathLike[str],
-    pack_index_entries: list[tuple[str, list[tuple[bytes, int, int | None]]]],
+    pack_index_entries: list[tuple[str, list[tuple[RawObjectID, int, int | None]]]],
     hash_algorithm: int = HASH_ALGORITHM_SHA1,
     hash_algorithm: int = HASH_ALGORITHM_SHA1,
 ) -> bytes:
 ) -> bytes:
     """Write a multi-pack-index file to disk.
     """Write a multi-pack-index file to disk.

+ 8 - 5
dulwich/object_store.py

@@ -2106,7 +2106,7 @@ class DiskObjectStore(PackBasedObjectStore):
         self._pack_cache[base_name] = pack
         self._pack_cache[base_name] = pack
         return pack
         return pack
 
 
-    def contains_packed(self, sha: bytes) -> bool:
+    def contains_packed(self, sha: ObjectID | RawObjectID) -> bool:
         """Check if a particular object is present by SHA1 and is packed.
         """Check if a particular object is present by SHA1 and is packed.
 
 
         This checks the MIDX first if available, then falls back to checking
         This checks the MIDX first if available, then falls back to checking
@@ -2126,7 +2126,7 @@ class DiskObjectStore(PackBasedObjectStore):
         # Fall back to checking individual packs
         # Fall back to checking individual packs
         return super().contains_packed(sha)
         return super().contains_packed(sha)
 
 
-    def get_raw(self, name: bytes) -> tuple[int, bytes]:
+    def get_raw(self, name: RawObjectID | ObjectID) -> tuple[int, bytes]:
         """Obtain the raw fulltext for an object.
         """Obtain the raw fulltext for an object.
 
 
         This uses the MIDX if available for faster lookups.
         This uses the MIDX if available for faster lookups.
@@ -2143,10 +2143,13 @@ class DiskObjectStore(PackBasedObjectStore):
         if name == ZERO_SHA:
         if name == ZERO_SHA:
             raise KeyError(name)
             raise KeyError(name)
 
 
+        sha: RawObjectID
         if len(name) == 40:
         if len(name) == 40:
-            sha = hex_to_sha(name)
+            # name is ObjectID (hex), convert to RawObjectID
+            sha = hex_to_sha(cast(ObjectID, name))
         elif len(name) == 20:
         elif len(name) == 20:
-            sha = name
+            # name is already RawObjectID (binary)
+            sha = RawObjectID(name)
         else:
         else:
             raise AssertionError(f"Invalid object name {name!r}")
             raise AssertionError(f"Invalid object name {name!r}")
 
 
@@ -2186,7 +2189,7 @@ class DiskObjectStore(PackBasedObjectStore):
             return b"\x00" * 20
             return b"\x00" * 20
 
 
         # Collect entries from all packs
         # Collect entries from all packs
-        pack_entries: list[tuple[str, list[tuple[bytes, int, int | None]]]] = []
+        pack_entries: list[tuple[str, list[tuple[RawObjectID, int, int | None]]]] = []
 
 
         for pack in packs:
         for pack in packs:
             # Git stores .idx extension in MIDX, not .pack
             # Git stores .idx extension in MIDX, not .pack