#!/bin/sh
# RefineID Windows push gate.
#
# Rejects a push whose Cargo.lock does not match the manifests, or pins a
# revision that is neither in Cargo's local cache nor fetchable from its
# remote. It guards consistency, not freshness: a lockfile that pins an
# older refineid-core commit pushes, and moving to a newer core is a
# deliberate change made in its own pull request.
#
# Then builds the workspace documentation with rustdoc warnings denied,
# once per target: for the native host and, when the host is another
# target and the Windows target is installed, x86_64-pc-windows-msvc.
#
# Enable local hooks once per clone with:
#
#     git config core.hooksPath .githooks

set -eu

echo "pre-push: checking that Cargo.lock matches the manifests"

if ! cargo fetch --locked --quiet; then
    echo "pre-push: error: Cargo.lock does not match the manifests, or a pinned revision could not be fetched." >&2
    echo "pre-push: if the manifests changed, update the lockfile for that dependency, review the diff, and commit it;" >&2
    echo "pre-push: if a remote was unreachable, retry once it is." >&2
    exit 1
fi

WINDOWS_TARGET="x86_64-pc-windows-msvc"
HOST_TARGET="$(rustc -vV | sed -n 's/^host: //p')"

echo "pre-push: cargo doc with warnings denied (native host, $HOST_TARGET)"
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --quiet

if [ "$HOST_TARGET" != "$WINDOWS_TARGET" ] \
    && rustup target list --installed | grep -qx "$WINDOWS_TARGET"; then
    echo "pre-push: cargo doc with warnings denied ($WINDOWS_TARGET)"
    RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --quiet --target "$WINDOWS_TARGET"
fi

echo "pre-push: ok (Cargo.lock matches the manifests, documentation builds)"
