# Pre-commit hook script for rskv project (PowerShell version)
# This script runs before each commit to ensure code quality
param(
[switch]$SkipTests = $false
)
# Colors for output
$Red = "Red"
$Green = "Green"
$Yellow = "Yellow"
$Blue = "Blue"
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
Write-ColorOutput "๐ Running pre-commit checks..." $Blue
# Check if we're in a git repository
try {
$null = git rev-parse --git-dir 2>$null
} catch {
Write-ColorOutput "โ Not in a git repository" $Red
exit 1
}
# Get staged files
$StagedFiles = git diff --cached --name-only --diff-filter=ACM | Where-Object { $_ -match '\.rs$' }
if (-not $StagedFiles) {
Write-ColorOutput "โ ๏ธ No Rust files staged for commit" $Yellow
exit 0
}
Write-ColorOutput "๐ Staged Rust files:" $Blue
$StagedFiles | ForEach-Object { Write-Host " $_" }
# Function to check if a command exists
function Test-Command {
param([string]$Command)
try {
Get-Command $Command -ErrorAction Stop | Out-Null
return $true
} catch {
return $false
}
}
# Check code formatting
Write-ColorOutput "๐ง Checking code formatting..." $Yellow
try {
cargo fmt --all -- --check
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Code formatting check failed" $Red
Write-ColorOutput "๐ก Run 'cargo fmt --all' to fix formatting issues" $Yellow
exit 1
}
Write-ColorOutput "โ
Code formatting check passed" $Green
} catch {
Write-ColorOutput "โ Code formatting check failed" $Red
exit 1
}
# Check clippy
Write-ColorOutput "๐ Running clippy..." $Yellow
try {
cargo clippy --all-features --workspace -- -D warnings
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Clippy check failed" $Red
exit 1
}
Write-ColorOutput "โ
Clippy check passed" $Green
} catch {
Write-ColorOutput "โ Clippy check failed" $Red
exit 1
}
# Check compilation
Write-ColorOutput "๐จ Checking compilation..." $Yellow
try {
cargo check --all-features --workspace
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Compilation check failed" $Red
exit 1
}
Write-ColorOutput "โ
Compilation check passed" $Green
} catch {
Write-ColorOutput "โ Compilation check failed" $Red
exit 1
}
# Run tests (unless skipped)
if (-not $SkipTests) {
Write-ColorOutput "๐งช Running tests..." $Yellow
try {
cargo test --all-features --workspace
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Tests failed" $Red
exit 1
}
Write-ColorOutput "โ
Tests passed" $Green
} catch {
Write-ColorOutput "โ Tests failed" $Red
exit 1
}
} else {
Write-ColorOutput "โ ๏ธ Skipping tests (--SkipTests flag used)" $Yellow
}
# Security audit (if cargo-audit is available)
if (Test-Command "cargo-audit") {
Write-ColorOutput "๐ Running security audit..." $Yellow
try {
cargo audit
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Security audit failed" $Red
exit 1
}
Write-ColorOutput "โ
Security audit passed" $Green
} catch {
Write-ColorOutput "โ Security audit failed" $Red
exit 1
}
} else {
Write-ColorOutput "โ ๏ธ cargo-audit not found, skipping security audit" $Yellow
}
# Cargo-deny check (if available)
if (Test-Command "cargo-deny") {
Write-ColorOutput "๐ Running cargo-deny..." $Yellow
try {
cargo deny check
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Cargo-deny check failed" $Red
exit 1
}
Write-ColorOutput "โ
Cargo-deny check passed" $Green
} catch {
Write-ColorOutput "โ Cargo-deny check failed" $Red
exit 1
}
} else {
Write-ColorOutput "โ ๏ธ cargo-deny not found, skipping dependency check" $Yellow
}
Write-ColorOutput "๐ All pre-commit checks passed!" $Green
Write-ColorOutput "๐ Ready to commit" $Blue