瀏覽代碼

Support empty path in Tree.lookup_path

Allow Tree.lookup_path to handle empty byte strings by returning the tree
itself with S_IFDIR mode. This matches the expected behavior when looking
up the root of a tree.
Jelmer Vernooij 3 周之前
父節點
當前提交
eb1a028e2b
共有 2 個文件被更改,包括 10 次插入0 次删除
  1. 4 0
      dulwich/objects.py
  2. 6 0
      tests/test_object_store.py

+ 4 - 0
dulwich/objects.py

@@ -1313,6 +1313,10 @@ class Tree(ShaFile):
           path: Path to lookup
         Returns: A tuple of (mode, SHA) of the resulting path.
         """
+        # Handle empty path - return the tree itself
+        if not path:
+            return stat.S_IFDIR, self.id
+
         parts = path.split(b"/")
         sha = self.id
         mode: Optional[int] = None

+ 6 - 0
tests/test_object_store.py

@@ -826,6 +826,12 @@ class TreeLookupPathTests(TestCase):
             b"ad/b/j",
         )
 
+    def test_lookup_empty_path(self) -> None:
+        # Empty path should return the tree itself
+        mode, sha = tree_lookup_path(self.get_object, self.tree_id, b"")
+        self.assertEqual(sha, self.tree_id)
+        self.assertEqual(mode, stat.S_IFDIR)
+
 
 class ObjectStoreGraphWalkerTests(TestCase):
     def get_walker(self, heads, parent_map):