({
repo: repoName,
ref = 'HEAD',
path,
}: GetPathTypeRequest)
| 22 | * object lookup — no walking, just an index read. |
| 23 | */ |
| 24 | export const getPathType = async ({ |
| 25 | repo: repoName, |
| 26 | ref = 'HEAD', |
| 27 | path, |
| 28 | }: GetPathTypeRequest): Promise<GitObjectPathType | ServiceError> => sew(() => |
| 29 | withOptionalAuth(async ({ org, prisma }) => { |
| 30 | if (path === '' || path === '/') { |
| 31 | return 'tree'; |
| 32 | } |
| 33 | |
| 34 | const repo = await prisma.repo.findFirst({ |
| 35 | where: { |
| 36 | name: repoName, |
| 37 | orgId: org.id, |
| 38 | }, |
| 39 | }); |
| 40 | |
| 41 | if (!repo) { |
| 42 | return notFound(`Repository "${repoName}" not found.`); |
| 43 | } |
| 44 | |
| 45 | if (!isGitRefValid(ref)) { |
| 46 | return invalidGitRef(ref); |
| 47 | } |
| 48 | |
| 49 | const { path: repoPath } = getRepoPath(repo); |
| 50 | const git = simpleGit().cwd(repoPath); |
| 51 | |
| 52 | try { |
| 53 | const output = await git.raw(['cat-file', '-t', `${ref}:${path}`]); |
| 54 | const type = output.trim(); |
| 55 | if (type === 'blob' || type === 'tree') { |
| 56 | return type; |
| 57 | } |
| 58 | return notFound(`Path "${path}" at ref "${ref}" is not a file or directory.`); |
| 59 | } catch (error: unknown) { |
| 60 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 61 | |
| 62 | if (errorMessage.includes('not a git repository')) { |
| 63 | return unexpectedError( |
| 64 | `Invalid git repository at ${repoPath}. ` |
| 65 | + `The directory exists but is not a valid git repository.`, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | // `git cat-file` returns "Not a valid object name <ref>:<path>" when |
| 70 | // the path doesn't exist at that ref. Treat as not-found. |
| 71 | if (errorMessage.includes('Not a valid object name')) { |
| 72 | return notFound(`Path "${path}" not found at ref "${ref}".`); |
| 73 | } |
| 74 | |
| 75 | if (error instanceof Error) { |
| 76 | throw new Error( |
| 77 | `Failed to resolve path type for ${repoName}:${path}: ${error.message}`, |
| 78 | ); |
| 79 | } |
| 80 | throw new Error( |
| 81 | `Failed to resolve path type for ${repoName}:${path}: ${errorMessage}`, |
no test coverage detected