Bash'teki git komutlarını yakalamak, main'e doğrudan commit, hatalı mesaj formatı ve tehlikeli işlemleri engellemek için PreToolUse Hooks kullanın.
Claude Code, git commit, git push ve git checkout'u doğrudan çalıştırma yetkisine sahiptir. Bu genellikle kullanışlıdır, ancak main'e doğrudan commit yapabilir, keyfi formatlarda mesaj yazabilir veya yanlış zamanda force push yapabilir. Hooks, bu işlemleri gerçekleşmeden önce engeller.
#!/bin/bash
input=$(cat); cmd=$(echo "$input" | jq -r '.tool_input.command')
echo "$cmd" | grep -qE '^git (commit|push)' || exit 0
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [[ "$current_branch" == "main" || "$current_branch" == "master" ]]; then
echo "$current_branch dalına doğrudan commit yapılamaz. Bir feature dalına geçin." >&2; exit 2
fi
#!/bin/bash
input=$(cat); cmd=$(echo "$input" | jq -r '.tool_input.command')
echo "$cmd" | grep -qE '^git commit' || exit 0
msg=$(echo "$cmd" | grep -oP '(?<=-m )["\x27].*?["\x27]' | tr -d '"'"'" || true)
[[ -z "$msg" ]] && exit 0
if ! echo "$msg" | grep -qE '^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: .{3,}'; then
echo "Commit mesajı formatı yanlış. Format: <type>(<scope>): <description>" >&2; exit 2
fi
#!/bin/bash
input=$(cat); cmd=$(echo "$input" | jq -r '.tool_input.command')
if echo "$cmd" | grep -qE 'git push.*--force|git reset --hard|git push.*--delete'; then
if ! echo "$cmd" | grep -q '# ALLOW:'; then
echo "Tehlikeli işlem engellendi. Devam etmek için # ALLOW: <neden> ekleyin." >&2; exit 2
fi
fi
.claude/hooks/git-guard.sh:
#!/bin/bash
set -e
input=$(cat); cmd=$(echo "$input" | jq -r '.tool_input.command // empty')
[[ -z "$cmd" ]] && exit 0
echo "$cmd" | grep -q '^git' || exit 0
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
if echo "$cmd" | grep -qE '^git (commit|push)'; then
if [[ "$current_branch" == "main" || "$current_branch" == "master" ]]; then
echo "$current_branch dalında commit/push engellendi. Feature dalına geçin." >&2; exit 2
fi
fi
if echo "$cmd" | grep -qE '^git commit.*-m'; then
msg=$(echo "$cmd" | grep -oP '(?<=-m )["\x27][^\x27"]*["\x27]' | tr -d '"'"'" || true)
if [[ -n "$msg" ]] && ! echo "$msg" | grep -qE '^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: .{3,}'; then
echo "Yanlış format. Format: <type>(<scope>): <description>" >&2; exit 2
fi
fi
if echo "$cmd" | grep -qE 'git push.*--force|git reset --hard|git push.*--delete'; then
if ! echo "$cmd" | grep -q '# ALLOW:'; then
echo "Tehlikeli işlem engellendi. # ALLOW: <neden> ekleyin." >&2; exit 2
fi
fi
PreToolUse + Bash matcher + git komut tespiti. Öncelikli 3 kural: main'e commit engeli, mesaj format doğrulaması, force push ve reset --hard engeli.