diff --git a/plugins/diff-review/README.md b/plugins/diff-review/README.md index 4e81221..eceb233 100644 --- a/plugins/diff-review/README.md +++ b/plugins/diff-review/README.md @@ -8,6 +8,7 @@ Intercepts Edit and Write tool calls, constructs original/proposed temp files, a - Configurable diff tool (defaults to `nvim -d`, supports meld, kdiff3, `code --diff --wait`, vimdiff, helix, etc.) - 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 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) @@ -26,10 +27,10 @@ Intercepts Edit and Write tool calls, constructs original/proposed temp files, a 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 | +| 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. @@ -55,30 +56,32 @@ All fields are optional. Run `--init` to create a config file with defaults. Environment variables override the config file. -| Variable | Default | Description | -|:---------|:--------|:------------| -| `CLAUDE_DIFF_CMD` | `nvim -d` | Diff command (receives two file args) | -| `CLAUDE_DIFF_CONFIG` | (XDG default) | Alternate config file path | -| `CLAUDE_DIFF_DECISION` | `auto` | Decision mode: `auto`, `ask`, `exit-code` | -| `CLAUDE_DIFF_DISPLAY` | `auto` | Display strategy: `auto`, `direct`, `tmux-popup`, `tmux-split` | -| `CLAUDE_DIFF_ENABLED` | `1` | Set to `0` to disable the hook | +| Variable | Default | Description | +| :--------------------- | :------------ | :------------------------------------------------------------- | +| `CLAUDE_DIFF_CMD` | `nvim -d` | Diff command (receives two file args) | +| `CLAUDE_DIFF_CONFIG` | (XDG default) | Alternate config file path | +| `CLAUDE_DIFF_DECISION` | `auto` | Decision mode: `auto`, `ask`, `exit-code` | +| `CLAUDE_DIFF_DISPLAY` | `auto` | Display strategy: `auto`, `direct`, `tmux-popup`, `tmux-split` | +| `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 | +| 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 -| Strategy | Behavior | -|:---------|:---------| -| `auto` | tmux popup for terminal tools when in tmux, direct otherwise | -| `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` | +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 for terminal tools when in tmux, direct otherwise | +| `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` | ### Mid-session toggle @@ -94,11 +97,11 @@ rm ~/.config/claude-diff-review/disabled ## 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 | +| 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 | ## Installation diff --git a/plugins/diff-review/scripts/diff-review.sh b/plugins/diff-review/scripts/diff-review.sh index f60028c..efb6583 100755 --- a/plugins/diff-review/scripts/diff-review.sh +++ b/plugins/diff-review/scripts/diff-review.sh @@ -16,11 +16,11 @@ config_path="${CLAUDE_DIFF_CONFIG:-$config_dir/config.json}" # --init: seed a default config file if [[ "${1:-}" == "--init" ]]; then - if [[ -f "$config_path" ]]; then - echo "Config already exists: $config_path" - else - mkdir -p "$(dirname "$config_path")" - cat >"$config_path" <<'EOF' + if [[ -f "$config_path" ]]; then + echo "Config already exists: $config_path" + else + mkdir -p "$(dirname "$config_path")" + cat >"$config_path" <<'EOF' { "enabled": true, "command": "nvim -d", @@ -28,9 +28,9 @@ if [[ "${1:-}" == "--init" ]]; then "display": "auto" } EOF - echo "Created: $config_path" - fi - exit 0 + echo "Created: $config_path" + fi + exit 0 fi # Read config file defaults (all optional) @@ -40,10 +40,10 @@ cfg_decision="auto" cfg_display="auto" if [[ -f "$config_path" ]]; then - cfg_enabled=$(jq -r '.enabled // true' "$config_path") - cfg_cmd=$(jq -r '.command // "nvim -d"' "$config_path") - cfg_decision=$(jq -r '.decision // "auto"' "$config_path") - cfg_display=$(jq -r '.display // "auto"' "$config_path") + cfg_enabled=$(jq -r '.enabled // true' "$config_path") + cfg_cmd=$(jq -r '.command // "nvim -d"' "$config_path") + cfg_decision=$(jq -r '.decision // "auto"' "$config_path") + cfg_display=$(jq -r '.display // "auto"' "$config_path") fi # Env vars override config @@ -54,13 +54,13 @@ diff_enabled="${CLAUDE_DIFF_ENABLED:-}" # Check enabled state: env var -> config -> toggle file if [[ "$diff_enabled" == "0" ]]; then - exit 0 + exit 0 fi if [[ "$cfg_enabled" == "false" ]] && [[ -z "$diff_enabled" ]]; then - exit 0 + exit 0 fi if [[ -f "$config_dir/disabled" ]]; then - exit 0 + exit 0 fi # --- Read hook input --- @@ -70,8 +70,8 @@ tool_name=$(echo "$input" | jq -r '.tool_name') # Only handle Edit and Write case "$tool_name" in - Edit|Write) ;; - *) exit 0 ;; +Edit | Write) ;; +*) exit 0 ;; esac # --- Temp file management --- @@ -81,12 +81,12 @@ chmod 0700 "$diff_tmpdir" trap 'rm -rf "$diff_tmpdir"' EXIT make_temp() { - local ext="$1" - local label="$2" - local tmp="$diff_tmpdir/${label}${ext}" - touch "$tmp" - chmod 0600 "$tmp" - echo "$tmp" + local ext="$1" + local label="$2" + local tmp="$diff_tmpdir/${label}${ext}" + touch "$tmp" + chmod 0600 "$tmp" + echo "$tmp" } # --- Construct diff files --- @@ -94,46 +94,47 @@ make_temp() { file_path=$(echo "$input" | jq -r '.tool_input.file_path') ext="${file_path##*.}" [[ "$ext" == "$file_path" ]] && ext="" || ext=".$ext" +file_dir="$(dirname "$file_path")" if [[ "$tool_name" == "Edit" ]]; then - # Build proposed content via jq split/join - replace_all=$(echo "$input" | jq -r '.tool_input.replace_all // false') + # Build proposed content via jq split/join + replace_all=$(echo "$input" | jq -r '.tool_input.replace_all // false') - original_tmp=$(make_temp "$ext" "original") - proposed_tmp=$(make_temp "$ext" "proposed") + original_tmp=$(make_temp "$ext" "original") + proposed_tmp=$(make_temp "$ext" "proposed") - cp "$file_path" "$original_tmp" - chmod 0600 "$original_tmp" + cp "$file_path" "$original_tmp" + chmod 0600 "$original_tmp" - if [[ "$replace_all" == "true" ]]; then - echo "$input" | jq -r --rawfile orig "$file_path" ' + if [[ "$replace_all" == "true" ]]; then + echo "$input" | jq -r --rawfile orig "$file_path" ' .tool_input as $ti | $orig | split($ti.old_string) | join($ti.new_string) ' >"$proposed_tmp" - else - echo "$input" | jq -r --rawfile orig "$file_path" ' + else + echo "$input" | jq -r --rawfile orig "$file_path" ' .tool_input as $ti | ($orig | split($ti.old_string)) as $parts | if ($parts | length) <= 1 then $orig else $parts[0] + $ti.new_string + ($parts[1:] | join($ti.old_string)) end ' >"$proposed_tmp" - fi - chmod 0600 "$proposed_tmp" + fi + chmod 0600 "$proposed_tmp" elif [[ "$tool_name" == "Write" ]]; then - original_tmp=$(make_temp "$ext" "original") - proposed_tmp=$(make_temp "$ext" "proposed") - - if [[ -f "$file_path" ]]; then - cp "$file_path" "$original_tmp" - else - : >"$original_tmp" - fi - chmod 0600 "$original_tmp" - - echo "$input" | jq -r '.tool_input.content' >"$proposed_tmp" - chmod 0600 "$proposed_tmp" + original_tmp=$(make_temp "$ext" "original") + proposed_tmp=$(make_temp "$ext" "proposed") + + if [[ -f "$file_path" ]]; then + cp "$file_path" "$original_tmp" + else + : >"$original_tmp" + fi + chmod 0600 "$original_tmp" + + echo "$input" | jq -r '.tool_input.content' >"$proposed_tmp" + chmod 0600 "$proposed_tmp" fi # --- Resolve display strategy --- @@ -143,51 +144,51 @@ diff_bin="${diff_cmd%% *}" diff_bin="${diff_bin##*/}" is_terminal_tool() { - [[ "$diff_bin" =~ ^($EXIT_CODE_TOOLS)$ ]] + [[ "$diff_bin" =~ ^($EXIT_CODE_TOOLS)$ ]] } effective_display="$diff_display" if [[ "$effective_display" == "auto" ]]; then - if [[ -n "${TMUX:-}" ]] && is_terminal_tool; then - effective_display="tmux-popup" - else - effective_display="direct" - fi + if [[ -n "${TMUX:-}" ]] && is_terminal_tool; then + effective_display="tmux-popup" + else + effective_display="direct" + fi fi # --- Resolve decision mode --- effective_decision="$diff_decision" if [[ "$effective_decision" == "auto" ]]; then - if is_terminal_tool; then - effective_decision="exit-code" - else - effective_decision="ask" - fi + if is_terminal_tool; then + effective_decision="exit-code" + else + effective_decision="ask" + fi fi # --- Launch diff tool --- # Build command array -read -ra cmd_parts <<< "$diff_cmd" +read -ra cmd_parts <<<"$diff_cmd" diff_exit=0 case "$effective_display" in - tmux-popup) - tmux display-popup -E -w 90% -h 90% -- "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp" || diff_exit=$? - ;; - tmux-split) - tmux split-window -h -- "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp" || diff_exit=$? - ;; - direct|*) - "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp" || diff_exit=$? - ;; +tmux-popup) + tmux display-popup -E -w 90% -h 90% -c "$file_dir" -- "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp" || diff_exit=$? + ;; +tmux-split) + tmux split-window -h -c "$file_dir" -- "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp" || diff_exit=$? + ;; +direct | *) + (cd "$file_dir" && "${cmd_parts[@]}" "$original_tmp" "$proposed_tmp") || diff_exit=$? + ;; esac # --- Output decision --- if [[ "$effective_decision" == "ask" ]]; then - jq -n '{ + jq -n '{ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "ask", @@ -195,7 +196,7 @@ if [[ "$effective_decision" == "ask" ]]; then } }' elif [[ "$diff_exit" -eq 0 ]]; then - jq -n '{ + jq -n '{ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow", @@ -203,7 +204,7 @@ elif [[ "$diff_exit" -eq 0 ]]; then } }' else - jq -n '{ + jq -n '{ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "deny", diff --git a/plugins/diff-review/tests/diff-review.bats b/plugins/diff-review/tests/diff-review.bats index 35e2a3d..c73be61 100644 --- a/plugins/diff-review/tests/diff-review.bats +++ b/plugins/diff-review/tests/diff-review.bats @@ -429,6 +429,95 @@ MOCK rm -rf "$(dirname "$spacedir")" } +# --- CWD1: tmux-popup passes start-directory --- + +@test "tmux-popup: passes -c with the file's parent directory" { + mock_diff_tool "nvim" 0 + cat >"$MOCK_BIN/tmux" < "$MOCK_BIN/tmux_args.log" +# Skip flags to find and run the actual command +while [ \$# -gt 0 ]; do + case "\$1" in + --) shift; "\$@"; exit \$?;; + *) shift;; + esac +done +MOCK + chmod +x "$MOCK_BIN/tmux" + export CLAUDE_DIFF_CMD="nvim -d" + export CLAUDE_DIFF_DECISION="exit-code" + export CLAUDE_DIFF_DISPLAY="tmux-popup" + export TMUX="/tmp/tmux-test/default,12345,0" + run pipe_payload "$(edit_payload "$FIXTURES/sample.rb" "hello" "hi")" + [ "$status" -eq 0 ] + tmux_args=$(cat "$MOCK_BIN/tmux_args.log") + [[ "$tmux_args" == *"-c $FIXTURES"* ]] +} + +# --- CWD2: tmux-split passes start-directory --- + +@test "tmux-split: passes -c with the file's parent directory" { + mock_diff_tool "nvim" 0 + cat >"$MOCK_BIN/tmux" < "$MOCK_BIN/tmux_args.log" +while [ \$# -gt 0 ]; do + case "\$1" in + --) shift; "\$@"; exit \$?;; + *) shift;; + esac +done +MOCK + chmod +x "$MOCK_BIN/tmux" + export CLAUDE_DIFF_CMD="nvim -d" + export CLAUDE_DIFF_DECISION="exit-code" + export CLAUDE_DIFF_DISPLAY="tmux-split" + export TMUX="/tmp/tmux-test/default,12345,0" + run pipe_payload "$(edit_payload "$FIXTURES/sample.rb" "hello" "hi")" + [ "$status" -eq 0 ] + tmux_args=$(cat "$MOCK_BIN/tmux_args.log") + [[ "$tmux_args" == *"-c $FIXTURES"* ]] +} + +# --- CWD3: Paths with spaces set cwd correctly --- + +@test "direct mode: cwd is correct when path contains spaces" { + spacedir="$(mktemp -d)/dir with spaces" + mkdir -p "$spacedir" + cp "$FIXTURES/sample.rb" "$spacedir/my file.rb" + cat >"$MOCK_BIN/nvim" < "$MOCK_BIN/diff_cwd.log" +MOCK + chmod +x "$MOCK_BIN/nvim" + export CLAUDE_DIFF_CMD="nvim -d" + export CLAUDE_DIFF_DECISION="exit-code" + export CLAUDE_DIFF_DISPLAY="direct" + run pipe_payload "$(edit_payload "$spacedir/my file.rb" "hello" "hi")" + [ "$status" -eq 0 ] + cwd=$(cat "$MOCK_BIN/diff_cwd.log") + [ "$cwd" = "$spacedir" ] + rm -rf "$(dirname "$spacedir")" +} + +# --- CWD4: Direct mode sets cwd to the file's directory --- + +@test "direct mode: cwd is the file's parent directory" { + cat >"$MOCK_BIN/nvim" < "$MOCK_BIN/diff_cwd.log" +MOCK + chmod +x "$MOCK_BIN/nvim" + export CLAUDE_DIFF_CMD="nvim -d" + export CLAUDE_DIFF_DECISION="exit-code" + export CLAUDE_DIFF_DISPLAY="direct" + run pipe_payload "$(edit_payload "$FIXTURES/sample.rb" "hello" "hi")" + [ "$status" -eq 0 ] + cwd=$(cat "$MOCK_BIN/diff_cwd.log") + [ "$cwd" = "$FIXTURES" ] +} + # --- S1: --init creates config dir and default config.json --- @test "--init creates config dir and default config.json" {