#!/usr/bin/env bash # Install git hooks for the project set -e HOOKS_DIR=".git/hooks" SCRIPTS_DIR="scripts" echo "Installing git hooks..." echo "" # Create hooks directory if it doesn't exist mkdir -p "$HOOKS_DIR" # Pre-commit hook cat > "$HOOKS_DIR/pre-commit" << 'EOF' #!/usr/bin/env bash # Git pre-commit hook - runs linting before commit echo "๐Ÿ” Running pre-commit linting..." echo "" # Try to use nix run if available, otherwise use script directly if command -v nix &> /dev/null && [ -f "flake.nix" ]; then # Use nix run for consistent environment if ! nix run .#lint; then echo "" echo "โŒ Linting failed. Fix errors or use --no-verify to skip." echo " Auto-fix: nix run .#lint-fix" exit 1 fi else # Fallback to script if ! ./scripts/lint.sh; then echo "" echo "โŒ Linting failed. Fix errors or use --no-verify to skip." echo " Auto-fix: ./scripts/lint.sh --fix" exit 1 fi fi echo "" echo "โœ… Pre-commit checks passed!" EOF chmod +x "$HOOKS_DIR/pre-commit" echo "โœ“ Installed pre-commit hook" # Pre-push hook (DISABLED by default - enable when tests are ready) cat > "$HOOKS_DIR/pre-push.disabled" << 'EOF' #!/usr/bin/env bash # Git pre-push hook - runs tests before push # # TO ENABLE: Rename this file to 'pre-push' (remove .disabled) # TO DISABLE: Rename back to 'pre-push.disabled' # OR use: git push --no-verify echo "๐Ÿงช Running tests before push..." echo "" # Backend tests (if pytest is available) if [ -d "backend" ] && command -v pytest &> /dev/null; then cd backend # Run tests without coverage requirement for pre-push if ! pytest -xvs --tb=short --no-cov; then echo "" echo "โŒ Backend tests failed. Fix tests or use --no-verify to skip." exit 1 fi cd .. fi # Frontend tests (if npm test is available) if [ -d "frontend/node_modules" ]; then cd frontend if ! npm test -- --run; then echo "" echo "โŒ Frontend tests failed. Fix tests or use --no-verify to skip." exit 1 fi cd .. fi echo "" echo "โœ… All tests passed!" EOF chmod +x "$HOOKS_DIR/pre-push.disabled" echo "โœ“ Created pre-push hook template (disabled by default)" echo " To enable: mv .git/hooks/pre-push.disabled .git/hooks/pre-push" echo "" echo "=========================================" echo "โœ… Git hooks installed successfully!" echo "=========================================" echo "" echo "Hooks installed:" echo " โ€ข pre-commit - Runs linting before commit" echo " โ€ข pre-push - Runs tests before push (optional)" echo "" echo "To skip hooks when committing:" echo " git commit --no-verify" echo "" echo "To uninstall:" echo " rm .git/hooks/pre-commit .git/hooks/pre-push" echo ""