# diff-review Review Claude Code file edits in your preferred diff tool before they are applied. Intercepts Edit and Write tool calls, constructs original/proposed temp files, and opens a configurable diff viewer. ## Features - Configurable diff tool with template tokens (defaults to `nvim -d +{line} {path1} {path2}`; works with meld, kdiff3, `code --diff --wait`, vimdiff, helix, etc.) - Jumps to the first changed line via a `{line}` token in the command - Context block: extracts Claude's reasoning from the transcript and shows it inline in the diff (top, bottom, or both), so you see _why_ the change is being made - Smart decision mode: vim-family tools use exit code (`:qa` to accept, `:cq` to reject), GUI tools prompt for confirmation - Smart display mode: auto-detects tmux/zellij and opens terminal tools in a popup overlay - Opens diffs in the file's directory so you can navigate sibling files and the project tree - XDG-compliant JSON config file with environment variable overrides - Mid-session toggle via a sentinel file (no restart needed) - Secure temp file handling (random names, mode 0600, automatic cleanup) ## Quick start ```bash # Install the plugin /plugin install diff-review@jcmuller-plugins # Seed a config file (optional, defaults work out of the box) ~/.claude/plugins/diff-review/scripts/diff-review.sh --init ``` ## Accept and reject How you accept or reject depends on your diff tool: | Tool | Accept | Reject | | :-------------------- | :--------------- | :----------------------------------- | | nvim / vim / vimdiff | `:qa` (exit 0) | `:cq` (exit 1) | | helix | `:q` (exit 0) | `:q!` with error | | meld, kdiff3, VS Code | Close the window | Claude Code prompts for confirmation | The plugin auto-detects which tools support exit-code rejection and falls back to a confirmation prompt for the rest. ## Configuration ### Config file Location: `${XDG_CONFIG_HOME:-~/.config}/claude-diff-review/config.json` ```json { "enabled": true, "command": "nvim -d +{line} {path1} {path2}", "context": "bottom", "decision": "auto", "display": "auto", "exclude": [] } ``` All fields are optional. Run `--init` to create a config file with defaults. ### Command template tokens The `command` field takes placeholder tokens so you can shape it around your editor. Drop every path token and the two paths get appended at the end, which is what `"nvim -d"` already does. | Token | Expands to | | :------------------ | :-------------------------------- | | `{line}` | 1-based line number of the change | | `{path1}` | left (read-only) pane path | | `{path1-with-line}` | `{path1}:{line}` | | `{path2}` | right (editable) pane path | | `{path2-with-line}` | `{path2}:{line}` | `{line}` is the first line where the `Edit` tool's `old_string` matches. For `Write`, new files, or no match, it is `1`. Examples: ```sh nvim -d +{line} {path1} {path2} # default: jump to that line in both panes nvim -d {path1-with-line} {path2} # editors that parse file:line on argv code --diff --wait --goto {path2-with-line} {path1} ``` ### Environment variables Environment variables override the config file. | Variable | Default | Description | | :--------------------- | :-------------------------------- | :------------------------------------------------------------------------------------------------ | | `CLAUDE_DIFF_CMD` | `nvim -d +{line} {path1} {path2}` | Diff command (supports token substitution) | | `CLAUDE_DIFF_CONFIG` | (XDG default) | Alternate config file path | | `CLAUDE_DIFF_CONTEXT` | `bottom` | Placement: `top`, `bottom`, `both`, `none`. Also accepts `true`/`1` (top) and `false`/`0` (none). | | `CLAUDE_DIFF_DECISION` | `auto` | Decision mode: `auto`, `ask`, `exit-code` | | `CLAUDE_DIFF_DISPLAY` | `auto` | Display strategy: `auto`, `direct`, `tmux-popup`, `tmux-split`, `zellij-float` | | `CLAUDE_DIFF_ENABLED` | `1` | Set to `0` to disable the hook | ### Decision modes | Mode | Behavior | | :---------- | :-------------------------------------------------------------------------------- | | `auto` | Detects tool: `exit-code` for vim-family/helix/kakoune, `ask` for everything else | | `ask` | Always prompts for confirmation in Claude Code after closing the diff tool | | `exit-code` | Diff tool exit 0 = allow, non-zero = deny | ### Display strategies All strategies set the working directory to the edited file's parent directory, so you can navigate sibling files and the project tree from inside the diff tool. | Strategy | Behavior | | :------------- | :------------------------------------------------------------------------- | | `auto` | tmux popup if `$TMUX` set, else zellij float if `$ZELLIJ` set, else direct | | `direct` | Run command directly (blocks terminal or opens GUI window) | | `tmux-popup` | `tmux display-popup -E -w 90% -h 90%` | | `tmux-split` | `tmux split-window -h` | | `zellij-float` | `zellij run --floating --width=80% --height=80% --close-on-exit` | ### Context The plugin grabs Claude's last transcript message, wraps it in sentinel-bracketed comment lines, and writes that block into the file under review. Both panes of the diff get the same bytes so the gutter stays lined up. When the diff tool exits, the block is stripped back out of the real file. Comment prefix depends on extension: `//` for Go/JS/TS/Rust, `#` for Python/Ruby/Shell/Nix/Markdown, `--` for Lua/SQL, and so on. In tmux-popup mode the same text doubles as the popup border title. Set placement with `context` in the config or `CLAUDE_DIFF_CONTEXT`: | Value | Behavior | | :------- | :-------------------------------------------------- | | `bottom` | (default) Append the block to the end of both panes | | `top` | Prepend; cursor-jump line shifts to compensate | | `both` | Prepend and append | | `none` | Skip context entirely | Legacy booleans still work: `"context": true` maps to `top`, `false` to `none`. The env var accepts the same values as strings (`1`, `true`, `0`, `false`). ### Exclude Anything matching a pattern in `exclude` skips the hook outright. The diff tool does not open, the edit goes through as-is. Handy for lockfiles, generated code, or files you would rather not review every time. ```json { "exclude": ["/home/you/repo/vendor/*", "*/generated/*.go"] } ``` Matching uses Go's `filepath.Match` against the full path. `*` does not cross a path separator and there is no `**`. For deeper matches, stack segments (`*/*/*.pb.go`). Plan files under `~/.claude/plans/` are excluded automatically, regardless of what is in this list. ### Mid-session toggle Disable or re-enable the hook during a live Claude Code session: ```bash # Disable touch ~/.config/claude-diff-review/disabled # Re-enable rm ~/.config/claude-diff-review/disabled ``` ## Defense in depth The hook modifies the real file on disk during the review session (so your editor sees native syntax highlighting and LSP integration). Several layers prevent context-block markers from leaking into committed files if the cleanup path fails: 1. Primary strip: `StripContextBlockTop` / `StripContextBlock` remove the sentinel pair after the diff tool exits. 2. Scrub backstop: `ScrubSentinels` runs after the strip step and catches any markers the strip missed (e.g. if an external process mutated the sentinel lines during the review session). 3. Emit sanitizer: `emitUpdatedInput` scrubs `updated_input.content` and `updated_input.new_string` before writing JSON to stdout, so Claude Code never receives marker bytes even if the on-disk cleanup failed. 4. Deferred restore: a `defer` guard in `run()` restores the original file (or deletes new files) on every exit path after the proposed content is applied. Covers crashes, timeouts, and early-return errors. 5. jj private-commits revset: configure `git.private-commits` to block pushes of commits whose added lines contain the begin marker. This is the safest layer since it lives outside the hook entirely. ### jj private-commits setup ```sh jj config set --user 'git.private-commits' \ 'diff_lines_added(substring:"")' ``` `jj git push` will refuse any commit whose diff adds the begin marker. Recovery: delete the markers from the file, commit normally, push. `diff_lines_added` only matches the `+` side, so the cleanup commit is not flagged. ## Dependencies | Binary | Purpose | Required? | | :-------- | :----------------------------------- | :--------------------------- | | `jq` | JSON parsing and string substitution | Yes | | Diff tool | Configured via `CLAUDE_DIFF_CMD` | Yes (default: `nvim`) | | `tmux` | Popup/split display | Only if using tmux display | | `zellij` | Floating pane display | Only if using zellij display | ## Installation ```bash /plugin install diff-review@jcmuller-plugins ```