({ repoName, revisionName }: GetFilesRequest)
| 14 | export type GetFilesResponse = z.infer<typeof getFilesResponseSchema>; |
| 15 | |
| 16 | export const getFiles = async ({ repoName, revisionName }: GetFilesRequest): Promise<GetFilesResponse | ServiceError> => sew(() => |
| 17 | withOptionalAuth(async ({ org, prisma }) => { |
| 18 | const repo = await prisma.repo.findFirst({ |
| 19 | where: { |
| 20 | name: repoName, |
| 21 | orgId: org.id, |
| 22 | }, |
| 23 | }); |
| 24 | |
| 25 | if (!repo) { |
| 26 | return notFound(); |
| 27 | } |
| 28 | |
| 29 | const { path: repoPath } = getRepoPath(repo); |
| 30 | |
| 31 | const git = simpleGit().cwd(repoPath); |
| 32 | |
| 33 | let result: string; |
| 34 | try { |
| 35 | result = await git.raw([ |
| 36 | // Disable quoting of non-ASCII characters in paths |
| 37 | '-c', 'core.quotePath=false', |
| 38 | 'ls-tree', |
| 39 | revisionName, |
| 40 | // recursive |
| 41 | '-r', |
| 42 | // only return the names of the files |
| 43 | '--name-only', |
| 44 | ]); |
| 45 | } catch (error) { |
| 46 | logger.error('git ls-tree failed.', { error }); |
| 47 | return unexpectedError('git ls-tree command failed.'); |
| 48 | } |
| 49 | |
| 50 | const paths = result.split('\n').filter(line => line.trim()); |
| 51 | |
| 52 | const files: FileTreeItem[] = paths.map(path => { |
| 53 | const name = path.split('/').pop() ?? ''; |
| 54 | return { |
| 55 | type: 'blob', |
| 56 | path, |
| 57 | name, |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | return files; |
| 62 | })); |
no test coverage detected