| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -173,6 +173,14 @@ def __init__(self, repo, sha, mode=None, path=None): | |||
| 173 | 173 | if isinstance(mode, basestring): | |
| 174 | 174 | self.mode = self._mode_str_to_int(mode) | |
| 175 | 175 | ||
| 176 | + def __hash__(self): | ||
| 177 | + """ | ||
| 178 | + Returns | ||
| 179 | + Hash of our path as index items are uniquely identifyable by path, not | ||
| 180 | + by their data ! | ||
| 181 | + """ | ||
| 182 | + return hash(self.path) | ||
| 183 | + | ||
| 176 | 184 | def _set_cache_(self, attr): | |
| 177 | 185 | if attr in IndexObject.__slots__: | |
| 178 | 186 | # they cannot be retrieved lateron ( not without searching for them ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | import base | |
| 10 | 10 | import binascii | |
| 11 | 11 | import git.diff as diff | |
| 12 | + import utils | ||
| 12 | 13 | from git.utils import join_path | |
| 13 | 14 | ||
| 14 | 15 | def sha_to_hex(sha): | |
@@ -17,7 +18,8 @@ def sha_to_hex(sha): | |||
| 17 | 18 | assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha | |
| 18 | 19 | return hexsha | |
| 19 | 20 | ||
| 20 | - class Tree(base.IndexObject, diff.Diffable): | ||
| 21 | + | ||
| 22 | + class Tree(base.IndexObject, diff.Diffable, utils.Traversable): | ||
| 21 | 23 | """ | |
| 22 | 24 | Tress represent a ordered list of Blobs and other Trees. Hence it can be | |
| 23 | 25 | accessed like a list. | |
@@ -48,6 +50,13 @@ class Tree(base.IndexObject, diff.Diffable): | |||
| 48 | 50 | def __init__(self, repo, sha, mode=0, path=None): | |
| 49 | 51 | super(Tree, self).__init__(repo, sha, mode, path) | |
| 50 | 52 | ||
| 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 | + | ||
| 51 | 60 | def _set_cache_(self, attr): | |
| 52 | 61 | if attr == "_cache": | |
| 53 | 62 | # Set the data when we need it | |
@@ -154,46 +163,7 @@ def __div__(self, file): | |||
| 154 | 163 | ||
| 155 | 164 | def __repr__(self): | |
| 156 | 165 | 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`` | ||
| 191 | 166 | ||
| 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 | - | ||
| 197 | 167 | @property | |
| 198 | 168 | def trees(self): | |
| 199 | 169 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,7 +134,7 @@ def addToStack( stack, lst, branch_first, dpth ): | |||
| 134 | 134 | ||
| 135 | 135 | while stack: | |
| 136 | 136 | d, item = stack.pop() # depth of item, item | |
| 137 | - | ||
| 137 | + | ||
| 138 | 138 | if item in visited: | |
| 139 | 139 | continue | |
| 140 | 140 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,10 +16,10 @@ | |||
| 16 | 16 | ||
| 17 | 17 | class TestBase(TestBase): | |
| 18 | 18 | ||
| 19 | - type_tuples = ( ("blob", "8741fc1d09d61f02ffd8cded15ff603eff1ec070"), | ||
| 20 | - ("tree", "3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79"), | ||
| 21 | - ("commit", "4251bd59fb8e11e40c40548cba38180a9536118c"), | ||
| 22 | - ("tag", "e56a60e8e9cd333cfba0140a77cd12b0d9398f10") ) | ||
| 19 | + type_tuples = ( ("blob", "8741fc1d09d61f02ffd8cded15ff603eff1ec070", "blob.py"), | ||
| 20 | + ("tree", "3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79", "directory"), | ||
| 21 | + ("commit", "4251bd59fb8e11e40c40548cba38180a9536118c", None), | ||
| 22 | + ("tag", "e56a60e8e9cd333cfba0140a77cd12b0d9398f10", None) ) | ||
| 23 | 23 | ||
| 24 | 24 | def test_base_object(self): | |
| 25 | 25 | # test interface of base object classes | |
@@ -29,8 +29,12 @@ def test_base_object(self): | |||
| 29 | 29 | s = set() | |
| 30 | 30 | num_objs = 0 | |
| 31 | 31 | num_index_objs = 0 | |
| 32 | - for obj_type, (typename, hexsha) in zip(types, self.type_tuples): | ||
| 33 | - item = obj_type(self.rorepo,hexsha) | ||
| 32 | + for obj_type, (typename, hexsha, path) in zip(types, self.type_tuples): | ||
| 33 | + item = None | ||
| 34 | + if path is None: | ||
| 35 | + item = obj_type(self.rorepo,hexsha) | ||
| 36 | + else: | ||
| 37 | + item = obj_type(self.rorepo,hexsha, 0, path) | ||
| 34 | 38 | num_objs += 1 | |
| 35 | 39 | assert item.sha == hexsha | |
| 36 | 40 | assert item.type == typename | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,11 +83,16 @@ def _cmp_tree_index(self, tree, index): | |||
| 83 | 83 | tree = self.rorepo.commit(tree).tree | |
| 84 | 84 | ||
| 85 | 85 | num_blobs = 0 | |
| 86 | - for blob in tree.traverse(predicate = lambda e: e.type == "blob"): | ||
| 86 | + blist = list() | ||
| 87 | + for blob in tree.traverse(predicate = lambda e: e.type == "blob", branch_first=False): | ||
| 87 | 88 | assert (blob.path,0) in index.entries | |
| 88 | - num_blobs += 1 | ||
| 89 | + blist.append(blob) | ||
| 89 | 90 | # END for each blob in tree | |
| 90 | - assert num_blobs == len(index.entries) | ||
| 91 | + if len(blist) != len(index.entries): | ||
| 92 | + iset = set(k[0] for k in index.entries.keys()) | ||
| 93 | + bset = set(b.path for b in blist) | ||
| 94 | + raise AssertionError( "CMP Failed: Missing entries in index: %s, missing in tree: %s" % (bset-iset, iset-bset) ) | ||
| 95 | + # END assertion message | ||
| 91 | 96 | ||
| 92 | 97 | def test_index_file_from_tree(self): | |
| 93 | 98 | common_ancestor_sha = "5117c9c8a4d3af19a9958677e45cda9269de1541" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ def test_traverse(self): | |||
| 29 | 29 | # limit recursion level to 0 - should be same as default iteration | |
| 30 | 30 | assert all_items | |
| 31 | 31 | assert 'CHANGES' in root | |
| 32 | - assert len(list(root)) == len(list(root.traverse(max_depth=0))) | ||
| 32 | + assert len(list(root)) == len(list(root.traverse(depth=1))) | ||
| 33 | 33 | ||
| 34 | 34 | # only choose trees | |
| 35 | 35 | trees_only = lambda i: i.type == "tree" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments