]> git.ekhem.eu.org Git - guix.git/commitdiff
[ai] Fix block-worktrees responses and path checks.
authorJakub Czajka <jakub@ekhem.eu.org>
Sun, 22 Nov 2026 21:03:32 +0000 (21:03 +0000)
committerJakub Czajka <jakub@ekhem.eu.org>
Sun, 22 Nov 2026 21:03:32 +0000 (21:03 +0000)
Resolve git from PATH and return early outside a worktree, so a
missing worktree cannot block unrelated tools.  Derive the project
root from git-common-dir, add hookEventName to hook responses, use
updatedInput for ExitWorktree, and match project-root writes only at
a path boundary.

.claude/hooks/block-worktrees

index 6d75e05db2a72dc1e6b1ddb17abd43b2e3741460..94f53cacee5c079d252247a9805e0088e7af7177 100755 (executable)
 # Silently converts ExitWorktree remove→keep so worktrees are
 # never deleted.
 
+# Only active inside a worktree.
+case "$PWD" in
+    */worktrees/*) ;;
+    *) exit 0 ;;
+esac
+
 # Pre-flight: critical environment variables.
 if [ -z "$GUIX_BIN" ]; then
     echo '{"systemMessage":"❌ block-worktrees: GUIX_BIN not set"}' >&2
     exit 0
 fi
 if [ -z "$CLAUDE_CODE_SESSION_ID" ]; then
-    echo '{"systemMessage":"❌ block-worktrees: CLAUDE_CODE_SESSION_ID not set"}' >&2
+    echo '{"systemMessage":' \
+         '"❌ block-worktrees: CLAUDE_CODE_SESSION_ID not set"}' >&2
     exit 0
 fi
 
 # Pre-flight: commands referenced by this hook.
 if [ ! -x "$GUIX_BIN/sed" ]; then
-    echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \
-         '"permissionDecisionReason":"block-worktrees: GUIX_BIN/sed missing",' \
-         '"systemMessage":"❌ block-worktrees broken: GUIX_BIN/sed not found"}}'
+    echo '{"systemMessage":' \
+         '"❌ block-worktrees broken: GUIX_BIN/sed not found",' \
+         '"hookSpecificOutput":{' \
+         '"hookEventName":"PreToolUse",' \
+         '"permissionDecision":"deny",' \
+         '"permissionDecisionReason":' \
+         '"block-worktrees: GUIX_BIN/sed missing"}}'
     exit 0
 fi
 if [ ! -x "$GUIX_BIN/echo" ]; then
-    echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \
-         '"permissionDecisionReason":"block-worktrees: GUIX_BIN/echo missing",' \
-         '"systemMessage":"❌ block-worktrees broken: GUIX_BIN/echo not found"}}'
+    echo '{"systemMessage":' \
+         '"❌ block-worktrees broken: GUIX_BIN/echo not found",' \
+         '"hookSpecificOutput":{' \
+         '"hookEventName":"PreToolUse",' \
+         '"permissionDecision":"deny",' \
+         '"permissionDecisionReason":' \
+         '"block-worktrees: GUIX_BIN/echo missing"}}'
     exit 0
 fi
-GIT_BIN="$HOME/.guix-profile/bin/git"
-if [ ! -x "$GIT_BIN" ]; then
-    echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \
-         '"permissionDecisionReason":"block-worktrees: git missing",' \
-         '"systemMessage":"❌ block-worktrees broken: git not found"}}'
-    exit 0
-fi
-
-# Only active inside a worktree.
-if ! echo "$PWD" \
-    | $GUIX_BIN/sed --quiet '\|/worktrees/|q0;$q1'
-then
+GIT_BIN=$(command -v git)
+if [ -z "$GIT_BIN" ] || [ ! -x "$GIT_BIN" ]; then
+    echo '{"systemMessage":' \
+         '"❌ block-worktrees broken: git not found",' \
+         '"hookSpecificOutput":{' \
+         '"hookEventName":"PreToolUse",' \
+         '"permissionDecision":"deny",' \
+         '"permissionDecisionReason":' \
+         '"block-worktrees: git missing"}}'
     exit 0
 fi
 
@@ -52,13 +64,17 @@ stdin=$(cat)
 tool=$(echo "$stdin" \
     | $GUIX_BIN/sed -n \
         's/.*"tool_name"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p')
-proj_root=$($GIT_BIN -C "$PWD" rev-parse --show-toplevel 2>/dev/null)
-[ -z "$proj_root" ] && exit 0
+common_dir=$($GIT_BIN -C "$PWD" rev-parse --path-format=absolute \
+    --git-common-dir 2>/dev/null)
+[ -z "$common_dir" ] && exit 0
+proj_root=${common_dir%/.git}
+[ "$proj_root" = "$common_dir" ] && exit 0
 worktree="$proj_root/.claude/worktrees/$CLAUDE_CODE_SESSION_ID"
 
 die() {
-    echo \
-        '{"hookSpecificOutput":{"permissionDecision":"deny",' \
+    echo '{"hookSpecificOutput":' \
+        '{"hookEventName":"PreToolUse",' \
+        '"permissionDecision":"deny",' \
         '"permissionDecisionReason":"'"$1"'"}}'
     exit 0
 }
@@ -72,10 +88,10 @@ ExitWorktree)
         | $GUIX_BIN/sed -n \
             's/.*"action"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p')
     if [ "$action" = "remove" ]; then
-        echo \
-            '{"hookSpecificOutput":{' \
+        echo '{"hookSpecificOutput":' \
+            '{"hookEventName":"PreToolUse",' \
             '"permissionDecision":"allow",' \
-            '"modifiedInput":{"action":"keep"}}}'
+            '"updatedInput":{"action":"keep"}}}'
         exit 0
     fi ;;
 
@@ -84,7 +100,7 @@ Write|Edit)
         | $GUIX_BIN/sed -n \
             's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p')
     case "$path" in
-        "$proj_root"*)
+        "$proj_root"|"$proj_root"/*)
             if ! echo "$path" \
                 | $GUIX_BIN/sed --quiet "\|$worktree|q0;\$q1"
             then