99import base
1010import binascii
1111import git .diff as diff
12+ import utils
1213from git .utils import join_path
1314
1415def sha_to_hex (sha ):
@@ -17,7 +18,8 @@ def sha_to_hex(sha):
1718 assert len (hexsha ) == 40 , "Incorrect length of sha1 string: %d" % hexsha
1819 return hexsha
1920
20- class Tree (base .IndexObject , diff .Diffable ):
21+
22+ class Tree (base .IndexObject , diff .Diffable , utils .Traversable ):
2123 """
2224 Tress represent a ordered list of Blobs and other Trees. Hence it can be
2325 accessed like a list.
@@ -48,6 +50,13 @@ class Tree(base.IndexObject, diff.Diffable):
4850 def __init__ (self , repo , sha , mode = 0 , path = None ):
4951 super (Tree , self ).__init__ (repo , sha , mode , path )
5052
53+ @classmethod
54+ def _get_intermediate_items (cls , index_object ):
55+ if index_object .type == "tree" :
56+ return index_object ._cache
57+ return tuple ()
58+
59+
5160 def _set_cache_ (self , attr ):
5261 if attr == "_cache" :
5362 # Set the data when we need it
@@ -154,46 +163,7 @@ def __div__(self, file):
154163
155164 def __repr__ (self ):
156165 return '<git.Tree "%s">' % self .sha
157-
158- @classmethod
159- def _iter_recursive (cls , repo , tree , cur_depth , max_depth , predicate , prune ):
160-
161- for obj in tree :
162- if predicate (obj ):
163- yield obj
164- if obj .type == "tree" and ( max_depth < 0 or cur_depth + 1 <= max_depth ) and not prune (obj ):
165- for recursive_obj in cls ._iter_recursive ( repo , obj , cur_depth + 1 , max_depth , predicate , prune ):
166- yield recursive_obj
167- # END for each recursive object
168- # END if we may enter recursion
169- # END for each object
170-
171- def traverse (self , max_depth = - 1 , predicate = lambda i : True , prune = lambda t : False ):
172- """
173- Returns
174-
175- Iterator to traverse the tree recursively up to the given level.
176- The traversal is depth-first.
177- The iterator returns Blob and Tree objects with paths relative to their
178- repository.
179-
180- ``max_depth``
181-
182- if -1, the whole tree will be traversed
183- if 0, only the first level will be traversed which is the same as
184- the default non-recursive iterator
185-
186- ``predicate``
187-
188- If predicate(item) returns True, item will be returned by iterator
189-
190- ``prune``
191166
192- If prune(tree) returns True, the traversal will not continue into the
193- given tree object.
194- """
195- return self ._iter_recursive ( self .repo , self , 0 , max_depth , predicate , prune )
196-
197167 @property
198168 def trees (self ):
199169 """
0 commit comments