Skip to content

Commit 253fd3a

Browse files
committed
Add git hooks for automated code quality enforcement
Implement pre-commit and pre-push hooks to ensure code quality standards are maintained. Pre-commit runs formatting, static analysis, and build verification. Pre-push adds security scanning and test execution. Includes make targets for manual execution and hooks management.
1 parent b3ac510 commit 253fd3a

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

.githooks/pre-commit

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
# Pre-commit hook - Runs on git commit
3+
# Ensures code quality before allowing commits
4+
5+
set -e
6+
7+
echo "🔍 Running pre-commit checks..."
8+
9+
# Run go fmt to check formatting
10+
echo "→ Checking code formatting (go fmt)..."
11+
unformatted=$(gofmt -l .)
12+
if [ -n "$unformatted" ]; then
13+
echo "❌ The following files are not properly formatted:"
14+
echo "$unformatted"
15+
echo ""
16+
echo "Please run 'go fmt ./...' to format your code."
17+
exit 1
18+
fi
19+
echo "✅ Code formatting check passed"
20+
21+
# Run go vet for static analysis
22+
echo "→ Running static analysis (go vet)..."
23+
if ! go vet ./...; then
24+
echo "❌ Static analysis failed. Please fix the issues reported by 'go vet'."
25+
exit 1
26+
fi
27+
echo "✅ Static analysis passed"
28+
29+
# Verify the project builds
30+
echo "→ Verifying build (make build)..."
31+
if ! make build > /dev/null 2>&1; then
32+
echo "❌ Build failed. Please ensure your code compiles before committing."
33+
echo "Run 'make build' to see the full error output."
34+
exit 1
35+
fi
36+
echo "✅ Build verification passed"
37+
38+
echo "✨ All pre-commit checks passed! Proceeding with commit..."

.githooks/pre-push

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
# Pre-push hook - Runs on git push
3+
# Adds security scanning and test verification before pushing to remote
4+
5+
set -e
6+
7+
echo "🚀 Running pre-push checks..."
8+
9+
# Run gosec security scanner (if installed)
10+
if command -v gosec &> /dev/null; then
11+
echo "→ Running security scan (gosec)..."
12+
if ! gosec -quiet ./...; then
13+
echo "❌ Security vulnerabilities detected by gosec."
14+
echo "Please fix the security issues before pushing."
15+
exit 1
16+
fi
17+
echo "✅ Security scan passed"
18+
else
19+
echo "⚠️ gosec not installed. Skipping security scan."
20+
echo " Install with: go install github.com/securego/gosec/v2/cmd/gosec@latest"
21+
fi
22+
23+
# Run trivy vulnerability scanner (if installed)
24+
if command -v trivy &> /dev/null; then
25+
echo "→ Running vulnerability scan (trivy)..."
26+
if ! trivy fs . --scanners vuln --quiet; then
27+
echo "❌ Vulnerabilities detected by trivy."
28+
echo "Please fix the vulnerabilities before pushing."
29+
exit 1
30+
fi
31+
echo "✅ Vulnerability scan passed"
32+
else
33+
echo "⚠️ trivy not installed. Skipping vulnerability scan."
34+
echo " Install with: brew install trivy (macOS) or see https://github.com/aquasecurity/trivy"
35+
fi
36+
37+
# Run all tests
38+
echo "→ Running tests (go test)..."
39+
if ! go test ./... > /dev/null 2>&1; then
40+
echo "❌ Tests failed. Please ensure all tests pass before pushing."
41+
echo "Run 'go test ./...' to see the full test output."
42+
exit 1
43+
fi
44+
echo "✅ All tests passed"
45+
46+
echo "✨ All pre-push checks passed! Pushing to remote..."

Makefile

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ COVERAGE_HTML=coverage.html
1818
# Default target - show help
1919
.DEFAULT_GOAL := help
2020

21-
.PHONY: all help version build package clean test test-unit test-clean test-verbose test-race test-integration test-integration-verbose test-nats test-vault test-all test-diskfull integration integration-nats integration-vault integration-syslog integration-debug integration-diskfull bench bench-full fmt vet security gosec trivy coverage coverage-text deploy deps check ci build-examples run-examples install-tools
21+
.PHONY: all help version build package clean test test-unit test-clean test-verbose test-race test-integration test-integration-verbose test-nats test-vault test-all test-diskfull integration integration-nats integration-vault integration-syslog integration-debug integration-diskfull bench bench-full fmt vet security gosec trivy coverage coverage-text deploy deps check ci build-examples run-examples install-tools hooks-install hooks-uninstall hooks-check pre-commit pre-push
2222

2323
# Show help with version
2424
help:
@@ -59,6 +59,8 @@ help:
5959
@printf " \033[36m%-25s\033[0m %s\n" "trivy" "Run trivy vulnerability scanner"
6060
@printf " \033[36m%-25s\033[0m %s\n" "coverage" "Generate HTML coverage report"
6161
@printf " \033[36m%-25s\033[0m %s\n" "coverage-text" "Show coverage summary in terminal"
62+
@printf " \033[36m%-25s\033[0m %s\n" "pre-commit" "Run all pre-commit checks (fmt, vet, build)"
63+
@printf " \033[36m%-25s\033[0m %s\n" "pre-push" "Run all pre-push checks (gosec, trivy, test)"
6264
@echo ""
6365
@echo "Performance:"
6466
@printf " \033[36m%-25s\033[0m %s\n" "bench" "Run benchmarks"
@@ -73,6 +75,11 @@ help:
7375
@echo "Examples:"
7476
@printf " \033[36m%-25s\033[0m %s\n" "run-examples" "Run all example applications"
7577
@echo ""
78+
@echo "Git Hooks:"
79+
@printf " \033[36m%-25s\033[0m %s\n" "hooks-install" "Install git hooks for code quality"
80+
@printf " \033[36m%-25s\033[0m %s\n" "hooks-uninstall" "Remove git hooks"
81+
@printf " \033[36m%-25s\033[0m %s\n" "hooks-check" "Show current hooks configuration"
82+
@echo ""
7683
@echo "Version: $(VERSION)"
7784

7885
# Show version
@@ -242,6 +249,16 @@ trivy:
242249
security: gosec trivy
243250
@echo "All security checks completed!"
244251

252+
# Pre-commit checks - Run all checks required before committing
253+
pre-commit: fmt vet build
254+
@echo ""
255+
@echo "✅ All pre-commit checks passed!"
256+
257+
# Pre-push checks - Run all checks required before pushing
258+
pre-push: gosec trivy test
259+
@echo ""
260+
@echo "✅ All pre-push checks passed!"
261+
245262
# Coverage targets
246263
coverage:
247264
@echo "Generating coverage report..."
@@ -300,3 +317,52 @@ install-tools:
300317
@$(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
301318
@$(GOCMD) install github.com/securego/gosec/v2/cmd/gosec@latest
302319
@$(GOCMD) install golang.org/x/tools/cmd/goimports@latest
320+
321+
# Git hooks management
322+
hooks-install:
323+
@echo "Installing git hooks..."
324+
@if [ -d .git ]; then \
325+
git config core.hooksPath .githooks; \
326+
echo "✅ Git hooks installed successfully!"; \
327+
echo " Pre-commit: go fmt, go vet, make build"; \
328+
echo " Pre-push: gosec, trivy, go test"; \
329+
else \
330+
echo "❌ Not a git repository. Please run this command from the project root."; \
331+
exit 1; \
332+
fi
333+
334+
hooks-uninstall:
335+
@echo "Uninstalling git hooks..."
336+
@if [ -d .git ]; then \
337+
git config --unset core.hooksPath; \
338+
echo "✅ Git hooks uninstalled successfully!"; \
339+
else \
340+
echo "❌ Not a git repository. Please run this command from the project root."; \
341+
exit 1; \
342+
fi
343+
344+
hooks-check:
345+
@echo "Checking git hooks configuration..."
346+
@if [ -d .git ]; then \
347+
hooks_path=$$(git config core.hooksPath); \
348+
if [ -n "$$hooks_path" ]; then \
349+
echo "✅ Git hooks are active"; \
350+
echo " Hooks path: $$hooks_path"; \
351+
if [ -f "$$hooks_path/pre-commit" ]; then \
352+
echo " Pre-commit hook: Installed"; \
353+
else \
354+
echo " Pre-commit hook: Not found"; \
355+
fi; \
356+
if [ -f "$$hooks_path/pre-push" ]; then \
357+
echo " Pre-push hook: Installed"; \
358+
else \
359+
echo " Pre-push hook: Not found"; \
360+
fi; \
361+
else \
362+
echo "❌ Git hooks are not active"; \
363+
echo " Run 'make hooks-install' to activate them"; \
364+
fi; \
365+
else \
366+
echo "❌ Not a git repository. Please run this command from the project root."; \
367+
exit 1; \
368+
fi

examples/disk-full-handling/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestDiskFullHandlingExample(t *testing.T) {
1313
testhelpers.SkipIfUnit(t)
14-
14+
1515
// Create a temporary directory for testing
1616
tempDir := t.TempDir()
1717
logPath := filepath.Join(tempDir, "test-diskfull.log")
@@ -49,7 +49,7 @@ func TestDiskFullHandlingExample(t *testing.T) {
4949

5050
func TestDiskFullRecovery(t *testing.T) {
5151
testhelpers.SkipIfUnit(t)
52-
52+
5353
// This test verifies that the logger continues to work even with limited disk space
5454
tempDir := t.TempDir()
5555
logPath := filepath.Join(tempDir, "recovery-test.log")

0 commit comments

Comments
 (0)