Open History to see the last commit
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));
35}
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 );
46}
47
48/**
49 * Moves every path in a NUL-delimited numstat under `prefix`, as if Git had run in the folder
50 * that holds the repository. Used to merge the per-repository diffs of a multi-repo workspace.
51 */
52export function prefixNumstatPaths(numstat: string, prefix: string): string {
53 const records = numstat.split("\0");
54 for (let index = 0; index < records.length; index += 1) {
55 const counts = /^(\d+|-)\t(\d+|-)\t/.exec(records[index]!);
56 if (!counts) continue;
57 const path = records[index]!.slice(counts[0].length);
58 if (path.length > 0) {
59 records[index] = `${counts[0]}${prefix}/${path}`;
60 continue;
61 }
62 // Renames and copies use two more records: the source and destination.
63 for (const pathIndex of [index + 1, index + 2]) {
64 if (records[pathIndex]) records[pathIndex] = `${prefix}/${records[pathIndex]}`;
65 }
66 index += 2;
67 }
68 return records.join("\0");
69}
70
71/**
72 * Moves every file header path in a unified Git patch under `prefix`. Hunk lines are left
73 * alone, so content that happens to look like a header is never rewritten.
74 */
75export function prefixPatchPaths(patch: string, prefix: string): string {
76 let inHeader = false;
77 return patch
78 .split("\n")
79 .map((line) => {
80 if (line.startsWith("diff --git ")) {
81 inHeader = true;
82 return prefixDiffGitLine(line, prefix);
83 }
84 if (!inHeader) return line;
85 if (line.startsWith("@@")) {
86 inHeader = false;
87 return line;
88 }
89 return prefixPatchHeaderLine(line, prefix);
90 })
91 .join("\n");
92}
93
94// `a/x` -> `a/<prefix>/x`, keeping Git's optional C-style quotes.
95function prefixSidePath(path: string, prefix: string): string {
96 if (path.startsWith('"a/') || path.startsWith('"b/')) {
97 return `${path.slice(0, 3)}${prefix}/${path.slice(3)}`;
98 }
99 if (path.startsWith("a/") || path.startsWith("b/")) {
100 return `${path.slice(0, 2)}${prefix}/${path.slice(2)}`;
101 }
102 return path;
103}
104
105// `x` -> `<prefix>/x` for rename and copy headers, which carry no side marker.
106function prefixBarePath(path: string, prefix: string): string {
107 return path.startsWith('"') ? `"${prefix}/${path.slice(1)}` : `${prefix}/${path}`;
108}
109
110function prefixDiffGitLine(line: string, prefix: string): string {
111 const paths = line.slice("diff --git ".length);
112 // Paths may contain spaces. Unless renamed, both sides name the same file, so the line splits
113 // in the middle; a rename's destination starts at the last ` b/` or ` "b/`.
114 const middle = (paths.length - 1) / 2;
115 const destination =
116 paths[middle] === " " && paths.slice(2, middle) === paths.slice(middle + 3)
117 ? middle
118 : Math.max(paths.lastIndexOf(" b/"), paths.lastIndexOf(' "b/'));
119 if (destination < 0) return line;
120 return `diff --git ${prefixSidePath(paths.slice(0, destination), prefix)} ${prefixSidePath(
121 paths.slice(destination + 1),
122 prefix,
123 )}`;
124}
125
126function prefixPatchHeaderLine(line: string, prefix: string): string {
127 for (const marker of ["--- ", "+++ "]) {
128 if (line.startsWith(marker)) return marker + prefixSidePath(line.slice(marker.length), prefix);
129 }
130 for (const marker of ["rename from ", "rename to ", "copy from ", "copy to "]) {
131 if (line.startsWith(marker)) return marker + prefixBarePath(line.slice(marker.length), prefix);
132 }
133 const binary = /^Binary files (.+) and (.+) differ$/.exec(line);
134 if (binary) {
135 return `Binary files ${prefixSidePath(binary[1]!, prefix)} and ${prefixSidePath(
136 binary[2]!,
137 prefix,
138 )} differ`;
139 }
140 return line;
141}