gbp.git: Add support for long listing format (with object sizes)
authorAndrej Shadura <andrew.shadura@collabora.co.uk>
Sun, 7 Feb 2021 12:16:00 +0000 (13:16 +0100)
committerGuido Günther <agx@sigxcpu.org>
Thu, 11 Mar 2021 11:48:47 +0000 (12:48 +0100)
This is useful to be able to scan a tree for a specific file
but only act if it’s non-empty.

Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
gbp/git/repository.py
tests/doctests/test_GitRepository.py

index ef93a4d89fc8340dc84cd537af63c581e85f0c88..75b3e035766e5750ac9f2992c2d8d235a8057381 100644 (file)
@@ -1069,20 +1069,24 @@ class GitRepository(object):
             raise GitRepositoryError("Not a Git repository object: '%s'" % obj)
         return out[0].decode().strip()
 
-    def list_tree(self, treeish, recurse=False, paths=None):
+    def list_tree(self, treeish, recurse=False, paths=None, sizes=False):
         """
         Get a trees content. It yields tuples that match the
-        'ls-tree' output: (mode, type, sha1, path).
+        'ls-tree' output: (mode, type, sha1, path). When sizes is True,
+        includes object sizes: (mode, type, sha1, size, path)
 
         @param treeish: the treeish object to list
         @type treeish: C{str}
         @param recurse: whether to list the tree recursively
         @type recurse: C{bool}
+        @param sizes: whether to include object sizes
+        @type recurse: C{bool}
         @return: the tree
         @rtype: C{list} of objects. See above.
         """
         args = GitArgs('-z')
         args.add_true(recurse, '-r')
+        args.add_true(sizes, '-l')
         args.add(treeish)
         args.add("--")
         args.add_cond(paths, paths)
@@ -1093,11 +1097,15 @@ class GitRepository(object):
 
         for line in out.split(b'\0'):
             if line:
-                parts = line.split(None, 3)
+                parts = line.split(None, 4 if sizes else 3)
                 # decode everything but the file name
                 filename = parts.pop()
-                mode, type, sha1 = (part.decode() for part in parts)
-                yield mode, type, sha1, filename
+                if sizes:
+                    mode, type, sha1, size = (part.decode() for part in parts)
+                    yield mode, type, sha1, int(size), filename
+                else:
+                    mode, type, sha1 = (part.decode() for part in parts)
+                    yield mode, type, sha1, filename
 
 #}
 
index 98fede048264ed5078ad417dfb2cf3354459fd92..5b3c8f4eb5e22a0fa636b9cc0c8eb2649a137ca4 100644 (file)
@@ -951,6 +951,8 @@ def test_make_tree():
     '745951810c9e22fcc6de9b23f05efd6ab5512123'
     >>> list(repo.list_tree(newtree, recurse=False, paths='testfile'))
     [('100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', b'testfile')]
+    >>> list(repo.list_tree(newtree, recurse=False, paths='testfile', sizes=True))
+    [('100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', 20, b'testfile')]
     >>> repo.make_tree([])
     '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
     """