diff options
author | Andrej Shadura <andrew.shadura@collabora.co.uk> | 2021-02-07 13:16:00 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2021-03-11 12:48:47 +0100 |
commit | 3fa9f4d35c38fcf0fdd387e6e0a0199a14a5160e (patch) | |
tree | a308db8778546c919042bb554054815cf2c07d0e | |
parent | b17c14ae91b5f68e1b0f0c80c256ce984036299c (diff) |
gbp.git: Add support for long listing format (with object sizes)
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>
-rw-r--r-- | gbp/git/repository.py | 18 | ||||
-rw-r--r-- | tests/doctests/test_GitRepository.py | 2 |
2 files changed, 15 insertions, 5 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py index ef93a4d8..75b3e035 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -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 #} diff --git a/tests/doctests/test_GitRepository.py b/tests/doctests/test_GitRepository.py index 98fede04..5b3c8f4e 100644 --- a/tests/doctests/test_GitRepository.py +++ b/tests/doctests/test_GitRepository.py @@ -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' """ |