Ver código fonte

Raise exception when encountering submodules.

Jelmer Vernooij 3 anos atrás
pai
commit
076647f6dc
2 arquivos alterados com 20 adições e 1 exclusões
  1. 11 1
      dulwich/objects.py
  2. 9 0
      dulwich/tests/test_object_store.py

+ 11 - 1
dulwich/objects.py

@@ -1032,6 +1032,14 @@ def pretty_format_tree_entry(name, mode, hexsha, encoding="utf-8"):
     )
 
 
+class SubmoduleEncountered(Exception):
+    """A submodule was encountered while resolving a path."""
+
+    def __init__(self, path, sha):
+        self.path = path
+        self.sha = sha
+
+
 class Tree(ShaFile):
     """A Git tree object"""
 
@@ -1182,9 +1190,11 @@ class Tree(ShaFile):
         parts = path.split(b"/")
         sha = self.id
         mode = None
-        for p in parts:
+        for i, p in enumerate(parts):
             if not p:
                 continue
+            if mode is not None and S_ISGITLINK(mode):
+                raise SubmoduleEncountered(b'/'.join(parts[:i]), sha)
             obj = lookup_obj(sha)
             if not isinstance(obj, Tree):
                 raise NotTreeError(sha)

+ 9 - 0
dulwich/tests/test_object_store.py

@@ -42,6 +42,8 @@ from dulwich.objects import (
     Tree,
     TreeEntry,
     EmptyFileException,
+    SubmoduleEncountered,
+    S_IFGITLINK,
 )
 from dulwich.object_store import (
     DiskObjectStore,
@@ -582,6 +584,7 @@ class TreeLookupPathTests(TestCase):
             (b"ad/bd/c", blob_c.id, 0o100755),
             (b"ad/c", blob_c.id, 0o100644),
             (b"c", blob_c.id, 0o100644),
+            (b"d", blob_c.id, S_IFGITLINK),
         ]
         self.tree_id = commit_tree(self.store, blobs)
 
@@ -600,6 +603,12 @@ class TreeLookupPathTests(TestCase):
         o_id = tree_lookup_path(self.get_object, self.tree_id, b"ad/bd/")[1]
         self.assertTrue(isinstance(self.store[o_id], Tree))
 
+    def test_lookup_submodule(self):
+        tree_lookup_path(self.get_object, self.tree_id, b"d")[1]
+        self.assertRaises(
+            SubmoduleEncountered, tree_lookup_path, self.get_object,
+            self.tree_id, b"d/a")
+
     def test_lookup_nonexistent(self):
         self.assertRaises(
             KeyError, tree_lookup_path, self.get_object, self.tree_id, b"j"