1
0

pre-commit 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/sh
  2. # Pre-commit hook: runs Ruff on staged Python files and frontend tests on staged TS/TSX files
  3. #
  4. # Install: cp scripts/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
  5. set -e
  6. # Colors
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[0;33m'
  10. NC='\033[0m'
  11. # --- Ruff lint on staged Python files ---
  12. STAGED_PY=$(git diff --cached --name-only --diff-filter=ACM -- '*.py')
  13. if [ -n "$STAGED_PY" ]; then
  14. printf "${YELLOW}Running Ruff on staged Python files...${NC}\n"
  15. if command -v ruff >/dev/null 2>&1; then
  16. if ! echo "$STAGED_PY" | xargs ruff check; then
  17. printf "${RED}Ruff found issues. Fix them or run 'ruff check --fix' then re-stage.${NC}\n"
  18. exit 1
  19. fi
  20. printf "${GREEN}Ruff passed.${NC}\n"
  21. else
  22. printf "${YELLOW}Ruff not installed, skipping Python lint. Install with: pip install ruff${NC}\n"
  23. fi
  24. fi
  25. # --- Frontend tests on staged TS/TSX files ---
  26. STAGED_FE=$(git diff --cached --name-only --diff-filter=ACM -- 'frontend/src/**/*.ts' 'frontend/src/**/*.tsx')
  27. if [ -n "$STAGED_FE" ]; then
  28. printf "${YELLOW}Running frontend tests...${NC}\n"
  29. if [ -d "frontend/node_modules" ]; then
  30. if ! (cd frontend && npx vitest run --reporter=dot 2>&1); then
  31. printf "${RED}Frontend tests failed. Fix them before committing.${NC}\n"
  32. exit 1
  33. fi
  34. printf "${GREEN}Frontend tests passed.${NC}\n"
  35. else
  36. printf "${YELLOW}frontend/node_modules not found, skipping tests. Run: cd frontend && npm install${NC}\n"
  37. fi
  38. fi