2
0

setup-pre-commit-hooks.ps1 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # PowerShell script to setup pre-commit hooks
  2. # Function to print colored log messages
  3. function Write-LogInfo {
  4. param([string]$Message)
  5. Write-Host "[INFO] $Message" -ForegroundColor Cyan
  6. }
  7. function Write-LogSuccess {
  8. param([string]$Message)
  9. Write-Host "[SUCCESS] $Message" -ForegroundColor Green
  10. }
  11. function Write-LogWarning {
  12. param([string]$Message)
  13. Write-Host "[WARNING] $Message" -ForegroundColor Yellow
  14. }
  15. function Write-LogError {
  16. param([string]$Message)
  17. Write-Host "[ERROR] $Message" -ForegroundColor Red
  18. }
  19. # Check if we're in a git repository
  20. if (-not (Test-Path ".git")) {
  21. Write-LogError "Not in a git repository. Please run this script from the root of your git repository."
  22. exit 1
  23. }
  24. Write-LogInfo "Setting up pre-commit hooks..."
  25. # Create .git/hooks directory if it doesn't exist
  26. if (-not (Test-Path ".git/hooks")) {
  27. Write-LogInfo "Creating .git/hooks directory..."
  28. New-Item -ItemType Directory -Path ".git/hooks" -Force | Out-Null
  29. }
  30. # Pre-commit hook content (Windows batch version)
  31. $PreCommitHookContent = @'
  32. @echo off
  33. setlocal enabledelayedexpansion
  34. :: ANSI color codes for Windows 10+ Command Prompt
  35. set "RED=[91m"
  36. set "GREEN=[92m"
  37. set "YELLOW=[93m"
  38. set "BLUE=[94m"
  39. set "CYAN=[96m"
  40. set "NC=[0m"
  41. echo %CYAN%[INFO]%NC% Running pre-commit checks...
  42. :: Check if cargo is available
  43. where cargo >nul 2>&1
  44. if !errorlevel! neq 0 (
  45. echo %RED%[ERROR]%NC% Cargo is not installed or not in PATH
  46. exit /b 1
  47. )
  48. :: Run cargo fmt check
  49. echo %CYAN%[INFO]%NC% Checking code formatting with cargo fmt...
  50. cargo fmt --all -- --check >nul 2>&1
  51. if !errorlevel! neq 0 (
  52. echo %RED%[ERROR]%NC% Code formatting check failed. Please run 'cargo fmt' to fix formatting issues.
  53. exit /b 1
  54. )
  55. echo %GREEN%[SUCCESS]%NC% Code formatting check passed
  56. :: Run cargo clippy
  57. echo %CYAN%[INFO]%NC% Running cargo clippy...
  58. cargo clippy --all-targets --all-features -- -D warnings -A dead_code >nul 2>&1
  59. if !errorlevel! neq 0 (
  60. echo %RED%[ERROR]%NC% Clippy check failed. Please fix the warnings and errors.
  61. exit /b 1
  62. )
  63. echo %GREEN%[SUCCESS]%NC% Clippy check passed
  64. :: Run tests
  65. echo %CYAN%[INFO]%NC% Running tests...
  66. cargo test >nul 2>&1
  67. if !errorlevel! neq 0 (
  68. echo %RED%[ERROR]%NC% Tests failed. Please fix the failing tests.
  69. exit /b 1
  70. )
  71. echo %GREEN%[SUCCESS]%NC% All tests passed
  72. echo %GREEN%[SUCCESS]%NC% All pre-commit checks passed!
  73. '@
  74. # Write the pre-commit hook (both standard and .cmd versions for Windows compatibility)
  75. Write-LogInfo "Writing pre-commit hook to .git/hooks/pre-commit..."
  76. $PreCommitHookContent | Out-File -FilePath ".git/hooks/pre-commit" -Encoding ASCII
  77. $PreCommitHookContent | Out-File -FilePath ".git/hooks/pre-commit.cmd" -Encoding ASCII
  78. # Set executable permissions (Windows equivalent)
  79. try {
  80. $acl = Get-Acl ".git/hooks/pre-commit"
  81. $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "FullControl", "Allow")
  82. $acl.SetAccessRule($accessRule)
  83. Set-Acl ".git/hooks/pre-commit" $acl
  84. Set-Acl ".git/hooks/pre-commit.cmd" $acl
  85. Write-LogInfo "Set executable permissions on pre-commit hooks"
  86. } catch {
  87. Write-LogWarning "Could not set executable permissions: $($_.Exception.Message)"
  88. }
  89. # Verify the installation
  90. if ((Test-Path ".git/hooks/pre-commit") -and (Test-Path ".git/hooks/pre-commit.cmd")) {
  91. Write-LogSuccess "Pre-commit hook successfully installed!"
  92. Write-LogInfo "The hook will run automatically before each commit."
  93. Write-LogInfo "Created both 'pre-commit' and 'pre-commit.cmd' for maximum Windows compatibility."
  94. Write-LogInfo "To test the hook manually, run: .git/hooks/pre-commit.cmd"
  95. } else {
  96. Write-LogError "Failed to install pre-commit hook"
  97. exit 1
  98. }