Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

door: cut per-write git cost (ignore filter in front of check-ignore, one rev-parse fewer)

garrytan45644c24ffde
diff --git a/src/core/persistence/git-door.ts b/src/core/persistence/git-door.ts
index aa91d3f0ba..36c4e64d0d 100644
--- a/src/core/persistence/git-door.ts
+++ b/src/core/persistence/git-door.ts
@@ -28,12 +28,13 @@
* once through publishGroup.
*/
import { createHash } from 'node:crypto';
-import { existsSync, lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import { existsSync, lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { join, relative, resolve, sep } from 'node:path';
import { OperationError, opError } from '../ops/contract.ts';
-import { commitMessage, git, gitBytes, gitFailure, ignoredTargetError, withSession, type GitSession } from './effect-git.ts';
+import { commitMessage, git, gitBytes, gitFailure, gitStepTimeoutMs, ignoredTargetError, withSession, type GitSession } from './effect-git.ts';
import type { GitCommitNote } from './effect-model.ts';
import { persistenceHome } from './identity.ts';
+import { ignoreCandidates } from './ignore-rules.ts';
import { hashFile } from './large-file.ts';
import type { FileRecoveryRecord, SqlEngine } from './model.ts';
import { nativeFileTarget } from './native-file-target.ts';
@@ -80,7 +81,7 @@ export async function commitThroughDoor(root: string, files: { path: string; not
doorTurns.set(key, turn);
await prior;
try {
- return await withSession(root, undefined, writerLock ? { writerLock } : undefined, true, session => doorCommit(session, root, files));
+ return await withSession(root, undefined, writerLock ? { writerLock } : undefined, false, session => doorCommit(session, root, files));
} catch (error) {
if (!(error instanceof OperationError) || TYPED.has(error.code) || landedFailures.has(error)) throw error;
const paths = files.map(file => relative(root, file.path).split(sep).join('/'));
@@ -170,9 +171,11 @@ async function doorCommit(session: GitSession, root: string, files: { path: stri
if (symbolic.code !== 0) throw opError('git_detached_head', `${root} is on a detached HEAD, so the strict source's write has no branch to commit to; nothing was published.`,
'Check out the branch the source tracks (git switch <branch>) on the brain host, then retry the write with a new request_id.');
const branch = symbolic.stdout.trim();
- const layout = await git(session, ['rev-parse', '--show-object-format', '--show-prefix']);
+ const layout = await git(session, ['rev-parse', '--show-object-format', '--show-prefix', '--git-path', 'index']);
if (layout.code !== 0) throw gitFailure('Cannot inspect the canonical Git checkout.', 'git rev-parse failed.');
- const [format, prefix = ''] = layout.stdout.split('\n');
+ const [format, prefix = '', indexPath = ''] = layout.stdout.split('\n');
+ const index = resolve(root, indexPath);
+ try { session.timeoutMs = gitStepTimeoutMs(statSync(index).size); } catch { /* the step floor stands */ }
const algorithm = format === 'sha256' ? 'sha256' : 'sha1';
const oidBytes = algorithm === 'sha256' ? 32 : 20;
@@ -254,7 +257,7 @@ async function doorCommit(session: GitSession, root: string, files: { path: stri
}
changed.push(target);
}
- if (fresh.length) await refuseIgnored(session, fresh);
+ if (fresh.length) await refuseIgnored(session, root, fresh);
if (!changed.length) {
const stale = await git(session, ['diff-index', '--cached', '--quiet', head, '--', ...targets.keys()]);
if (stale.code !== 0) await syncIndex(session, root, prefix, [...targets.values()], head);
@@ -300,13 +303,19 @@ async function doorCommit(session: GitSession, root: string, files: { path: stri
} finally { rmSync(scratch, { recursive: true, force: true }); }
}
-/** The new targets a .gitignore rule excludes (a negated rule matching last un-ignores) refuse as `git_target_ignored`. */
-async function refuseIgnored(session: GitSession, fresh: Target[]): Promise<void> {
+/**
+ * The new targets a .gitignore rule excludes (a negated rule matching last un-ignores) refuse as `git_target_ignored`.
+ * Only the targets some ignore rule could match (ignore-rules.ts) are asked of check-ignore.
+ */
+async function refuseIgnored(session: GitSession, root: string, fresh: Target[]): Promise<void> {
+ const candidates = new Set(await ignoreCandidates(root, fresh.map(target => target.rel)));
+ const asked = fresh.filter(target => candidates.has(target.rel));
+ if (!asked.length) return;
// check-ignore takes no literal pathspecs; -n prints one line per path, in order, so no path is parsed back.
- const matched = await git(session, ['check-ignore', '-v', '-n', '--', ...fresh.map(target => target.rel)], false);
- if (matched.code !== 0 && matched.code !== 1) throw gitFailure('Cannot inspect the canonical Git target.', `git check-ignore failed for ${fresh.length} file(s).`);
+ const matched = await git(session, ['check-ignore', '-v', '-n', '--', ...asked.map(target => target.rel)], false);
+ if (matched.code !== 0 && matched.code !== 1) throw gitFailure('Cannot inspect the canonical Git target.', `git check-ignore failed for ${asked.length} file(s).`);
const lines = matched.stdout.split('\n');
- fresh.forEach((target, i) => {
+ asked.forEach((target, i) => {
const rule = /^(.*):(\d+):(.*)$/.exec((lines[i] ?? '').split('\t')[0]!);
if (rule && rule[3] && !rule[3].startsWith('!')) throw ignoredTargetError(target.rel, `${rule[1]}:${rule[2]}: ${rule[3]}`);
});
diff --git a/src/core/persistence/ignore-rules.ts b/src/core/persistence/ignore-rules.ts
new file mode 100644
index 0000000000..0e885f6871
--- /dev/null
+++ b/src/core/persistence/ignore-rules.ts
@@ -0,0 +1,274 @@
+/**
+ * Which paths Git might ignore, answered without a git process per write. A strict write asked `git check-ignore` up to
+ * three times (page preparation twice, the door once), and each run reads the checkout's whole index: about 50-60 ms at
+ * 155k files. This module reads the ignore sources themselves (every .gitignore from the top level down to the path,
+ * .git/info/exclude and core.excludesFile) and answers "not ignored" only when no rule that could ignore the path exists.
+ * Every other path is returned for Git to classify exactly as before, so Git stays the only judge of an ignored path:
+ * this is a filter in front of check-ignore, never a replacement.
+ *
+ * The filter over-approximates. Negated rules are dropped (they only un-ignore), every wildcard (`*`, `?`, a bracket
+ * expression, `**`) matches any run of characters including `/`, directory-only rules match files too, letters
+ * compare case-insensitively, and a rule written with a backslash or a character outside printable ASCII, or an ignore
+ * file that is not valid UTF-8 or cannot be read, counts as matching everything under its directory. Each rule is
+ * tested against the path and every directory above it (Git ignores a path under an ignored directory), in both the
+ * path's own and its NFC spelling, so a path the filter passes is one no positive rule could match.
+ *
+ * Git is asked whenever the filter cannot see what Git would read: a sparse checkout or an index entry marked
+ * skip-worktree (Git then reads .gitignore files from the index), a split or sparse index, a core.excludesFile or an
+ * include it cannot resolve (`~user`, `%(prefix)`, a relative path), a gitlink or a symlink along the path
+ * (check-ignore refuses those paths), or a probe that failed. The probe (the layout from rev-parse and the
+ * configuration with every file it came from) is cached per checkout and taken again when any configuration file it
+ * read or could read, HEAD (for includeIf onbranch) or the configuration environment changes. Ignore files are re-read
+ * on every call and parsed once per content, so a write that edits a .gitignore is classified against its new rules.
+ */
+import { lstatSync, readFileSync, realpathSync, statSync } from 'node:fs';
+import { dirname, isAbsolute, join, resolve } from 'node:path';
+import { execFileBounded } from '../bounded-child-exec.ts';
+import { gitChildEnv } from '../git-env.ts';
+
+/** A positive rule: literal chunks that must appear in order, separated by wildcards; `anchored` rules match the path below their directory, the others a single name. */
+interface Rule { anchored: boolean; chunks: string[]; open: boolean; close: boolean; literal: boolean }
+interface Rules { all: boolean; rules: Rule[] }
+interface Probe { top: string; prefix: string; oidBytes: number; exclude: string; index: string; excludesFile: string | null; decidable: boolean;
+ watched: Map<string, Buffer | null>; env: string }
+interface IndexScan { decidable: boolean; gitlinks: Set<string> }
+
+const GIT_ENV = { GIT_TERMINAL_PROMPT: '0', GIT_OPTIONAL_LOCKS: '0', LC_ALL: 'C' };
+const PROBE_TIMEOUT_MS = 20_000;
+const probes = new Map<string, Probe>();
+const indexScans = new Map<string, { stamp: string; scan: IndexScan }>();
+const sources = new Map<string, { bytes: Buffer; rules: Rules }>();
+const NONE: Rules = { all: false, rules: [] };
+const ALL: Rules = { all: true, rules: [] };
+const WILDCARDS = /\/?\*\*\/?|[*?]/;
+
+const fold = (text: string) => text.replace(/[A-Z]+/g, upper => upper.toLowerCase());
+
+function readOrNull(path: string): Buffer | null | undefined {
+ try { return readFileSync(path); } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ return code === 'ENOENT' || code === 'ENOTDIR' ? null : undefined;
+ }
+}
+
+/** The positive rules of one ignore file, as the over-approximation the module header describes. */
+export function parseIgnoreRules(bytes: Buffer): Rules {
+ const text = bytes.toString('utf8');
+ if (!Buffer.from(text, 'utf8').equals(bytes)) return ALL;
+ const rules: Rule[] = [];
+ for (let line of text.split('\n')) {
+ if (line.endsWith('\r')) line = line.slice(0, -1);
+ if (!line || line[0] === '#' || line[0] === '!') continue;
+ if (line.includes('\\') || /[^\x20-\x7e]/.test(line)) return ALL;
+ line = line.replace(/ +$/, '');
+ if (line.endsWith('/')) line = line.slice(0, -1);
+ const anchored = line.includes('/');
+ if (anchored && line[0] === '/') line = line.slice(1);
+ if (!line) continue;
+ const bracket = line.indexOf('[');
+ if (bracket >= 0) line = line.slice(0, bracket) + '*';
+ const parts = line.split(WILDCARDS);
+ rules.push({ anchored, chunks: parts.filter(Boolean).map(fold), open: parts[0] === '', close: parts[parts.length - 1] === '', literal: parts.length === 1 });
+ }
+ return { all: false, rules };
+}
+
+/** Whether `rule` could match `text` (already case-folded): its chunks in order, the first a prefix and the last a suffix unless a wildcard stands there. */
+function matches(rule: Rule, text: string): boolean {
+ if (rule.literal) return text === rule.chunks[0];
+ let at = 0, end = text.length, first = 0, last = rule.chunks.length;
+ if (!rule.open) {
+ const head = rule.chunks[first++]!;
+ if (!text.startsWith(head)) return false;
+ at = head.length;
+ }
+ if (!rule.close && last > first) {
+ const tail = rule.chunks[--last]!;
+ if (end - tail.length < at || !text.endsWith(tail)) return false;
+ end -= tail.length;
+ }
+ for (let i = first; i < last; i++) {
+ const found = text.indexOf(rule.chunks[i]!, at);
+ if (found < 0 || found + rule.chunks[i]!.length > end) return false;
+ at = found + rule.chunks[i]!.length;
+ }
+ return true;
+}
+
+async function git(root: string, args: string[]): Promise<string | null> {
+ const { error, stdout } = await execFileBounded('git', ['-C', root, ...args], { timeout: PROBE_TIMEOUT_MS, maxBuffer: 16 * 1024 * 1024, env: gitChildEnv(GIT_ENV) });
+ return error ? null : stdout;
+}
+
+/** The configuration environment Git reads: a change takes the probe again. */
+function configEnv(): string {
+ const env = gitChildEnv();
+ return ['HOME', 'XDG_CONFIG_HOME', 'GIT_CONFIG_GLOBAL', 'GIT_CONFIG_SYSTEM', 'GIT_CONFIG_NOSYSTEM', 'GIT_CONFIG_COUNT']
+ .map(key => `${key}=${env[key] ?? ''}`).concat(Object.keys(env).filter(key => /^GIT_CONFIG_(KEY|VALUE)_\d+$/.test(key)).sort().map(key => `${key}=${env[key]}`)).join('\0');
+}
+
+/** A configured path as Git expands it (`~/` is the home directory), or undefined when Git's expansion is not reproduced here. */
+function configPath(value: string, base?: string): string | undefined {
+ const home = process.env.HOME;
+ if (value.startsWith('~/') && home) return join(home, value.slice(2));
+ if (isAbsolute(value)) return value;
+ if (base && value && !value.startsWith('~') && !value.startsWith('%(') && !value.startsWith(':(')) return resolve(base, value);
+ return undefined;
+}
+
+async function probe(root: string): Promise<Probe | null> {
+ const [layout, listed, system, global] = await Promise.all([
+ git(root, ['rev-parse', '--show-toplevel', '--show-prefix', '--show-object-format', '--git-path', 'info/exclude', '--git-path', 'index', '--git-path', 'HEAD',
+ '--git-path', 'config', '--git-path', 'config.worktree']),
+ git(root, ['config', '--list', '--show-origin', '-z']),
+ git(root, ['var', 'GIT_CONFIG_SYSTEM']), git(root, ['var', 'GIT_CONFIG_GLOBAL'])]);
+ const lines = layout?.split('\n');
+ if (!lines || lines.length !== 9 || listed === null) return null;
+ const [top, prefix, format, exclude, index, head, config, worktreeConfig] = lines.map((line, i) => i < 3 || isAbsolute(line) ? line : resolve(root, line));
+ if (!top || (format !== 'sha1' && format !== 'sha256')) return null;
+ const home = process.env.HOME, xdg = process.env.XDG_CONFIG_HOME;
+ const watchedPaths = new Set<string>([head!, config!, worktreeConfig!, ...(system ?? '/etc/gitconfig').split('\n').filter(Boolean),
+ ...(global ?? [home && join(home, '.gitconfig'), xdg ? join(xdg, 'git', 'config') : home && join(home, '.config', 'git', 'config')].filter(Boolean).join('\n')).split('\n').filter(Boolean)]);
+ let excludes: string | undefined | null = null, sparse = false, unresolved = false;
+ const fields = listed.split('\0');
+ for (let i = 0; i + 1 < fields.length; i += 2) {
+ const origin = fields[i]!, entry = fields[i + 1]!;
+ const newline = entry.indexOf('\n');
+ const key = (newline < 0 ? entry : entry.slice(0, newline)).toLowerCase(), value = newline < 0 ? null : entry.slice(newline + 1);
+ const file = origin.startsWith('file:') ? resolve(top, origin.slice(5)) : null;
+ if (file) watchedPaths.add(file);
+ if (key === 'core.excludesfile') excludes = value === null ? undefined : configPath(value);
+ if (key === 'core.sparsecheckout') sparse = value === null || !/^(false|no|off|0)$/i.test(value.trim());
+ if ((key === 'include.path' || /^includeif\..*\.path$/.test(key)) && value !== null) {
+ const included = configPath(value, file ? dirname(file) : undefined);
+ if (included) watchedPaths.add(included); else unresolved = true;
+ }
+ }
+ if (excludes === null) excludes = xdg ? join(xdg, 'git', 'ignore') : home ? join(home, '.config', 'git', 'ignore') : null;
+ const watched = new Map<string, Buffer | null>();
+ for (const path of watchedPaths) { const bytes = readOrNull(path); if (bytes === undefined) return null; watched.set(path, bytes); }
+ return { top, prefix: prefix!, oidBytes: format === 'sha256' ? 32 : 20, exclude: exclude!, index: index!, excludesFile: excludes ?? null,
+ decidable: excludes !== undefined && !sparse && !unresolved, watched, env: configEnv() };
+}
+
+function current(probed: Probe): boolean {
+ if (probed.env !== configEnv()) return false;
+ for (const [path, bytes] of probed.watched) {
+ const now = readOrNull(path);
+ if (now === undefined || (now === null) !== (bytes === null) || (now && !now.equals(bytes!))) return false;
+ }
+ return true;
+}
+
+/**
+ * What the index tells the filter: no skip-worktree entry (Git would read such a .gitignore from the index), no split
+ * or sparse index (or any other required extension), and the gitlinks (check-ignore refuses a path inside a submodule).
+ * Version 4 entries are not named here, so a gitlink there makes the whole index undecidable.
+ */
+export function scanIndex(bytes: Buffer, oidBytes: number): IndexScan {
+ const undecidable = { decidable: false, gitlinks: new Set<string>() };
+ if (bytes.length < 12 || bytes.toString('latin1', 0, 4) !== 'DIRC') return undecidable;
+ const version = bytes.readUInt32BE(4), count = bytes.readUInt32BE(8);
+ if (version < 2 || version > 4) return undecidable;
+ const gitlinks = new Set<string>();
+ let at = 12;
+ for (let i = 0; i < count; i++) {
+ if (at + 42 + oidBytes > bytes.length) return undecidable;
+ const mode = bytes.readUInt32BE(at + 24), flags = bytes.readUInt16BE(at + 40 + oidBytes);
+ let name = at + 42 + oidBytes;
+ if (flags & 0x4000) {
+ if (version < 3 || name + 2 > bytes.length || bytes.readUInt16BE(name) & 0x4000) return undecidable;
+ name += 2;
+ }
+ const gitlink = (mode & 0o170000) === 0o160000;
+ if (version === 4) {
+ while (name < bytes.length && bytes[name]! & 0x80) name++;
+ const nul = bytes.indexOf(0, name + 1);
+ if (nul < 0 || gitlink) return undecidable;
+ at = nul + 1;
+ continue;
+ }
+ const length = (flags & 0xfff) === 0xfff ? bytes.indexOf(0, name) - name : flags & 0xfff;
+ if (length < 0 || name + length > bytes.length) return undecidable;
+ if (gitlink) gitlinks.add(bytes.toString('utf8', name, name + length));
+ at += ((name - at) + length + 8) & ~7;
+ }
+ while (at + 8 <= bytes.length - oidBytes) {
+ if (!/^[A-Z]/.test(bytes.toString('latin1', at, at + 4))) return undecidable;
+ at += 8 + bytes.readUInt32BE(at + 4);
+ }
+ return { decidable: true, gitlinks };
+}
+
+function indexScan(probed: Probe): IndexScan {
+ let stamp: string;
+ try { const s = statSync(probed.index, { bigint: true }); stamp = `${s.dev}:${s.ino}:${s.size}:${s.mtimeNs}:${s.ctimeNs}`; }
+ catch (error) { return { decidable: (error as NodeJS.ErrnoException).code === 'ENOENT', gitlinks: new Set() }; }
+ const cached = indexScans.get(probed.index);
+ if (cached?.stamp === stamp) return cached.scan;
+ const bytes = readOrNull(probed.index);
+ const scan = bytes ? scanIndex(bytes, probed.oidBytes) : { decidable: bytes === null, gitlinks: new Set<string>() };
+ indexScans.set(probed.index, { stamp, scan });
+ return scan;
+}
+
+function rulesOf(path: string): Rules {
+ const bytes = readOrNull(path);
+ if (bytes === undefined) return ALL;
+ if (bytes === null) return NONE;
+ const cached = sources.get(path);
+ if (cached?.bytes.equals(bytes)) return cached.rules;
+ const rules = parseIgnoreRules(bytes);
+ sources.set(path, { bytes, rules });
+ return rules;
+}
+
+/** Whether any of `rules` (from the ignore file of directory `base`, '' for the top level) could match the path in `segments` or a directory above it. */
+function couldMatch(rules: Rules, base: string, segments: string[]): boolean {
+ const skip = base ? base.split('/').length : 0;
+ if (skip >= segments.length) return false;
+ if (rules.all) return true;
+ for (const rule of rules.rules) {
+ for (let depth = skip + 1; depth <= segments.length; depth++) {
+ if (matches(rule, rule.anchored ? segments.slice(skip, depth).join('/') : segments[depth - 1]!)) return true;
+ }
+ }
+ return false;
+}
+
+/**
+ * The paths among `relativePaths` (relative to `root`, `/`-separated) whose ignore status needs Git's answer, in
+ * order: every path when the filter cannot decide, otherwise only those some ignore rule could match.
+ */
+export async function ignoreCandidates(root: string, relativePaths: string[]): Promise<string[]> {
+ if (!relativePaths.length) return [];
+ let key: string;
+ try { key = realpathSync(root); } catch { return relativePaths; }
+ let probed = probes.get(key);
+ if (!probed || !current(probed)) {
+ const fresh = await probe(root);
+ if (!fresh) { probes.delete(key); return relativePaths; }
+ probes.set(key, probed = fresh);
+ }
+ const scan = indexScan(probed);
+ if (!probed.decidable || !scan.decidable) return relativePaths;
+ const shared = [probed.excludesFile ? rulesOf(probed.excludesFile) : NONE, rulesOf(probed.exclude)];
+ return relativePaths.filter(relativePath => {
+ const full = `${probed.prefix}${relativePath}`;
+ const segments = full.split('/');
+ if (!relativePath || segments.some(segment => !segment || segment === '.' || segment === '..')) return true;
+ for (let depth = 1; depth <= segments.length; depth++) {
+ const path = segments.slice(0, depth).join('/');
+ if (scan.gitlinks.has(path)) return true;
+ if (depth < segments.length) {
+ try { if (lstatSync(join(probed.top, path)).isSymbolicLink()) return true; } catch { /* a missing directory holds no rule */ }
+ }
+ }
+ const variants = [...new Set([full, full.normalize('NFC')].map(fold))].map(text => text.split('/'));
+ const local = segments.map((_, depth) => ({ base: segments.slice(0, depth).join('/'), rules: rulesOf(join(probed.top, ...segments.slice(0, depth), '.gitignore')) }));
+ return variants.some(variant => shared.some(rules => couldMatch(rules, '', variant)) || local.some(({ base, rules }) => couldMatch(rules, base, variant)));
+ });
+}
+
+/** Test seam: forget every cached probe, index scan and parsed ignore file. */
+export function resetIgnoreCandidatesForTests(): void { probes.clear(); indexScans.clear(); sources.clear(); }
diff --git a/src/core/persistence/sync-origin-evidence.ts b/src/core/persistence/sync-origin-evidence.ts
index 836969bc15..61c78ca7d5 100644
--- a/src/core/persistence/sync-origin-evidence.ts
+++ b/src/core/persistence/sync-origin-evidence.ts
@@ -15,6 +15,7 @@ import { execFile, spawn } from '../spawn.ts';
import { promisify } from 'node:util';
import { gitChildEnv } from '../git-env.ts';
import { NulRecordSplitter } from '../hardened-git.ts';
+import { ignoreCandidates } from './ignore-rules.ts';
const execFileAsync = promisify(execFile);
@@ -61,9 +62,11 @@ function checkIgnore(gitRoot: string, paths: readonly string[]): Promise<{ code:
/**
* #7130 (write-through): whether the Git work tree around `root` ignores `relativePath` and does not track it, so a
* file written there would never be committed. False outside a work tree or when Git cannot answer (the write keeps its
- * old behavior then; sync's own evidence read still fails closed).
+ * old behavior then; sync's own evidence read still fails closed). A path no ignore rule could match (ignore-rules.ts)
+ * is answered without running check-ignore.
*/
export async function gitIgnoresUntracked(root: string, relativePath: string): Promise<boolean> {
+ if (!(await ignoreCandidates(root, [relativePath])).length) return false;
return (await git(root, ['check-ignore', '-q', '--', relativePath])).code === 0;
}
diff --git a/test/git-door-write-cost.test.ts b/test/git-door-write-cost.test.ts
new file mode 100644
index 0000000000..01419eb7c9
--- /dev/null
+++ b/test/git-door-write-cost.test.ts
@@ -0,0 +1,244 @@
+/**
+ * The door's per-write cost cut (src/core/persistence/git-door.ts, ignore-rules.ts). Protects:
+ * - once the checkout's ignore probe is cached, a new file no ignore rule could match commits without running
+ * check-ignore, the index is still staged by one git update-index, and the step deadline needs no rev-parse of its
+ * own; a file a rule could match still asks check-ignore (red before: every new file ran check-ignore, and every
+ * commit ran a separate rev-parse for the step deadline);
+ * - an ignored path is still refused, through a nested .gitignore, info/exclude and core.excludesFile, naming the rule;
+ * - an edit of a .gitignore between writes, and a publication that carries the .gitignore ignoring its sibling, are
+ * classified against the new rules;
+ * - a commit holds only its paths while other files are dirty, staged or untracked; removals and renames land, an
+ * executable keeps its bit, and `git status` afterwards shows only what it showed before;
+ * - the index entry the door stages is the oid hash-object returned for the bytes it committed, never a refresh of
+ * the work tree, so bytes changed after staging show as modified (the staged-oid check of the push scan relies on it);
+ * - external git commits racing the door never corrupt the index or lose a commit;
+ * - on both engines, a strict page under a rule added mid-session is declared git_ignored (no file, no commit), a mixed
+ * page there on a non-durable checkout is database-only, and a durable mixed page still parks its Git effect as
+ * git_target_ignored.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { execFile, execFileSync } from 'node:child_process';
+import { randomUUID } from 'node:crypto';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { promisify } from 'node:util';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { gitChildEnv } from '../src/core/git-env.ts';
+import { operations, type OperationContext } from '../src/core/operations.ts';
+import { runPersistenceEffects } from '../src/core/persistence/effects.ts';
+import { commitThroughDoor, doorSeams } from '../src/core/persistence/git-door.ts';
+import { localHostId } from '../src/core/persistence/identity.ts';
+import { resetIgnoreCandidatesForTests } from '../src/core/persistence/ignore-rules.ts';
+import { durableGitRepo } from './helpers/git-publication.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
+import { waitFor } from './helpers/wait-for.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const env = gitChildEnv();
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], env }).trim();
+const lines = (text: string) => text.split('\n').filter(Boolean);
+const OLD = new Date('2001-01-01T00:00:00Z');
+const roots: string[] = [];
+afterEach(() => { delete doorSeams.beforeUpdateRef; resetIgnoreCandidatesForTests(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
+
+/** A repository on branch main whose files and index are at rest (no racily clean entry). */
+function repo(files: Record<string, string>): string {
+ const root = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-door-cost-')));
+ roots.push(root);
+ git(root, 'init', '-q', '-b', 'main');
+ git(root, 'config', 'user.name', 'Example Writer'); git(root, 'config', 'user.email', '[email protected]');
+ for (const [path, body] of Object.entries(files)) put(root, path, body);
+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'seed');
+ return root;
+}
+function put(root: string, path: string, body: string): void {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), body);
+ utimesSync(join(root, path), OLD, OLD);
+}
+const door = (root: string, ...paths: string[]) => commitThroughDoor(root, paths.map(path => ({ path: join(root, path) })));
+const refusal = (work: Promise<unknown>) => work.then(() => null, (error: { code?: string; message?: string; suggestion?: string }) => error);
+const changed = (root: string, commit: string) => lines(git(root, 'show', '--no-renames', '--name-only', '--format=', commit)).sort();
+
+/** The git subcommands a door commit runs, read from a trace2 event log. */
+async function traced(root: string, work: () => Promise<unknown>): Promise<string[]> {
+ const log = `${root}-trace.jsonl`;
+ roots.push(log);
+ await withEnv({ GIT_TRACE2_EVENT: log }, work);
+ return lines(readFileSync(log, 'utf8')).map(line => JSON.parse(line) as { event: string; sid: string; argv?: string[] })
+ .filter(event => event.event === 'start' && !event.sid.includes('/'))
+ .map(event => { const argv = event.argv!; for (let i = 1; i < argv.length; i++) { if (argv[i] === '-C' || argv[i] === '-c') { i++; continue; } if (!argv[i]!.startsWith('-')) return argv[i]!; } return '?'; });
+}
+
+describe('door write cost', () => {
+ test('a new file no rule could match runs no check-ignore and one update-index; one a rule could match asks check-ignore', async () => {
+ const root = repo({ 'notes/a.md': 'a\n', '.gitignore': 'scratch/\nnotes/odd-*\n!notes/odd-kept.md\n' });
+ put(root, 'notes/warm.md', 'warm\n');
+ await door(root, 'notes/warm.md');
+ put(root, 'notes/new.md', 'new\n');
+ const plain = await traced(root, () => door(root, 'notes/new.md'));
+ expect(plain.filter(cmd => ['check-ignore', 'update-index', 'config', 'var'].includes(cmd))).toEqual(['update-index']);
+ expect(plain.filter(cmd => cmd === 'rev-parse')).toHaveLength(2);
+ expect(git(root, 'status', '--porcelain')).toBe('');
+ expect(git(root, 'ls-files', '-s', '--', 'notes/new.md')).toBe(`100644 ${git(root, 'rev-parse', 'HEAD:notes/new.md')} 0\tnotes/new.md`);
+ put(root, 'notes/odd-kept.md', 'odd\n');
+ const asked = await traced(root, () => door(root, 'notes/odd-kept.md'));
+ expect(asked.filter(cmd => cmd === 'check-ignore')).toHaveLength(1);
+ expect(git(root, 'show', 'HEAD:notes/odd-kept.md')).toBe('odd');
+ expect(git(root, 'status', '--porcelain')).toBe('');
+ });
+
+ test('an ignored new file still refuses through a nested .gitignore, info/exclude and core.excludesFile, naming the rule', async () => {
+ const root = repo({ 'notes/a.md': 'a\n', 'notes/.gitignore': 'private-*\n' });
+ writeFileSync(join(root, '.git', 'info', 'exclude'), 'local/\n');
+ const excludes = join(root, '.git', 'personal-excludes');
+ writeFileSync(excludes, '*.draft.md\n');
+ git(root, 'config', 'core.excludesFile', excludes);
+ const head = git(root, 'rev-parse', 'HEAD');
+ for (const [path, rule] of [['notes/private-x.md', 'notes/.gitignore:1: private-*'], ['local/x.md', '.git/info/exclude:1: local/'], ['notes/x.draft.md', `${excludes}:1: *.draft.md`]] as const) {
+ put(root, path, 'x\n');
+ expect(await refusal(door(root, path))).toMatchObject({ code: 'git_target_ignored', suggestion: expect.stringContaining(`Git ignores ${path} (${rule})`) });
+ expect(git(root, 'rev-parse', 'HEAD')).toBe(head);
+ expect(git(root, 'ls-files', '--', path)).toBe('');
+ }
+ });
+
+ test('a .gitignore edited between writes, or carried by the same publication, is classified against its new rules', async () => {
+ const root = repo({ 'notes/a.md': 'a\n', '.gitignore': 'scratch/\n' });
+ put(root, 'notes/later-1.md', 'one\n');
+ await door(root, 'notes/later-1.md');
+ put(root, '.gitignore', 'scratch/\nnotes/later-*\n');
+ put(root, 'notes/later-2.md', 'two\n');
+ expect(await refusal(door(root, 'notes/later-2.md'))).toMatchObject({ code: 'git_target_ignored', suggestion: expect.stringContaining('(.gitignore:2: notes/later-*)') });
+ put(root, '.gitignore', 'scratch/\n');
+ const commit = await door(root, 'notes/later-2.md');
+ expect(changed(root, commit!)).toEqual(['notes/later-2.md']);
+ const head = git(root, 'rev-parse', 'HEAD');
+ put(root, '.gitignore', 'scratch/\nnotes/sibling.md\n');
+ put(root, 'notes/sibling.md', 'sibling\n');
+ expect(await refusal(door(root, '.gitignore', 'notes/sibling.md'))).toMatchObject({ code: 'git_target_ignored' });
+ expect(git(root, 'rev-parse', 'HEAD')).toBe(head);
+ });
+
+ test('removals and renames commit exactly their paths while other files are dirty, staged or untracked; the bit survives', async () => {
+ const root = repo({ 'notes/a.md': 'a\n', 'notes/old.md': 'old\n', 'notes/gone.md': 'gone\n', 'bin/tool.md': 'tool\n', 'other/dirty.md': 'dirty\n', 'other/staged.md': 'staged\n' });
+ chmodSync(join(root, 'bin/tool.md'), 0o755); git(root, 'add', '-A'); git(root, 'commit', '-qm', 'executable');
+ put(root, 'other/dirty.md', 'dirty, edited\n');
+ put(root, 'other/staged.md', 'staged, edited\n'); git(root, 'add', 'other/staged.md');
+ put(root, 'other/untracked.md', 'untracked\n');
+ const before = git(root, 'status', '--porcelain');
+ unlinkSync(join(root, 'notes/gone.md'));
+ expect(changed(root, (await door(root, 'notes/gone.md'))!)).toEqual(['notes/gone.md']);
+ mkdirSync(join(root, 'archive'));
+ renameSync(join(root, 'notes/old.md'), join(root, 'archive/old.md'));
+ renameSync(join(root, 'bin/tool.md'), join(root, 'bin/tool-renamed.md'));
+ const rename = await door(root, 'notes/old.md', 'archive/old.md', 'bin/tool.md', 'bin/tool-renamed.md');
+ expect(changed(root, rename!)).toEqual(['archive/old.md', 'bin/tool-renamed.md', 'bin/tool.md', 'notes/old.md']);
+ expect(git(root, 'ls-tree', 'HEAD', 'bin/tool-renamed.md').split(' ')[0]).toBe('100755');
+ expect(git(root, 'ls-files', '-s', '--', 'bin/tool-renamed.md').split(' ')[0]).toBe('100755');
+ expect(git(root, 'status', '--porcelain')).toBe(before);
+ expect(git(root, 'diff', '--cached', '--name-only')).toBe('other/staged.md');
+ expect(git(root, 'show', 'HEAD:other/staged.md')).toBe('staged');
+ });
+
+ test('the staged index entry is the hashed oid, not a refresh: bytes changed after staging show as modified', async () => {
+ const root = repo({ 'notes/a.md': 'a\n' });
+ put(root, 'notes/a.md', 'hashed\n');
+ put(root, 'notes/new.md', 'hashed new\n');
+ const hashed = git(root, 'hash-object', '--', 'notes/a.md');
+ doorSeams.beforeUpdateRef = () => { writeFileSync(join(root, 'notes/a.md'), 'rewritten after staging\n'); writeFileSync(join(root, 'notes/new.md'), 'rewritten new\n'); };
+ const commit = await door(root, 'notes/a.md', 'notes/new.md');
+ expect(git(root, 'rev-parse', `${commit}:notes/a.md`)).toBe(hashed);
+ for (const path of ['notes/a.md', 'notes/new.md']) expect(git(root, 'ls-files', '-s', '--', path).split(' ')[1]).toBe(git(root, 'rev-parse', `${commit}:${path}`));
+ expect(lines(git(root, 'status', '--porcelain')).map(line => line.trim()).sort()).toEqual(['M notes/a.md', 'M notes/new.md']);
+ expect(git(root, 'diff', '--cached', '--name-only')).toBe('');
+ });
+
+ test('external git commits racing door commits never corrupt the index or lose a commit', async () => {
+ const root = repo({ 'notes/a.md': 'a\n' });
+ const run = promisify(execFile);
+ const landed: string[] = [], external: string[] = [];
+ const outsider = (async () => {
+ for (let i = 0; i < 8; i++) {
+ put(root, `outside/o${i}.md`, `outside ${i}\n`);
+ try {
+ await run('git', ['-C', root, 'add', '--', `outside/o${i}.md`], { env, timeout: 20_000 });
+ await run('git', ['-C', root, 'commit', '-q', '-m', `outside ${i}`, '--', `outside/o${i}.md`], { env, timeout: 20_000 });
+ external.push(`outside/o${i}.md`);
+ } catch { /* the door held index.lock; this commit is skipped */ }
+ }
+ })();
+ for (let i = 0; i < 8; i++) {
+ put(root, `notes/d${i}.md`, `door ${i}\n`);
+ const outcome = await door(root, `notes/d${i}.md`).then(commit => commit, (error: { code?: string }) => error);
+ if (typeof outcome === 'string') landed.push(`notes/d${i}.md`);
+ else expect(outcome).toMatchObject({ code: expect.stringMatching(/^(git_unavailable|git_index_locked)$/) });
+ }
+ await outsider;
+ git(root, 'fsck', '--no-progress', '--no-dangling');
+ const tree = new Set(lines(git(root, 'ls-tree', '-r', '--name-only', 'HEAD')));
+ for (const path of [...landed, ...external]) expect(tree.has(path)).toBe(true);
+ expect(landed.length).toBeGreaterThan(0);
+ const index = new Set(lines(git(root, 'ls-files')));
+ for (const path of landed) expect(index.has(path)).toBe(true);
+ }, 60_000);
+});
+
+const putPage = operations.find(op => op.name === 'put_page')!;
+const page = (title: string, body: string) => '---\ntype: note\ntitle: ' + title + '\n---\n\n' + body + '\n';
+const write = (ctx: OperationContext, slug: string) => putPage.handler(ctx, { slug, content: page(slug, `Body of ${slug}.`), request_id: randomUUID(), wait_ms: 30_000 }) as Promise<Record<string, any>>;
+const strict = (engine: BrainEngine) => engine.executeRaw(`UPDATE sources SET config = config || '{"canonical":"git"}'::jsonb WHERE id='default'`);
+const reason = async (engine: BrainEngine, slug: string) => (await engine.executeRaw<{ reason: string | null }>(
+ "SELECT database_only_reason AS reason FROM pages WHERE source_id='default' AND slug=$1 AND deleted_at IS NULL", [slug]))[0]?.reason;
+const ignoreLater = (root: string, rule: string) => { writeFileSync(join(root, '.gitignore'), rule); git(root, '-c', 'commit.gpgsign=false', 'commit', '-qam', 'ignore rule'); };
+const durable = ({ root }: { root: string }) => { writeFileSync(join(root, '.gitignore'), '\n'); durableGitRepo(root, ['.gitignore']); };
+const plain = ({ root }: { root: string }) => {
+ writeFileSync(join(root, '.gitignore'), '\n');
+ git(root, 'init', '-q'); git(root, 'config', 'user.name', 'Example Writer'); git(root, 'config', 'user.email', '[email protected]');
+ git(root, 'add', '.gitignore'); git(root, '-c', 'commit.gpgsign=false', 'commit', '-qm', 'Initial');
+};
+
+for (const backend of testBackends()) describe(`[${backend}] door write cost: ignore rules edited mid-session`, () => {
+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;
+
+ test('strict: a page under a rule added mid-session is declared git_ignored; after the rule goes a new page commits', () => managedBrain(async ({ engine, ctx, root }) => {
+ await strict(engine);
+ expect(await write(ctx, 'notes/before')).toMatchObject({ state: 'committed' });
+ expect(git(root, 'log', '--format=%H', '--', 'notes/before.md')).not.toBe('');
+ ignoreLater(root, 'notes/later-*\n');
+ const head = git(root, 'rev-parse', 'HEAD');
+ expect(await write(ctx, 'notes/later-one')).toMatchObject({ state: 'committed' });
+ expect(existsSync(join(root, 'notes/later-one.md'))).toBe(false);
+ expect(git(root, 'rev-parse', 'HEAD')).toBe(head);
+ expect(await reason(engine, 'notes/later-one')).toBe('git_ignored');
+ ignoreLater(root, '\n');
+ expect(await write(ctx, 'notes/later-two')).toMatchObject({ state: 'committed' });
+ expect(git(root, 'show', 'HEAD:notes/later-two.md')).toContain('Body of notes/later-two.');
+ expect(await reason(engine, 'notes/later-two')).toBeNull();
+ }, { databaseUrl, setup: durable }), 120_000);
+
+ test('mixed, not durable: a new page under a rule added mid-session is database-only', () => managedBrain(async ({ engine, ctx, root }) => {
+ expect(await write(ctx, 'notes/before')).toMatchObject({ state: 'committed' });
+ expect(existsSync(join(root, 'notes/before.md'))).toBe(true);
+ ignoreLater(root, 'notes/later-*\n');
+ expect(await write(ctx, 'notes/later-one')).toMatchObject({ state: 'committed' });
+ expect(existsSync(join(root, 'notes/later-one.md'))).toBe(false);
+ const [row] = await engine.executeRaw<{ path: string | null }>("SELECT source_path AS path FROM pages WHERE source_id='default' AND slug='notes/later-one'");
+ expect(row?.path ?? null).toBeNull();
+ }, { databaseUrl, setup: plain }), 120_000);
+
+ test('mixed, durable: a page under a rule added mid-session still parks its Git effect as git_target_ignored', () => managedBrain(async ({ engine, ctx, root }) => {
+ expect(await write(ctx, 'notes/before')).toMatchObject({ state: 'committed' });
+ ignoreLater(root, 'notes/later-*\n');
+ expect(await write(ctx, 'notes/later-one')).toMatchObject({ state: 'committed' });
+ expect(existsSync(join(root, 'notes/later-one.md'))).toBe(true);
+ const parked = async () => (await engine.executeRaw<{ code: string | null }>(
+ "SELECT e.data->'parked'->0->>'error_code' AS code FROM persistence_effects e JOIN persistence_requests r ON r.id=e.request_id WHERE r.slug='notes/later-one' AND e.kind='git' AND e.state='failed' AND e.error_code='targets_parked'"))[0]?.code;
+ await waitFor(async () => { await runPersistenceEffects(engine, { engine: engine.kind }, { hostId: localHostId(), limit: 10 }); return (await parked()) !== undefined; },
+ { timeoutMs: 30_000, label: 'the Git effect parks' });
+ expect(await parked()).toBe('git_target_ignored');
+ expect(git(root, 'ls-files', '--', 'notes/later-one.md')).toBe('');
+ }, { databaseUrl, setup: durable }), 120_000);
+});
diff --git a/test/ignore-rules.test.ts b/test/ignore-rules.test.ts
new file mode 100644
index 0000000000..c4de0a404e
--- /dev/null
+++ b/test/ignore-rules.test.ts
@@ -0,0 +1,149 @@
+/**
+ * The ignore filter in front of check-ignore (src/core/persistence/ignore-rules.ts). Protects:
+ * - soundness: over generated rule sets (nested .gitignore files, info/exclude, core.excludesFile, negations,
+ * wildcards, brackets, `**`, dir-only and anchored rules, escapes, trailing spaces, case) every path
+ * `git check-ignore` ignores is returned for Git to decide, and the filter still clears many of the others;
+ * - an edit of a .gitignore, info/exclude or the excludes file, or a changed core.excludesFile setting, is seen on the
+ * next call;
+ * - Git decides whenever the filter cannot see what Git reads: a sparse checkout, a skip-worktree entry, a gitlink or
+ * a symlinked directory on the path, a version 4 index holding a gitlink;
+ * - the index scan reads versions 2, 3 and 4.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { gitChildEnv } from '../src/core/git-env.ts';
+import { ignoreCandidates, resetIgnoreCandidatesForTests, scanIndex } from '../src/core/persistence/ignore-rules.ts';
+
+const env = gitChildEnv();
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'], env }).trim();
+const roots: string[] = [];
+afterEach(() => { resetIgnoreCandidatesForTests(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
+
+function repo(): string {
+ const root = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-ignore-rules-')));
+ roots.push(root);
+ git(root, 'init', '-q', '-b', 'main');
+ git(root, 'config', 'user.name', 'Example Writer'); git(root, 'config', 'user.email', '[email protected]');
+ return root;
+}
+function put(root: string, path: string, body = 'x\n'): void {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), body);
+}
+/** The paths Git ignores among `paths`, from the checkout's own check-ignore (index included). */
+function gitIgnored(root: string, paths: string[]): Set<string> {
+ try {
+ const out = execFileSync('git', ['-C', root, 'check-ignore', '-z', '--stdin'], { input: paths.map(path => path + '\0').join(''), env, stdio: ['pipe', 'pipe', 'pipe'] });
+ return new Set(out.toString('utf8').split('\0').filter(Boolean));
+ } catch (error) {
+ if ((error as { status?: number }).status === 1) return new Set();
+ throw error;
+ }
+}
+
+/** Deterministic generator (mulberry32). */
+function random(seed: number): () => number {
+ return () => { seed |= 0; seed = seed + 0x6d2b79f5 | 0; let t = Math.imul(seed ^ seed >>> 15, 1 | seed); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; };
+}
+
+const NAMES = ['notes', 'Notes', 'drafts', 'a', 'b', 'cache-1', 'keep.log', 'x.md', 'y.LOG', 'page.tmp', '.DS_Store', 'node_modules', 'dist', 'sub', 'page.md', 'foo bar', 'é.md', 'ab', 'abc.md', 'Build'];
+const RULES = ['notes', 'NOTES/', '*.log', '!keep.log', '*.tmp', '/dist', 'dist/', 'a/b', 'a/**/x.md', '**/drafts', 'sub/**', 'cache-*', '?b', '[ab]*.md', 'a/*/page.md',
+ '#notes', 'page.md ', '\\#lit', 'x\\.md', '*', '!*.md', 'node_modules/', '.DS_Store', 'foo bar', 'é.md', '/a/', 'ab*c.md', 'Build/', '**', '!sub/', 'b/', '/notes/drafts/', 'NOTES', 'y.log'];
+
+describe('ignore filter', () => {
+ test('every path check-ignore ignores is returned for Git, over generated rule sets; many others are cleared', async () => {
+ const next = random(7);
+ const pick = <T>(list: T[]) => list[Math.floor(next() * list.length)]!;
+ let cleared = 0, total = 0;
+ for (let round = 0; round < 40; round++) {
+ const root = repo();
+ if (next() < 0.3) git(root, 'config', 'core.ignoreCase', 'true');
+ const paths = new Set<string>();
+ while (paths.size < 30) paths.add(Array.from({ length: 1 + Math.floor(next() * 4) }, () => pick(NAMES)).join('/'));
+ const dirs = new Set([...paths].flatMap(path => path.split('/').slice(0, -1).map((_, i, parts) => parts.slice(0, i + 1).join('/'))));
+ const rules = (count: number) => Array.from({ length: count }, () => pick(RULES)).join('\n') + '\n';
+ const placed: string[] = [];
+ for (const path of paths) {
+ if ([...paths].some(other => other.startsWith(path + '/'))) continue;
+ try { put(root, path); placed.push(path); } catch { /* a file where another path needs a directory */ }
+ }
+ const tracked = placed.filter(() => next() < 0.15);
+ if (tracked.length) git(root, 'add', '-f', '--', ...tracked);
+ writeFileSync(join(root, '.gitignore'), rules(2 + Math.floor(next() * 4)));
+ for (const dir of dirs) if (next() < 0.3) { try { writeFileSync(join(root, dir, '.gitignore'), rules(1 + Math.floor(next() * 2))); } catch { /* not a directory */ } }
+ if (next() < 0.5) writeFileSync(join(root, '.git', 'info', 'exclude'), rules(1));
+ if (next() < 0.5) { const file = `${root}-excludes`; roots.push(file); writeFileSync(file, rules(1)); git(root, 'config', 'core.excludesFile', file); }
+ const listed = [...paths];
+ const ignored = gitIgnored(root, listed);
+ const candidates = new Set(await ignoreCandidates(root, listed));
+ for (const path of ignored) expect({ path, rules: readFileSync(join(root, '.gitignore'), 'utf8'), candidate: candidates.has(path) }).toMatchObject({ candidate: true });
+ total += listed.length;
+ cleared += listed.length - candidates.size;
+ }
+ expect(cleared).toBeGreaterThan(total / 10);
+ }, 120_000);
+
+ test('an edit of a .gitignore, info/exclude or the excludes file, or a new core.excludesFile, is seen on the next call', async () => {
+ const root = repo();
+ put(root, 'notes/a.md'); put(root, '.gitignore', 'scratch/\n');
+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'seed');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ put(root, 'notes/.gitignore', 'new*\n');
+ expect(await ignoreCandidates(root, ['notes/new.md', 'notes/other.md'])).toEqual(['notes/new.md']);
+ put(root, 'notes/.gitignore', 'old*\n');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ writeFileSync(join(root, '.git', 'info', 'exclude'), 'notes/n*.md\n');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual(['notes/new.md']);
+ writeFileSync(join(root, '.git', 'info', 'exclude'), '');
+ const excludes = join(root, '.git', 'personal-excludes');
+ writeFileSync(excludes, '*.md\n');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ git(root, 'config', 'core.excludesFile', excludes);
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual(['notes/new.md']);
+ writeFileSync(excludes, '*.txt\n');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ });
+
+ test('Git decides on a sparse checkout, a skip-worktree entry, a gitlink or symlinked directory on the path, and a version 4 gitlink', async () => {
+ const root = repo();
+ put(root, 'notes/a.md'); put(root, 'notes/b.md'); put(root, 'real/c.md');
+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'seed');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ git(root, 'update-index', '--skip-worktree', 'notes/b.md');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual(['notes/new.md']);
+ git(root, 'update-index', '--no-skip-worktree', 'notes/b.md');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ git(root, 'config', 'core.sparseCheckout', 'true');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual(['notes/new.md']);
+ git(root, 'config', '--unset', 'core.sparseCheckout');
+ symlinkSync(join(root, 'real'), join(root, 'linked'));
+ expect(await ignoreCandidates(root, ['linked/new.md', 'notes/new.md'])).toEqual(['linked/new.md']);
+ git(root, 'update-index', '--add', '--cacheinfo', `160000,${git(root, 'rev-parse', 'HEAD')},vendor/lib`);
+ expect(await ignoreCandidates(root, ['vendor/lib/new.md', 'vendor/other.md', 'notes/new.md'])).toEqual(['vendor/lib/new.md']);
+ git(root, 'update-index', '--index-version', '4');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual(['notes/new.md']);
+ git(root, 'update-index', '--force-remove', 'vendor/lib');
+ expect(await ignoreCandidates(root, ['notes/new.md'])).toEqual([]);
+ });
+
+ test('the index scan reads versions 2, 3 and 4 and names version 2 and 3 gitlinks', () => {
+ const root = repo();
+ for (let i = 0; i < 50; i++) put(root, `dir-${i % 7}/${'long-name-'.repeat(i % 5)}page-${i}.md`);
+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'seed');
+ git(root, 'update-index', '--add', '--cacheinfo', `160000,${git(root, 'rev-parse', 'HEAD')},dir-3/module`);
+ const index = join(root, '.git', 'index');
+ expect(scanIndex(readFileSync(index), 20)).toEqual({ decidable: true, gitlinks: new Set(['dir-3/module']) });
+ put(root, 'dir-1/new.md');
+ git(root, 'add', '--intent-to-add', '--', 'dir-1/new.md');
+ expect(readFileSync(index).readUInt32BE(4)).toBe(3);
+ expect(scanIndex(readFileSync(index), 20)).toEqual({ decidable: true, gitlinks: new Set(['dir-3/module']) });
+ git(root, 'update-index', '--index-version', '4');
+ expect(scanIndex(readFileSync(index), 20).decidable).toBe(false);
+ git(root, 'update-index', '--force-remove', 'dir-3/module');
+ expect(scanIndex(readFileSync(index), 20)).toEqual({ decidable: true, gitlinks: new Set() });
+ expect(scanIndex(Buffer.from('not an index'), 20).decidable).toBe(false);
+ });
+});
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index 3c2ed50bec..52d7f39dc8 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -88,6 +88,7 @@ test/git-canonical-indexer-g3.test.ts
test/git-door-plumbing-gbra133.test.ts
test/git-door-remote-g7b.test.ts
test/git-door-strict-gbra133.test.ts
+test/git-door-write-cost.test.ts
test/git-gap-census.test.ts
test/google-connector-facts-fence-7292.test.ts
test/grade-takes-window.test.ts