#!/usr/bin/env bash
# Pre-commit hook: run the same fast lint/format checks as ci-pr-fast.
#
# Enabled by pointing git at the repo-managed hooks dir:
#   scripts/dev/install-hooks.sh
# (which just runs `git config core.hooksPath .githooks`).
#
# Escape hatch: SKIP_PRECOMMIT=1 git commit ...   # bypasses this hook
# or equivalently: git commit --no-verify         # standard git bypass
set -euo pipefail

if [[ "${SKIP_PRECOMMIT:-0}" == "1" ]]; then
  echo "[pre-commit] SKIP_PRECOMMIT=1 set; skipping."
  exit 0
fi

repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

# Only run the Rust checks when Rust sources or Cargo metadata are staged.
# Other commits (docs, SQL, config) are still subject to whatever other hooks
# exist, but don't need to pay the clippy cost.
staged_rust="$(git diff --cached --name-only --diff-filter=ACMR \
  | grep -E '(\.rs$|^Cargo\.(toml|lock)$|^rust-toolchain(\.toml)?$)' || true)"

if [[ -z "$staged_rust" ]]; then
  echo "[pre-commit] no Rust changes staged; skipping cargo checks."
  exit 0
fi

echo "[pre-commit] make fmt"
make fmt

echo "[pre-commit] clippy (strict baseline, matches CI)"
make clippy

echo "[pre-commit] ok"
