#!/bin/sh
# Before main moves: what CI would fail on, here, in about a minute. Commits go
# straight to main and CI only reports afterwards - and `uv tool install
# git+...runspector` installs whatever main is at that moment.
#
#   git config core.hooksPath .githooks      # once per clone, opt-in
#   git push --no-verify                     # skip, when you know why
set -e
cd "$(git rev-parse --show-toplevel)"

zero=0000000000000000000000000000000000000000
while read -r local_ref local_sha remote_ref remote_sha; do
    case "$local_ref" in refs/tags/*) continue ;; esac
    [ "$local_sha" = "$zero" ] && continue
    [ "$remote_sha" = "$zero" ] && continue
    changed=$(git diff --name-only "$remote_sha" "$local_sha")
    # The coach's prompt is code with its own tests: a change to it ships with
    # an eval run, which is what rewrites evals/RESULTS.md.
    if echo "$changed" | grep -qE '^src/runspector/skills/|^evals/cases\.yaml$' &&
       ! echo "$changed" | grep -qx 'evals/RESULTS.md'; then
        echo "pre-push: the coach prompt or the eval cases changed, evals/RESULTS.md did not."
        echo "          uv run python evals/run_evals.py   (or --no-verify, and say why)"
        exit 1
    fi
done

uv run ruff check
uv run pytest -q -x -p no:cacheprovider
uv run python scripts/pii_gate.py
# The frontend lint, when node is here at all (same version as ci.yml).
if command -v npx >/dev/null 2>&1; then
    npx --yes @biomejs/biome@2.5.15 lint --error-on-warnings
fi
