Quellcode durchsuchen

Update for upstream type changes

Jelmer Vernooij vor 1 Monat
Ursprung
Commit
e909af89ba
2 geänderte Dateien mit 17 neuen und 13 gelöschten Zeilen
  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
 
 from .file import GitFile, _GitFile
+from .objects import ObjectID, RawObjectID
 from .pack import SHA1Writer
 
 # MIDX signature
@@ -304,7 +305,7 @@ class MultiPackIndex:
         """Return the number of objects in this MIDX."""
         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.
 
         Args:
@@ -317,7 +318,7 @@ class MultiPackIndex:
             raise IndexError(f"Index {index} out of range")
 
         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]:
         """Get pack ID and offset for object at the given index.
@@ -351,7 +352,7 @@ class MultiPackIndex:
 
         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.
 
         Args:
@@ -386,7 +387,7 @@ class MultiPackIndex:
 
         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.
 
         Args:
@@ -397,7 +398,7 @@ class MultiPackIndex:
         """
         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.
 
         Yields:
@@ -453,7 +454,7 @@ def load_midx_file(
 
 def write_midx(
     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,
 ) -> bytes:
     """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])
 
     # 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] = []
 
     for pack_id, (pack_name, entries) in enumerate(pack_index_entries_sorted):
@@ -621,7 +622,7 @@ def write_midx(
 
 def write_midx_file(
     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,
 ) -> bytes:
     """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
         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.
 
         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
         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.
 
         This uses the MIDX if available for faster lookups.
@@ -2143,10 +2143,13 @@ class DiskObjectStore(PackBasedObjectStore):
         if name == ZERO_SHA:
             raise KeyError(name)
 
+        sha: RawObjectID
         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:
-            sha = name
+            # name is already RawObjectID (binary)
+            sha = RawObjectID(name)
         else:
             raise AssertionError(f"Invalid object name {name!r}")
 
@@ -2186,7 +2189,7 @@ class DiskObjectStore(PackBasedObjectStore):
             return b"\x00" * 20
 
         # 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:
             # Git stores .idx extension in MIDX, not .pack