feat: Conventional-Commits-Validierung im PreToolUse-Hook
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,13 +3,47 @@ set -euo pipefail
|
||||
|
||||
cmd=$(jq -r '.tool_input.command // ""')
|
||||
|
||||
# Git-Branch-Schutz: kein direkter Commit auf main
|
||||
VALID_TYPES="feat|fix|docs|refactor|test|chore|perf|ci|build|style"
|
||||
|
||||
extract_subject() {
|
||||
local cmd="$1"
|
||||
if echo "$cmd" | grep -qE "cat\s+<<"; then
|
||||
echo "$cmd" | sed -n "/cat <</{n; :loop; /^[[:space:]]*EOF/q; /^[[:space:]]*$/!{s/^[[:space:]]*//; p; q;}; n; b loop;}"
|
||||
elif echo "$cmd" | grep -qP -- '-[a-z]*m\s+"'; then
|
||||
echo "$cmd" | grep -oP -- '-[a-z]*m\s+"\K[^"]*' | head -1
|
||||
elif echo "$cmd" | grep -qP -- "-[a-z]*m\s+'"; then
|
||||
echo "$cmd" | grep -oP -- "-[a-z]*m\s+'\\K[^']*" | head -1
|
||||
fi
|
||||
}
|
||||
|
||||
# Git-Commit-Pruefungen
|
||||
if echo "$cmd" | grep -qE '^\s*git\s+commit\b'; then
|
||||
# Branch-Schutz: kein direkter Commit auf main
|
||||
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
|
||||
if [ "$branch" = "main" ]; then
|
||||
printf '{"continue":false,"stopReason":"BLOCKIERT: Direkter Commit auf main verboten (globale Regel). Bitte zuerst Feature-Branch erstellen: git checkout -b session/YYYY-MM-DD-titel"}\n'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Commit-Message: Conventional Commits Format pruefen
|
||||
subject=$(extract_subject "$cmd")
|
||||
|
||||
if [ -n "$subject" ]; then
|
||||
if ! echo "$subject" | grep -qP "^(${VALID_TYPES})(\\(.+\\))?\\!?:\\s.+"; then
|
||||
printf '{"continue":false,"stopReason":"BLOCKIERT: Commit-Message folgt nicht dem Conventional-Commits-Format. Erwartet: <type>: <Beschreibung>. Erlaubte Typen: feat, fix, docs, refactor, test, chore, perf, ci, build, style"}\n'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ ${#subject} -gt 72 ]; then
|
||||
printf '{"continue":false,"stopReason":"BLOCKIERT: Commit-Message Subject zu lang (%d Zeichen, max 72)."}\n' "${#subject}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if echo "$subject" | grep -qP '\.\s*$'; then
|
||||
printf '{"continue":false,"stopReason":"BLOCKIERT: Commit-Message Subject darf nicht mit einem Punkt enden."}\n'
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# apt -y verboten
|
||||
|
||||
Reference in New Issue
Block a user