@@ -0,0 +1,171 @@
1+import { type Dirent, lstatSync, readFileSync, readdirSync, type Stats } from "node:fs";
2+import { join, relative } from "node:path";
3+import { createTwoFilesPatch } from "diff";
4+import type { FileDiff } from "./common.ts";
5+
6+const MAX_FILE_BYTES = 1 << 20;
7+const MAX_TOTAL_BYTES = 32 << 20;
8+const BINARY_SNIFF = 8192;
9+
10+const IGNORED_DIRS = new Set([
11+ ".git",
12+ ".hg",
13+ ".svn",
14+ "node_modules",
15+ ".venv",
16+ "venv",
17+ "__pycache__",
18+ ".mypy_cache",
19+ ".pytest_cache",
20+]);
21+
22+type Kind = "text" | "binary" | "large" | "link" | "untracked";
23+
24+interface FileState {
25+ size: number;
26+ mtimeMs: number;
27+ ino: number;
28+ kind: Kind;
29+ content: string | null;
30+}
31+
32+/** Relpath -> state. A shallow copy, safe against later captures. */
33+export type Tree = Map<string, FileState>;
34+
35+interface Entry {
36+ rel: string;
37+ abs: string;
38+ size: number;
39+ mtimeMs: number;
40+ ino: number;
41+ link: boolean;
42+}
43+
44+function* walk(): Generator<Entry> {
45+ const root = process.cwd();
46+ const stack = [root];
47+ while (stack.length > 0) {
48+ const dir = stack.pop()!;
49+ let entries: Dirent[];
50+ try {
51+ entries = readdirSync(dir, { withFileTypes: true });
52+ } catch {
53+ continue;
54+ }
55+ for (const dirent of entries) {
56+ const abs = join(dir, dirent.name);
57+ if (dirent.isDirectory()) {
58+ if (!IGNORED_DIRS.has(dirent.name)) stack.push(abs);
59+ continue;
60+ }
61+ const link = dirent.isSymbolicLink();
62+ if (!link && !dirent.isFile()) continue;
63+ let stat: Stats;
64+ try {
65+ stat = lstatSync(abs);
66+ } catch {
67+ continue;
68+ }
69+ yield { rel: relative(root, abs), abs, size: stat.size, mtimeMs: stat.mtimeMs, ino: stat.ino, link };
70+ }
71+ }
72+}
73+
74+function build(entry: Entry, overBudget: boolean): FileState {
75+ const base = { size: entry.size, mtimeMs: entry.mtimeMs, ino: entry.ino };
76+ if (entry.size > MAX_FILE_BYTES) return { ...base, kind: "large", content: null };
77+ if (overBudget) return { ...base, kind: "untracked", content: null };
78+ let buffer;
79+ try {
80+ buffer = readFileSync(entry.abs);
81+ } catch {
82+ return { ...base, kind: "untracked", content: null };
83+ }
84+ if (buffer.subarray(0, BINARY_SNIFF).includes(0)) return { ...base, kind: "binary", content: null };
85+ return { ...base, kind: "text", content: buffer.toString("utf8") };
86+}
87+
88+type StatKey = Pick<FileState, "size" | "mtimeMs" | "ino">;
89+
90+function sameStat(a: StatKey, b: StatKey): boolean {
91+ return a.size === b.size && a.mtimeMs === b.mtimeMs && a.ino === b.ino;
92+}
93+
94+/**
95+ * Caches file content across calls, keyed by the stat triple `(size, mtimeMs,
96+ * ino)`, so a second capture only reads what the stat walk shows as changed.
97+ */
98+class SnapshotCache {
99+ private cache = new Map<string, FileState>();
100+
101+ capture(forceSinceMs?: number): Tree {
102+ const next: Tree = new Map();
103+ let total = 0;
104+ for (const entry of walk()) {
105+ const cached = this.cache.get(entry.rel);
106+ const forced = forceSinceMs !== undefined && entry.mtimeMs >= forceSinceMs;
107+ const reuse = !entry.link && !forced && cached !== undefined && sameStat(cached, entry);
108+ const state = reuse
109+ ? cached!
110+ : entry.link
111+ ? { size: entry.size, mtimeMs: entry.mtimeMs, ino: entry.ino, kind: "link" as const, content: null }
112+ : build(entry, total >= MAX_TOTAL_BYTES);
113+ if (state.content !== null) total += state.size;
114+ next.set(entry.rel, state);
115+ }
116+ this.cache = next;
117+ return new Map(next);
118+ }
119+}
120+
121+let cache: SnapshotCache | undefined;
122+
123+/** Captures the working tree, re-reading content only when its stat key moved. */
124+export function snapshot(forceSinceMs?: number): Tree {
125+ cache ??= new SnapshotCache();
126+ return cache.capture(forceSinceMs);
127+}
128+
129+function patch(path: string, before: string, after: string): string {
130+ return createTwoFilesPatch(path, path, before, after, "", "", { context: 3 });
131+}
132+
133+function noteFor(kind: Kind, verb: string): string {
134+ if (kind === "binary") return `binary file ${verb}`;
135+ if (kind === "large") return `large file ${verb}`;
136+ return `file not tracked (${verb})`;
137+}
138+
139+function diffOne(path: string, before: FileState | undefined, after: FileState | undefined): FileDiff | null {
140+ if (before === undefined) {
141+ if (after!.kind === "link") return { path, note: "symlink created" };
142+ return after!.content === null
143+ ? { path, note: noteFor(after!.kind, "created") }
144+ : { path, patch: patch(path, "", after!.content) };
145+ }
146+ if (after === undefined) {
147+ if (before.kind === "link") return { path, note: "symlink removed" };
148+ return before.content === null
149+ ? { path, note: noteFor(before.kind, "deleted") }
150+ : { path, patch: patch(path, before.content, "") };
151+ }
152+ if (before.kind === "link" || after.kind === "link") {
153+ return sameStat(before, after) ? null : { path, note: "symlink changed" };
154+ }
155+ if (before.content !== null && after.content !== null) {
156+ return before.content === after.content ? null : { path, patch: patch(path, before.content, after.content) };
157+ }
158+ if (sameStat(before, after)) return null;
159+ return { path, note: noteFor(after.content === null ? after.kind : before.kind, "changed") };
160+}
161+
162+/** The files that differ between two captures, sorted for stable output. */
163+export function diffTrees(before: Tree, after: Tree): FileDiff[] {
164+ const paths = new Set([...before.keys(), ...after.keys()]);
165+ const diffs: FileDiff[] = [];
166+ for (const path of [...paths].sort()) {
167+ const diff = diffOne(path, before.get(path), after.get(path));
168+ if (diff !== null) diffs.push(diff);
169+ }
170+ return diffs;
171+}