Open History to see the last commit
1import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2
3export interface TurnDiffFileSummary {
4 readonly path: string;
5 readonly additions: number;
6 readonly deletions: number;
7}
8
9export function parseTurnDiffFilesFromUnifiedDiff(
10 diff: string,
11): ReadonlyArray<TurnDiffFileSummary> {
12 const normalized = diff.replace(/\r\n/g, "\n").trim();
13 if (normalized.length === 0) {
14 return [];
15 }
16
17 const parsedPatches = parsePatchFiles(normalized);
18 const files = parsedPatches.flatMap((patch) =>
19 patch.files.map((file) => ({
20 path: file.name,
21 additions: file.hunks.reduce((total, hunk) => total + hunk.additionLines, 0),
22 deletions: file.hunks.reduce((total, hunk) => total + hunk.deletionLines, 0),
23 })),
24 );
25
26 return files.toSorted((left, right) => left.path.localeCompare(right.path));
27}