From 86a2021c92c4a386904cb5308d383af3dceb1f3b Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Sat, 9 May 2026 16:12:58 +0300 Subject: [PATCH] chore(githooks): Add pre-commit hook allowing two staged paths Ship a tracked hook that rejects more than two staged paths so commits stay small while still allowing tightly coupled pairs. - Activate with: git config core.hooksPath .githooks (from repo root) --- .githooks/pre-commit | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..9e45bbc --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,32 @@ +#!/bin/bash +# Pre-commit hook: prefer small commits; allow at most two staged paths. +# Enable with: git config core.hooksPath .githooks + +staged_files=$(git diff --cached --name-only) +file_count=$(echo "$staged_files" | grep -v '^$' | wc -l | tr -d ' ') + +if [ "$file_count" -gt 2 ]; then + echo "❌ COMMIT PROTOCOL VIOLATION: Attempting to commit $file_count files (maximum 2)" + echo "" + echo "Prefer tiny commits; combining more than two paths needs splitting or a hook bypass." + echo "" + echo "Staged files:" + echo "$staged_files" + echo "" + echo "See .github/prompts/commit.prompt.md" + exit 1 +fi + +if [ "$file_count" -eq 2 ]; then + echo "✅ Committing two files:" + echo "$staged_files" + exit 0 +fi + +if [ "$file_count" -eq 1 ]; then + echo "✅ Committing single file: $(echo "$staged_files" | head -1)" + exit 0 +fi + +echo "⚠️ No files staged for commit" +exit 1