diff --git a/hooks/pre-bash-checks.sh b/hooks/pre-bash-checks.sh index 329cf0b..21e0efb 100755 --- a/hooks/pre-bash-checks.sh +++ b/hooks/pre-bash-checks.sh @@ -5,6 +5,12 @@ cmd=$(jq -r '.tool_input.command // ""') VALID_TYPES="feat|fix|docs|refactor|test|chore|perf|ci|build|style" +deny() { + jq -cn --arg reason "$1" \ + '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$reason}}' + exit 0 +} + extract_subject() { local cmd="$1" if echo "$cmd" | grep -qE "cat\s+<<"; then @@ -22,8 +28,7 @@ if echo "$cmd" | grep -qE '^\s*git\s+commit\b'; then branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "") repo_root=$(git rev-parse --show-toplevel 2>/dev/null || echo "") if [ "$branch" = "main" ] && [ ! -f "${repo_root}/.commit-on-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 + deny "BLOCKIERT: Direkter Commit auf main verboten (globale Regel). Bitte zuerst Feature-Branch erstellen: git checkout -b session/YYYY-MM-DD-titel" fi # Commit-Message: Conventional Commits Format pruefen @@ -31,26 +36,22 @@ if echo "$cmd" | grep -qE '^\s*git\s+commit\b'; then 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: : . Erlaubte Typen: feat, fix, docs, refactor, test, chore, perf, ci, build, style"}\n' - exit 0 + deny "BLOCKIERT: Commit-Message folgt nicht dem Conventional-Commits-Format. Erwartet: : . Erlaubte Typen: feat, fix, docs, refactor, test, chore, perf, ci, build, style" fi if [ ${#subject} -gt 72 ]; then - printf '{"continue":false,"stopReason":"BLOCKIERT: Commit-Message Subject zu lang (%d Zeichen, max 72)."}\n' "${#subject}" - exit 0 + deny "BLOCKIERT: Commit-Message Subject zu lang (${#subject} Zeichen, max 72)." 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 + deny "BLOCKIERT: Commit-Message Subject darf nicht mit einem Punkt enden." fi fi fi # apt -y verboten if echo "$cmd" | grep -qE '\bapt(-get)?\b' && echo "$cmd" | grep -qE '\bapt(-get)?\b.*\s-y\b'; then - printf '{"continue":false,"stopReason":"BLOCKIERT: apt -y ist verboten. Bitte ohne -y ausfuehren, damit du die Zusammenfassung selbst bestaetigen kannst."}\n' - exit 0 + deny "BLOCKIERT: apt -y ist verboten. Bitte ohne -y ausfuehren, damit du die Zusammenfassung selbst bestaetigen kannst." fi exit 0