@ryuugg/stanza 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/cli.ts +35 -142
- package/src/compile/darwin-arm64.ts +1 -1
- package/src/compile/darwin-x64.ts +1 -1
- package/src/compile/linux-arm64-musl.ts +1 -1
- package/src/compile/linux-arm64.ts +1 -1
- package/src/compile/linux-x64-musl.ts +1 -1
- package/src/compile/linux-x64.ts +1 -1
- package/src/compiled.ts +3 -0
- package/src/depth.ts +368 -0
- package/src/directories.ts +77 -0
- package/src/engine/gaps.ts +26 -2
- package/src/engine/index.ts +43 -23
- package/src/engine/types.ts +2 -1
- package/src/files.ts +176 -22
- package/src/format.ts +159 -0
- package/src/hook.ts +32 -6
- package/src/languages/javascript/ast.ts +57 -16
- package/src/languages/javascript/config/find.ts +7 -10
- package/src/languages/javascript/config/index.ts +1 -1
- package/src/languages/javascript/config/layers.ts +50 -6
- package/src/languages/javascript/config/module.ts +3 -2
- package/src/languages/javascript/config/width.ts +32 -26
- package/src/languages/javascript/deserializers.ts +17 -0
- package/src/languages/javascript/embedded.ts +7 -0
- package/src/languages/javascript/oxc-raw.d.ts +31 -0
- package/src/languages/javascript/parse.ts +74 -1
- package/src/languages/javascript/raw.ts +95 -0
- package/src/pool.ts +312 -0
- package/src/step.ts +6 -1
- package/src/usage.ts +5 -2
- package/src/worker.ts +4 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { existsSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
|
|
4
|
+
type Listing = { names: Set<string>; folded: Set<string> } | null;
|
|
5
|
+
|
|
6
|
+
const maps: Map<unknown, unknown>[] = [];
|
|
7
|
+
export function invocationMap<K, V>(): Map<K, V> {
|
|
8
|
+
const map = new Map<K, V>();
|
|
9
|
+
maps.push(map);
|
|
10
|
+
return map;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function beginInvocation(): void {
|
|
14
|
+
for (const map of maps) map.clear();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const realDirectories = invocationMap<string, string>();
|
|
18
|
+
const listings = invocationMap<string, Listing>();
|
|
19
|
+
const existence = invocationMap<string, boolean>();
|
|
20
|
+
const realPaths = invocationMap<string, string>();
|
|
21
|
+
|
|
22
|
+
export function realDirectory(dir: string): string {
|
|
23
|
+
const cached = realDirectories.get(dir);
|
|
24
|
+
if (cached !== undefined) return cached;
|
|
25
|
+
|
|
26
|
+
const parent = dirname(dir);
|
|
27
|
+
const real = existsSync(dir)
|
|
28
|
+
? realpathSync(dir)
|
|
29
|
+
: parent === dir
|
|
30
|
+
? dir
|
|
31
|
+
: join(realDirectory(parent), basename(dir));
|
|
32
|
+
|
|
33
|
+
realDirectories.set(dir, real);
|
|
34
|
+
realDirectories.set(real, real);
|
|
35
|
+
return real;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function listing(dir: string): Listing {
|
|
39
|
+
const cached = listings.get(dir);
|
|
40
|
+
if (cached !== undefined) return cached;
|
|
41
|
+
|
|
42
|
+
let result: Listing;
|
|
43
|
+
try {
|
|
44
|
+
const names = readdirSync(dir);
|
|
45
|
+
result = { names: new Set(names), folded: new Set(names.map((name) => name.toLowerCase())) };
|
|
46
|
+
} catch {
|
|
47
|
+
result = null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
listings.set(dir, result);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function holds(dir: string, name: string): boolean {
|
|
55
|
+
const path = join(dir, name);
|
|
56
|
+
const cached = existence.get(path);
|
|
57
|
+
if (cached !== undefined) return cached;
|
|
58
|
+
|
|
59
|
+
const entries = listing(dir);
|
|
60
|
+
const exists = (entries === null || entries.folded.has(name.toLowerCase())) && existsSync(path);
|
|
61
|
+
|
|
62
|
+
existence.set(path, exists);
|
|
63
|
+
return exists;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function listed(dir: string, name: string): boolean {
|
|
67
|
+
return listing(dir)?.names.has(name) ?? false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function realPath(path: string): string {
|
|
71
|
+
const cached = realPaths.get(path);
|
|
72
|
+
if (cached !== undefined) return cached;
|
|
73
|
+
|
|
74
|
+
const real = realpathSync(path);
|
|
75
|
+
realPaths.set(path, real);
|
|
76
|
+
return real;
|
|
77
|
+
}
|
package/src/engine/gaps.ts
CHANGED
|
@@ -261,6 +261,30 @@ function walls(
|
|
|
261
261
|
const findings: Finding[] = [];
|
|
262
262
|
if (list.kind === "switch") return findings;
|
|
263
263
|
|
|
264
|
+
const excluded = new Set<number>();
|
|
265
|
+
|
|
266
|
+
let groupStart = 0;
|
|
267
|
+
for (let index = 0; index <= list.stmts.length; index++) {
|
|
268
|
+
const stmt = list.stmts[index];
|
|
269
|
+
const gap = gaps[index - 1];
|
|
270
|
+
const counted = stmt && !stmt.multiline && stmt.kind !== "import";
|
|
271
|
+
const forcedJoined =
|
|
272
|
+
gap &&
|
|
273
|
+
gap.decision.want === "none" &&
|
|
274
|
+
!gap.next.detached &&
|
|
275
|
+
(gap.blank === 0 || touches(gap.prev.startLine, gap.next.endLine));
|
|
276
|
+
|
|
277
|
+
if (!counted || !forcedJoined) {
|
|
278
|
+
if (index - groupStart >= 6)
|
|
279
|
+
for (let forcedIndex = groupStart; forcedIndex < index; forcedIndex++)
|
|
280
|
+
excluded.add(forcedIndex);
|
|
281
|
+
|
|
282
|
+
groupStart = index;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!counted) groupStart = index + 1;
|
|
286
|
+
}
|
|
287
|
+
|
|
264
288
|
let runStart: Stmt | undefined;
|
|
265
289
|
let length = 0;
|
|
266
290
|
for (let index = 0; index <= list.stmts.length; index++) {
|
|
@@ -268,13 +292,13 @@ function walls(
|
|
|
268
292
|
const gap = gaps[index - 1];
|
|
269
293
|
const separated =
|
|
270
294
|
gap &&
|
|
271
|
-
(touches(gap.prev.startLine, gap.next.endLine)
|
|
295
|
+
(!gap.next.detached && touches(gap.prev.startLine, gap.next.endLine)
|
|
272
296
|
? gap.decision.want === "frozen" ||
|
|
273
297
|
gap.decision.want === "at-least-one" ||
|
|
274
298
|
(gap.decision.want === "keep" && gap.blank > 0)
|
|
275
299
|
: gap.decision.want === "frozen" || gap.blank > 0);
|
|
276
300
|
|
|
277
|
-
const counted = stmt && !stmt.multiline && stmt.kind !== "import";
|
|
301
|
+
const counted = stmt && !stmt.multiline && stmt.kind !== "import" && !excluded.has(index);
|
|
278
302
|
if (!counted || separated) {
|
|
279
303
|
if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
|
|
280
304
|
findings.push(finding(doc, runStart.start, "wall"));
|
package/src/engine/index.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { languageOf } from "../languages/index.ts";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
BracePass,
|
|
4
|
+
Language,
|
|
5
|
+
Parsed,
|
|
6
|
+
Rejection,
|
|
7
|
+
Scan,
|
|
8
|
+
Touches,
|
|
9
|
+
} from "../languages/language.ts";
|
|
3
10
|
import { blankLines, document, lineAt, parseFinding, position } from "./doc.ts";
|
|
4
11
|
import { applyLines, applyOffsets, offsetMap, type OffsetEdit } from "./edits.ts";
|
|
5
12
|
import { spacing } from "./gaps.ts";
|
|
@@ -109,19 +116,25 @@ function netGaps(trail: Trail, first: Scan, final: Doc, finalScan: Scan): Findin
|
|
|
109
116
|
});
|
|
110
117
|
}
|
|
111
118
|
|
|
119
|
+
interface Parse {
|
|
120
|
+
doc: Doc;
|
|
121
|
+
scanned: Scan;
|
|
122
|
+
error: Rejection | undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
112
125
|
function unbrace(
|
|
113
126
|
language: Language,
|
|
114
|
-
|
|
115
|
-
scanned: Scan,
|
|
127
|
+
last: Parse,
|
|
116
128
|
pass: BracePass,
|
|
117
129
|
lines: Changed | undefined,
|
|
118
130
|
trail: Trail,
|
|
119
131
|
collapseChains: boolean,
|
|
120
132
|
width: Options["width"],
|
|
121
133
|
passes?: OffsetEdit[][],
|
|
122
|
-
):
|
|
134
|
+
): Parse & { lines: Changed | undefined } {
|
|
123
135
|
let touches = touching(lines);
|
|
124
|
-
for (; pass.edits.length > 0; pass = bracePass(doc, scanned, touches, collapseChains)) {
|
|
136
|
+
for (; pass.edits.length > 0; pass = bracePass(last.doc, last.scanned, touches, collapseChains)) {
|
|
137
|
+
const { doc } = last;
|
|
125
138
|
passes?.push(pass.edits);
|
|
126
139
|
record(trail, doc, pass.findings);
|
|
127
140
|
retrace(trail, offsetMap(pass.edits).back);
|
|
@@ -132,11 +145,10 @@ function unbrace(
|
|
|
132
145
|
|
|
133
146
|
lines = afterOffsets(doc, lines, pass.edits, next);
|
|
134
147
|
touches = touching(lines);
|
|
135
|
-
|
|
136
|
-
scanned = parsed.scan(doc, width);
|
|
148
|
+
last = { doc: next, scanned: parsed.scan(next, width), error: parsed.error };
|
|
137
149
|
}
|
|
138
150
|
|
|
139
|
-
return {
|
|
151
|
+
return { ...last, lines };
|
|
140
152
|
}
|
|
141
153
|
|
|
142
154
|
export interface Trace {
|
|
@@ -160,8 +172,7 @@ export function traceFix(
|
|
|
160
172
|
const passes: OffsetEdit[][] = [];
|
|
161
173
|
const { doc, scanned } = unbrace(
|
|
162
174
|
language,
|
|
163
|
-
original,
|
|
164
|
-
first,
|
|
175
|
+
{ doc: original, scanned: first, error: parsed.error },
|
|
165
176
|
pass,
|
|
166
177
|
undefined,
|
|
167
178
|
startTrail(original),
|
|
@@ -181,6 +192,7 @@ export function processFile(path: string, text: string, mode: Mode, options: Opt
|
|
|
181
192
|
if (rejected)
|
|
182
193
|
return {
|
|
183
194
|
text,
|
|
195
|
+
lastParse: { text, error: parsed.error },
|
|
184
196
|
findings: [parseFinding(doc, rejected.start, rejected.message)],
|
|
185
197
|
parseError: true,
|
|
186
198
|
};
|
|
@@ -190,15 +202,13 @@ export function processFile(path: string, text: string, mode: Mode, options: Opt
|
|
|
190
202
|
let touches = touching(lines);
|
|
191
203
|
const trail = startTrail(doc);
|
|
192
204
|
|
|
193
|
-
let last = doc;
|
|
194
|
-
let lastScan = scanned;
|
|
205
|
+
let last: Parse = { doc, scanned, error: parsed.error };
|
|
195
206
|
let settled: Finding[] | undefined;
|
|
196
207
|
let pass = options.keepBraces ? NO_BRACES : bracePass(doc, scanned, touches, lines === undefined);
|
|
197
208
|
while (true) {
|
|
198
209
|
const unbraced = unbrace(
|
|
199
210
|
language,
|
|
200
211
|
last,
|
|
201
|
-
lastScan,
|
|
202
212
|
pass,
|
|
203
213
|
lines,
|
|
204
214
|
trail,
|
|
@@ -221,33 +231,43 @@ export function processFile(path: string, text: string, mode: Mode, options: Opt
|
|
|
221
231
|
touches = touching(lines);
|
|
222
232
|
|
|
223
233
|
if (spacedText === unbraced.doc.text || (mode === "check" && lines === undefined)) {
|
|
224
|
-
last = unbraced
|
|
225
|
-
lastScan = unbraced.scanned;
|
|
234
|
+
last = unbraced;
|
|
226
235
|
settled = spaced.findings;
|
|
227
236
|
break;
|
|
228
237
|
}
|
|
229
238
|
|
|
230
239
|
const spacedParse = language.parse(path, spacedText);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
240
|
+
const spacedDoc = document(path, spacedText, spacedParse.comments);
|
|
241
|
+
last = {
|
|
242
|
+
doc: spacedDoc,
|
|
243
|
+
scanned: spacedParse.scan(spacedDoc, options.width),
|
|
244
|
+
error: spacedParse.error,
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
retrace(trail, lineBack(unbraced.doc, last.doc, spaced.edits));
|
|
234
248
|
if (lines === undefined || options.keepBraces) break;
|
|
235
249
|
|
|
236
|
-
pass = bracePass(last,
|
|
250
|
+
pass = bracePass(last.doc, last.scanned, touches, false);
|
|
237
251
|
if (pass.edits.length === 0) break;
|
|
238
252
|
}
|
|
239
253
|
|
|
240
254
|
const remaining = (
|
|
241
|
-
settled ?? spacing(last,
|
|
255
|
+
settled ?? spacing(last.doc, last.scanned.lists, last.scanned.frozen, touches).findings
|
|
242
256
|
).filter((item) => !item.fixable);
|
|
243
257
|
|
|
244
258
|
if (mode === "fix")
|
|
245
|
-
return {
|
|
259
|
+
return {
|
|
260
|
+
text: last.doc.text,
|
|
261
|
+
lastParse: { text: last.doc.text, error: last.error },
|
|
262
|
+
findings: remaining.sort(compareFindings),
|
|
263
|
+
parseError: false,
|
|
264
|
+
};
|
|
246
265
|
|
|
247
|
-
record(trail, last, remaining);
|
|
266
|
+
record(trail, last.doc, remaining);
|
|
248
267
|
return {
|
|
249
268
|
text,
|
|
250
|
-
|
|
269
|
+
lastParse: { text: last.doc.text, error: last.error },
|
|
270
|
+
findings: netGaps(trail, scanned, last.doc, last.scanned).sort(compareFindings),
|
|
251
271
|
parseError: false,
|
|
252
272
|
};
|
|
253
273
|
}
|
package/src/engine/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Layout } from "../languages/language.ts";
|
|
1
|
+
import type { Layout, Rejection } from "../languages/language.ts";
|
|
2
2
|
import type { Changed } from "./model.ts";
|
|
3
3
|
import type { RuleId } from "./rules.ts";
|
|
4
4
|
|
|
@@ -32,6 +32,7 @@ export function compareFindings(left: Finding, right: Finding): number {
|
|
|
32
32
|
|
|
33
33
|
export interface FileResult {
|
|
34
34
|
text: string;
|
|
35
|
+
lastParse: { text: string; error: Rejection | undefined };
|
|
35
36
|
findings: Finding[];
|
|
36
37
|
parseError: boolean;
|
|
37
38
|
}
|
package/src/files.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
accessSync,
|
|
3
|
+
closeSync,
|
|
4
|
+
constants,
|
|
5
|
+
existsSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
readSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
statSync,
|
|
13
|
+
type Stats,
|
|
14
|
+
} from "node:fs";
|
|
2
15
|
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
16
|
+
import { invocationMap, listed, realDirectory } from "./directories.ts";
|
|
3
17
|
import { entryFor, isCandidate, prunes } from "./languages/index.ts";
|
|
4
18
|
import { ignoredByRules, parseIgnore, type Rule } from "./gitignore.ts";
|
|
5
19
|
import { diffChanges } from "./engine/hunks.ts";
|
|
@@ -275,9 +289,105 @@ function hasGitMarker(dir: string): boolean {
|
|
|
275
289
|
}
|
|
276
290
|
}
|
|
277
291
|
|
|
278
|
-
|
|
292
|
+
const locations = invocationMap<string, Location>();
|
|
293
|
+
const repositoryConfigs = invocationMap<string, boolean>();
|
|
294
|
+
|
|
295
|
+
const discoveryEnvironment = [
|
|
296
|
+
"GIT_DIR",
|
|
297
|
+
"GIT_WORK_TREE",
|
|
298
|
+
"GIT_COMMON_DIR",
|
|
299
|
+
"GIT_OBJECT_DIRECTORY",
|
|
300
|
+
"GIT_CEILING_DIRECTORIES",
|
|
301
|
+
"GIT_DISCOVERY_ACROSS_FILESYSTEM",
|
|
302
|
+
"GIT_TEST_ASSUME_DIFFERENT_OWNER",
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
function plainConfig(root: string): boolean {
|
|
306
|
+
const cached = repositoryConfigs.get(root);
|
|
307
|
+
if (cached !== undefined) return cached;
|
|
308
|
+
|
|
309
|
+
const result = Bun.spawnSync(["git", "-C", root, "var", "-l"], {
|
|
310
|
+
env: process.env,
|
|
311
|
+
stderr: "pipe",
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const config = new TextDecoder().decode(result.stdout);
|
|
315
|
+
const plain =
|
|
316
|
+
result.exitCode === 0 &&
|
|
317
|
+
result.stderr.length === 0 &&
|
|
318
|
+
!/^(?:core\.worktree|extensions\.[^=]*|include(?:if\.[^=]*)?\.path)(?:=|$)/im.test(config) &&
|
|
319
|
+
!/^core\.bare(?:=(?!(?:false|no|off|0)$)|$)/im.test(config) &&
|
|
320
|
+
!/^core\.repositoryformatversion(?:=(?![01]$)|$)/im.test(config);
|
|
321
|
+
|
|
322
|
+
repositoryConfigs.set(root, plain);
|
|
323
|
+
return plain;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function plainRepository(worktree: string, marker: string, markerStat: Stats): boolean {
|
|
327
|
+
if (lstatSync(worktree).uid !== process.geteuid!() || markerStat.uid !== process.geteuid!())
|
|
328
|
+
return false;
|
|
329
|
+
|
|
330
|
+
const head = join(marker, "HEAD");
|
|
331
|
+
if (!lstatSync(head).isFile()) return false;
|
|
332
|
+
|
|
333
|
+
const descriptor = openSync(head, "r");
|
|
334
|
+
const bytes = Buffer.alloc(255);
|
|
335
|
+
|
|
336
|
+
let text: string;
|
|
337
|
+
try {
|
|
338
|
+
text = bytes.subarray(0, readSync(descriptor, bytes, 0, bytes.length, 0)).toString("latin1");
|
|
339
|
+
} finally {
|
|
340
|
+
closeSync(descriptor);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!/^(?:ref: refs\/|[0-9a-f]{40})/.test(text)) return false;
|
|
344
|
+
|
|
345
|
+
accessSync(join(marker, "objects"), constants.X_OK);
|
|
346
|
+
accessSync(join(marker, "refs"), constants.X_OK);
|
|
347
|
+
if (lstatSync(join(marker, "commondir"), { throwIfNoEntry: false }) !== undefined) return false;
|
|
348
|
+
|
|
349
|
+
return plainConfig(worktree);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function knownRoot(dir: string): string | undefined {
|
|
353
|
+
try {
|
|
354
|
+
if (discoveryEnvironment.some((name) => process.env[name] !== undefined)) return undefined;
|
|
355
|
+
if (Object.keys(process.env).some((name) => name.startsWith("GIT_CONFIG"))) return undefined;
|
|
356
|
+
if (typeof process.geteuid !== "function") return undefined;
|
|
357
|
+
|
|
358
|
+
const start = realpathSync(dir);
|
|
359
|
+
const startStat = statSync(start);
|
|
360
|
+
if (!startStat.isDirectory() || /[^\x20-\x7e]/.test(start) || start.split(sep).includes(".git"))
|
|
361
|
+
return undefined;
|
|
362
|
+
|
|
363
|
+
const device = startStat.dev;
|
|
364
|
+
|
|
365
|
+
let current = start;
|
|
366
|
+
while (true) {
|
|
367
|
+
const marker = join(current, ".git");
|
|
368
|
+
const entry = lstatSync(marker, { throwIfNoEntry: false });
|
|
369
|
+
if (entry !== undefined)
|
|
370
|
+
return entry.isDirectory() && plainRepository(current, marker, entry) ? current : undefined;
|
|
371
|
+
|
|
372
|
+
if (lstatSync(join(current, "HEAD"), { throwIfNoEntry: false }) !== undefined)
|
|
373
|
+
return undefined;
|
|
374
|
+
|
|
375
|
+
const parent = dirname(current);
|
|
376
|
+
if (parent === current || statSync(parent).dev !== device) return undefined;
|
|
377
|
+
|
|
378
|
+
current = parent;
|
|
379
|
+
}
|
|
380
|
+
} catch {
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function discoverLocation(dir: string): Location {
|
|
279
386
|
if (!hasGit()) return { kind: "outside" };
|
|
280
387
|
|
|
388
|
+
const known = knownRoot(dir);
|
|
389
|
+
if (known !== undefined) return { kind: "repository", root: known.trim() };
|
|
390
|
+
|
|
281
391
|
const result = runGit(dir, ["rev-parse", "--show-toplevel"]);
|
|
282
392
|
const root = result.ok ? result.output.trim() : "";
|
|
283
393
|
if (root) return { kind: "repository", root };
|
|
@@ -285,6 +395,15 @@ export function locate(dir: string): Location {
|
|
|
285
395
|
return { kind: "outside" };
|
|
286
396
|
}
|
|
287
397
|
|
|
398
|
+
export function locate(dir: string): Location {
|
|
399
|
+
const known = locations.get(dir);
|
|
400
|
+
if (known !== undefined) return known;
|
|
401
|
+
|
|
402
|
+
const location = discoverLocation(dir);
|
|
403
|
+
locations.set(dir, location);
|
|
404
|
+
return location;
|
|
405
|
+
}
|
|
406
|
+
|
|
288
407
|
function repositoryPath(root: string, file: string): string {
|
|
289
408
|
return relative(root, file).split(sep).join("/");
|
|
290
409
|
}
|
|
@@ -381,12 +500,25 @@ function directoryFiles(dir: string, root: string, keepGenerated: boolean): Sele
|
|
|
381
500
|
const files = nulItems(result.output)
|
|
382
501
|
.filter((path) => isCandidate(path, keepGenerated))
|
|
383
502
|
.map((path) => resolve(dir, path))
|
|
384
|
-
.filter((path) =>
|
|
503
|
+
.filter((path) => {
|
|
504
|
+
try {
|
|
505
|
+
return lstatSync(path).isFile();
|
|
506
|
+
} catch {
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
});
|
|
385
510
|
|
|
386
|
-
const inside = files.map((path) =>
|
|
511
|
+
const inside = files.map((path) => listedFileRealpath(path)).filter((path) => within(root, path));
|
|
387
512
|
return { ok: true, files: inside };
|
|
388
513
|
}
|
|
389
514
|
|
|
515
|
+
// realpathSync returns the name in its on disk case and normalization, so only an exact listing match may join.
|
|
516
|
+
function listedFileRealpath(path: string): string {
|
|
517
|
+
const dir = dirname(path);
|
|
518
|
+
const name = basename(path);
|
|
519
|
+
return listed(dir, name) ? join(realDirectory(dir), name) : fileRealpath(path);
|
|
520
|
+
}
|
|
521
|
+
|
|
390
522
|
function fileRealpath(path: string): string {
|
|
391
523
|
try {
|
|
392
524
|
return realpathSync(path);
|
|
@@ -401,7 +533,6 @@ function sorted(files: string[]): string[] {
|
|
|
401
533
|
}
|
|
402
534
|
|
|
403
535
|
export function collectFiles(paths: string[], cwd: string, keepGenerated = false): Collected {
|
|
404
|
-
const locations = new Map<string, Location>();
|
|
405
536
|
const byRoot = new Map<string, string[]>();
|
|
406
537
|
const outside: string[] = [];
|
|
407
538
|
const errors: string[] = [];
|
|
@@ -409,15 +540,6 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
409
540
|
? []
|
|
410
541
|
: ["git not found on PATH, file selection fell back to the directory walk"];
|
|
411
542
|
|
|
412
|
-
function locationOf(dir: string): Location {
|
|
413
|
-
const known = locations.get(dir);
|
|
414
|
-
if (known) return known;
|
|
415
|
-
|
|
416
|
-
const location = locate(dir);
|
|
417
|
-
locations.set(dir, location);
|
|
418
|
-
return location;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
543
|
function add(root: string, files: string[]): void {
|
|
422
544
|
const listed = byRoot.get(root);
|
|
423
545
|
if (listed) listed.push(...files);
|
|
@@ -437,7 +559,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
437
559
|
} catch {}
|
|
438
560
|
|
|
439
561
|
if (directory) {
|
|
440
|
-
const location =
|
|
562
|
+
const location = locate(path);
|
|
441
563
|
if (location.kind === "failed") {
|
|
442
564
|
errors.push(location.error);
|
|
443
565
|
continue;
|
|
@@ -463,7 +585,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
463
585
|
}
|
|
464
586
|
|
|
465
587
|
const file = fileRealpath(path);
|
|
466
|
-
const location =
|
|
588
|
+
const location = locate(dirname(file));
|
|
467
589
|
if (location.kind === "failed") {
|
|
468
590
|
errors.push(location.error);
|
|
469
591
|
continue;
|
|
@@ -596,6 +718,7 @@ function diffLines(
|
|
|
596
718
|
return { ok: true, lines: sections.map(diffChanges) };
|
|
597
719
|
}
|
|
598
720
|
|
|
721
|
+
const pathspecBudget = 64 * 1024;
|
|
599
722
|
export function collectChanged(
|
|
600
723
|
cwd: string,
|
|
601
724
|
location: Location = locate(cwd),
|
|
@@ -618,24 +741,55 @@ export function collectChanged(
|
|
|
618
741
|
return { files: [], errors: ["not inside a git repository"], warnings: [], changedLines };
|
|
619
742
|
|
|
620
743
|
const root = location.root;
|
|
744
|
+
const realRoot = realpathSync(root);
|
|
745
|
+
const scope = [...(written ?? [])]
|
|
746
|
+
.filter((path) => within(realRoot, path))
|
|
747
|
+
.map((path) => repositoryPath(realRoot, path));
|
|
621
748
|
|
|
622
|
-
const
|
|
749
|
+
const pathspecs = Buffer.byteLength(scope.join("")) <= pathspecBudget ? scope : [];
|
|
750
|
+
|
|
751
|
+
let changed = runGit(root, [
|
|
752
|
+
"--literal-pathspecs",
|
|
753
|
+
"diff",
|
|
754
|
+
"--name-only",
|
|
755
|
+
"-z",
|
|
756
|
+
"--no-renames",
|
|
757
|
+
"--submodule=short",
|
|
758
|
+
"HEAD",
|
|
759
|
+
"--",
|
|
760
|
+
...pathspecs,
|
|
761
|
+
]);
|
|
762
|
+
|
|
763
|
+
const born = changed.ok || runGit(root, ["rev-parse", "--verify", "-q", "HEAD"]).ok;
|
|
623
764
|
if (!born) {
|
|
624
765
|
const branch = runGit(root, ["symbolic-ref", "-q", "HEAD"]);
|
|
625
766
|
if (!branch.ok) return { files: [], errors: [branch.error], warnings: [], changedLines };
|
|
767
|
+
|
|
768
|
+
changed = runGit(root, [
|
|
769
|
+
"--literal-pathspecs",
|
|
770
|
+
"ls-files",
|
|
771
|
+
"-z",
|
|
772
|
+
"--cached",
|
|
773
|
+
"--",
|
|
774
|
+
...pathspecs,
|
|
775
|
+
]);
|
|
626
776
|
}
|
|
627
777
|
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
778
|
+
const untracked = runGit(root, [
|
|
779
|
+
"--literal-pathspecs",
|
|
780
|
+
"ls-files",
|
|
781
|
+
"-z",
|
|
782
|
+
"--others",
|
|
783
|
+
"--exclude-standard",
|
|
784
|
+
"--",
|
|
785
|
+
...pathspecs,
|
|
786
|
+
]);
|
|
631
787
|
|
|
632
|
-
const untracked = runGit(root, ["ls-files", "-z", "--others", "--exclude-standard"]);
|
|
633
788
|
if (!changed.ok || !untracked.ok) {
|
|
634
789
|
const errors = [changed, untracked].flatMap((result) => (result.ok ? [] : [result.error]));
|
|
635
790
|
return { files: [], errors: [...new Set(errors)], warnings: [], changedLines };
|
|
636
791
|
}
|
|
637
792
|
|
|
638
|
-
const realRoot = realpathSync(root);
|
|
639
793
|
const kept = (name: string) => written === undefined || written.has(resolve(realRoot, name));
|
|
640
794
|
const names = nulItems(changed.output).filter(kept);
|
|
641
795
|
const candidates = names.filter((name) => isCandidate(name));
|