|
2 | 2 |
|
3 | 3 | __all__ = ('tree_to_stream', 'tree_entries_from_data')
|
4 | 4 |
|
| 5 | +from stat import S_ISDIR |
| 6 | + |
| 7 | + |
5 | 8 | def tree_to_stream(entries, write):
|
6 | 9 | """Write the give list of entries into a stream using its write method
|
7 | 10 | :param entries: **sorted** list of tuples with (binsha, mode, name)
|
@@ -64,3 +67,118 @@ def tree_entries_from_data(data):
|
64 | 67 | out.append((sha, mode, name))
|
65 | 68 | # END for each byte in data stream
|
66 | 69 | return out
|
| 70 | + |
| 71 | + |
| 72 | +def _find_by_name(tree_data, name, is_dir, start_at): |
| 73 | + """return data entry matching the given name and tree mode |
| 74 | + or None. |
| 75 | + Before the item is returned, the respective data item is set |
| 76 | + None in the tree_data list to mark it done""" |
| 77 | + try: |
| 78 | + item = tree_data[start_at] |
| 79 | + if item and item[2] == name and S_ISDIR(item[1]) == is_dir: |
| 80 | + tree_data[start_at] = None |
| 81 | + return item |
| 82 | + except IndexError: |
| 83 | + pass |
| 84 | + # END exception handling |
| 85 | + for index, item in enumerate(tree_data): |
| 86 | + if item and item[2] == name and S_ISDIR(item[1]) == is_dir: |
| 87 | + tree_data[index] = None |
| 88 | + return item |
| 89 | + # END if item matches |
| 90 | + # END for each item |
| 91 | + return None |
| 92 | + |
| 93 | +def _to_full_path(item, path_prefix): |
| 94 | + """Rebuild entry with given path prefix""" |
| 95 | + if not item: |
| 96 | + return item |
| 97 | + return (item[0], item[1], path_prefix+item[2]) |
| 98 | + |
| 99 | +def traverse_trees_recursive(odb, tree_shas, path_prefix): |
| 100 | + """ |
| 101 | + :return: list with entries according to the given tree-shas. |
| 102 | + The result is encoded in a list |
| 103 | + of n tuple|None per blob/commit, (n == len(tree_shas)), where |
| 104 | + * [0] == 20 byte sha |
| 105 | + * [1] == mode as int |
| 106 | + * [2] == path relative to working tree root |
| 107 | + The entry tuple is None if the respective blob/commit did not |
| 108 | + exist in the given tree. |
| 109 | + :param tree_shas: iterable of shas pointing to trees. All trees must |
| 110 | + be on the same level. A tree-sha may be None in which case None |
| 111 | + :param path_prefix: a prefix to be added to the returned paths on this level, |
| 112 | + set it '' for the first iteration |
| 113 | + :note: The ordering of the returned items will be partially lost""" |
| 114 | + trees_data = list() |
| 115 | + nt = len(tree_shas) |
| 116 | + for tree_sha in tree_shas: |
| 117 | + if tree_sha is None: |
| 118 | + data = list() |
| 119 | + else: |
| 120 | + data = tree_entries_from_data(odb.stream(tree_sha).read()) |
| 121 | + # END handle muted trees |
| 122 | + trees_data.append(data) |
| 123 | + # END for each sha to get data for |
| 124 | + |
| 125 | + out = list() |
| 126 | + out_append = out.append |
| 127 | + |
| 128 | + # find all matching entries and recursively process them together if the match |
| 129 | + # is a tree. If the match is a non-tree item, put it into the result. |
| 130 | + # Processed items will be set None |
| 131 | + for ti, tree_data in enumerate(trees_data): |
| 132 | + for ii, item in enumerate(tree_data): |
| 133 | + if not item: |
| 134 | + continue |
| 135 | + # END skip already done items |
| 136 | + entries = [ None for n in range(nt) ] |
| 137 | + entries[ti] = item |
| 138 | + sha, mode, name = item # its faster to unpack |
| 139 | + is_dir = S_ISDIR(mode) # type mode bits |
| 140 | + |
| 141 | + # find this item in all other tree data items |
| 142 | + # wrap around, but stop one before our current index, hence |
| 143 | + # ti+nt, not ti+1+nt |
| 144 | + for tio in range(ti+1, ti+nt): |
| 145 | + tio = tio % nt |
| 146 | + entries[tio] = _find_by_name(trees_data[tio], name, is_dir, ii) |
| 147 | + # END for each other item data |
| 148 | + |
| 149 | + # if we are a directory, enter recursion |
| 150 | + if is_dir: |
| 151 | + out.extend(traverse_trees_recursive(odb, [ei[0] for ei in entries if ei], path_prefix+name+'/')) |
| 152 | + else: |
| 153 | + out_append(tuple(_to_full_path(e, path_prefix) for e in entries)) |
| 154 | + # END handle recursion |
| 155 | + |
| 156 | + # finally mark it done |
| 157 | + tree_data[ii] = None |
| 158 | + # END for each item |
| 159 | + |
| 160 | + # we are done with one tree, set all its data empty |
| 161 | + del(tree_data[:]) |
| 162 | + # END for each tree_data chunk |
| 163 | + return out |
| 164 | + |
| 165 | +def traverse_tree_recursive(odb, tree_sha, path_prefix): |
| 166 | + """ |
| 167 | + :return: list of entries of the tree pointed to by tree_sha. An entry |
| 168 | + has the following format: |
| 169 | + * [0] 20 byte sha |
| 170 | + * [1] mode as int |
| 171 | + * [2] path relative to the repository |
| 172 | + :param path_prefix: prefix to prepend to the front of all returned paths""" |
| 173 | + entries = list() |
| 174 | + data = tree_entries_from_data(odb.stream(tree_sha).read()) |
| 175 | + |
| 176 | + # unpacking/packing is faster than accessing individual items |
| 177 | + for sha, mode, name in data: |
| 178 | + if S_ISDIR(mode): |
| 179 | + entries.extend(traverse_tree_recursive(odb, sha, path_prefix+name+'/')) |
| 180 | + else: |
| 181 | + entries.append((sha, mode, path_prefix+name)) |
| 182 | + # END for each item |
| 183 | + |
| 184 | + return entries |
0 commit comments