#!/bin/bash # # Pre-commit hook for MEV Bot V2 # Ensures code quality and consistency before commits # # Install: ln -sf ../../.git-hooks/pre-commit .git/hooks/pre-commit # set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}๐Ÿ” Running pre-commit checks...${NC}" echo "" # ============================================================================== # 1. CHECK BRANCH NAME # ============================================================================== BRANCH_NAME=$(git branch --show-current) if [[ "$BRANCH_NAME" == "feature/v2-prep" ]] || [[ "$BRANCH_NAME" == "master" ]]; then echo -e "${YELLOW}โš ๏ธ Warning: Committing to protected branch: $BRANCH_NAME${NC}" echo -e "${YELLOW} Consider creating a feature branch instead${NC}" echo "" fi if [[ "$BRANCH_NAME" =~ ^feature/v2/ ]]; then # Validate branch naming convention if [[ ! "$BRANCH_NAME" =~ ^feature/v2/[a-z0-9-]+/[A-Z0-9]+-[0-9]+-[a-z0-9-]+$ ]]; then echo -e "${YELLOW}โš ๏ธ Branch name doesn't follow convention:${NC}" echo -e "${YELLOW} feature/v2//-${NC}" echo "" fi fi # ============================================================================== # 2. CHECK FOR MERGE CONFLICTS # ============================================================================== echo -e "${GREEN}๐Ÿ“‹ Checking for merge conflicts...${NC}" if git diff --cached --name-only | xargs grep -l "^<<<<<<< HEAD" 2>/dev/null; then echo -e "${RED}โŒ Merge conflict markers found${NC}" echo -e "${RED} Resolve conflicts before committing${NC}" exit 1 fi echo -e "${GREEN}โœ… No merge conflicts${NC}" echo "" # ============================================================================== # 3. CHECK FOR FORBIDDEN PATTERNS # ============================================================================== echo -e "${GREEN}๐Ÿ”’ Checking for secrets and forbidden patterns...${NC}" # Check for common secret patterns if git diff --cached --name-only -z | xargs -0 grep -E "password|secret|api[_-]?key|private[_-]?key|token" --include="*.go" 2>/dev/null | grep -v "test" | grep -v "example"; then echo -e "${RED}โŒ Potential secrets found${NC}" echo -e "${RED} Remove secrets before committing${NC}" exit 1 fi # Check for debugging statements if git diff --cached --name-only -z | xargs -0 grep -E "fmt\.Println|log\.Println|panic\(|TODO.*URGENT|FIXME.*CRITICAL" --include="*.go" 2>/dev/null; then echo -e "${YELLOW}โš ๏ธ Warning: Found debugging statements or urgent TODOs${NC}" echo -e "${YELLOW} Consider removing or creating issues for them${NC}" echo "" fi echo -e "${GREEN}โœ… No forbidden patterns found${NC}" echo "" # ============================================================================== # 4. GO MOD TIDY CHECK # ============================================================================== if [ -f "go.mod" ]; then echo -e "${GREEN}๐Ÿ“ฆ Checking if go.mod is tidy...${NC}" # Save current go.mod and go.sum cp go.mod go.mod.backup cp go.sum go.sum.backup # Run go mod tidy go mod tidy # Check if anything changed if ! diff -q go.mod go.mod.backup > /dev/null 2>&1 || ! diff -q go.sum go.sum.backup > /dev/null 2>&1; then echo -e "${YELLOW}โš ๏ธ go.mod or go.sum was not tidy${NC}" echo -e "${YELLOW} Auto-fixed and staged${NC}" git add go.mod go.sum fi # Clean up backups rm -f go.mod.backup go.sum.backup echo -e "${GREEN}โœ… Dependencies are tidy${NC}" echo "" fi # ============================================================================== # 5. CODE FORMATTING # ============================================================================== echo -e "${GREEN}๐ŸŽจ Checking code formatting...${NC}" # Get list of staged Go files STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.go$" || true) if [ -n "$STAGED_GO_FILES" ]; then # Check formatting UNFORMATTED=$(gofmt -l $STAGED_GO_FILES 2>/dev/null || true) if [ -n "$UNFORMATTED" ]; then echo -e "${YELLOW}โš ๏ธ Auto-formatting files:${NC}" echo "$UNFORMATTED" # Auto-format files echo "$UNFORMATTED" | xargs gofmt -w -s # Re-stage formatted files echo "$UNFORMATTED" | xargs git add echo -e "${GREEN}โœ… Code formatted and re-staged${NC}" else echo -e "${GREEN}โœ… All files properly formatted${NC}" fi else echo -e "${GREEN}โ„น๏ธ No Go files to format${NC}" fi echo "" # ============================================================================== # 6. RUN TESTS ON CHANGED FILES # ============================================================================== if [ -n "$STAGED_GO_FILES" ]; then echo -e "${GREEN}๐Ÿงช Running tests on changed packages...${NC}" # Get unique package directories PACKAGES=$(echo "$STAGED_GO_FILES" | xargs -n1 dirname | sort -u | sed 's/$/\/.../') # Run tests with timeout if ! go test -short -timeout=2m $PACKAGES 2>&1; then echo -e "${RED}โŒ Tests failed${NC}" echo -e "${RED} Fix tests before committing${NC}" exit 1 fi echo -e "${GREEN}โœ… Tests passed${NC}" else echo -e "${GREEN}โ„น๏ธ No Go files changed, skipping tests${NC}" fi echo "" # ============================================================================== # 7. RUN GO VET # ============================================================================== if [ -n "$STAGED_GO_FILES" ]; then echo -e "${GREEN}๐Ÿ” Running go vet...${NC}" if ! go vet ./... 2>&1; then echo -e "${RED}โŒ go vet found issues${NC}" echo -e "${RED} Fix issues before committing${NC}" exit 1 fi echo -e "${GREEN}โœ… go vet passed${NC}" fi echo "" # ============================================================================== # 8. CHECK FILE SIZES # ============================================================================== echo -e "${GREEN}๐Ÿ“ Checking file sizes...${NC}" LARGE_FILES=$(git diff --cached --name-only | xargs -I {} sh -c 'if [ -f "{}" ]; then stat -f%z "{}" 2>/dev/null || stat -c%s "{}" 2>/dev/null; fi' | awk '$1 > 1048576 {print}' || true) if [ -n "$LARGE_FILES" ]; then echo -e "${YELLOW}โš ๏ธ Warning: Large files detected (>1MB)${NC}" echo -e "${YELLOW} Consider if these should be committed${NC}" git diff --cached --name-only | while read file; do size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null) if [ "$size" -gt 1048576 ]; then size_mb=$(echo "scale=2; $size / 1048576" | bc) echo -e "${YELLOW} $file: ${size_mb}MB${NC}" fi done echo "" fi echo -e "${GREEN}โœ… File size check complete${NC}" echo "" # ============================================================================== # 9. FINAL SUMMARY # ============================================================================== echo -e "${GREEN}โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—${NC}" echo -e "${GREEN}โ•‘ โœ… PRE-COMMIT CHECKS PASSED โœ… โ•‘${NC}" echo -e "${GREEN}โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•${NC}" echo "" echo -e "${GREEN}Proceeding with commit...${NC}" exit 0