2
0
Эх сурвалжийг харах

Replace some hardcoded SHA-1 values with object_format attributes

Jelmer Vernooij 2 сар өмнө
parent
commit
36d89869aa

+ 19 - 7
dulwich/object_store.py

@@ -1321,12 +1321,12 @@ class PackBasedObjectStore(PackCapableObjectStore, PackedObjectContainer):
           sha1: sha for the object.
           include_comp: Whether to include compression metadata.
         """
-        if sha1 == ZERO_SHA:
+        if sha1 == self.object_format.zero_oid:
             raise KeyError(sha1)
-        if len(sha1) == 40:
+        if len(sha1) == self.object_format.hex_length:
             sha = hex_to_sha(cast(ObjectID, sha1))
             hexsha = cast(ObjectID, sha1)
-        elif len(sha1) == 20:
+        elif len(sha1) == self.object_format.oid_length:
             sha = cast(RawObjectID, sha1)
             hexsha = None
         else:
@@ -2330,8 +2330,20 @@ class DiskObjectStore(PackBasedObjectStore):
             # Get all reachable commits
             commit_ids = get_reachable_commits(self, all_refs)
         else:
-            # Just use the direct ref targets (already ObjectIDs)
-            commit_ids = all_refs
+            # Just use the direct ref targets - ensure they're hex ObjectIDs
+            commit_ids = []
+            for ref in all_refs:
+                if isinstance(ref, bytes) and len(ref) == self.object_format.hex_length:
+                    # Already hex ObjectID
+                    commit_ids.append(ref)
+                elif isinstance(ref, bytes) and len(ref) == self.object_format.oid_length:
+                    # Binary SHA, convert to hex ObjectID
+                    from .objects import sha_to_hex
+
+                    commit_ids.append(sha_to_hex(ref))
+                else:
+                    # Assume it's already correct format
+                    commit_ids.append(ref)
 
         if commit_ids:
             # Write commit graph directly to our object store path
@@ -2447,9 +2459,9 @@ class MemoryObjectStore(PackCapableObjectStore):
         self.pack_compression_level = -1
 
     def _to_hexsha(self, sha: ObjectID | RawObjectID) -> ObjectID:
-        if len(sha) == 40:
+        if len(sha) == self.object_format.hex_length:
             return cast(ObjectID, sha)
-        elif len(sha) == 20:
+        elif len(sha) == self.object_format.oid_length:
             return sha_to_hex(cast(RawObjectID, sha))
         else:
             raise ValueError(f"Invalid sha {sha!r}")

+ 6 - 4
dulwich/repo.py

@@ -893,10 +893,12 @@ class BaseRepo:
         # TODO: move this method to WorkTree
         return self.refs[HEADREF]
 
-    def _get_object(self, sha: bytes, cls: type[T]) -> T:
-        assert len(sha) in (20, 40)
-        obj_id = ObjectID(sha) if len(sha) == 40 else RawObjectID(sha)
-        ret = self.get_object(obj_id)
+    def _get_object(self, sha: ObjectID | RawObjectID, cls: type[T]) -> T:
+        assert len(sha) in (
+            self.object_format.oid_length,
+            self.object_format.hex_length,
+        )
+        ret = self.get_object(sha)
         if not isinstance(ret, cls):
             if cls is Commit:
                 raise NotCommitError(ret.id)