#!/usr/bin/env bash # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright the Vortex contributors # # Git Pre-Commit Hook for Vortex # Validates commits to ensure code formatting and REUSE compliance. # # If you want to use this hook, simply copy and paste the file into a new file # `.git/hooks/pre-commit` and everything should work correctly. If you are in the project root, you # can use these command: # # ```sh # cp scripts/pre-commit .git/hooks/ # ``` set -Euo pipefail # Configuration. MAIN_BRANCH="develop" # The main branch name for vortex. # Color codes for terminal output. RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' BOLD='\033[1m' RESET='\033[0m' # Output helper functions. print_error() { echo -e "${RED}$1${RESET}" } print_success() { echo -e "${GREEN}$1${RESET}" } print_warning() { echo -e "${YELLOW}$1${RESET}" } print_info() { echo -e "${BLUE}$1${RESET}" } print_header() { echo -e "\n${BOLD}$1${RESET}" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" } # Navigate to repository root to ensure consistent execution. ROOT_DIR="$(git rev-parse --show-toplevel)" cd "$ROOT_DIR" # ============================================================================ # CHECK: Code Formatting # ============================================================================ # Verify that all Rust code and TOML files follow the project's formatting standards. # Consistent formatting improves readability and reduces merge conflicts. print_header "🎨 Checking code formatting" if ! command -v cargo &> /dev/null; then print_error "❌ Cargo not found in PATH" exit 1 fi # Check if Rust code needs formatting. FMT_OUTPUT=$(cargo +nightly fmt --all -- --check 2>&1) FMT_EXIT_CODE=$? if [ $FMT_EXIT_CODE -eq 0 ]; then print_success "✅ Rust code formatting looks good" else print_error "❌ Rust code needs formatting" echo echo "$FMT_OUTPUT" echo print_info "💡 Fix with: cargo +nightly fmt" echo print_warning "⚠️ To skip checks: git commit --no-verify" exit 1 fi # Check if TOML files need formatting. if ! command -v taplo &> /dev/null; then print_warning "⚠️ taplo not found - skipping TOML formatting check." echo " Install from: https://github.com/tamasfe/taplo" else TAPLO_OUTPUT=$(taplo fmt --check 2>&1) TAPLO_EXIT_CODE=$? if [ $TAPLO_EXIT_CODE -eq 0 ]; then print_success "✅ TOML formatting looks good" else print_error "❌ TOML files need formatting" echo echo "$TAPLO_OUTPUT" echo print_info "💡 Fix with: taplo fmt" echo print_warning "⚠️ To skip checks: git commit --no-verify" exit 1 fi fi # ============================================================================ # Success # ============================================================================ echo print_success "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" print_success "🎉 All pre-commit checks passed! Proceeding with push..." print_success "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo exit 0