Skip to content
Source
Sign inSign up

Diffs.ts

46 lines1.3 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 previousPath = records[index + 1];
21 path = records[index + 2] ?? "";
22 index += 2;
23 }
24 if (path.length === 0) continue;
25​
26 files.push({
27 path,
28 ...(previousPath === undefined ? {} : { previousPath }),
29 additions: counts[1] === "-" ? 0 : Number(counts[1]),
30 deletions: counts[2] === "-" ? 0 : Number(counts[2]),
31 });
32 }
33​
34 return files.toSorted((left, right) => left.path.localeCompare(right.path));
36​
37export function isGitImport(
38 file: TurnDiffFileSummary,
39 authoredPaths: ReadonlySet<string> | null,
40): boolean {
41 return (
42 authoredPaths !== null &&
43 !authoredPaths.has(file.path) &&
44 (file.previousPath === undefined || !authoredPaths.has(file.previousPath))
45 );