#!/bin/bash # Pre-commit hook: validates staged .sh and .ts files # Install: git config core.hooksPath .githooks set -eo pipefail RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' errors=0 # ─── Shell script validation ────────────────────────────────────────── staged_sh=$(git diff --cached --name-only --diff-filter=ACM | grep '\.sh$' || true) if [[ -n "$staged_sh" ]]; then echo "Validating staged shell scripts..." for file in $staged_sh; do # 1. Syntax check if ! bash -n "$file" 2>/dev/null; then printf "${RED}FAIL${NC} %s: syntax error\n" "$file" bash -n "$file" 2>&1 | head -3 errors=$((errors + 1)) continue fi # 2. No relative source (breaks curl|bash) if grep -qn 'source \.\./' "$file" 2>/dev/null || grep -qn 'source \./' "$file" 2>/dev/null; then printf "${RED}FAIL${NC} %s: relative source path (breaks curl|bash)\n" "$file" grep -n 'source \.\.' "$file" 2>/dev/null || true errors=$((errors + 1)) fi # 3. No echo -e (breaks macOS bash 3.x) if grep -qn 'echo -e ' "$file" 2>/dev/null; then printf "${RED}FAIL${NC} %s: echo -e (use printf for macOS compat)\n" "$file" errors=$((errors + 1)) fi # 4. No set -u / set -euo (breaks env var checks) if grep -qn 'set -euo' "$file" 2>/dev/null; then printf "${RED}FAIL${NC} %s: set -euo pipefail (drop the 'u', use set -eo pipefail)\n" "$file" errors=$((errors + 1)) fi done if [[ $errors -gt 0 ]]; then echo "" printf "${RED}%d error(s) found. Commit blocked.${NC}\n" "$errors" echo "Fix the issues above and try again." exit 1 fi printf "${GREEN}All %d scripts passed validation.${NC}\n" "$(echo "$staged_sh" | wc -w | tr -d ' ')" fi # ─── Biome lint for TypeScript ───────────────────────────────────────── staged_ts=$(git diff --cached --name-only --diff-filter=ACM | grep '^packages/cli/src/.*\.ts$' || true) if [[ -n "$staged_ts" ]]; then echo "Running Biome lint on staged TypeScript files..." if ! (cd packages/cli && bunx @biomejs/biome lint src/); then echo "" printf "${RED}Biome lint failed. Commit blocked.${NC}\n" echo "Run 'cd packages/cli && bunx @biomejs/biome lint src/' to see details." exit 1 fi printf "${GREEN}Biome lint passed.${NC}\n" fi