setup-pre-commit-hooks.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/bash
  2. # Colors for output
  3. RED='\033[0;31m'
  4. GREEN='\033[0;32m'
  5. YELLOW='\033[1;33m'
  6. BLUE='\033[0;34m'
  7. CYAN='\033[0;36m'
  8. NC='\033[0m' # No Color
  9. # Function to print colored log messages
  10. log_info() {
  11. echo -e "${CYAN}[INFO]${NC} $1"
  12. }
  13. log_success() {
  14. echo -e "${GREEN}[SUCCESS]${NC} $1"
  15. }
  16. log_warning() {
  17. echo -e "${YELLOW}[WARNING]${NC} $1"
  18. }
  19. log_error() {
  20. echo -e "${RED}[ERROR]${NC} $1"
  21. }
  22. # Check if we're in a git repository
  23. if [ ! -d ".git" ]; then
  24. log_error "Not in a git repository. Please run this script from the root of your git repository."
  25. exit 1
  26. fi
  27. log_info "Setting up pre-commit hooks..."
  28. # Create .git/hooks directory if it doesn't exist
  29. if [ ! -d ".git/hooks" ]; then
  30. log_info "Creating .git/hooks directory..."
  31. mkdir -p .git/hooks
  32. fi
  33. # Create the pre-commit hook file
  34. cat > .git/hooks/pre-commit << 'EOF'
  35. #!/bin/bash
  36. # Colors for output
  37. RED='\033[0;31m'
  38. GREEN='\033[0;32m'
  39. YELLOW='\033[1;33m'
  40. BLUE='\033[0;34m'
  41. CYAN='\033[0;36m'
  42. NC='\033[0m' # No Color
  43. log_info() {
  44. echo -e "${CYAN}[INFO]${NC} $1"
  45. }
  46. log_success() {
  47. echo -e "${GREEN}[SUCCESS]${NC} $1"
  48. }
  49. log_warning() {
  50. echo -e "${YELLOW}[WARNING]${NC} $1"
  51. }
  52. log_error() {
  53. echo -e "${RED}[ERROR]${NC} $1"
  54. }
  55. log_info "Running pre-commit checks..."
  56. # Check if cargo is available
  57. if ! command -v cargo &> /dev/null; then
  58. log_error "Cargo is not installed or not in PATH"
  59. exit 1
  60. fi
  61. # Run cargo fmt check
  62. log_info "Checking code formatting with cargo fmt..."
  63. if ! cargo fmt --all -- --check; then
  64. log_error "Code formatting check failed. Please run '\''cargo fmt'\'' to fix formatting issues."
  65. exit 1
  66. fi
  67. log_success "Code formatting check passed"
  68. # Run cargo clippy
  69. log_info "Running cargo clippy..."
  70. if ! cargo clippy --all-targets --all-features -- -D warnings -A dead_code; then
  71. log_error "Clippy check failed. Please fix the warnings and errors."
  72. exit 1
  73. fi
  74. log_success "Clippy check passed"
  75. # Run tests
  76. log_info "Running tests..."
  77. if ! cargo test; then
  78. log_error "Tests failed. Please fix the failing tests."
  79. exit 1
  80. fi
  81. log_success "All tests passed"
  82. log_success "All pre-commit checks passed!"
  83. EOF
  84. # Write the pre-commit hook
  85. log_info "Writing pre-commit hook to .git/hooks/pre-commit..."
  86. # Make the hook executable
  87. chmod +x .git/hooks/pre-commit
  88. log_success "Pre-commit hook installed and made executable"
  89. # Verify the installation
  90. if [ -f ".git/hooks/pre-commit" ] && [ -x ".git/hooks/pre-commit" ]; then
  91. log_success "Pre-commit hook successfully installed!"
  92. log_info "The hook will run automatically before each commit."
  93. log_info "To test the hook manually, run: .git/hooks/pre-commit"
  94. else
  95. log_error "Failed to install pre-commit hook"
  96. exit 1
  97. fi