From 7de011492737e27ef75daa0a2505626bc548b958 Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Fri, 13 Mar 2026 20:17:15 +0200 Subject: [PATCH] feat: add pre-commit and pre-push git hooks Pre-commit runs go vet, golangci-lint (optional), and fast tests. Pre-push runs full test suite and build verification. Installable via `make hooks`. Co-Authored-By: Claude Opus 4.6 --- Makefile | 10 +++++++++- scripts/hooks/pre-commit | 31 +++++++++++++++++++++++++++++++ scripts/hooks/pre-push | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 scripts/hooks/pre-commit create mode 100755 scripts/hooks/pre-push diff --git a/Makefile b/Makefile index b62ffe7..2adaa97 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build test dev web clean lint +.PHONY: build test dev web clean lint hooks BINARY := synapbus MODULE := github.com/smart-mcp-proxy/synapbus @@ -29,4 +29,12 @@ clean: lint: golangci-lint run ./... +hooks: + @echo "Installing git hooks..." + @mkdir -p scripts/hooks + @chmod +x scripts/hooks/pre-commit scripts/hooks/pre-push + @ln -sf ../../scripts/hooks/pre-commit .git/hooks/pre-commit + @ln -sf ../../scripts/hooks/pre-push .git/hooks/pre-push + @echo "✅ Git hooks installed" + .DEFAULT_GOAL := build diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit new file mode 100755 index 0000000..e85af4b --- /dev/null +++ b/scripts/hooks/pre-commit @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Pre-commit hook: fast quality checks on staged Go files +set -e + +echo "🔍 Pre-commit checks..." + +# Check if any Go files are staged +STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true) + +if [ -z "$STAGED_GO_FILES" ]; then + echo "✅ No Go files staged, skipping checks" + exit 0 +fi + +# 1. go vet +echo " Running go vet..." +CGO_ENABLED=0 go vet ./... || { echo "❌ go vet failed"; exit 1; } + +# 2. golangci-lint (skip if not installed) +if command -v golangci-lint &> /dev/null; then + echo " Running golangci-lint..." + golangci-lint run --new-from-rev=HEAD~1 ./... || { echo "❌ golangci-lint failed"; exit 1; } +else + echo " ⚠️ golangci-lint not found, skipping (install: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest)" +fi + +# 3. Fast unit tests (short mode) +echo " Running fast tests..." +CGO_ENABLED=0 go test -short -count=1 ./... || { echo "❌ tests failed"; exit 1; } + +echo "✅ All pre-commit checks passed" diff --git a/scripts/hooks/pre-push b/scripts/hooks/pre-push new file mode 100755 index 0000000..2754468 --- /dev/null +++ b/scripts/hooks/pre-push @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Pre-push hook: full validation before pushing +set -e + +echo "🔍 Pre-push checks..." + +# 1. Full test suite +echo " Running full test suite..." +CGO_ENABLED=0 go test -count=1 -timeout 120s ./... || { echo "❌ tests failed"; exit 1; } + +# 2. Build verification +echo " Verifying build..." +CGO_ENABLED=0 go build -o /dev/null ./cmd/synapbus/ || { echo "❌ build failed"; exit 1; } + +echo "✅ All pre-push checks passed"