1#!/bin/bash
2
3# Install git hooks for the Teal project
4# This script sets up pre-commit hooks for code formatting and linting
5
6set -e
7
8# Colors for output
9RED='\033[0;31m'
10GREEN='\033[0;32m'
11YELLOW='\033[1;33m'
12BLUE='\033[0;34m'
13NC='\033[0m' # No Color
14
15print_status() {
16 echo -e "${BLUE}[INFO]${NC} $1"
17}
18
19print_success() {
20 echo -e "${GREEN}[SUCCESS]${NC} $1"
21}
22
23print_error() {
24 echo -e "${RED}[ERROR]${NC} $1"
25}
26
27print_warning() {
28 echo -e "${YELLOW}[WARNING]${NC} $1"
29}
30
31# Check if we're in a git repository
32if [ ! -d ".git" ]; then
33 print_error "This script must be run from the root of a git repository"
34 exit 1
35fi
36
37print_status "Installing git hooks for Teal project..."
38
39# Create hooks directory if it doesn't exist
40mkdir -p .git/hooks
41
42# Install pre-commit hook
43if [ -f "scripts/pre-commit-hook.sh" ]; then
44 print_status "Installing pre-commit hook..."
45 cp scripts/pre-commit-hook.sh .git/hooks/pre-commit
46 chmod +x .git/hooks/pre-commit
47 print_success "Pre-commit hook installed"
48else
49 print_error "Pre-commit hook script not found at scripts/pre-commit-hook.sh"
50 exit 1
51fi
52
53# Optional: Install other hooks
54# You can add more hooks here if needed
55
56print_status "Testing hook installation..."
57
58# Test if the hook is executable
59if [ -x ".git/hooks/pre-commit" ]; then
60 print_success "Pre-commit hook is executable"
61else
62 print_error "Pre-commit hook is not executable"
63 exit 1
64fi
65
66# Check if required tools are available
67print_status "Checking required tools..."
68
69MISSING_TOOLS=""
70
71if ! command -v pnpm >/dev/null 2>&1; then
72 MISSING_TOOLS="$MISSING_TOOLS pnpm"
73fi
74
75if ! command -v node >/dev/null 2>&1; then
76 MISSING_TOOLS="$MISSING_TOOLS node"
77fi
78
79if ! command -v cargo >/dev/null 2>&1; then
80 MISSING_TOOLS="$MISSING_TOOLS cargo"
81fi
82
83if [ -n "$MISSING_TOOLS" ]; then
84 print_warning "Some tools are missing:$MISSING_TOOLS"
85 print_warning "The git hooks may not work properly without these tools"
86else
87 print_success "All required tools are available"
88fi
89
90print_success "Git hooks installation complete! 🎉"
91print_status "The following hooks have been installed:"
92echo " - pre-commit: Runs formatting and linting checks before commits"
93
94print_status "To test the pre-commit hook, try making a commit with staged files"
95print_status "To temporarily skip hooks, use: git commit --no-verify"
96
97# Optional: Show hook status
98echo ""
99print_status "Installed hooks:"
100ls -la .git/hooks/ | grep -v sample | grep -v "^d" | sed 's/^/ /'