-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathloaders.ts
More file actions
464 lines (413 loc) · 13.7 KB
/
Copy pathloaders.ts
File metadata and controls
464 lines (413 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import {
parseDiffFromFile,
parsePatchFiles,
type FileContents,
type FileDiffMetadata,
} from "@pierre/diffs";
import { createTwoFilesPatch } from "diff";
import { resolve as resolvePath } from "node:path";
import { agentOverviewFields, findAgentFileContext, loadAgentContext } from "./agent";
import { createSkippedBinaryMetadata, isProbablyBinaryFile } from "./binary";
import { buildDiffFile, type BuildDiffFileOptions, type DiffFileSourceContext } from "./diffFile";
import { createFileSourceFetcher, type FileSourceSpec } from "./fileSource";
import { splitPatchIntoFileChunks, findPatchChunk } from "./patch/chunks";
import { normalizePatchText, stripTerminalControl } from "./patch/normalize";
import { getConfiguredVcsAdapter, loadVcsReview, operationFromInput } from "./vcs";
import type {
AppBootstrap,
AgentContext,
Changeset,
CliInput,
CustomThemeConfig,
DiffFile,
DiffLineMoveKind,
DiffLineMoveKinds,
DiffToolCommandInput,
FileCommandInput,
PatchCommandInput,
VcsShowCommandInput,
VcsDiffCommandInput,
VcsStashShowCommandInput,
} from "./types";
interface LoadAppBootstrapOptions {
cwd?: string;
customTheme?: CustomThemeConfig;
gitExecutable?: string;
}
/** Return the final path segment for display-oriented labels. */
function basename(path: string) {
return path.split(/[\\/]/).filter(Boolean).pop() ?? path;
}
interface ResolvedFileSourceSpecs {
old: FileSourceSpec;
new: FileSourceSpec;
}
/** Build a binary-aware source-fetcher factory from per-file source specs. */
function createSourceFetcherBuilder(
resolveSpecs: (file: DiffFileSourceContext) => ResolvedFileSourceSpecs | undefined,
): NonNullable<BuildDiffFileOptions["sourceFetcherBuilder"]> {
return (file) => {
if (file.isBinary) {
return undefined;
}
const specs = resolveSpecs(file);
return specs ? createFileSourceFetcher(specs) : undefined;
};
}
/** Return SGR parameter strings that Git emitted before one diff line marker. */
function leadingSgrParameters(rawLine: string, expectedSign: "+" | "-") {
const parameters: string[] = [];
let index = 0;
while (index < rawLine.length) {
if (rawLine[index] === "\x1b") {
const csi = rawLine.slice(index).match(/^\x1b\[([0-?]*)([ -/]*)([@-~])/);
if (csi) {
if (csi[3] === "m") {
parameters.push(csi[1] ?? "");
}
index += csi[0].length;
continue;
}
}
return rawLine[index] === expectedSign ? parameters : [];
}
return [];
}
/** Return whether one SGR parameter list contains the Git color Hunk reserves for moved lines. */
function sgrContainsColor(parameters: string[], colorCode: "35" | "36") {
return parameters.some((parameter) => parameter.split(";").includes(colorCode));
}
/** Classify one ANSI-colored Git diff line as moved when it carries Hunk's reserved color. */
function movedLineKindFromAnsi(
rawLine: string,
side: "addition" | "deletion",
): DiffLineMoveKind | undefined {
const colorCode = side === "addition" ? "36" : "35";
const sign = side === "addition" ? "+" : "-";
return sgrContainsColor(leadingSgrParameters(rawLine, sign), colorCode) ? "moved" : undefined;
}
/** Capture Git's color-moved ANSI classes before the normal patch parser strips colors. */
function collectLineMoveKinds(patchText: string): DiffLineMoveKinds[] {
const files: DiffLineMoveKinds[] = [];
let current: DiffLineMoveKinds | null = null;
let inHunk = false;
let additionLineIndex = 0;
let deletionLineIndex = 0;
const createFileMoveKinds = () => {
const moveKinds: DiffLineMoveKinds = { additionLines: [], deletionLines: [] };
files.push(moveKinds);
inHunk = false;
additionLineIndex = 0;
deletionLineIndex = 0;
return moveKinds;
};
for (const rawLine of patchText.replaceAll("\r\n", "\n").split("\n")) {
const plainLine = stripTerminalControl(rawLine);
if (plainLine.startsWith("diff --git ")) {
current = createFileMoveKinds();
continue;
}
if (!current && (plainLine.startsWith("--- ") || plainLine.startsWith("@@ "))) {
current = createFileMoveKinds();
}
const activeMoveKinds = current;
if (!activeMoveKinds) {
continue;
}
if (plainLine.startsWith("@@ ")) {
inHunk = true;
continue;
}
if (!inHunk) {
continue;
}
if (plainLine.startsWith("+") && !plainLine.startsWith("+++")) {
activeMoveKinds.additionLines[additionLineIndex] = movedLineKindFromAnsi(rawLine, "addition");
additionLineIndex += 1;
continue;
}
if (plainLine.startsWith("-") && !plainLine.startsWith("---")) {
activeMoveKinds.deletionLines[deletionLineIndex] = movedLineKindFromAnsi(rawLine, "deletion");
deletionLineIndex += 1;
continue;
}
if (plainLine.startsWith(" ")) {
additionLineIndex += 1;
deletionLineIndex += 1;
}
}
return files;
}
/** Return whether one file has any captured moved-line classifications. */
function hasLineMoveKinds(moveKinds: DiffLineMoveKinds | undefined) {
return Boolean(moveKinds?.additionLines.some(Boolean) || moveKinds?.deletionLines.some(Boolean));
}
/** Reorder files to follow agent-context narrative order when a sidecar provides one. */
export function orderDiffFiles(files: DiffFile[], agentContext: AgentContext | null) {
if (!agentContext || agentContext.files.length === 0) {
return files;
}
const ranks = new Map<string, number>();
agentContext.files.forEach((file, index) => {
if (!ranks.has(file.path)) {
ranks.set(file.path, index);
}
});
return files
.map((file, index) => {
const rankCandidates = [file.path, file.previousPath]
.filter((path): path is string => Boolean(path))
.map((path) => ranks.get(path))
.filter((rank): rank is number => rank !== undefined);
return {
file,
index,
rank: rankCandidates.length > 0 ? Math.min(...rankCandidates) : Number.POSITIVE_INFINITY,
};
})
.sort((left, right) => {
if (left.rank !== right.rank) {
return left.rank - right.rank;
}
return left.index - right.index;
})
.map((entry) => entry.file);
}
/** Parse raw patch text into the shared changeset model used by the app. */
function normalizePatchChangeset(
patchText: string,
title: string,
sourceLabel: string,
agentContext: AgentContext | null,
perFileOptions?: Pick<BuildDiffFileOptions, "sourceFetcherBuilder">,
): Changeset {
const lineMoveKinds = collectLineMoveKinds(patchText);
const normalizedPatchText = normalizePatchText(patchText);
let parsedPatches: ReturnType<typeof parsePatchFiles>;
try {
parsedPatches = parsePatchFiles(normalizedPatchText, "patch", true);
} catch {
return {
id: `changeset:${Date.now()}`,
sourceLabel,
title,
summary: normalizedPatchText.trim() || undefined,
...agentOverviewFields(agentContext),
files: [],
};
}
const metadataFiles = parsedPatches.flatMap((entry) => entry.files);
const chunks = splitPatchIntoFileChunks(normalizedPatchText);
return {
id: `changeset:${Date.now()}`,
sourceLabel,
title,
summary:
parsedPatches
.map((entry) => entry.patchMetadata)
.filter(Boolean)
.join("\n\n") || undefined,
...agentOverviewFields(agentContext),
files: metadataFiles.map((metadata, index) =>
buildDiffFile(
metadata,
findPatchChunk(metadata, chunks, index),
index,
sourceLabel,
agentContext,
{
...perFileOptions,
lineMoveKinds: hasLineMoveKinds(lineMoveKinds[index]) ? lineMoveKinds[index] : undefined,
},
),
),
};
}
/** Return the change type to show when direct file comparison skips binary contents. */
function resolveBinaryComparisonType(
leftPath: string,
rightPath: string,
): FileDiffMetadata["type"] {
if (leftPath === "/dev/null") {
return "new";
}
if (rightPath === "/dev/null") {
return "deleted";
}
return "change";
}
/** Build a placeholder changeset for direct file comparisons that include binary content. */
function buildBinaryFileDiffChangeset(
input: FileCommandInput | DiffToolCommandInput,
displayPath: string,
title: string,
leftPath: string,
rightPath: string,
agentContext: AgentContext | null,
) {
return {
id: `pair:${displayPath}`,
sourceLabel: input.kind === "difftool" ? "git difftool" : "file compare",
title,
...agentOverviewFields(agentContext),
files: [
buildDiffFile(
createSkippedBinaryMetadata(displayPath, resolveBinaryComparisonType(leftPath, rightPath)),
`Binary file skipped: ${basename(input.left)} ↔ ${basename(input.right)}\n`,
0,
displayPath,
agentContext,
{
previousPath: basename(input.left),
isBinary: true,
},
),
],
} satisfies Changeset;
}
/** Build a changeset by diffing two concrete files on disk. */
async function loadFileDiffChangeset(
input: FileCommandInput | DiffToolCommandInput,
agentContext: AgentContext | null,
cwd = process.cwd(),
) {
const leftPath = resolvePath(cwd, input.left);
const rightPath = resolvePath(cwd, input.right);
const displayPath =
input.kind === "difftool" ? (input.path ?? basename(input.right)) : basename(input.right);
const title =
input.kind === "difftool"
? `git difftool: ${displayPath}`
: input.left === input.right
? displayPath
: `${basename(input.left)} ↔ ${basename(input.right)}`;
if (isProbablyBinaryFile(leftPath) || isProbablyBinaryFile(rightPath)) {
return buildBinaryFileDiffChangeset(
input,
displayPath,
title,
leftPath,
rightPath,
agentContext,
);
}
const leftText = await Bun.file(leftPath).text();
const rightText = await Bun.file(rightPath).text();
const oldFile: FileContents = {
name: displayPath,
contents: leftText,
cacheKey: `${leftPath}:left`,
};
const newFile: FileContents = {
name: displayPath,
contents: rightText,
cacheKey: `${rightPath}:right`,
};
const metadata = parseDiffFromFile(oldFile, newFile, { context: 3 }, true);
const patch = createTwoFilesPatch(displayPath, displayPath, leftText, rightText, "", "", {
context: 3,
});
return {
id: `pair:${displayPath}`,
sourceLabel: input.kind === "difftool" ? "git difftool" : "file compare",
title,
...agentOverviewFields(agentContext),
files: [
buildDiffFile(metadata, patch, 0, displayPath, agentContext, {
previousPath: basename(input.left),
sourceFetcherBuilder: createSourceFetcherBuilder(() => ({
old: { kind: "fs", absolutePath: leftPath },
new: { kind: "fs", absolutePath: rightPath },
})),
}),
],
} satisfies Changeset;
}
/** Build a changeset from an adapter-backed VCS review operation. */
async function loadVcsChangeset(
input: VcsDiffCommandInput | VcsShowCommandInput | VcsStashShowCommandInput,
agentContext: AgentContext | null,
cwd = process.cwd(),
gitExecutable = "git",
) {
const adapter = getConfiguredVcsAdapter(input.options.vcs);
const operation = operationFromInput(input);
const result = await loadVcsReview(adapter, operation, { cwd, gitExecutable });
const parsedChangeset = normalizePatchChangeset(
result.patchText,
result.title,
result.sourceLabel,
agentContext,
result.sourceFetcherBuilder ? { sourceFetcherBuilder: result.sourceFetcherBuilder } : undefined,
);
const adapterFiles = (result.extraFiles ?? []).map((file, index) => ({
...file,
id: `${file.id}:extra:${index}`,
agent: findAgentFileContext(agentContext, file.path, file.previousPath),
}));
return {
...parsedChangeset,
files: [...parsedChangeset.files, ...adapterFiles],
} satisfies Changeset;
}
/** Build a changeset from patch text supplied by file or stdin. */
async function loadPatchChangeset(
input: PatchCommandInput,
agentContext: AgentContext | null,
cwd = process.cwd(),
) {
const patchText =
input.text ??
(!input.file || input.file === "-"
? await new Response(Bun.stdin.stream()).text()
: await Bun.file(resolvePath(cwd, input.file)).text());
const label = input.file && input.file !== "-" ? input.file : "stdin patch";
return normalizePatchChangeset(
patchText,
`Patch review: ${basename(label)}`,
label,
agentContext,
);
}
/** Resolve CLI input into the fully loaded app bootstrap state. */
export async function loadAppBootstrap(
input: CliInput,
{ cwd = process.cwd(), customTheme, gitExecutable = "git" }: LoadAppBootstrapOptions = {},
): Promise<AppBootstrap> {
const agentContext = await loadAgentContext(input.options.agentContext, {
cwd,
});
let changeset: Changeset;
switch (input.kind) {
case "vcs":
case "show":
case "stash-show":
changeset = await loadVcsChangeset(input, agentContext, cwd, gitExecutable);
break;
case "diff":
changeset = await loadFileDiffChangeset(input, agentContext, cwd);
break;
case "patch":
changeset = await loadPatchChangeset(input, agentContext, cwd);
break;
case "difftool":
changeset = await loadFileDiffChangeset(input, agentContext, cwd);
break;
}
changeset = {
...changeset,
files: orderDiffFiles(changeset.files, agentContext),
};
return {
input,
changeset,
initialMode: input.options.mode ?? "auto",
initialTheme: input.options.theme,
customTheme,
initialShowLineNumbers: input.options.lineNumbers ?? true,
initialWrapLines: input.options.wrapLines ?? false,
initialShowHunkHeaders: input.options.hunkHeaders ?? true,
initialShowAgentNotes: input.options.agentNotes ?? false,
initialCopyDecorations: input.options.copyDecorations ?? false,
};
}