#!/usr/bin/env bash # PostToolUse / Bash — after a successful `git worktree add`, stamp the new worktree with # this session's id (.claude-worktree-owner) so pretooluse-worktree-guard.sh (H7) can tell # a sibling worktree another session created apart from this session's own. # Fail-safe: any parse trouble → do nothing (the guard stays fail-open without a marker). set -euo pipefail input=$(cat) cmd=$(printf '%s' "$input" | jq -r '.tool_input.command // ""' 2>/dev/null || true) cwd=$(printf '%s' "$input" | jq -r '.cwd // ""' 2>/dev/null || true) me=$(printf '%s' "$input" | jq -r '.session_id // ""' 2>/dev/null || true) printf '%s' "$cmd" | grep -qE 'git[[:space:]]+worktree[[:space:]]+add\b' || exit 0 [ -z "$me" ] && exit 0 [ -z "$cwd" ] && cwd="$PWD" # Extract the arg of `git worktree add [flags] []`. # Skip flags; skip the values of the value-taking flags (-b/-B/--reason). Worktree paths # in this repo have no spaces, so whitespace tokenization is safe. add_args=$(printf '%s' "$cmd" | sed -E 's/.*git[[:space:]]+worktree[[:space:]]+add[[:space:]]+//') path="" skip=0 for tok in $add_args; do if [ "$skip" = 1 ]; then skip=0; continue; fi case "$tok" in -b|-B|--reason) skip=1; continue ;; --) continue ;; -*) continue ;; *) path=$(printf '%s' "$tok" | tr -d '"'"'"''); break ;; esac done [ -z "$path" ] && exit 0 case "$path" in /*) abs="$path" ;; *) abs="$cwd/$path" ;; esac [ -d "$abs" ] || exit 0 # Don't clobber a marker a different session already planted. [ -f "$abs/.claude-worktree-owner" ] && exit 0 printf '%s\n' "$me" > "$abs/.claude-worktree-owner" 2>/dev/null || true exit 0