-rw-r--r-- claude-context/hook.sh
1 #!/bin/bash 2 input=$(cat) 3 tool_name=$(echo "$input" | jq -r '.tool_name // empty') 4 5 suffix="" 6 [ -n "$CLAUDE_CONTEXT_ID" ] && suffix=".$CLAUDE_CONTEXT_ID" 7 base="$HOME/.local/share/claude" 8 mkdir -p "$base" 9 wt_file="${base}/worktree${suffix}" 10 11 if [ "$tool_name" = "ExitWorktree" ]; then 12 rm -f "$wt_file" 13 exit 0 14 fi 15 16 if [ "$tool_name" = "EnterWorktree" ]; then 17 cwd=$(echo "$input" | jq -r '.cwd // empty') 18 [ -n "$cwd" ] && [ -d "$cwd" ] && echo "$cwd" > "$wt_file" 19 exit 0 20 fi 21 22 check_worktree() { 23 local dir="$1" 24 [ -z "$dir" ] && return 25 local toplevel 26 toplevel=$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null) 27 if [ -n "$toplevel" ] && [ -f "$toplevel/.git" ]; then 28 echo "$toplevel" > "$wt_file" 29 fi 30 } 31 32 if [ "$tool_name" = "Bash" ]; then 33 cwd=$(echo "$input" | jq -r '.cwd // empty') 34 check_worktree "$cwd" 35 exit 0 36 fi 37 38 file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty') 39 [ -z "$file_path" ] && exit 0 40 [ -f "$file_path" ] || exit 0 41 42 context_file="${base}/context-files${suffix}" 43 tmp="${context_file}.$$" 44 if [ -f "$context_file" ]; then 45 grep -v -F -x "$file_path" "$context_file" > "$tmp" 2>/dev/null || : > "$tmp" 46 else 47 : > "$tmp" 48 fi 49 echo "$file_path" >> "$tmp" 50 tail -50 "$tmp" > "$context_file" 51 rm -f "$tmp" 52 53 check_worktree "$(dirname "$file_path")" 54 55 exit 0 56