diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 76fe9c9e..c7e5f901 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,14 +1,61 @@ -#!/bin/bash +#!/usr/bin/env bash + +# Exit immediately if a command exits with a non-zero status set -e -echo "Running pre-commit hook..." +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +echo -e "${BLUE}Running pre-commit hooks...${NC}" + +# Get the list of staged files +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) + +if [ -z "$STAGED_FILES" ]; then + echo -e "${YELLOW}No staged files found.${NC}" + exit 0 +fi + +# Flag to track if any files were modified +FILES_MODIFIED=false -# Run clippy -echo "Running cargo clippy..." -./scripts/clippy.sh +# Process Rust files +RUST_FILES=$(echo "$STAGED_FILES" | grep '\.rs$' || true) +if [ -n "$RUST_FILES" ]; then + echo -e "${BLUE}Formatting Rust files...${NC}" + + # Format Rust files using nightly (as per project convention) + echo "$RUST_FILES" | xargs cargo +nightly fmt --edition 2021 + + # Check if formatting changed any files + for file in $RUST_FILES; do + if ! git diff --exit-code "$file" > /dev/null; then + echo -e "${YELLOW}Formatted: $file${NC}" + FILES_MODIFIED=true + fi + done + + # Run clippy with strict settings + echo -e "${BLUE}Running clippy on Rust files...${NC}" + if ! cargo clippy --all-targets --all-features -- -D warnings -D future-incompatible -D nonstandard-style -D rust-2018-idioms -D unused; then + echo -e "${RED}Clippy failed. Please fix the issues and try again.${NC}" + exit 1 + fi +fi -# Run rustfmt -echo "Running cargo +nightly fmt..." -./scripts/rust_fmt_fix.sh +# If any files were modified, inform the user +if [ "$FILES_MODIFIED" = true ]; then + echo -e "${YELLOW}" + echo "Some files were automatically formatted. Please review the changes and re-stage them:" + echo " git add " + echo " git commit" + echo -e "${NC}" + exit 1 +fi -echo "Pre-commit hook completed successfully." +echo -e "${GREEN}✅ All pre-commit checks passed!${NC}" +exit 0 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..8dca1060 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,33 @@ +name: Lint + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: nightly + components: rustfmt, clippy + + - name: Check Rust formatting + run: cargo +nightly fmt --all -- --check + + - name: Run Rust linting (Clippy) - strict mode + run: cargo clippy --all-targets --all-features -- -D warnings -D future-incompatible -D nonstandard-style -D rust-2018-idioms -D unused + + - name: Build project + run: cargo build --all-targets + + - name: Run tests + run: cargo test --all-features \ No newline at end of file