#!/bin/bash # Installation script for Git hooks in Hastebin # This script sets up pre-commit hooks to prevent pushing broken code set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Get the repository root REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)") HOOKS_DIR="$REPO_ROOT/.git/hooks" PRE_COMMIT_HOOK="$HOOKS_DIR/pre-commit" SCRIPT_DIR="$REPO_ROOT/scripts" echo -e "${BLUE}🔧 Installing Git hooks for Hastebin${NC}" echo "" # Check if we're in a git repository if [ ! -d "$REPO_ROOT/.git" ]; then echo -e "${RED}❌ Error: Not a git repository${NC}" exit 1 fi # Create hooks directory if it doesn't exist mkdir -p "$HOOKS_DIR" # Check if pre-commit hook already exists if [ -f "$PRE_COMMIT_HOOK" ]; then echo -e "${YELLOW}⚠️ Pre-commit hook already exists${NC}" read -p "Do you want to overwrite it? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo -e "${YELLOW}Skipping hook installation${NC}" exit 0 fi # Backup existing hook mv "$PRE_COMMIT_HOOK" "$PRE_COMMIT_HOOK.backup.$(date +%Y%m%d_%H%M%S)" echo -e "${YELLOW}Backed up existing hook${NC}" fi # Create the pre-commit hook cat > "$PRE_COMMIT_HOOK" << 'HOOK_EOF' #!/bin/bash # Git pre-commit hook for Hastebin # Runs tests before allowing commits to prevent pushing broken code set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # No Color echo -e "${YELLOW}Running pre-commit checks...${NC}" # Get the repository root REPO_ROOT=$(git rev-parse --show-toplevel) cd "$REPO_ROOT" # Check if node_modules exists, if not, install dependencies if [ ! -d "node_modules" ]; then echo -e "${YELLOW}Installing dependencies...${NC}" npm ci fi # Run the core tests (faster than full test suite) echo -e "${YELLOW}Running core tests...${NC}" if npm run test:core; then echo -e "${GREEN}✅ Core tests passed${NC}" else echo -e "${RED}❌ Core tests failed. Commit aborted.${NC}" echo -e "${YELLOW}To skip this check, use: git commit --no-verify${NC}" exit 1 fi # Clean up test artifacts if [ -d "test-data" ]; then rm -rf test-data fi echo -e "${GREEN}✅ Pre-commit checks passed${NC}" exit 0 HOOK_EOF # Make the hook executable chmod +x "$PRE_COMMIT_HOOK" echo -e "${GREEN}✅ Pre-commit hook installed successfully${NC}" echo "" echo -e "${BLUE}The hook will now run tests before each commit.${NC}" echo -e "${YELLOW}To skip the hook, use: git commit --no-verify${NC}" echo "" # Check if dependencies are installed if [ ! -d "$REPO_ROOT/node_modules" ]; then echo -e "${YELLOW}⚠️ Dependencies not found. Installing...${NC}" cd "$REPO_ROOT" npm ci echo -e "${GREEN}✅ Dependencies installed${NC}" fi echo -e "${GREEN}🎉 Git hooks installation complete!${NC}"