#!/usr/bin/env bash
set -e

if ! command -v cargo >/dev/null 2>&1; then
    exit 0
fi

# Ensure Cargo.lock is in sync with Cargo.toml
# CI uses --locked, so a stale lockfile breaks the build
if git diff --cached --name-only | grep -q 'Cargo.toml'; then
    echo "pre-commit: Cargo.toml changed, syncing Cargo.lock..."
    cargo check --quiet 2>/dev/null
    git add Cargo.lock
fi

# Quick format check only — full tests run in pre-push
echo "pre-commit: running cargo fmt --check..."
cargo fmt --check
