@ryuugg/stanza 0.3.1 → 0.3.3
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 +68 -150
- 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/depth.ts +372 -0
- package/src/directories.ts +77 -0
- package/src/engine/explain.ts +7 -2
- package/src/engine/gaps.ts +46 -18
- package/src/engine/rules.ts +2 -1
- package/src/files.ts +388 -23
- package/src/format.ts +159 -0
- package/src/hook.ts +53 -12
- package/src/languages/javascript/config/find.ts +7 -10
- package/src/languages/javascript/config/index.ts +1 -1
- package/src/languages/javascript/config/module.ts +3 -2
- package/src/languages/javascript/config/width.ts +32 -26
- package/src/pool.ts +473 -0
- package/src/usage.ts +6 -3
- package/src/worker.ts +5 -0
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,16 +289,155 @@ function hasGitMarker(dir: string): boolean {
|
|
|
275
289
|
}
|
|
276
290
|
}
|
|
277
291
|
|
|
278
|
-
|
|
292
|
+
const locations = invocationMap<string, Location>();
|
|
293
|
+
|
|
294
|
+
interface GitVariable {
|
|
295
|
+
key: string;
|
|
296
|
+
value: string | undefined;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
interface GitVariables {
|
|
300
|
+
readable: boolean;
|
|
301
|
+
text: string;
|
|
302
|
+
entries: GitVariable[];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const gitVariables = invocationMap<string, GitVariables>();
|
|
306
|
+
|
|
307
|
+
const discoveryEnvironment = [
|
|
308
|
+
"GIT_DIR",
|
|
309
|
+
"GIT_WORK_TREE",
|
|
310
|
+
"GIT_COMMON_DIR",
|
|
311
|
+
"GIT_OBJECT_DIRECTORY",
|
|
312
|
+
"GIT_CEILING_DIRECTORIES",
|
|
313
|
+
"GIT_TEST_ASSUME_DIFFERENT_OWNER",
|
|
314
|
+
];
|
|
315
|
+
|
|
316
|
+
function parseGitVariable(line: string): GitVariable {
|
|
317
|
+
const separator = line.indexOf("=");
|
|
318
|
+
if (separator === -1) return { key: line, value: undefined };
|
|
319
|
+
return { key: line.slice(0, separator), value: line.slice(separator + 1) };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function listGitVariables(root: string): GitVariables {
|
|
323
|
+
const cached = gitVariables.get(root);
|
|
324
|
+
if (cached !== undefined) return cached;
|
|
325
|
+
|
|
326
|
+
const result = Bun.spawnSync(["git", "-C", root, "var", "-l"], {
|
|
327
|
+
env: process.env,
|
|
328
|
+
stderr: "pipe",
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const text = new TextDecoder().decode(result.stdout);
|
|
332
|
+
const variables = {
|
|
333
|
+
readable: result.exitCode === 0 && result.stderr.length === 0,
|
|
334
|
+
text,
|
|
335
|
+
entries: text.replace(/\n$/, "").split("\n").map(parseGitVariable),
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
gitVariables.set(root, variables);
|
|
339
|
+
return variables;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function plainConfig(root: string): boolean {
|
|
343
|
+
const { readable, text } = listGitVariables(root);
|
|
344
|
+
return (
|
|
345
|
+
readable &&
|
|
346
|
+
!/^(?:core\.worktree|extensions\.[^=]*)(?:=|$)/im.test(text) &&
|
|
347
|
+
!/^core\.bare(?:=(?!(?:false|no|off|0)$)|$)/im.test(text) &&
|
|
348
|
+
!/^core\.repositoryformatversion(?:=(?![01]$)|$)/im.test(text)
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function plainRepository(
|
|
353
|
+
worktree: string,
|
|
354
|
+
marker: string,
|
|
355
|
+
markerStat: Stats,
|
|
356
|
+
user: number,
|
|
357
|
+
): boolean {
|
|
358
|
+
if (lstatSync(worktree).uid !== user || markerStat.uid !== user) return false;
|
|
359
|
+
|
|
360
|
+
const head = join(marker, "HEAD");
|
|
361
|
+
if (!lstatSync(head).isFile()) return false;
|
|
362
|
+
|
|
363
|
+
const descriptor = openSync(head, "r");
|
|
364
|
+
const bytes = Buffer.alloc(255);
|
|
365
|
+
|
|
366
|
+
let text: string;
|
|
367
|
+
try {
|
|
368
|
+
text = bytes.subarray(0, readSync(descriptor, bytes, 0, bytes.length, 0)).toString("latin1");
|
|
369
|
+
} finally {
|
|
370
|
+
closeSync(descriptor);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (!/^(?:ref: refs\/|[0-9a-f]{40})/.test(text)) return false;
|
|
374
|
+
|
|
375
|
+
accessSync(join(marker, "objects"), constants.X_OK);
|
|
376
|
+
accessSync(join(marker, "refs"), constants.X_OK);
|
|
377
|
+
if (lstatSync(join(marker, "commondir"), { throwIfNoEntry: false }) !== undefined) return false;
|
|
378
|
+
|
|
379
|
+
return plainConfig(worktree);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function knownRoot(dir: string): string | undefined {
|
|
383
|
+
try {
|
|
384
|
+
if (discoveryEnvironment.some((name) => process.env[name] !== undefined)) return undefined;
|
|
385
|
+
if (Object.keys(process.env).some((name) => name.startsWith("GIT_CONFIG"))) return undefined;
|
|
386
|
+
|
|
387
|
+
const user = process.geteuid?.();
|
|
388
|
+
if (user === undefined) return undefined;
|
|
389
|
+
|
|
390
|
+
const start = realpathSync(dir);
|
|
391
|
+
const startStat = statSync(start);
|
|
392
|
+
if (!startStat.isDirectory() || /[^\x20-\x7e]/.test(start) || start.split(sep).includes(".git"))
|
|
393
|
+
return undefined;
|
|
394
|
+
|
|
395
|
+
const device = startStat.dev;
|
|
396
|
+
|
|
397
|
+
let current = start;
|
|
398
|
+
while (true) {
|
|
399
|
+
const marker = join(current, ".git");
|
|
400
|
+
const entry = lstatSync(marker, { throwIfNoEntry: false });
|
|
401
|
+
if (entry !== undefined)
|
|
402
|
+
return entry.isDirectory() && plainRepository(current, marker, entry, user)
|
|
403
|
+
? current
|
|
404
|
+
: undefined;
|
|
405
|
+
|
|
406
|
+
if (lstatSync(join(current, "HEAD"), { throwIfNoEntry: false }) !== undefined)
|
|
407
|
+
return undefined;
|
|
408
|
+
|
|
409
|
+
const parent = dirname(current);
|
|
410
|
+
if (parent === current || statSync(parent).dev !== device) return undefined;
|
|
411
|
+
|
|
412
|
+
current = parent;
|
|
413
|
+
}
|
|
414
|
+
} catch {
|
|
415
|
+
return undefined;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function discoverLocation(dir: string): Location {
|
|
279
420
|
if (!hasGit()) return { kind: "outside" };
|
|
280
421
|
|
|
422
|
+
const known = knownRoot(dir);
|
|
423
|
+
if (known !== undefined) return { kind: "repository", root: known };
|
|
424
|
+
|
|
281
425
|
const result = runGit(dir, ["rev-parse", "--show-toplevel"]);
|
|
282
|
-
const root = result.ok ? result.output.
|
|
426
|
+
const root = result.ok ? result.output.replace(/\n$/, "") : "";
|
|
283
427
|
if (root) return { kind: "repository", root };
|
|
284
428
|
if (!result.ok && hasGitMarker(dir)) return { kind: "failed", error: result.error };
|
|
285
429
|
return { kind: "outside" };
|
|
286
430
|
}
|
|
287
431
|
|
|
432
|
+
export function locate(dir: string): Location {
|
|
433
|
+
const known = locations.get(dir);
|
|
434
|
+
if (known !== undefined) return known;
|
|
435
|
+
|
|
436
|
+
const location = discoverLocation(dir);
|
|
437
|
+
locations.set(dir, location);
|
|
438
|
+
return location;
|
|
439
|
+
}
|
|
440
|
+
|
|
288
441
|
function repositoryPath(root: string, file: string): string {
|
|
289
442
|
return relative(root, file).split(sep).join("/");
|
|
290
443
|
}
|
|
@@ -321,6 +474,176 @@ function isSet(value: string | undefined): boolean {
|
|
|
321
474
|
|
|
322
475
|
type Attributes = { ok: true; values: Map<string, string> } | { ok: false; error: string };
|
|
323
476
|
|
|
477
|
+
function sourceMaySetAttributes(path: string, names: string[]): boolean {
|
|
478
|
+
const entry = lstatSync(path, { throwIfNoEntry: false });
|
|
479
|
+
if (entry === undefined) return false;
|
|
480
|
+
if (!entry.isFile()) return true;
|
|
481
|
+
|
|
482
|
+
const text = readFileSync(path, "utf8");
|
|
483
|
+
return text.includes("[attr]") || names.some((name) => text.includes(name));
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function indexMayHaveAttributes(root: string): boolean {
|
|
487
|
+
const path = join(root, ".git/index");
|
|
488
|
+
const entry = lstatSync(path, { throwIfNoEntry: false });
|
|
489
|
+
if (entry === undefined) return false;
|
|
490
|
+
if (!entry.isFile()) return true;
|
|
491
|
+
|
|
492
|
+
const bytes = readFileSync(path);
|
|
493
|
+
if (bytes.length < 32 || bytes.toString("ascii", 0, 4) !== "DIRC") return true;
|
|
494
|
+
|
|
495
|
+
const version = bytes.readUInt32BE(4);
|
|
496
|
+
if (version !== 2 && version !== 3) return true;
|
|
497
|
+
|
|
498
|
+
let offset = 12;
|
|
499
|
+
const count = bytes.readUInt32BE(8);
|
|
500
|
+
for (let entryIndex = 0; entryIndex < count; entryIndex++) {
|
|
501
|
+
if (offset + 62 > bytes.length - 20) return true;
|
|
502
|
+
if ((bytes.readUInt32BE(offset + 24) & 0o170000) === 0o040000) return true;
|
|
503
|
+
|
|
504
|
+
const extended = (bytes.readUInt16BE(offset + 60) & 0x4000) !== 0;
|
|
505
|
+
if (extended && version !== 3) return true;
|
|
506
|
+
|
|
507
|
+
const start = offset + 62 + (extended ? 2 : 0);
|
|
508
|
+
const end = bytes.indexOf(0, start);
|
|
509
|
+
if (end < start || end >= bytes.length - 20) return true;
|
|
510
|
+
|
|
511
|
+
const name = bytes.toString("utf8", start, end);
|
|
512
|
+
if (name === ".gitattributes" || name.endsWith("/.gitattributes")) return true;
|
|
513
|
+
|
|
514
|
+
offset += Math.ceil((end + 1 - offset) / 8) * 8;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
while (offset < bytes.length - 20) {
|
|
518
|
+
if (offset + 8 > bytes.length - 20) return true;
|
|
519
|
+
if (bytes.toString("ascii", offset, offset + 4) !== "TREE") return true;
|
|
520
|
+
offset += 8 + bytes.readUInt32BE(offset + 4);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return offset !== bytes.length - 20;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// git var -l prints GIT_EDITOR, GIT_SEQUENCE_EDITOR and GIT_PAGER raw, so a newline could forge a line.
|
|
527
|
+
const inertGitEnvironment = new Set([
|
|
528
|
+
"GIT_EDITOR",
|
|
529
|
+
"GIT_SEQUENCE_EDITOR",
|
|
530
|
+
"GIT_PAGER",
|
|
531
|
+
"GIT_TERMINAL_PROMPT",
|
|
532
|
+
"GIT_ASKPASS",
|
|
533
|
+
"GIT_SSH",
|
|
534
|
+
"GIT_SSH_COMMAND",
|
|
535
|
+
"GIT_SSH_VARIANT",
|
|
536
|
+
"GIT_AUTHOR_NAME",
|
|
537
|
+
"GIT_AUTHOR_EMAIL",
|
|
538
|
+
"GIT_AUTHOR_DATE",
|
|
539
|
+
"GIT_COMMITTER_NAME",
|
|
540
|
+
"GIT_COMMITTER_EMAIL",
|
|
541
|
+
"GIT_COMMITTER_DATE",
|
|
542
|
+
"GIT_OPTIONAL_LOCKS",
|
|
543
|
+
]);
|
|
544
|
+
|
|
545
|
+
function environmentMayAffectAttributes(): boolean {
|
|
546
|
+
return Object.entries(process.env).some(
|
|
547
|
+
([name, value]) =>
|
|
548
|
+
name.startsWith("GIT_") && (!inertGitEnvironment.has(name) || value?.includes("\n") === true),
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function rebuildablePath(path: string | undefined): path is string {
|
|
553
|
+
return (
|
|
554
|
+
path !== undefined && (path.startsWith("~/") || !path.startsWith("~")) && !path.includes("%(")
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function normalizeAsText(path: string): string {
|
|
559
|
+
return path
|
|
560
|
+
.split("/")
|
|
561
|
+
.filter((segment, position) => position === 0 || (segment !== "" && segment !== "."))
|
|
562
|
+
.join("/");
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function defaultGlobalAttributes(): string | undefined {
|
|
566
|
+
const { HOME: home, XDG_CONFIG_HOME: configHome } = process.env;
|
|
567
|
+
if (configHome) return `${configHome}/git/attributes`;
|
|
568
|
+
return home === undefined ? undefined : `${home}/.config/git/attributes`;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function expectedGlobalAttributes(configured: (string | undefined)[]): string | undefined {
|
|
572
|
+
if (!configured.every(rebuildablePath)) return undefined;
|
|
573
|
+
|
|
574
|
+
const path = configured.at(-1);
|
|
575
|
+
const home = process.env.HOME;
|
|
576
|
+
if (path === undefined) return defaultGlobalAttributes();
|
|
577
|
+
if (!path.startsWith("~/")) return path;
|
|
578
|
+
return home === undefined ? undefined : `${home}/${path.slice(2)}`;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const attributeVariableOrder = ["GIT_ATTR_SYSTEM", "GIT_ATTR_GLOBAL", "GIT_CONFIG_SYSTEM"];
|
|
582
|
+
// Once each, adjacent and in git's order, so neither path value spans or forges a line.
|
|
583
|
+
function attributeVariableValues(entries: GitVariable[]): (string | undefined)[] | undefined {
|
|
584
|
+
const sequence = entries.flatMap((entry, position) =>
|
|
585
|
+
attributeVariableOrder.includes(entry.key) ? [{ ...entry, position }] : [],
|
|
586
|
+
);
|
|
587
|
+
|
|
588
|
+
const start = sequence[0]?.position;
|
|
589
|
+
const exact =
|
|
590
|
+
sequence.length === attributeVariableOrder.length &&
|
|
591
|
+
sequence.every(
|
|
592
|
+
(entry, offset) =>
|
|
593
|
+
entry.key === attributeVariableOrder[offset] && entry.position - offset === start,
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
return exact ? sequence.map((entry) => entry.value) : undefined;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function attributeSourcePaths(root: string): string[] | undefined {
|
|
600
|
+
const variables = gitVariables.get(root);
|
|
601
|
+
if (variables === undefined || !variables.readable) return undefined;
|
|
602
|
+
|
|
603
|
+
const { entries } = variables;
|
|
604
|
+
if (entries.some(({ key }) => /^attr\./i.test(key))) return undefined;
|
|
605
|
+
|
|
606
|
+
const [system, global] = attributeVariableValues(entries) ?? [];
|
|
607
|
+
if (system === undefined || global === undefined) return undefined;
|
|
608
|
+
|
|
609
|
+
const configured = entries
|
|
610
|
+
.filter(({ key }) => key.toLowerCase() === "core.attributesfile")
|
|
611
|
+
.map(({ value }) => value);
|
|
612
|
+
|
|
613
|
+
// git var collapses .. as text but check-attr opens the raw path, where a symlink resolves first.
|
|
614
|
+
const expected = expectedGlobalAttributes(configured);
|
|
615
|
+
if (expected === undefined || normalizeAsText(expected) !== global) return undefined;
|
|
616
|
+
|
|
617
|
+
const paths = [system, global];
|
|
618
|
+
return paths.every(isAbsolute) ? paths : undefined;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function attributesMayApply(files: string[], root: string, names: string[]): boolean {
|
|
622
|
+
if (environmentMayAffectAttributes()) return true;
|
|
623
|
+
|
|
624
|
+
const sources = attributeSourcePaths(root);
|
|
625
|
+
if (sources === undefined) return true;
|
|
626
|
+
|
|
627
|
+
try {
|
|
628
|
+
if (indexMayHaveAttributes(root)) return true;
|
|
629
|
+
|
|
630
|
+
const paths = new Set([...sources, join(root, ".git/info/attributes")]);
|
|
631
|
+
for (const file of files) {
|
|
632
|
+
if (!within(root, file)) return true;
|
|
633
|
+
|
|
634
|
+
for (let directory = dirname(file); ; directory = dirname(directory)) {
|
|
635
|
+
paths.add(join(directory, ".gitattributes"));
|
|
636
|
+
if (directory === root) break;
|
|
637
|
+
if (!within(root, directory)) return true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return [...paths].some((path) => sourceMaySetAttributes(path, names));
|
|
642
|
+
} catch {
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
324
647
|
function readAttributes(
|
|
325
648
|
files: string[],
|
|
326
649
|
root: string,
|
|
@@ -328,6 +651,13 @@ function readAttributes(
|
|
|
328
651
|
source: "worktree" | "index",
|
|
329
652
|
): Attributes {
|
|
330
653
|
if (files.length === 0) return { ok: true, values: new Map() };
|
|
654
|
+
if (
|
|
655
|
+
source === "worktree" &&
|
|
656
|
+
names.length === 1 &&
|
|
657
|
+
names[0] === "linguist-generated" &&
|
|
658
|
+
!attributesMayApply(files, root, names)
|
|
659
|
+
)
|
|
660
|
+
return { ok: true, values: new Map() };
|
|
331
661
|
|
|
332
662
|
const paths = files.map((file) => `${repositoryPath(root, file)}\0`).join("");
|
|
333
663
|
const result = runGit(
|
|
@@ -381,12 +711,25 @@ function directoryFiles(dir: string, root: string, keepGenerated: boolean): Sele
|
|
|
381
711
|
const files = nulItems(result.output)
|
|
382
712
|
.filter((path) => isCandidate(path, keepGenerated))
|
|
383
713
|
.map((path) => resolve(dir, path))
|
|
384
|
-
.filter((path) =>
|
|
714
|
+
.filter((path) => {
|
|
715
|
+
try {
|
|
716
|
+
return lstatSync(path).isFile();
|
|
717
|
+
} catch {
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
});
|
|
385
721
|
|
|
386
|
-
const inside = files.map((path) =>
|
|
722
|
+
const inside = files.map((path) => listedFileRealpath(path)).filter((path) => within(root, path));
|
|
387
723
|
return { ok: true, files: inside };
|
|
388
724
|
}
|
|
389
725
|
|
|
726
|
+
// realpathSync returns the name in its on disk case and normalization, so only an exact listing match may join.
|
|
727
|
+
function listedFileRealpath(path: string): string {
|
|
728
|
+
const dir = dirname(path);
|
|
729
|
+
const name = basename(path);
|
|
730
|
+
return listed(dir, name) ? join(realDirectory(dir), name) : fileRealpath(path);
|
|
731
|
+
}
|
|
732
|
+
|
|
390
733
|
function fileRealpath(path: string): string {
|
|
391
734
|
try {
|
|
392
735
|
return realpathSync(path);
|
|
@@ -401,7 +744,6 @@ function sorted(files: string[]): string[] {
|
|
|
401
744
|
}
|
|
402
745
|
|
|
403
746
|
export function collectFiles(paths: string[], cwd: string, keepGenerated = false): Collected {
|
|
404
|
-
const locations = new Map<string, Location>();
|
|
405
747
|
const byRoot = new Map<string, string[]>();
|
|
406
748
|
const outside: string[] = [];
|
|
407
749
|
const errors: string[] = [];
|
|
@@ -409,15 +751,6 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
409
751
|
? []
|
|
410
752
|
: ["git not found on PATH, file selection fell back to the directory walk"];
|
|
411
753
|
|
|
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
754
|
function add(root: string, files: string[]): void {
|
|
422
755
|
const listed = byRoot.get(root);
|
|
423
756
|
if (listed) listed.push(...files);
|
|
@@ -437,7 +770,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
437
770
|
} catch {}
|
|
438
771
|
|
|
439
772
|
if (directory) {
|
|
440
|
-
const location =
|
|
773
|
+
const location = locate(path);
|
|
441
774
|
if (location.kind === "failed") {
|
|
442
775
|
errors.push(location.error);
|
|
443
776
|
continue;
|
|
@@ -463,7 +796,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
463
796
|
}
|
|
464
797
|
|
|
465
798
|
const file = fileRealpath(path);
|
|
466
|
-
const location =
|
|
799
|
+
const location = locate(dirname(file));
|
|
467
800
|
if (location.kind === "failed") {
|
|
468
801
|
errors.push(location.error);
|
|
469
802
|
continue;
|
|
@@ -596,6 +929,7 @@ function diffLines(
|
|
|
596
929
|
return { ok: true, lines: sections.map(diffChanges) };
|
|
597
930
|
}
|
|
598
931
|
|
|
932
|
+
const pathspecBudget = 64 * 1024;
|
|
599
933
|
export function collectChanged(
|
|
600
934
|
cwd: string,
|
|
601
935
|
location: Location = locate(cwd),
|
|
@@ -618,24 +952,55 @@ export function collectChanged(
|
|
|
618
952
|
return { files: [], errors: ["not inside a git repository"], warnings: [], changedLines };
|
|
619
953
|
|
|
620
954
|
const root = location.root;
|
|
955
|
+
const realRoot = realpathSync(root);
|
|
956
|
+
const scope = [...(written ?? [])]
|
|
957
|
+
.filter((path) => within(realRoot, path))
|
|
958
|
+
.map((path) => repositoryPath(realRoot, path));
|
|
959
|
+
|
|
960
|
+
const pathspecs = Buffer.byteLength(scope.join("")) <= pathspecBudget ? scope : [];
|
|
961
|
+
|
|
962
|
+
let changed = runGit(root, [
|
|
963
|
+
"--literal-pathspecs",
|
|
964
|
+
"diff",
|
|
965
|
+
"--name-only",
|
|
966
|
+
"-z",
|
|
967
|
+
"--no-renames",
|
|
968
|
+
"--submodule=short",
|
|
969
|
+
"HEAD",
|
|
970
|
+
"--",
|
|
971
|
+
...pathspecs,
|
|
972
|
+
]);
|
|
621
973
|
|
|
622
|
-
const born = runGit(root, ["rev-parse", "--verify", "-q", "HEAD"]).ok;
|
|
974
|
+
const born = changed.ok || runGit(root, ["rev-parse", "--verify", "-q", "HEAD"]).ok;
|
|
623
975
|
if (!born) {
|
|
624
976
|
const branch = runGit(root, ["symbolic-ref", "-q", "HEAD"]);
|
|
625
977
|
if (!branch.ok) return { files: [], errors: [branch.error], warnings: [], changedLines };
|
|
978
|
+
|
|
979
|
+
changed = runGit(root, [
|
|
980
|
+
"--literal-pathspecs",
|
|
981
|
+
"ls-files",
|
|
982
|
+
"-z",
|
|
983
|
+
"--cached",
|
|
984
|
+
"--",
|
|
985
|
+
...pathspecs,
|
|
986
|
+
]);
|
|
626
987
|
}
|
|
627
988
|
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
989
|
+
const untracked = runGit(root, [
|
|
990
|
+
"--literal-pathspecs",
|
|
991
|
+
"ls-files",
|
|
992
|
+
"-z",
|
|
993
|
+
"--others",
|
|
994
|
+
"--exclude-standard",
|
|
995
|
+
"--",
|
|
996
|
+
...pathspecs,
|
|
997
|
+
]);
|
|
631
998
|
|
|
632
|
-
const untracked = runGit(root, ["ls-files", "-z", "--others", "--exclude-standard"]);
|
|
633
999
|
if (!changed.ok || !untracked.ok) {
|
|
634
1000
|
const errors = [changed, untracked].flatMap((result) => (result.ok ? [] : [result.error]));
|
|
635
1001
|
return { files: [], errors: [...new Set(errors)], warnings: [], changedLines };
|
|
636
1002
|
}
|
|
637
1003
|
|
|
638
|
-
const realRoot = realpathSync(root);
|
|
639
1004
|
const kept = (name: string) => written === undefined || written.has(resolve(realRoot, name));
|
|
640
1005
|
const names = nulItems(changed.output).filter(kept);
|
|
641
1006
|
const candidates = names.filter((name) => isCandidate(name));
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
accessSync,
|
|
4
|
+
chmodSync,
|
|
5
|
+
constants,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
type Stats,
|
|
10
|
+
statSync,
|
|
11
|
+
unlinkSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import { errorCode } from "./files.ts";
|
|
16
|
+
import type { Changed } from "./engine/model.ts";
|
|
17
|
+
import { compareFindings, type Braces, type Finding, type Mode } from "./engine/types.ts";
|
|
18
|
+
import { decode, type Decoded, formatText } from "./step.ts";
|
|
19
|
+
|
|
20
|
+
export interface Input {
|
|
21
|
+
path: string;
|
|
22
|
+
read: () => Decoded;
|
|
23
|
+
changedLines?: Changed | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FormatRun {
|
|
27
|
+
mode: Mode;
|
|
28
|
+
braces: Braces | undefined;
|
|
29
|
+
write: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Warnings {
|
|
33
|
+
unread: Set<string>;
|
|
34
|
+
unreadWidth: Set<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FileOutcome {
|
|
38
|
+
path: string;
|
|
39
|
+
findings: Finding[];
|
|
40
|
+
failed: boolean;
|
|
41
|
+
rewritten?: string;
|
|
42
|
+
kept?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Formatted extends Warnings {
|
|
46
|
+
findings: Finding[];
|
|
47
|
+
failed: boolean;
|
|
48
|
+
rewritten: string[];
|
|
49
|
+
kept: Map<string, string>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function readText(path: string): Decoded {
|
|
53
|
+
try {
|
|
54
|
+
return decode(readFileSync(path));
|
|
55
|
+
} catch (error: unknown) {
|
|
56
|
+
return { message: String(error) };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function removeQuietly(path: string): void {
|
|
61
|
+
try {
|
|
62
|
+
unlinkSync(path);
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function replaceFile(real: string, text: string, target: Stats): boolean {
|
|
67
|
+
const temp = join(dirname(real), `.stanza-${randomUUID()}.tmp`);
|
|
68
|
+
try {
|
|
69
|
+
writeFileSync(temp, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
70
|
+
} catch (error: unknown) {
|
|
71
|
+
removeQuietly(temp);
|
|
72
|
+
if (["EACCES", "EPERM"].includes(errorCode(error))) return false;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const created = statSync(temp);
|
|
78
|
+
if (created.uid !== target.uid || created.gid !== target.gid) {
|
|
79
|
+
removeQuietly(temp);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
chmodSync(temp, target.mode & 0o7777);
|
|
84
|
+
renameSync(temp, real);
|
|
85
|
+
return true;
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
removeQuietly(temp);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function writeInPlace(real: string, text: string): void {
|
|
93
|
+
const original = readFileSync(real);
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(real, text, "utf8");
|
|
96
|
+
} catch (error: unknown) {
|
|
97
|
+
try {
|
|
98
|
+
writeFileSync(real, original);
|
|
99
|
+
} catch {}
|
|
100
|
+
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function writeFixed(path: string, text: string, output: string): Finding | undefined {
|
|
106
|
+
try {
|
|
107
|
+
const real = realpathSync(path);
|
|
108
|
+
const target = statSync(real);
|
|
109
|
+
accessSync(real, constants.W_OK);
|
|
110
|
+
|
|
111
|
+
if (target.nlink > 1 || !replaceFile(real, text, target)) writeInPlace(real, text);
|
|
112
|
+
} catch (error: unknown) {
|
|
113
|
+
return { path: output, line: 1, col: 1, rule: "write", message: String(error), fixable: false };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function formatFile(
|
|
118
|
+
input: Input,
|
|
119
|
+
run: FormatRun,
|
|
120
|
+
output: string,
|
|
121
|
+
warnings: Warnings,
|
|
122
|
+
): FileOutcome {
|
|
123
|
+
const result = formatText(input.path, input.read(), {
|
|
124
|
+
mode: run.mode,
|
|
125
|
+
braces: run.braces,
|
|
126
|
+
changedLines: input.changedLines,
|
|
127
|
+
...warnings,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const outcome: FileOutcome = {
|
|
131
|
+
path: input.path,
|
|
132
|
+
findings: result.findings.map((finding) => ({ ...finding, path: output })),
|
|
133
|
+
failed: result.parseError,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
if (result.fixed === undefined) return outcome;
|
|
137
|
+
if (!run.write) return { ...outcome, kept: result.fixed };
|
|
138
|
+
|
|
139
|
+
const unwritten = writeFixed(input.path, result.fixed, output);
|
|
140
|
+
if (unwritten) return { ...outcome, findings: [...outcome.findings, unwritten], failed: true };
|
|
141
|
+
|
|
142
|
+
return { ...outcome, rewritten: output };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function mergeOutcomes(outcomes: FileOutcome[], warnings: Warnings): Formatted {
|
|
146
|
+
const findings: Finding[] = [];
|
|
147
|
+
const rewritten: string[] = [];
|
|
148
|
+
const kept = new Map<string, string>();
|
|
149
|
+
|
|
150
|
+
let failed = false;
|
|
151
|
+
for (const outcome of outcomes) {
|
|
152
|
+
findings.push(...outcome.findings);
|
|
153
|
+
failed ||= outcome.failed;
|
|
154
|
+
if (outcome.rewritten !== undefined) rewritten.push(outcome.rewritten);
|
|
155
|
+
if (outcome.kept !== undefined) kept.set(outcome.path, outcome.kept);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return { findings: findings.sort(compareFindings), failed, rewritten, kept, ...warnings };
|
|
159
|
+
}
|