Co-authored-by: Dylan Zahn <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
export interface TurnDiffFileSummary {
|
|
readonly path: string;
|
|
readonly previousPath?: string;
|
|
readonly additions: number;
|
|
readonly deletions: number;
|
|
}
|
|
|
|
/** Reads Git's NUL-delimited numstat output without decoding display paths. */
|
|
export function parseTurnDiffFilesFromNumstat(numstat: string): ReadonlyArray<TurnDiffFileSummary> {
|
|
const records = numstat.split("\0");
|
|
const files: TurnDiffFileSummary[] = [];
|
|
|
|
for (let index = 0; index < records.length; index += 1) {
|
|
const record = records[index]!;
|
|
const counts = /^(\d+|-)\t(\d+|-)\t/.exec(record);
|
|
if (!counts) continue;
|
|
|
|
let path = record.slice(counts[0].length);
|
|
let previousPath: string | undefined;
|
|
if (path.length === 0) {
|
|
// Renames and copies use two more records: the source and destination.
|
|
previousPath = records[index + 1];
|
|
path = records[index + 2] ?? "";
|
|
index += 2;
|
|
}
|
|
if (path.length === 0) continue;
|
|
|
|
files.push({
|
|
path,
|
|
...(previousPath === undefined ? {} : { previousPath }),
|
|
additions: counts[1] === "-" ? 0 : Number(counts[1]),
|
|
deletions: counts[2] === "-" ? 0 : Number(counts[2]),
|
|
});
|
|
}
|
|
|
|
return files.toSorted((left, right) => left.path.localeCompare(right.path));
|
|
}
|
|
|
|
/**
|
|
* True when Git brought this file in and no work in the range touched it.
|
|
* `authoredPaths` comes from `CheckpointStore.listAuthoredPaths`; null means nothing was imported.
|
|
* A rename stays visible when either side was authored, because the summary shows it as one file.
|
|
*/
|
|
export function isGitImport(
|
|
file: TurnDiffFileSummary,
|
|
authoredPaths: ReadonlySet<string> | null,
|
|
): boolean {
|
|
return (
|
|
authoredPaths !== null &&
|
|
!authoredPaths.has(file.path) &&
|
|
(file.previousPath === undefined || !authoredPaths.has(file.previousPath))
|
|
);
|
|
}
|