setup-pre-commit-hooks.bat 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. :: ANSI color codes for Windows 10+ Command Prompt
  4. set "RED=[91m"
  5. set "GREEN=[92m"
  6. set "YELLOW=[93m"
  7. set "BLUE=[94m"
  8. set "CYAN=[96m"
  9. set "NC=[0m"
  10. :: Function to print colored log messages
  11. goto :main
  12. :log_info
  13. echo %CYAN%[INFO]%NC% %~1
  14. goto :eof
  15. :log_success
  16. echo %GREEN%[SUCCESS]%NC% %~1
  17. goto :eof
  18. :log_warning
  19. echo %YELLOW%[WARNING]%NC% %~1
  20. goto :eof
  21. :log_error
  22. echo %RED%[ERROR]%NC% %~1
  23. goto :eof
  24. :main
  25. :: Check if we're in a git repository
  26. if not exist ".git" (
  27. call :log_error "Not in a git repository. Please run this script from the root of your git repository."
  28. exit /b 1
  29. )
  30. call :log_info "Setting up pre-commit hooks..."
  31. :: Create .git/hooks directory if it doesn't exist
  32. if not exist ".git\hooks" (
  33. call :log_info "Creating .git/hooks directory..."
  34. mkdir ".git\hooks"
  35. )
  36. :: Create the pre-commit hook content
  37. call :log_info "Writing pre-commit hook to .git/hooks/pre-commit..."
  38. (
  39. echo @echo off
  40. echo setlocal enabledelayedexpansion
  41. echo.
  42. echo :: ANSI color codes for Windows 10+ Command Prompt
  43. echo set "RED=[91m"
  44. echo set "GREEN=[92m"
  45. echo set "YELLOW=[93m"
  46. echo set "BLUE=[94m"
  47. echo set "CYAN=[96m"
  48. echo set "NC=[0m"
  49. echo.
  50. echo echo %%CYAN%%[INFO]%%NC%% Running pre-commit checks...
  51. echo.
  52. echo :: Check if cargo is available
  53. echo where cargo ^>nul 2^>^&1
  54. echo if ^^!errorlevel^^! neq 0 ^(
  55. echo echo %%RED%%[ERROR]%%NC%% Cargo is not installed or not in PATH
  56. echo exit /b 1
  57. echo ^)
  58. echo.
  59. echo :: Run cargo fmt check
  60. echo echo %%CYAN%%[INFO]%%NC%% Checking code formatting with cargo fmt...
  61. echo cargo fmt --all -- --check ^>nul 2^>^&1
  62. echo if ^^!errorlevel^^! neq 0 ^(
  63. echo echo %%RED%%[ERROR]%%NC%% Code formatting check failed. Please run 'cargo fmt' to fix formatting issues.
  64. echo exit /b 1
  65. echo ^)
  66. echo echo %%GREEN%%[SUCCESS]%%NC%% Code formatting check passed
  67. echo.
  68. echo :: Run cargo clippy
  69. echo echo %%CYAN%%[INFO]%%NC%% Running cargo clippy...
  70. echo cargo clippy --all-targets --all-features -- -D warnings -A dead_code ^>nul 2^>^&1
  71. echo if ^^!errorlevel^^! neq 0 ^(
  72. echo echo %%RED%%[ERROR]%%NC%% Clippy check failed. Please fix the warnings and errors.
  73. echo exit /b 1
  74. echo ^)
  75. echo echo %%GREEN%%[SUCCESS]%%NC%% Clippy check passed
  76. echo.
  77. echo :: Run tests
  78. echo echo %%CYAN%%[INFO]%%NC%% Running tests...
  79. echo cargo test ^>nul 2^>^&1
  80. echo if ^^!errorlevel^^! neq 0 ^(
  81. echo echo %%RED%%[ERROR]%%NC%% Tests failed. Please fix the failing tests.
  82. echo exit /b 1
  83. echo ^)
  84. echo echo %%GREEN%%[SUCCESS]%%NC%% All tests passed
  85. echo.
  86. echo echo %%GREEN%%[SUCCESS]%%NC%% All pre-commit checks passed!
  87. ) > ".git\hooks\pre-commit"
  88. :: Also create .cmd version for Windows compatibility
  89. copy ".git\hooks\pre-commit" ".git\hooks\pre-commit.cmd" >nul
  90. :: Set executable permissions
  91. call :log_info "Setting executable permissions on pre-commit hooks..."
  92. icacls ".git\hooks\pre-commit" /grant Everyone:F >nul 2>&1
  93. icacls ".git\hooks\pre-commit.cmd" /grant Everyone:F >nul 2>&1
  94. if !errorlevel! equ 0 (
  95. call :log_info "Executable permissions set successfully"
  96. ) else (
  97. call :log_warning "Could not set executable permissions"
  98. )
  99. :: Verify the installation
  100. if exist ".git\hooks\pre-commit" (
  101. if exist ".git\hooks\pre-commit.cmd" (
  102. call :log_success "Pre-commit hook successfully installed!"
  103. call :log_info "The hook will run automatically before each commit."
  104. call :log_info "Created both 'pre-commit' and 'pre-commit.cmd' for maximum Windows compatibility."
  105. call :log_info "To test the hook manually, run: .git/hooks/pre-commit.cmd"
  106. ) else (
  107. call :log_error "Failed to create .cmd version of pre-commit hook"
  108. exit /b 1
  109. )
  110. ) else (
  111. call :log_error "Failed to install pre-commit hook"
  112. exit /b 1
  113. )