Browse Source

Fix abstraction layering for PackIndex.

Jelmer Vernooij 2 years ago
parent
commit
e523efa0c5
1 changed files with 24 additions and 11 deletions
  1. 24 11
      dulwich/pack.py

+ 24 - 11
dulwich/pack.py

@@ -46,7 +46,7 @@ from itertools import chain
 
 
 import os
 import os
 import sys
 import sys
-from typing import Optional, Callable, Tuple
+from typing import Optional, Callable, Tuple, List
 
 
 from hashlib import sha1
 from hashlib import sha1
 from os import (
 from os import (
@@ -397,15 +397,7 @@ class PackIndex(object):
         lives at within the corresponding pack file. If the pack file doesn't
         lives at within the corresponding pack file. If the pack file doesn't
         have the object then None will be returned.
         have the object then None will be returned.
         """
         """
-        if len(sha) == 40:
-            sha = hex_to_sha(sha)
-        try:
-            return self._object_index(sha)
-        except ValueError:
-            closed = getattr(self._contents, "closed", None)
-            if closed in (None, True):
-                raise PackFileDisappeared(self)
-            raise
+        raise NotImplementedError(self.object_index)
 
 
     def object_sha1(self, index):
     def object_sha1(self, index):
         """Return the SHA1 corresponding to the index in the pack file."""
         """Return the SHA1 corresponding to the index in the pack file."""
@@ -460,7 +452,9 @@ class MemoryPackIndex(PackIndex):
     def __len__(self):
     def __len__(self):
         return len(self._entries)
         return len(self._entries)
 
 
-    def _object_index(self, sha):
+    def object_index(self, sha):
+        if len(sha) == 40:
+            sha = hex_to_sha(sha)
         return self._by_sha[sha][0]
         return self._by_sha[sha][0]
 
 
     def object_sha1(self, index):
     def object_sha1(self, index):
@@ -485,6 +479,8 @@ class FilePackIndex(PackIndex):
     present.
     present.
     """
     """
 
 
+    _fan_out_table: List[int]
+
     def __init__(self, filename, file=None, contents=None, size=None):
     def __init__(self, filename, file=None, contents=None, size=None):
         """Create a pack index object.
         """Create a pack index object.
 
 
@@ -596,6 +592,23 @@ class FilePackIndex(PackIndex):
         """
         """
         return bytes(self._contents[-20:])
         return bytes(self._contents[-20:])
 
 
+    def object_index(self, sha):
+        """Return the index in to the corresponding packfile for the object.
+
+        Given the name of an object it will return the offset that object
+        lives at within the corresponding pack file. If the pack file doesn't
+        have the object then None will be returned.
+        """
+        if len(sha) == 40:
+            sha = hex_to_sha(sha)
+        try:
+            return self._object_index(sha)
+        except ValueError:
+            closed = getattr(self._contents, "closed", None)
+            if closed in (None, True):
+                raise PackFileDisappeared(self)
+            raise
+
     def _object_index(self, sha):
     def _object_index(self, sha):
         """See object_index.
         """See object_index.