|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Install Git hooks for this repository. |
| 4 | +# This script creates a symbolic link from .git/hooks/pre-commit to scripts/pre-commit.sh |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 10 | +HOOK_SOURCE="$REPO_ROOT/scripts/pre-commit.sh" |
| 11 | +HOOK_TARGET="$REPO_ROOT/.git/hooks/pre-commit" |
| 12 | + |
| 13 | +echo "Installing Git hooks..." |
| 14 | + |
| 15 | +# Check if .git directory exists |
| 16 | +if [ ! -d "$REPO_ROOT/.git" ]; then |
| 17 | + echo "Error: .git directory not found. Are you in a Git repository?" |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# Check if source script exists |
| 22 | +if [ ! -f "$HOOK_SOURCE" ]; then |
| 23 | + echo "Error: Pre-commit script not found at $HOOK_SOURCE" |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Create hooks directory if it doesn't exist |
| 28 | +mkdir -p "$REPO_ROOT/.git/hooks" |
| 29 | + |
| 30 | +# Remove existing hook if present |
| 31 | +if [ -e "$HOOK_TARGET" ]; then |
| 32 | + echo "Removing existing pre-commit hook..." |
| 33 | + rm "$HOOK_TARGET" |
| 34 | +fi |
| 35 | + |
| 36 | +# Create symbolic link to pre-commit script |
| 37 | +echo "Creating symbolic link to scripts/pre-commit.sh..." |
| 38 | +ln -s "$HOOK_SOURCE" "$HOOK_TARGET" |
| 39 | + |
| 40 | +# Verify the hook is executable |
| 41 | +if [ ! -x "$HOOK_SOURCE" ]; then |
| 42 | + echo "Warning: Making scripts/pre-commit.sh executable..." |
| 43 | + chmod +x "$HOOK_SOURCE" |
| 44 | +fi |
| 45 | + |
| 46 | +echo "" |
| 47 | +echo "✓ Git hooks installed successfully" |
| 48 | +echo "" |
| 49 | +echo "The pre-commit hook is now linked to ./scripts/pre-commit.sh" |
| 50 | +echo "Any changes to scripts/pre-commit.sh will automatically affect the Git hook." |
0 commit comments