gbp.git.repository: Add GitRepository.list_tree
authorGuido Günther <agx@sigxcpu.org>
Thu, 31 May 2012 12:30:22 +0000 (14:30 +0200)
committerGuido Günther <agx@sigxcpu.org>
Thu, 31 May 2012 14:34:12 +0000 (16:34 +0200)
gbp/git/repository.py
tests/test_GitRepository.py

index 16bad60..43a8c46 100644 (file)
@@ -672,6 +672,33 @@ class GitRepository(object):
         if ret:
             raise GitRepositoryError("Not a Git repository object: '%s'" % obj)
         return out[0].strip()
+
+    def list_tree(self, treeish, recurse=False):
+        """
+        Get a trees content. It returns a list of objects that match the
+        'ls-tree' output: [ mode, type, sha1, path ].
+
+        @param treeish: the treeish object to list
+        @type treeish: C{str}
+        @param recurse: whether to list the tree recursively
+        @type recurse: C{bool}
+        @return: the tree
+        @rtype: C{list} of objects. See above.
+        """
+        args = GitArgs('-z')
+        args.add_true(recurse, '-r')
+        args.add(treeish)
+
+        out, err, ret =  self._git_inout('ls-tree', args.args, capture_stderr=True)
+        if ret:
+            raise GitRepositoryError("Failed to ls-tree '%s': '%s'" % (treeish, err))
+
+        tree = []
+        for line in out.split('\0'):
+            if line:
+                tree.append(line.split(None, 3))
+        return tree
+
 #}
 
     def get_config(self, name):
index b467339..328a65f 100644 (file)
@@ -616,6 +616,20 @@ def test_update_ref():
     True
     """
 
+
+def test_list_tree():
+    """
+    Test git-ls-tree
+
+    Methods tested:
+        - L{gbp.git.GitRepository.list_tree}
+
+    >>> import gbp.git
+    >>> repo = gbp.git.GitRepository(repo_dir)
+    >>> repo.list_tree('HEAD')
+    [['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', 'testfile']]
+    """
+
 def test_update_submodules():
     """
     Updating submodules if we don't have any is a noop