Open History to see the last commit
1export interface TurnDiffFileSummary {
2 readonly path: string;
3 readonly additions: number;
4 readonly deletions: number;
5}
6
7/** Reads Git's NUL-delimited numstat output without decoding display paths. */
8export function parseTurnDiffFilesFromNumstat(numstat: string): ReadonlyArray<TurnDiffFileSummary> {
9 const records = numstat.split("\0");
10 const files: TurnDiffFileSummary[] = [];
11
12 for (let index = 0; index < records.length; index += 1) {
13 const record = records[index]!;
14 const counts = /^(\d+|-)\t(\d+|-)\t/.exec(record);
15 if (!counts) continue;
16
17 let path = record.slice(counts[0].length);
18 if (path.length === 0) {
19 // Renames and copies use two more records: the source and destination.
20 path = records[index + 2] ?? "";
21 index += 2;
22 }
23 if (path.length === 0) continue;
24
25 files.push({
26 path,
27 additions: counts[1] === "-" ? 0 : Number(counts[1]),
28 deletions: counts[2] === "-" ? 0 : Number(counts[2]),
29 });
30 }
31
32 return files.toSorted((left, right) => left.path.localeCompare(right.path));
33}