set -e
if git diff --cached --name-only --diff-filter=AM | grep -qx 'Cargo.lock'; then
if git symbolic-ref -q HEAD >/dev/null; then
echo "Cargo.lock is staged on branch $(git symbolic-ref --short HEAD)."
echo "It may only be committed on the release tag's own detached commit."
echo "Run 'git checkout --detach' first, then stage and commit it."
exit 1
fi
fi
echo "Running cargo fmt check..."
if ! cargo fmt --all -- --check; then
echo "Code is not formatted. Run 'cargo fmt --all' to fix."
exit 1
fi
echo "Running cargo clippy..."
if ! cargo clippy --all-targets --features tui,fixtures --message-format=short -- -D warnings; then
echo "Clippy found warnings. Fix them before committing."
exit 1
fi
echo "Running tests..."
if ! test_output=$(cargo test --release --features tui,fixtures 2>&1); then
echo "$test_output" | tail
echo "Tests failed. Fix them before committing."
exit 1
fi
echo "Running gitleaks..."
GITLEAKS_ARGS=()
if [ -f .gitleaks.local.toml ]; then
GITLEAKS_ARGS=(--config .gitleaks.local.toml)
fi
if ! gitleaks git --staged --no-banner "${GITLEAKS_ARGS[@]}"; then
echo "gitleaks detected secrets in the staged changes. Remove them before committing."
exit 1
fi
echo "Pre-commit checks passed"