Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(durability): classify core.hooksPath by containment, repair exclusion on re-harden (#5578, #5436)

garrytanf29c5d2149c1
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsv
index 52676bffc9..6cb9e2c8e8 100644
--- a/scripts/structural-suites.tsv
+++ b/scripts/structural-suites.tsv
@@ -42,7 +42,7 @@ test/brain-durability-hook.serial.test.ts brain-commit-push.sh (D13 guarantee) 4
test/brain-durability-hook.serial.test.ts durability push remote confirmation (#5138) 9 readFileSync
test/brain-durability-hook.serial.test.ts durability schedule (installCron:true) [D2/D12] 0 readFileSync
test/brain-durability-hook.serial.test.ts post-commit hook (D9 local, D7 self-contained) 4 readFileSync
-test/brain-repo-durability.serial.test.ts hardenBrainRepo 16 readFileSync
+test/brain-repo-durability.serial.test.ts hardenBrainRepo 20 readFileSync
test/brain-score-breakdown.test.ts Bug 11 — BrainHealth type shape 1 bun-file
test/brain-score-breakdown.test.ts Bug 11 — doctor renders brain_score breakdown 1 doctor-source-helper
test/brain-score-breakdown.test.ts Bug 11 — orphan_pages is "no inbound links" 3 doctor-source-helper
diff --git a/src/core/brain-repo-durability.ts b/src/core/brain-repo-durability.ts
index b90dc7a13b..0a3ca07898 100644
--- a/src/core/brain-repo-durability.ts
+++ b/src/core/brain-repo-durability.ts
@@ -393,6 +393,11 @@ function gitDirPath(repoPath: string, rel: string): string {
return join(repoPath, '.git', rel);
}
+function pathContains(parent: string, child: string): boolean {
+ const rel = relative(parent, child);
+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
+}
+
function resolveHooksDir(repoPath: string): { dir: string; tracked: boolean } {
let hooksPath = '';
try {
@@ -402,8 +407,10 @@ function resolveHooksDir(repoPath: string): { dir: string; tracked: boolean } {
} catch { /* unset — normal */ }
if (hooksPath) {
const dir = isAbsolute(hooksPath) ? hooksPath : join(repoPath, hooksPath);
- // A hooksPath outside .git/ (e.g. .githooks) is a TRACKED location.
- const tracked = !dir.includes(`${join('.git', '')}`) && !dir.endsWith('.git/hooks');
+ // A hooksPath in the working tree but outside the git dir (e.g. .githooks)
+ // is a TRACKED location. Classify by path containment, never by a '.git'
+ // substring, which matches '.githooks' and checkouts like 'site.github.io'.
+ const tracked = pathContains(repoPath, dir) && !pathContains(gitDirPath(repoPath, ''), dir);
return { dir, tracked };
}
return { dir: gitDirPath(repoPath, 'hooks'), tracked: false };
@@ -431,6 +438,7 @@ function installLocalHook(repoPath: string, dryRun: boolean): { status: StepStat
if (existsSync(hookPath)) {
const cur = readFileSync(hookPath, 'utf-8');
if (cur.includes(HOOK_BANNER)) {
+ if (tracked && !dryRun) ensureExcluded(repoPath, relative(repoPath, hookPath));
if (cur === script) return { status: 'ok', detail: `${relative(repoPath, hookPath)} already current` };
if (dryRun) return { status: 'fixed', detail: `would refresh ${relative(repoPath, hookPath)} (dry-run)` };
writeFileSync(hookPath, script); chmodSync(hookPath, 0o755);
diff --git a/test/brain-repo-durability.serial.test.ts b/test/brain-repo-durability.serial.test.ts
index bb9fbeae41..0c5ceb8eb9 100644
--- a/test/brain-repo-durability.serial.test.ts
+++ b/test/brain-repo-durability.serial.test.ts
@@ -242,6 +242,50 @@ describe('hardenBrainRepo', () => {
expect(readFileSync(log, 'utf-8')).toBe('small\n');
expect(statSync(log).mode & 0o077).toBe(0);
});
+
+ test('#5436 — core.hooksPath=.githooks: hook is excluded via info/exclude and stays untracked', async () => {
+ git(work, 'config', 'core.hooksPath', '.githooks');
+ await harden({ verify: false });
+ const hookPath = join(work, '.githooks', 'post-commit');
+ expect(existsSync(hookPath)).toBe(true);
+ const exclude = readFileSync(join(work, '.git', 'info', 'exclude'), 'utf-8');
+ expect(exclude).toContain('.githooks/post-commit');
+ // Ignored → the hook never shows up as untracked in git status.
+ const st = git(work, 'status', '--porcelain', '--untracked-files=all', '--', '.githooks/post-commit');
+ expect(st).toBe('');
+ });
+
+ test('#5436 — re-harden repairs the exclusion for a hook installed before the fix', async () => {
+ git(work, 'config', 'core.hooksPath', '.githooks');
+ await harden({ verify: false });
+ const exclude = join(work, '.git', 'info', 'exclude');
+ writeFileSync(exclude, readFileSync(exclude, 'utf-8').replace('.githooks/post-commit\n', ''));
+ expect(git(work, 'status', '--porcelain', '--untracked-files=all', '--', '.githooks/post-commit')).toBe('?? .githooks/post-commit');
+ await harden({ verify: false });
+ expect(readFileSync(exclude, 'utf-8').split('\n').filter(line => line === '.githooks/post-commit')).toHaveLength(1);
+ expect(git(work, 'status', '--porcelain', '--untracked-files=all', '--', '.githooks/post-commit')).toBe('');
+ });
+
+ test('#5436 — a hooks dir outside the working tree gets no exclude entry', async () => {
+ const outside = join(root, 'shared-hooks');
+ git(work, 'config', 'core.hooksPath', outside);
+ await harden({ verify: false });
+ expect(existsSync(join(outside, 'post-commit'))).toBe(true);
+ expect(readFileSync(join(work, '.git', 'info', 'exclude'), 'utf-8')).not.toContain('post-commit');
+ });
+
+ test('#5436 — a hooksPath whose repo path contains ".git" (site.github.io) is still excluded', async () => {
+ const weird = join(root, 'site.github.io', 'repo');
+ execFileSync('git', ['-c', 'protocol.file.allow=always', 'clone', '-q', bare, weird], { stdio: 'ignore' });
+ git(weird, 'config', 'user.email', '[email protected]');
+ git(weird, 'config', 'user.name', 'tester');
+ git(weird, 'config', 'core.hooksPath', 'myhooks');
+ await hardenBrainRepo({ repoPath: weird, sourceId: 'wiki', pat: PAT, installCron: false, verify: false });
+ const exclude = readFileSync(join(weird, '.git', 'info', 'exclude'), 'utf-8');
+ expect(exclude).toContain('myhooks/post-commit');
+ const st = git(weird, 'status', '--porcelain', '--untracked-files=all', '--', 'myhooks/post-commit');
+ expect(st).toBe('');
+ });
});
describe('unhardenBrainRepo', () => {