Parcourir la source

Add ObjectStore.iter_prefix

jelmer@jelmer.uk il y a 4 mois
Parent
commit
36602eca84
2 fichiers modifiés avec 14 ajouts et 3 suppressions
  1. 7 2
      dulwich/object_store.py
  2. 7 1
      dulwich/tests/test_object_store.py

+ 7 - 2
dulwich/object_store.py

@@ -1041,7 +1041,8 @@ class DiskObjectStore(PackBasedObjectStore):
 
     def iter_prefix(self, prefix):
         if len(prefix) < 2:
-            return super().iter_prefix(prefix)
+            yield from super().iter_prefix(prefix)
+            return
         seen = set()
         dir = prefix[:2].decode()
         rest = prefix[2:].decode()
@@ -1053,7 +1054,11 @@ class DiskObjectStore(PackBasedObjectStore):
                     yield sha
 
         for p in self.packs:
-            bin_prefix = binascii.unhexlify(prefix) if len(prefix) % 2 == 0 else binascii.unhexlify(prefix[:-1])
+            bin_prefix = (
+                binascii.unhexlify(prefix)
+                if len(prefix) % 2 == 0
+                else binascii.unhexlify(prefix[:-1])
+            )
             for sha in p.index.iter_prefix(bin_prefix):
                 sha = sha_to_hex(sha)
                 if sha.startswith(prefix) and sha not in seen:

+ 7 - 1
dulwich/tests/test_object_store.py

@@ -238,8 +238,14 @@ class ObjectStoreTests:
 
     def test_iter_prefix(self):
         self.store.add_object(testobject)
+        self.assertEqual([testobject.id], list(self.store.iter_prefix(testobject.id)))
+        self.assertEqual(
+            [testobject.id], list(self.store.iter_prefix(testobject.id[:10]))
+        )
+        self.assertEqual(
+            [testobject.id], list(self.store.iter_prefix(testobject.id[:4]))
+        )
         self.assertEqual([testobject.id], list(self.store.iter_prefix(b"")))
-        self.assertEqual([testobject.id], list(self.store.iter_prefix(testobject.id[:10])))
 
 
 class PackBasedObjectStoreTests(ObjectStoreTests):