#!/bin/bash # Installation script for Git hooks in Hastebin # This script sets up pre-commit hooks (tests) and pre-push hooks (unused code scans) 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}" # --- Pre-push hook --- PRE_PUSH_HOOK="$HOOKS_DIR/pre-push" # Check if pre-push hook already exists if [ -f "$PRE_PUSH_HOOK" ]; then echo -e "${YELLOW}⚠️ Pre-push 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 pre-push hook installation${NC}" else mv "$PRE_PUSH_HOOK" "$PRE_PUSH_HOOK.backup.$(date +%Y%m%d_%H%M%S)" echo -e "${YELLOW}Backed up existing pre-push hook${NC}" fi fi if [ ! -f "$PRE_PUSH_HOOK" ] || [[ $REPLY =~ ^[Yy]$ ]]; then cat > "$PRE_PUSH_HOOK" << 'HOOK_EOF' #!/bin/bash # Git pre-push hook for Hastebin # Scans for unused code/dependencies before pushing 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-push 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 # Scan for unused code/dependencies SCAN_FAILED=0 echo -e "${YELLOW}Scanning for unused files/exports/dependencies (knip)...${NC}" if npx --yes knip 2>/dev/null; then echo -e "${GREEN}✅ knip passed${NC}" else echo -e "${RED}❌ knip found unused code${NC}" SCAN_FAILED=1 fi echo -e "${YELLOW}Scanning for unused npm dependencies (depcheck)...${NC}" if npx --yes depcheck 2>/dev/null; then echo -e "${GREEN}✅ depcheck passed${NC}" else echo -e "${RED}❌ depcheck found issues${NC}" SCAN_FAILED=1 fi if [ $SCAN_FAILED -ne 0 ]; then echo -e "${RED}❌ Unused code/dependencies detected. Push aborted.${NC}" echo -e "${YELLOW}To skip this check, use: git push --no-verify${NC}" exit 1 fi echo -e "${GREEN}✅ Pre-push checks passed${NC}" exit 0 HOOK_EOF chmod +x "$PRE_PUSH_HOOK" echo -e "${GREEN}✅ Pre-push hook installed successfully${NC}" fi echo "" echo -e "${BLUE}Hooks installed:${NC}" echo -e "${BLUE} - pre-commit: runs core tests before each commit${NC}" echo -e "${BLUE} - pre-push: scans for unused code/deps before each push${NC}" echo -e "${YELLOW}To skip hooks, use: git commit --no-verify / git push --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}"