- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/check-ignore.ts?format=json
85 linesFirst import 96% · review 4%Last change review in version 27, 3 versions changed it
1// `abso check-ignore`: whether abso leaves a path out, and which line of
2// which ignore file decided. The same rules record, publish, sync, run and the
3// hook use (core.ts).
5import { existsSync, statSync } from "node:fs";
6import { dirname, join, resolve } from "node:path";
7import { type Parsed, UsageError } from "./args.ts";
8import type { Env, Output } from "./commands.ts";
9import { findRoot } from "./config.ts";
10import type { IgnoreMatch } from "./core.ts";
11import { gitHome, loadIgnore, relativeTo } from "./folder.ts";
13/** Starting .absoignore for `abso init`, when the folder has none. */
14export const STARTING_ABSOIGNORE = `# Files abso leaves out, on top of .gitignore. Same rules as .gitignore; this
15# file wins over .gitignore in the same folder. \`abso check-ignore <path>\`
16# says which line decided. .git, .abso, .avcs, .claude and node_modules are always
17# left out.
19# Each session's own Claude Code state
20.claude/
22# Operating system files
23.DS_Store
24._*
25.Spotlight-V100/
26.Trashes/
27Thumbs.db
28ehthumbs.db
29desktop.ini
30$RECYCLE.BIN/
31`;
33/** The abso folder around cwd, else the git repository around it, else cwd. */
34function rootFor(cwd: string): string {
35 const avcs = findRoot(cwd);
36 if (avcs !== undefined) return avcs;
37 for (let dir = resolve(cwd); ; dir = dirname(dir)) {
38 if (existsSync(join(dir, ".git"))) return dir;
39 if (dirname(dir) === dir) return resolve(cwd);
40 }
41}
43function rule(m: IgnoreMatch): string {
44 return m.rule ? `${m.rule.source}:${m.rule.line} "${m.rule.pattern}"` : "";
45}
47function describe(rel: string, m: IgnoreMatch): string {
48 if (m.alwaysSkipped !== undefined) return `${rel}: ignored: abso always leaves out ${m.alwaysSkipped}`;
49 if (m.folder !== undefined)
50 return `${rel}: ignored, because its folder ${m.folder} is ignored by ${rule(m)}. Nothing inside an ignored folder can be brought back with "!"; un-ignore the folder first.`;
51 if (m.rule === undefined) return `${rel}: not ignored (no rule matches)`;
52 return m.ignored ? `${rel}: ignored by ${rule(m)}` : `${rel}: not ignored: ${rule(m)} brings it back`;
53}
55export async function runCheckIgnore(args: Parsed, env: Env): Promise<Output> {
56 if (args.positionals.length === 0) throw new UsageError("give at least one path: abso check-ignore <path...>");
57 const root = rootFor(env.cwd);
58 const ignore = loadIgnore(root, gitHome(env.env));
59 const results = args.positionals.map((arg) => {
60 const full = resolve(env.cwd, arg);
61 const rel = relativeTo(root, full);
62 if (rel === undefined) throw new UsageError(`${arg} is not inside ${root}`);
63 let isDir = /[\\/]$/.test(arg);
64 try {
65 isDir = statSync(full).isDirectory();
66 } catch {
67 // A path that doesn't exist yet: a trailing slash means a folder.
68 }
69 return { rel, isDir, match: ignore.why(rel, isDir) };
70 });
71 return {
72 text: results.map(({ rel, match }) => describe(rel, match)).join("\n"),
73 json: {
74 root,
75 paths: results.map(({ rel, isDir, match: m }) => ({
76 path: rel,
77 folder: isDir,
78 ignored: m.ignored,
79 ...(m.rule ? { source: m.rule.source, line: m.rule.line, pattern: m.rule.pattern } : {}),
80 ...(m.folder !== undefined ? { because: m.folder } : {}),
81 ...(m.alwaysSkipped !== undefined ? { alwaysSkipped: m.alwaysSkipped } : {}),
82 })),
83 },
84 };
85}