Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/ignore.test.ts?format=json
436 linesFirst import 95% · review 5% · pinch 1%Last change pinch in version 78, 4 versions changed it
1// The client's ignore rules against real git: every file and folder in a
2// tree of tricky ignore files gets the same answer from loadIgnore as from
3// `git check-ignore`, down to the line that decided.
4
5import assert from "node:assert/strict";
6import { spawnSync } from "node:child_process";
7import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
8import { tmpdir } from "node:os";
9import { dirname, join } from "node:path";
10import { after, describe, it } from "node:test";
11import { parseIgnore } from "../src/core.ts";
12import { loadIgnore } from "../src/folder.ts";
13import { main } from "../src/main.ts";
15const hasGit = spawnSync("git", ["--version"]).status === 0;
16const dirs: string[] = [];
17after(() => {
18 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
19});
21function temp(): string {
22 const d = mkdtempSync(join(tmpdir(), "avcs-ignore-"));
23 dirs.push(d);
24 return d;
27function write(root: string, files: Record<string, string>): void {
28 for (const [rel, text] of Object.entries(files)) {
29 const full = join(root, ...rel.split("/"));
30 mkdirSync(dirname(full), { recursive: true });
31 writeFileSync(full, text);
32 }
35/** Every file and folder under root, except .git, as [path, isDir]. */
36function everything(root: string, prefix = ""): [string, boolean][] {
37 const out: [string, boolean][] = [];
38 for (const e of readdirSync(join(root, ...prefix.split("/").filter(Boolean)), { withFileTypes: true })) {
39 const rel = prefix === "" ? e.name : `${prefix}/${e.name}`;
40 if (rel === ".git") continue;
41 out.push([rel, e.isDirectory()]);
42 if (e.isDirectory()) out.push(...everything(root, rel));
43 }
44 return out;
47interface GitAnswer {
48 readonly source: string;
49 readonly line: string;
50 readonly pattern: string;
53/** `git check-ignore -v -n` for each path, with git's global files under home. */
54function gitAnswers(root: string, home: string, paths: readonly string[], ignoreCase = false): Map<string, GitAnswer> {
55 const env = { ...process.env, HOME: home, XDG_CONFIG_HOME: join(home, ".config"), GIT_CONFIG_NOSYSTEM: "1" };
56 const r = spawnSync(
57 "git",
58 ["-c", `core.ignorecase=${ignoreCase}`, "check-ignore", "-v", "-n", "-z", "--no-index", "--stdin"],
59 {
60 cwd: root,
61 env,
62 input: `${paths.join("\0")}\0`,
63 encoding: "utf8",
64 },
65 );
66 assert.ok(r.status === 0 || r.status === 1, r.stderr);
67 const f = r.stdout.split("\0");
68 const out = new Map<string, GitAnswer>();
69 for (let i = 0; i + 3 < f.length; i += 4)
70 out.set(f[i + 3]!, { source: f[i]!.replace(/\\/g, "/"), line: f[i + 1]!, pattern: f[i + 2]! });
71 return out;
74function gitInit(root: string, home: string): void {
75 const env = { ...process.env, HOME: home, XDG_CONFIG_HOME: join(home, ".config"), GIT_CONFIG_NOSYSTEM: "1" };
76 assert.equal(spawnSync("git", ["init", "-q"], { cwd: root, env }).status, 0);
79const ROOT_GITIGNORE = [
80 "# a comment",
81 "*.log",
82 "!keep.log",
83 "/root-only.txt",
84 "build/",
85 "docs/**/*.tmp",
86 "**/cache",
87 "a/**/z",
88 "logs/**",
89 "!logs/important/",
90 "trailing-space.txt ",
91 "with\\ space.txt",
92 "\\#hash.txt",
93 "\\!bang.txt",
94 "[abc].c",
95 "[!x]y.c",
96 "file?.md",
97 "*.[oa]",
98 "foo/bar/",
99 "deep/*.txt",
100 "data/[[:digit:]]*.csv",
101 "!ok.bak",
102 "mid*dle/x",
103 "",
104].join("\n");
106/** The tree for the comparison, with git's global excludes under home and a .git/info/exclude. */
107function tree(root: string, home: string): void {
108 write(root, {
109 ".gitignore": ROOT_GITIGNORE,
110 "keep.log": "x",
111 "other.log": "x",
112 "sub/inner.log": "x",
113 "sub/keep.log": "x",
114 "root-only.txt": "x",
115 "sub/root-only.txt": "x",
116 "build/out.js": "x",
117 "build/.gitignore": "!*.keep\n",
118 "build/x.keep": "x",
119 "src/build/y.js": "x",
120 "docs/a.tmp": "x",
121 "docs/one/two/b.tmp": "x",
122 "docs/one/two/b.md": "x",
123 "docs/inner/.gitignore": "!x.tmp\n",
124 "docs/inner/x.tmp": "x",
125 "docs/inner/y.tmp": "x",
126 "cache/c1": "x",
127 "src/deep/cache/c2": "x",
128 "a/z": "x",
129 "a/b/c/z": "x",
130 "b/a/z": "x",
131 "logs/l1.txt": "x",
132 "logs/important/i.txt": "x",
133 "logs/other/o.txt": "x",
134 "trailing-space.txt": "x",
135 "with space.txt": "x",
136 "#hash.txt": "x",
137 "!bang.txt": "x",
138 "a.c": "x",
139 "d.c": "x",
140 "ay.c": "x",
141 "xy.c": "x",
142 "file1.md": "x",
143 "file10.md": "x",
144 "m.o": "x",
145 "m.a": "x",
146 "m.b": "x",
147 "foo/bar/x.txt": "x",
148 "other/foo/bar/y.txt": "x",
149 "deep/a.txt": "x",
150 "deep/sub/b.txt": "x",
151 "data/1a.csv": "x",
152 "data/a1.csv": "x",
153 "mid-and-dle/x": "x",
154 "mid/dle/x": "x",
155 "src/.gitignore": "*.gen.ts\n!keep.gen.ts\n/local.txt\nsub/\n",
156 "src/a.gen.ts": "x",
157 "src/keep.gen.ts": "x",
158 "src/local.txt": "x",
159 "src/x/local.txt": "x",
160 "src/sub/s.ts": "x",
161 "sub2/sub/s.ts": "x",
162 "secret.key": "x",
163 "public.key": "x",
164 "v.bak": "x",
165 "ok.bak": "x",
166 ".env": "x",
167 // Names that match only when case is ignored (no two differ only by case, for Windows and macOS disks).
168 "X.LOG": "x",
169 "sub3/KEEP.LOG": "x",
170 "sub3/BUILD/b.js": "x",
171 "B.C": "x",
172 "N.O": "x",
173 "FILE2.MD": "x",
174 "src/Cache/c4": "x",
175 "src/B.GEN.TS": "x",
176 "W.BAK": "x",
177 "Shown.KEY": "x",
178 });
179 write(home, { ".config/git/ignore": "*.bak\n.env\n" });
180 gitInit(root, home);
181 write(root, { ".git/info/exclude": "*.key\n!public.key\n" });
184describe("ignore rules", { skip: hasGit ? false : "git is not installed" }, () => {
185 // abso matches every rule by pathKey (#49), so it answers as git does with core.ignorecase=true.
186 {
187 const ignoreCase = true;
188 it("give the same answer as git check-ignore with core.ignorecase=true for every path, with the same deciding line", () => {
189 const root = temp();
190 const home = temp();
191 tree(root, home);
192 const paths = everything(root);
193 const git = gitAnswers(
194 root,
195 home,
196 paths.map(([p]) => p),
197 ignoreCase,
198 );
199 const ignore = loadIgnore(root, { home, xdgConfigHome: join(home, ".config") });
200 const wrong: string[] = [];
201 for (const [rel, isDir] of paths) {
202 const g = git.get(rel);
203 assert.ok(g, `git said nothing about ${rel}`);
204 const m = ignore.why(rel, isDir);
205 const gitIgnored = g.pattern !== "" && !g.pattern.startsWith("!");
206 const ours = m.rule
207 ? `${m.rule.source.includes(":") || m.rule.source.startsWith("/") ? "GLOBAL" : m.rule.source}:${m.rule.line}:${m.rule.pattern}`
208 : "::";
209 const theirs = g.source.endsWith("/.config/git/ignore")
210 ? `GLOBAL:${g.line}:${g.pattern}`
211 : `${g.source}:${g.line}:${g.pattern}`;
212 if (m.ignored !== gitIgnored || ours !== theirs)
213 wrong.push(
214 `${rel}${isDir ? "/" : ""}: git ${theirs} (${gitIgnored ? "ignored" : "kept"}), avcs ${ours} (${m.ignored ? "ignored" : "kept"})`,
215 );
216 }
217 assert.deepEqual(wrong, []);
218 assert.ok(paths.length > 90, `only ${paths.length} paths`);
219 if (ignoreCase) {
220 // The case-only names really do flip: git answers differently for them with core.ignorecase=false,
221 // where abso leaves out more than git status does.
222 const strict = gitAnswers(
223 root,
224 home,
225 paths.map(([p]) => p),
226 false,
227 );
228 const ignoredBy = (a: Map<string, GitAnswer>, p: string) =>
229 a.get(p)!.pattern !== "" && !a.get(p)!.pattern.startsWith("!");
230 const flipped = paths.filter(([p]) => ignoredBy(git, p) !== ignoredBy(strict, p)).length;
231 assert.ok(flipped >= 8, `only ${flipped} paths depend on case`);
232 }
233 });
234 }
236 it("ignore case whatever core.ignorecase says, in a git repository or outside one (#49)", () => {
237 const repo = temp();
238 const home = temp();
239 write(repo, { ".gitignore": "*.log\n", "A.LOG": "x" });
240 gitInit(repo, home);
241 for (const set of ["true", "false"]) {
242 write(repo, { ".git/config": `[core]\n\tignorecase = ${set}\n` });
243 assert.equal(loadIgnore(repo, { home })("A.LOG", false), true, `core.ignorecase=${set}`);
244 }
245 const plain = temp();
246 write(plain, { ".gitignore": "*.log\n" });
247 assert.equal(loadIgnore(plain, { home })("A.LOG", false), true);
248 });
250 it("agree with git on the root `**` with `!*/` and `!*.*` (everything but dotted names and folders)", () => {
251 const root = temp();
252 const home = temp();
253 write(root, { ".gitignore": "**\n!*/\n!*.*\n", Makefile: "x", "a.txt": "x", "d/LICENSE": "x", "d/b.md": "x" });
254 gitInit(root, home);
255 const paths = everything(root);
256 const git = gitAnswers(
257 root,
258 home,
259 paths.map(([p]) => p),
260 );
261 const ignore = loadIgnore(root, { home });
262 for (const [rel, isDir] of paths) {
263 const g = git.get(rel)!;
264 assert.equal(ignore(rel, isDir), g.pattern !== "" && !g.pattern.startsWith("!"), rel);
265 }
266 });
268 it("let .avcsignore beat .gitignore in the same folder, like lines later in one file", () => {
269 const root = temp();
270 const home = temp();
271 write(root, {
272 ".gitignore": "*.txt\nout/\n",
273 ".avcsignore": "!keep.txt\n*.md\n",
274 "src/.avcsignore": "!*.md\n",
275 "a.txt": "x",
276 "keep.txt": "x",
277 "a.md": "x",
278 "src/b.md": "x",
279 "out/o.js": "x",
280 });
281 // git's twin: each folder's .avcsignore lines appended to its .gitignore.
282 const twin = temp();
283 write(twin, {
284 ".gitignore": "*.txt\nout/\n!keep.txt\n*.md\n",
285 "src/.gitignore": "!*.md\n",
286 "a.txt": "x",
287 "keep.txt": "x",
288 "a.md": "x",
289 "src/b.md": "x",
290 "out/o.js": "x",
291 });
292 gitInit(twin, home);
293 const ignore = loadIgnore(root, { home });
294 const paths = everything(twin).filter(([p]) => !p.endsWith(".gitignore"));
295 const git = gitAnswers(
296 twin,
297 home,
298 paths.map(([p]) => p),
299 );
300 for (const [rel, isDir] of paths) {
301 const g = git.get(rel)!;
302 assert.equal(ignore(rel, isDir), g.pattern !== "" && !g.pattern.startsWith("!"), rel);
303 }
304 assert.equal(ignore.why("keep.txt", false).rule?.source, ".avcsignore");
305 assert.equal(ignore.why("src/b.md", false).rule?.source, "src/.avcsignore");
306 });
308 it("read .absoignore, which beats .avcsignore and .gitignore in the same folder", () => {
309 const root = temp();
310 write(root, {
311 ".gitignore": "*.txt\n",
312 ".avcsignore": "*.md\n",
313 ".absoignore": "!a.txt\n!a.md\n*.log\n",
314 "a.txt": "x",
315 "a.md": "x",
316 "b.md": "x",
317 "c.log": "x",
318 });
319 const ignore = loadIgnore(root, { home: temp() });
320 assert.equal(ignore("a.txt", false), false);
321 assert.equal(ignore("a.md", false), false);
322 assert.equal(ignore("c.log", false), true);
323 assert.equal(ignore.why("c.log", false).rule?.source, ".absoignore");
324 });
326 it("always skip .git, .abso, .claude and node_modules", () => {
327 const ignore = loadIgnore(temp(), {});
328 assert.equal(ignore.why(".claude/scheduled_tasks.lock", false).alwaysSkipped, ".claude");
329 assert.equal(ignore.why("a/node_modules/m/i.js", false).alwaysSkipped, "a/node_modules");
330 assert.equal(ignore("src/a.ts", false), false);
331 });
333 it("parse escapes, comments, trailing spaces and CRLF like git", () => {
334 const rules = parseIgnore("# c\r\n\\#a\r\nb \r\nc\\ \r\n!\r\n/\r\n\r\n", "", ".gitignore");
335 assert.deepEqual(
336 rules.map((r) => [r.line, r.pattern]),
337 [
338 [2, "\\#a"],
339 [3, "b"],
340 [4, "c\\ "],
341 ],
342 );
343 });
345 it("abso check-ignore names the deciding line, the ignored folder above, or no rule", async () => {
346 const root = temp();
347 write(root, {
348 ".abso/config.json": "{}",
349 ".gitignore": "*.log\n!keep.log\nbuild/\n",
350 "src/.avcsignore": "gen/\n",
351 "build/x/a.txt": "x",
352 "src/gen/g.ts": "x",
353 });
354 const env = { cwd: join(root, "src"), env: { HOME: temp() }, write: () => {} };
355 const r = await main(
356 ["check-ignore", "../a.log", "../keep.log", "../build/x/a.txt", "gen", "a.ts", "../.claude/x", "--json"],
357 env,
358 );
359 assert.equal(r.exit, 0, r.stderr);
360 const paths = JSON.parse(r.stdout).paths;
361 assert.deepEqual(
362 paths.map((p: any) => [
363 p.path,
364 p.ignored,
365 p.source ?? p.alwaysSkipped ?? null,
366 p.line ?? null,
367 p.because ?? null,
368 ]),
369 [
370 ["a.log", true, ".gitignore", 1, null],
371 ["keep.log", false, ".gitignore", 2, null],
372 ["build/x/a.txt", true, ".gitignore", 3, "build"],
373 ["src/gen", true, "src/.avcsignore", 1, null],
374 ["src/a.ts", false, null, null, null],
375 [".claude/x", true, ".claude", null, null],
376 ],
377 );
378 const text = (await main(["check-ignore", "../keep.log", "../build/x/a.txt"], env)).stdout;
379 assert.match(text, /^keep\.log: not ignored: \.gitignore:2 "!keep\.log" brings it back$/m);
380 assert.match(text, /^build\/x\/a\.txt: ignored, because its folder build is ignored by \.gitignore:3 "build\/"/m);
381 });
382});
384describe("ignore rules match by the same key as paths (#49)", () => {
385 it("catches every spelling abso takes as the same file, from .avcsignore and .gitignore alike", () => {
386 const root = temp();
387 write(root, {
388 ".avcsignore": ".env\nsecrets/\n*.KEY\nCaf\u00e9.txt\n[[:upper:]]X.tmp\n",
389 "Cfg/.avcsignore": "local.json\n",
390 ".gitignore": "*.log\n",
391 });
392 // A folder's ignore file is read under the folder's name on disk, so Cfg/ keeps its spelling here: on a
393 // disk that matches case (Linux, CI's box), cfg/.avcsignore isn't Cfg/.avcsignore.
394 const ignore = loadIgnore(root, {});
395 for (const p of [
396 ".env",
397 ".ENV",
398 ".Env",
399 "app/.ENV",
400 "SECRETS/a.txt",
401 "a.key",
402 "b.Key",
403 "caf\u00e9.txt",
404 "Cafe\u0301.txt",
405 "CAF\u00c9.TXT",
406 "ax.tmp",
407 "Cfg/LOCAL.json",
408 "Cfg/local.JSON",
409 ]) {
410 assert.equal(ignore(p, false), true, JSON.stringify(p));
411 }
412 for (const p of ["env", ".envrc", "cafe.txt", "secretsx/a.txt", "local.json"])
413 assert.equal(ignore(p, false), false, JSON.stringify(p));
414 assert.equal(ignore.why(".ENV", false).rule?.pattern, ".env");
415 // .gitignore too, even where git status matches case exactly.
416 assert.equal(ignore("A.LOG", false), true);
417 });
419 it("the mod's pathKey is the engine's", async () => {
420 const engine = await import("../../worker/project/pathkey.ts");
421 const { pathKey } = await import("../src/core.ts");
422 for (const p of [
423 ".ENV",
424 "Stra\u00dfe.md",
425 "caf\u00e9",
426 "Cafe\u0301",
427 "\u212Aelvin",
428 ".avc\u017F",
429 ".g\u0131t",
430 "\u0130stanbul",
431 "\u01C5",
432 "a/B/c.TXT",
433 ])
434 assert.equal(pathKey(p), engine.pathKey(p), JSON.stringify(p));
435 });
436});