瀏覽代碼

objects: Add lookup_path method to Tree.

The proximate reason for this change is to avoid a circular import in
diff_tree.

Change-Id: I5516556739b08b1ebfbef9caab03cd43457ec28c
David Borowitz 13 年之前
父節點
當前提交
fe3a578297
共有 3 個文件被更改,包括 27 次插入12 次删除
  1. 2 0
      NEWS
  2. 6 12
      dulwich/object_store.py
  3. 19 0
      dulwich/objects.py

+ 2 - 0
NEWS

@@ -71,6 +71,8 @@
     value of unpack_object and various DeltaChainIterator methods.
     (Dave Borowitz)
 
+  * Add a lookup_path convenience method to Tree. (Dave Borowitz)
+
  TEST CHANGES
 
   * If setuptools is installed, "python setup.py test" will now run the testsuite.

+ 6 - 12
dulwich/object_store.py

@@ -685,23 +685,17 @@ class ObjectStoreIterator(ObjectIterator):
 
 
 def tree_lookup_path(lookup_obj, root_sha, path):
-    """Lookup an object in a Git tree.
+    """Look up an object in a Git tree.
 
     :param lookup_obj: Callback for retrieving object by SHA1
     :param root_sha: SHA1 of the root tree
     :param path: Path to lookup
+    :return: A tuple of (mode, SHA) of the resulting path.
     """
-    parts = path.split("/")
-    sha = root_sha
-    mode = None
-    for p in parts:
-        obj = lookup_obj(sha)
-        if not isinstance(obj, Tree):
-            raise NotTreeError(sha)
-        if p == '':
-            continue
-        mode, sha = obj[p]
-    return mode, sha
+    tree = lookup_obj(root_sha)
+    if not isinstance(tree, Tree):
+        raise NotTreeError(root_sha)
+    return tree.lookup_path(lookup_obj, path)
 
 
 class MissingObjectFinder(object):

+ 19 - 0
dulwich/objects.py

@@ -919,6 +919,25 @@ class Tree(ShaFile):
             text.append("%04o %s %s\t%s\n" % (mode, kind, hexsha, name))
         return "".join(text)
 
+    def lookup_path(self, lookup_obj, path):
+        """Look up an object in a Git tree.
+
+        :param lookup_obj: Callback for retrieving object by SHA1
+        :param path: Path to lookup
+        :return: A tuple of (mode, SHA) of the resulting path.
+        """
+        parts = path.split('/')
+        sha = self.id
+        mode = None
+        for p in parts:
+            if not p:
+                continue
+            obj = lookup_obj(sha)
+            if not isinstance(obj, Tree):
+                raise NotTreeError(sha)
+            mode, sha = obj[p]
+        return mode, sha
+
 
 def parse_timezone(text):
     """Parse a timezone text fragment (e.g. '+0100').