|
| 1 | +import { parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs"; |
| 2 | +import type { ToolResultCard } from "./card-types.js"; |
| 3 | + |
| 4 | +export type ReviewFileStatus = |
| 5 | + | "added" |
| 6 | + | "deleted" |
| 7 | + | "modified" |
| 8 | + | "renamed"; |
| 9 | + |
| 10 | +export interface ReviewFileEntry { |
| 11 | + path: string; |
| 12 | + previousPath?: string; |
| 13 | + type?: string; |
| 14 | + additions: number; |
| 15 | + removals: number; |
| 16 | + status: ReviewFileStatus; |
| 17 | + fileDiff?: FileDiffMetadata; |
| 18 | +} |
| 19 | + |
| 20 | +export function buildReviewFileEntries(card: ToolResultCard): ReviewFileEntry[] { |
| 21 | + const parsedFiles = parseReviewPatch(card.payload?.patch); |
| 22 | + const parsedByPath = new Map(parsedFiles.map((fileDiff) => [fileDiff.name, fileDiff])); |
| 23 | + const entries: ReviewFileEntry[] = []; |
| 24 | + |
| 25 | + for (const file of card.files ?? []) { |
| 26 | + if (!file.path) continue; |
| 27 | + |
| 28 | + const fileDiff = parsedByPath.get(file.path); |
| 29 | + const stats = fileDiff ? reviewFileStats(fileDiff) : undefined; |
| 30 | + entries.push({ |
| 31 | + path: file.path, |
| 32 | + previousPath: file.previousPath ?? fileDiff?.prevName, |
| 33 | + type: file.type, |
| 34 | + additions: file.additions ?? stats?.additions ?? 0, |
| 35 | + removals: file.removals ?? stats?.removals ?? 0, |
| 36 | + status: reviewFileStatus(file.type), |
| 37 | + fileDiff, |
| 38 | + }); |
| 39 | + parsedByPath.delete(file.path); |
| 40 | + } |
| 41 | + |
| 42 | + for (const fileDiff of parsedFiles) { |
| 43 | + if (!parsedByPath.has(fileDiff.name)) continue; |
| 44 | + |
| 45 | + const stats = reviewFileStats(fileDiff); |
| 46 | + entries.push({ |
| 47 | + path: fileDiff.name, |
| 48 | + previousPath: fileDiff.prevName, |
| 49 | + additions: stats.additions, |
| 50 | + removals: stats.removals, |
| 51 | + status: fileDiff.prevName && fileDiff.prevName !== fileDiff.name |
| 52 | + ? "renamed" |
| 53 | + : "modified", |
| 54 | + fileDiff, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + return entries; |
| 59 | +} |
| 60 | + |
| 61 | +export function parseReviewPatch(patch: string | undefined): FileDiffMetadata[] { |
| 62 | + if (!patch) return []; |
| 63 | + return parsePatchFiles(patch, "review", true).flatMap((parsedPatch) => parsedPatch.files); |
| 64 | +} |
| 65 | + |
| 66 | +export function reviewFileStats( |
| 67 | + fileDiff: FileDiffMetadata, |
| 68 | +): { additions: number; removals: number } { |
| 69 | + return fileDiff.hunks.reduce( |
| 70 | + (stats, hunk) => ({ |
| 71 | + additions: stats.additions + hunk.additionLines, |
| 72 | + removals: stats.removals + hunk.deletionLines, |
| 73 | + }), |
| 74 | + { additions: 0, removals: 0 }, |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +export function reviewFileStatus(type: string | undefined): ReviewFileStatus { |
| 79 | + if (type === "new") return "added"; |
| 80 | + if (type === "deleted") return "deleted"; |
| 81 | + if (type === "rename-pure" || type === "rename-changed") return "renamed"; |
| 82 | + return "modified"; |
| 83 | +} |
| 84 | + |
| 85 | +export function initialReviewPath( |
| 86 | + entries: ReviewFileEntry[], |
| 87 | + selectedPath?: string, |
| 88 | +): string | undefined { |
| 89 | + if (selectedPath && entries.some((entry) => entry.path === selectedPath)) { |
| 90 | + return selectedPath; |
| 91 | + } |
| 92 | + return entries[0]?.path; |
| 93 | +} |
0 commit comments