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

Function getTree

packages/web/src/features/git/getTreeApi.ts:22–102  ·  view source on GitHub ↗
({ repoName, revisionName, paths }: GetTreeRequest, { source }: { source?: string } = {})

Source from the content-addressed store, hash-verified

20 * into a single tree.
21 */
22export const getTree = async ({ repoName, revisionName, paths }: GetTreeRequest, { source }: { source?: string } = {}): Promise<GetTreeResponse | ServiceError> => sew(() =>
23 withOptionalAuth(async ({ org, prisma, user }) => {
24 if (user) {
25 const resolvedSource = source ?? (await headers()).get('X-Sourcebot-Client-Source') ?? undefined;
26 await createAudit({
27 action: 'user.fetched_file_tree',
28 actor: { id: user.id, type: 'user' },
29 target: { id: org.id.toString(), type: 'org' },
30 orgId: org.id,
31 metadata: { source: resolvedSource },
32 });
33 }
34
35 const repo = await prisma.repo.findFirst({
36 where: {
37 name: repoName,
38 orgId: org.id,
39 },
40 });
41
42 if (!repo) {
43 return notFound(`Repository "${repoName}" not found.`);
44 }
45
46 if (!isGitRefValid(revisionName)) {
47 return invalidGitRef(revisionName);
48 }
49
50 const { path: repoPath } = getRepoPath(repo);
51
52 const git = simpleGit().cwd(repoPath);
53 if (!paths.every(path => isPathValid(path))) {
54 return notFound();
55 }
56
57 const normalizedPaths = paths.map(path => normalizePath(path));
58
59 let result: string = '';
60 try {
61
62 const command = [
63 // Disable quoting of non-ASCII characters in paths
64 '-c', 'core.quotePath=false',
65 'ls-tree',
66 revisionName,
67 // format as output as {type},{path}
68 '--format=%(objecttype),%(path)',
69 // include tree nodes
70 '-t',
71 '--',
72 '.',
73 ...normalizedPaths,
74 ];
75
76 result = await git.raw(command);
77 } catch (error) {
78 logger.error('git ls-tree failed.', { error });
79 return unexpectedError('git ls-tree command failed.');

Callers 2

listTree.tsFile · 0.90
route.tsFile · 0.90

Calls 11

sewFunction · 0.90
withOptionalAuthFunction · 0.90
createAuditFunction · 0.90
notFoundFunction · 0.90
isGitRefValidFunction · 0.90
invalidGitRefFunction · 0.90
getRepoPathFunction · 0.90
isPathValidFunction · 0.90
normalizePathFunction · 0.90
unexpectedErrorFunction · 0.90
buildFileTreeFunction · 0.90

Tested by

no test coverage detected