1+ #! /bin/bash
2+
3+ set -e
4+
5+ echo " 🔧 Installing git hooks..."
6+
7+ # Find git repository root
8+ GIT_ROOT=$( git rev-parse --show-toplevel 2> /dev/null)
9+
10+ if [ -z " $GIT_ROOT " ]; then
11+ echo " ❌ Error: Not a git repository"
12+ exit 1
13+ fi
14+
15+ echo " 📁 Git root: $GIT_ROOT "
16+
17+ # Create hooks directory if it doesn't exist
18+ mkdir -p " $GIT_ROOT /.git/hooks"
19+
20+ # Create pre-commit hook
21+ cat > " $GIT_ROOT /.git/hooks/pre-commit" << 'HOOK_EOF '
22+ #!/bin/bash
23+
24+ echo "🔍 Running linters..."
25+
26+ echo "📝 Formatting staged Swift files..."
27+ git diff --diff-filter=d --staged --name-only | grep -e '\.swift$' | while read line; do
28+ if [[ $line == *"/Generated"* ]]; then
29+ echo "⏭️ Skipping generated file: $line"
30+ else
31+ echo "✨ Formatting: $line"
32+ mise exec swiftformat -- swiftformat "${line}"
33+ git add "$line"
34+ fi
35+ done
36+
37+ if ! mise run lint; then
38+ echo "❌ Lint failed. Please fix the issues before committing."
39+ echo "💡 Tip: Run 'mise run format' to auto-fix some issues"
40+ echo "⚠️ To skip this hook, use: git commit --no-verify"
41+ exit 1
42+ fi
43+
44+ echo "✅ All checks passed!"
45+ exit 0
46+ HOOK_EOF
47+
48+ chmod +x " $GIT_ROOT /.git/hooks/pre-commit"
49+
50+ echo " ✅ Git hooks installed successfully!"
51+ echo " 📍 Hook location: $GIT_ROOT /.git/hooks/pre-commit"
52+ echo " "
53+ echo " Pre-commit hook will:"
54+ echo " 1. Format staged Swift files (except /Generated)"
55+ echo " 2. Run mise lint"
56+ echo " "
57+ echo " To skip the hook, use: git commit --no-verify"
0 commit comments