MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / buildFileTree

Function buildFileTree

packages/web/src/features/git/utils.ts:49–96  ·  view source on GitHub ↗
(flatList: { type: string, path: string }[])

Source from the content-addressed store, hash-verified

47
48
49export const buildFileTree = (flatList: { type: string, path: string }[]): FileTreeNode => {
50 const root: FileTreeNode = {
51 name: 'root',
52 path: '',
53 type: 'tree',
54 children: [],
55 };
56
57 for (const item of flatList) {
58 const parts = item.path.split('/');
59 let current: FileTreeNode = root;
60
61 for (let i = 0; i < parts.length; i++) {
62 const part = parts[i];
63 const isLeaf = i === parts.length - 1;
64 const nodeType = isLeaf ? item.type : 'tree';
65 let next = current.children.find((child: FileTreeNode) => child.name === part && child.type === nodeType);
66
67 if (!next) {
68 next = {
69 name: part,
70 path: item.path,
71 type: nodeType,
72 children: [],
73 };
74 current.children.push(next);
75 }
76 current = next;
77 }
78 }
79
80 const sortTree = (node: FileTreeNode): FileTreeNode => {
81 if (node.type === 'blob') {
82 return node;
83 }
84
85 const sortedChildren = node.children
86 .map(sortTree)
87 .sort(compareFileTreeItems);
88
89 return {
90 ...node,
91 children: sortedChildren,
92 };
93 };
94
95 return sortTree(root);
96}

Callers 2

getTreeFunction · 0.90
utils.test.tsFile · 0.90

Calls 1

sortTreeFunction · 0.85

Tested by

no test coverage detected