Free

Hooks ile git iş akışlarını yönetmek: Claude'un da kurallara uymasını sağlamak

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.


Kural 1: Main'e Doğrudan Commit'i Engelle

#!/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

Kural 2: Commit Mesajı Formatını Zorla

#!/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

Kural 3: Tehlikeli İşlemleri Engelle

#!/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

Tam Yapılandırma

.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

Özet

PreToolUse + Bash matcher + git komut tespiti. Öncelikli 3 kural: main'e commit engeli, mesaj format doğrulaması, force push ve reset --hard engeli.