({ repoName, revisionName, paths }: GetTreeRequest, { source }: { source?: string } = {})
| 20 | * into a single tree. |
| 21 | */ |
| 22 | export 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.'); |
no test coverage detected