#!/bin/bash
# pre-commit hook: 在提交前强制运行 cargo fmt 和 cargo clippy 检查

set -e

echo "Running cargo fmt..."

# 检查是否有需要格式化的 Rust 文件
if ! cargo fmt -- --check 2>/dev/null; then
    echo ""
    echo "Code formatting issues found. Running 'cargo fmt' to fix..."
    cargo fmt
    echo ""
    echo "Formatting completed. Please review the changes and re-add the files:"
    echo "  git add <files>"
    echo "  git commit"
    exit 1
fi

echo "Code formatting OK."

echo ""
echo "Running cargo clippy..."

# 运行 clippy 检查，如果有警告则阻止提交
if ! cargo clippy -- -D warnings 2>&1; then
    echo ""
    echo "Clippy warnings found. Please fix the issues above before committing."
    exit 1
fi

echo "Clippy check OK."

exit 0
