|
| 1 | +# Claude Code Hooks for Codegraph |
| 2 | + |
| 3 | +Ready-to-use [Claude Code hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) that keep the codegraph database fresh and provide automatic dependency context as Claude edits your codebase. |
| 4 | + |
| 5 | +## Quick setup |
| 6 | + |
| 7 | +```bash |
| 8 | +# 1. Copy hooks into your project |
| 9 | +mkdir -p .claude/hooks |
| 10 | +cp docs/examples/claude-code-hooks/*.sh .claude/hooks/ |
| 11 | +chmod +x .claude/hooks/*.sh |
| 12 | + |
| 13 | +# 2. Copy settings (or merge into your existing .claude/settings.json) |
| 14 | +cp docs/examples/claude-code-hooks/settings.json .claude/settings.json |
| 15 | + |
| 16 | +# 3. Add session logs to .gitignore |
| 17 | +echo ".claude/session-edits.log" >> .gitignore |
| 18 | +echo ".claude/codegraph-checked.log" >> .gitignore |
| 19 | +``` |
| 20 | + |
| 21 | +## Hooks |
| 22 | + |
| 23 | +### Core hooks (recommended for all projects) |
| 24 | + |
| 25 | +| Hook | Trigger | What it does | |
| 26 | +|------|---------|-------------| |
| 27 | +| `enrich-context.sh` | PreToolUse on Read/Grep | Injects `codegraph deps` output (imports, importers, definitions) into Claude's context when it reads a file | |
| 28 | +| `remind-codegraph.sh` | PreToolUse on Edit/Write | Reminds Claude to run `codegraph where`, `explain`, `context`, and `fn-impact` before editing a file. Fires once per file per session | |
| 29 | +| `update-graph.sh` | PostToolUse on Edit/Write | Runs `codegraph build` incrementally after each source file edit to keep the graph fresh | |
| 30 | +| `post-git-ops.sh` | PostToolUse on Bash | Detects `git rebase/revert/cherry-pick/merge/pull` and rebuilds the graph, logs changed files, and resets the remind tracker | |
| 31 | + |
| 32 | +### Parallel session safety hooks (recommended for multi-agent workflows) |
| 33 | + |
| 34 | +| Hook | Trigger | What it does | |
| 35 | +|------|---------|-------------| |
| 36 | +| `guard-git.sh` | PreToolUse on Bash | Blocks `git add .`, `git reset`, `git restore`, `git clean`, `git stash`; validates commits only include files the session actually edited | |
| 37 | +| `track-edits.sh` | PostToolUse on Edit/Write | Logs every file edited via Edit/Write to `.claude/session-edits.log` | |
| 38 | +| `track-moves.sh` | PostToolUse on Bash | Logs files affected by `mv`/`git mv`/`cp` commands to `.claude/session-edits.log` | |
| 39 | + |
| 40 | +## Git operation resilience |
| 41 | + |
| 42 | +Git operations like `rebase`, `revert`, `cherry-pick`, `merge`, and `pull` change file contents without going through Edit/Write tools. Without `post-git-ops.sh`, this causes three problems: |
| 43 | + |
| 44 | +1. **Stale graph** — `enrich-context.sh` provides outdated dependency info |
| 45 | +2. **Blocked commits** — `guard-git.sh` rejects commits with rebase-modified files not in the edit log |
| 46 | +3. **Stale reminders** — `remind-codegraph.sh` won't re-fire for files changed by the git operation |
| 47 | + |
| 48 | +`post-git-ops.sh` fixes all three by detecting these git commands after they run and: |
| 49 | +- Rebuilding the codegraph (`codegraph build`) |
| 50 | +- Appending changed files (via `git diff --name-only ORIG_HEAD HEAD`) to the session edit log |
| 51 | +- Removing changed files from the remind tracker so the agent re-checks context |
| 52 | + |
| 53 | +## Worktree isolation |
| 54 | + |
| 55 | +All session-local state files (`session-edits.log`, `codegraph-checked.log`) use `git rev-parse --show-toplevel` to resolve the working tree root, rather than `CLAUDE_PROJECT_DIR`. This ensures each git worktree gets its own isolated state — session A's edit log doesn't leak into session B's commit validation. |
| 56 | + |
| 57 | +Without this fix, `CLAUDE_PROJECT_DIR` (which always points to the main project root) causes all worktree sessions to share a single edit log, defeating the parallel session safety model. |
| 58 | + |
| 59 | +## Customization |
| 60 | + |
| 61 | +**Subset installation:** You don't need all hooks. The core hooks work independently of the parallel session hooks. Pick what fits your workflow: |
| 62 | + |
| 63 | +- **Solo developer:** `enrich-context.sh` + `update-graph.sh` + `post-git-ops.sh` |
| 64 | +- **With reminders:** Add `remind-codegraph.sh` |
| 65 | +- **Multi-agent / worktrees:** Add `guard-git.sh` + `track-edits.sh` + `track-moves.sh` |
| 66 | + |
| 67 | +**Branch name validation:** The `guard-git.sh` in this repo's `.claude/hooks/` validates branch names against conventional prefixes (`feat/`, `fix/`, etc.). The example version omits this — add your own validation if needed. |
| 68 | + |
| 69 | +## Requirements |
| 70 | + |
| 71 | +- Node.js >= 20 |
| 72 | +- `codegraph` installed globally or available via `npx` |
| 73 | +- Graph built at least once (`codegraph build`) |
0 commit comments