#!/usr/bin/env bash
# Reject Cargo.lock changes that aren't accompanied by a Cargo.toml change.
# Lockfile drift should originate from cargo, not hand edits — see AGENTS.md.
set -euo pipefail

staged_changed() {
  git diff --cached --name-only --diff-filter=ACMR | grep -Fx "$1" >/dev/null
}

if staged_changed Cargo.lock && ! staged_changed Cargo.toml; then
  cat >&2 <<'EOF'
error: Cargo.lock is staged without a corresponding Cargo.toml change.

Lockfile drift must come from cargo, not hand edits. Either:
  - stage the matching Cargo.toml change, or
  - unstage Cargo.lock (`git restore --staged Cargo.lock`) and rerun cargo.
EOF
  exit 1
fi
