Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Diffs.ts

47 lines1.4 KB

1export interface TurnDiffFileSummary {
2 readonly path: string;
3 readonly previousPath?: string;
4 readonly additions: number;
5 readonly deletions: number;
6}
7​
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 let previousPath: string | undefined;
19 if (path.length === 0) {
20 // Renames and copies use two more records: the source and destination.
21 previousPath = records[index + 1];
22 path = records[index + 2] ?? "";
23 index += 2;
24 }
25 if (path.length === 0) continue;
26​
27 files.push({
28 path,
29 ...(previousPath === undefined ? {} : { previousPath }),
30 additions: counts[1] === "-" ? 0 : Number(counts[1]),
31 deletions: counts[2] === "-" ? 0 : Number(counts[2]),
32 });
33 }
34​
35 return files.toSorted((left, right) => left.path.localeCompare(right.path));
37​
38export function isGitImport(
39 file: TurnDiffFileSummary,
40 authoredPaths: ReadonlySet<string> | null,
41): boolean {
42 return (
43 authoredPaths !== null &&
44 !authoredPaths.has(file.path) &&
45 (file.previousPath === undefined || !authoredPaths.has(file.previousPath))
46 );