From 5e80139afa6a155d386a5178e3f10e3e71e887f3 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Sun, 4 Jan 2026 19:42:53 +0100 Subject: [PATCH] chore: add pre-commit hook for protected branch validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a git hook that: - Blocks commits to main/dev if validation fails - Runs `make validate` for backend changes - Runs `npm run validate` for frontend changes - Skips validation for feature branches (can run manually) To enable: git config core.hooksPath .githooks 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .githooks/pre-commit | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..3ceac20 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,61 @@ +#!/bin/bash +# Pre-commit hook to enforce validation before commits on protected branches +# Install: git config core.hooksPath .githooks + +set -e + +# Get the current branch name +BRANCH=$(git rev-parse --abbrev-ref HEAD) + +# Protected branches that require validation +PROTECTED_BRANCHES="main dev" + +# Check if we're on a protected branch +is_protected() { + for branch in $PROTECTED_BRANCHES; do + if [ "$BRANCH" = "$branch" ]; then + return 0 + fi + done + return 1 +} + +if is_protected; then + echo "🔒 Committing to protected branch '$BRANCH' - running validation..." + + # Check if we have backend changes + if git diff --cached --name-only | grep -q "^backend/"; then + echo "📦 Backend changes detected - running make validate..." + cd backend + if ! make validate; then + echo "" + echo "❌ Backend validation failed!" + echo " Please fix the issues and try again." + echo " Run 'cd backend && make validate' to see errors." + exit 1 + fi + cd .. + echo "✅ Backend validation passed!" + fi + + # Check if we have frontend changes + if git diff --cached --name-only | grep -q "^frontend/"; then + echo "🎨 Frontend changes detected - running npm run validate..." + cd frontend + if ! npm run validate 2>/dev/null; then + echo "" + echo "❌ Frontend validation failed!" + echo " Please fix the issues and try again." + echo " Run 'cd frontend && npm run validate' to see errors." + exit 1 + fi + cd .. + echo "✅ Frontend validation passed!" + fi + + echo "🎉 All validations passed! Proceeding with commit..." +else + echo "📝 Committing to feature branch '$BRANCH' - skipping validation (run manually if needed)" +fi + +exit 0