diff --git a/NEWS b/NEWS
index 36f332038..e5c8b89ec 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,8 @@
+0.22.6 UNRELEASED
+
+ * ``ObjectStore.iter_prefix``: fix handling of missing
+ loose object directories. (Jelmer Vernooij)
+
0.22.5 2024-11-07
* Drop support for Python 3.8. (Jelmer Vernooij)
diff --git a/dulwich/object_store.py b/dulwich/object_store.py
index b9ed1cb99..03ce91aee 100644
--- a/dulwich/object_store.py
+++ b/dulwich/object_store.py
@@ -1039,12 +1039,15 @@ def iter_prefix(self, prefix):
seen = set()
dir = prefix[:2].decode()
rest = prefix[2:].decode()
- for name in os.listdir(os.path.join(self.path, dir)):
- if name.startswith(rest):
- sha = os.fsencode(dir + name)
- if sha not in seen:
- seen.add(sha)
- yield sha
+ try:
+ for name in os.listdir(os.path.join(self.path, dir)):
+ if name.startswith(rest):
+ sha = os.fsencode(dir + name)
+ if sha not in seen:
+ seen.add(sha)
+ yield sha
+ except FileNotFoundError:
+ pass
for p in self.packs:
bin_prefix = (
diff --git a/dulwich/tests/test_object_store.py b/dulwich/tests/test_object_store.py
index aba93f4f9..c012706ed 100644
--- a/dulwich/tests/test_object_store.py
+++ b/dulwich/tests/test_object_store.py
@@ -247,6 +247,9 @@ def test_iter_prefix(self):
)
self.assertEqual([testobject.id], list(self.store.iter_prefix(b"")))
+ def test_iter_prefix_not_found(self):
+ self.assertEqual([], list(self.store.iter_prefix(b"1" * 40)))
+
class PackBasedObjectStoreTests(ObjectStoreTests):
def tearDown(self):