Build a ``{name, total_count, children}`` hierarchy. Each leaf is either a code symbol (class / top-level function) or a synthetic "(+N more)" placeholder for truncated wide directories. Each interior node carries the full descendant leaf count, including entries hidden by ``max_chi
(
graph: Dict[str, Any],
*,
root: Optional[str] = None,
max_children: int = DEFAULT_MAX_CHILDREN,
project_label: Optional[str] = None,
)
| 192 | |
| 193 | |
| 194 | def build_tree( |
| 195 | graph: Dict[str, Any], |
| 196 | *, |
| 197 | root: Optional[str] = None, |
| 198 | max_children: int = DEFAULT_MAX_CHILDREN, |
| 199 | project_label: Optional[str] = None, |
| 200 | ) -> Dict[str, Any]: |
| 201 | """Build a ``{name, total_count, children}`` hierarchy. |
| 202 | |
| 203 | Each leaf is either a code symbol (class / top-level function) or |
| 204 | a synthetic "(+N more)" placeholder for truncated wide directories. |
| 205 | Each interior node carries the full descendant leaf count, including |
| 206 | entries hidden by ``max_children``. Directory omission labels count hidden |
| 207 | immediate entries; their totals count all leaves behind those entries. |
| 208 | |
| 209 | A graph whose nodes carry two or more distinct ``repo`` tags (a |
| 210 | ``merge-graphs`` / ``global add`` result) gets one top-level root per |
| 211 | repo, each with its own directory tree, so identical repo-relative paths |
| 212 | in different repos stay separate. Graphs with fewer than two tags are |
| 213 | built exactly as before. |
| 214 | |
| 215 | Raises ``ValueError`` for negative ``max_children`` or when an explicit |
| 216 | ``root`` matches none of the |
| 217 | graph's ``source_file`` paths — every file would silently attach flat |
| 218 | to the root and the hierarchy would be lost (#2534). |
| 219 | """ |
| 220 | if max_children < 0: |
| 221 | raise ValueError("--max-children must be nonnegative") |
| 222 | directory_ids: set[int] = set() |
| 223 | raw_nodes = graph.get("nodes", []) if isinstance(graph, dict) else [] |
| 224 | nodes: List[Dict[str, Any]] = [ |
| 225 | n for n in (raw_nodes if isinstance(raw_nodes, list) else []) |
| 226 | if isinstance(n, dict) |
| 227 | ] |
| 228 | for n in nodes: |
| 229 | raw_src = n.get("source_file") |
| 230 | if not isinstance(raw_src, str) or not raw_src.strip(): |
| 231 | # Same alias order as build._fold_node_aliases (`src` last), so a node |
| 232 | # carrying two conflicting aliases resolves to the identical |
| 233 | # source_file in the built graph and in this viewer. |
| 234 | for alias in ("file", "path", "filepath", "src"): |
| 235 | val = n.get(alias) |
| 236 | if isinstance(val, str) and val.strip(): |
| 237 | n["source_file"] = n.pop(alias).strip() |
| 238 | break |
| 239 | file_nodes = [ |
| 240 | n for n in nodes |
| 241 | if isinstance(n.get("source_file"), str) and n["source_file"].strip() |
| 242 | ] |
| 243 | if not file_nodes: |
| 244 | return {"name": "(empty graph)", "total_count": 0, "children": []} |
| 245 | |
| 246 | explicit_root = root is not None |
| 247 | groups, untagged = _repo_groups(file_nodes) |
| 248 | |
| 249 | if len(groups) < 2: |
| 250 | # Zero or one real repo tag: not a merged graph. Every node, tagged or |
| 251 | # not, goes through the original single-root path. |