diff options
author | Andrej Shadura <andrew.shadura@collabora.co.uk> | 2021-02-07 11:11:16 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2021-03-11 12:48:47 +0100 |
commit | b17c14ae91b5f68e1b0f0c80c256ce984036299c (patch) | |
tree | 9828352181574da5688710f7b4d7a1c1ccbfddc4 | |
parent | d6a92cc6c1b4ac86ae0e83929bc33b5fca1e6359 (diff) |
gbp.git: Change list_tree to return an iterator
When working with huge trees, we want to avoid consuming large amounts
of memory just to throw away most of it almost immediately when we only
need one entry or a few of them.
Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
-rw-r--r-- | gbp/git/repository.py | 12 | ||||
-rw-r--r-- | tests/doctests/test_GitRepository.py | 8 |
2 files changed, 9 insertions, 11 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py index e21b19ec..ef93a4d8 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -1071,8 +1071,8 @@ class GitRepository(object): def list_tree(self, treeish, recurse=False, paths=None): """ - Get a trees content. It returns a list of objects that match the - 'ls-tree' output: [mode, type, sha1, path]. + Get a trees content. It yields tuples that match the + 'ls-tree' output: (mode, type, sha1, path). @param treeish: the treeish object to list @type treeish: C{str} @@ -1091,15 +1091,13 @@ class GitRepository(object): if ret: raise GitRepositoryError("Failed to ls-tree '%s': '%s'" % (treeish, err.decode().strip())) - tree = [] for line in out.split(b'\0'): if line: parts = line.split(None, 3) # decode everything but the file name - for i in range(len(parts) - 1): - parts[i] = parts[i].decode() - tree.append(parts) - return tree + filename = parts.pop() + 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 ca32394a..98fede04 100644 --- a/tests/doctests/test_GitRepository.py +++ b/tests/doctests/test_GitRepository.py @@ -942,15 +942,15 @@ def test_make_tree(): >>> sha1 = repo.write_file('testfile') >>> sha1 '19af7398c894bc5e86e17259317e4db519e9241f' - >>> head = repo.list_tree('HEAD') + >>> head = list(repo.list_tree('HEAD')) >>> head - [['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', b'testfile']] + [('100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', b'testfile')] >>> head.append(['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', 'testfile2']) >>> newtree = repo.make_tree(head) >>> newtree '745951810c9e22fcc6de9b23f05efd6ab5512123' - >>> repo.list_tree(newtree, recurse=False, paths='testfile') - [['100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', b'testfile']] + >>> list(repo.list_tree(newtree, recurse=False, paths='testfile')) + [('100644', 'blob', '19af7398c894bc5e86e17259317e4db519e9241f', b'testfile')] >>> repo.make_tree([]) '4b825dc642cb6eb9a060e54bf8d69288fbee4904' """ |