mirror of
https://github.com/luongnv89/claude-howto.git
synced 2026-04-21 21:45:58 +02:00
2cbb10c959
* fix(hooks): make hook scripts compatible with Windows Git Bash and use stdin JSON protocol - Replace `grep -P` (Perl regex) with `sed` for JSON field extraction, as Windows Git Bash does not support `grep -P` - Replace positional arg (`$1`) with stdin JSON parsing to match the actual Claude Code hook protocol (hooks receive data via stdin, not args) - Fix JSON double-quote escaping in grep patterns that silently fails on Windows Git Bash - Remove python3 dependency for JSON parsing, making scripts portable - Add Windows Git Bash to compatibility notes in script headers Affected scripts: security-scan.sh, validate-prompt.sh, log-bash.sh, format-code.sh Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(hooks): address review issues in Windows Git Bash compatibility scripts - security-scan.sh: wrap output in hookSpecificOutput format required by PostToolUse protocol; restore secret/token detection pattern that was dropped; escape file path and issues for safe JSON construction - validate-prompt.sh: extract "user_prompt" field first (Claude Code UserPromptSubmit protocol), falling back to "prompt" - log-bash.sh: document sed truncation limitation for commands with double-quoted strings - format-code.sh: fix misleading comment about updatedInput output * fix(hooks): produce valid JSON from security-scan.sh - Use \\n (JSON newline escape) when building ISSUES, not real newlines, so the value passes safely through printf into the JSON string - Remove the real-newline-to-\\n sed pass (no longer needed) - Output is now verifiably valid JSON per python3 json.loads * fix(hooks): fix grep -E POSIX compat and semgrep/trufflehog stdout mixing - Replace \s with [[:space:]] in grep -E patterns (security-scan.sh lines 34,44): \s is a Perl extension, silently fails on macOS BSD grep and BusyBox — password/secret detection was a no-op on macOS - Suppress stdout of semgrep/trufflehog (>/dev/null) to prevent their output mixing with hookSpecificOutput JSON, which would produce invalid JSON and cause the additionalContext to fail parsing - Fix format-code.sh hook comment: PreToolUse → PostToolUse (matches README example and actual hook behavior) --------- Co-authored-by: Bruce <binyuli1993@foxmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.2 KiB
Bash
50 lines
1.2 KiB
Bash
#!/bin/bash
|
|
# Auto-format code after writing
|
|
# Hook: PostToolUse:Write
|
|
#
|
|
# Reads the target file path from stdin JSON and runs the appropriate formatter
|
|
# in-place on the file after Claude writes it.
|
|
#
|
|
# Compatible with: macOS, Linux, Windows (Git Bash)
|
|
|
|
# Read JSON input from stdin (Claude Code hook protocol)
|
|
INPUT=$(cat)
|
|
|
|
# Extract file_path using sed (compatible with all platforms)
|
|
FILE_PATH=$(echo "$INPUT" | sed -n 's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)
|
|
|
|
if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
# Detect file type and format accordingly
|
|
case "$FILE_PATH" in
|
|
*.js|*.jsx|*.ts|*.tsx)
|
|
if command -v prettier &> /dev/null; then
|
|
prettier --write "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.py)
|
|
if command -v black &> /dev/null; then
|
|
black "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.go)
|
|
if command -v gofmt &> /dev/null; then
|
|
gofmt -w "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.rs)
|
|
if command -v rustfmt &> /dev/null; then
|
|
rustfmt "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.java)
|
|
if command -v google-java-format &> /dev/null; then
|
|
google-java-format -i "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
exit 0
|