// Installs the git hooks CLAUDE.md §5.2 relies on. Runs from `pnpm install` via // the root `prepare` script, so a fresh clone is guarded before its first commit. // Adapted from Ludus, including its worktree fix: in a worktree `.git` is a FILE // pointing at the real gitdir, and hooks resolve per worktree. import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, statSync } from 'node:fs'; import { dirname, isAbsolute, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const gitPath = join(root, '.git'); if (!existsSync(gitPath)) { console.log('[hooks] no .git — skipping hook install.'); process.exit(0); } function resolveGitDir(path) { if (statSync(path).isDirectory()) return path; const target = /^gitdir:\s*(.+)$/.exec(readFileSync(path, 'utf8').trim())?.[1]?.trim(); if (target === undefined) { console.log(`[hooks] ${path} is a file but not a gitdir pointer — skipping hook install.`); process.exit(0); } return isAbsolute(target) ? target : resolve(root, target); } const hooksDir = join(resolveGitDir(gitPath), 'hooks'); mkdirSync(hooksDir, { recursive: true }); for (const hook of ['pre-commit', 'commit-msg']) { const dest = join(hooksDir, hook); copyFileSync(join(root, 'scripts', hook), dest); chmodSync(dest, 0o755); console.log(`[hooks] installed ${hook}`); }