#!/bin/sh
set -eu

staged_rust_files="$(git diff --cached --name-only --diff-filter=ACMR -- '*.rs' 'build.rs')"

if [ -z "$staged_rust_files" ]; then
  exit 0
fi

echo "pre-commit: checking Rust formatting with cargo fmt --check"

if ! cargo fmt --check >/dev/null; then
  cat <<'EOF' >&2
pre-commit: Rust formatting check failed.
Run `cargo fmt` and stage the result before committing.
EOF
  exit 1
fi
