({
repo: repoName,
base,
head,
path,
}: GetDiffRequest)
| 36 | } |
| 37 | |
| 38 | export const getDiff = async ({ |
| 39 | repo: repoName, |
| 40 | base, |
| 41 | head, |
| 42 | path, |
| 43 | }: GetDiffRequest): Promise<GetDiffResult | ServiceError> => sew(() => |
| 44 | withOptionalAuth(async ({ org, prisma }) => { |
| 45 | if (!isGitRefValid(base)) { |
| 46 | return invalidGitRef(base); |
| 47 | } |
| 48 | |
| 49 | if (!isGitRefValid(head)) { |
| 50 | return invalidGitRef(head); |
| 51 | } |
| 52 | |
| 53 | const repo = await prisma.repo.findFirst({ |
| 54 | where: { |
| 55 | name: repoName, |
| 56 | orgId: org.id, |
| 57 | }, |
| 58 | }); |
| 59 | |
| 60 | if (!repo) { |
| 61 | return notFound(`Repository "${repoName}" not found.`); |
| 62 | } |
| 63 | |
| 64 | const { path: repoPath } = getRepoPath(repo); |
| 65 | const git = simpleGit().cwd(repoPath); |
| 66 | |
| 67 | try { |
| 68 | const diffArgs: string[] = ['diff', base, head]; |
| 69 | // The `--` pathspec separator both restricts the diff to the path |
| 70 | // and prevents anything path-shaped from being interpreted as a |
| 71 | // flag or ref by git. |
| 72 | if (path) { |
| 73 | diffArgs.push('--', path); |
| 74 | } |
| 75 | |
| 76 | const rawDiff = await git.raw(diffArgs); |
| 77 | const files = parseDiff(rawDiff); |
| 78 | |
| 79 | const nodes: FileDiff[] = files.map((file) => ({ |
| 80 | oldPath: file.from && file.from !== '/dev/null' ? file.from : null, |
| 81 | newPath: file.to && file.to !== '/dev/null' ? file.to : null, |
| 82 | hunks: file.chunks.map((chunk) => { |
| 83 | // chunk.content is the full @@ header line, e.g.: |
| 84 | // "@@ -7,6 +7,8 @@ some heading text" |
| 85 | // The heading is the optional text after the second @@. |
| 86 | const headingMatch = chunk.content.match(/^@@ .+ @@ (.+)$/); |
| 87 | const heading = headingMatch ? headingMatch[1].trim() : undefined; |
| 88 | |
| 89 | return { |
| 90 | oldRange: { start: chunk.oldStart, lines: chunk.oldLines }, |
| 91 | newRange: { start: chunk.newStart, lines: chunk.newLines }, |
| 92 | heading, |
| 93 | body: chunk.changes.map((change) => change.content).join('\n'), |
| 94 | }; |
| 95 | }), |
no test coverage detected