#!/bin/sh
# mkd pre-push gate: the intra-doc link check (ADR-0056) and cargo deny
# check (ADR-0053) must pass before every push.
# Pre-push, not pre-commit: commits stay fast and offline (ADR-0053
# supersedes ADR-0004's placement), the advisory database — which needs
# the network — is consulted only when work leaves the machine, and the
# offline link check runs once per push rather than once per commit.
# Enable once per clone with: git config core.hooksPath .githooks
set -eu

# Intra-doc links (ADR-0056). Offline; --document-private-items because a
# binary's links rot most between private modules. cargo doc ships with
# the toolchain, so no fail-closed install guard is needed here.
echo "pre-push: checking intra-doc links"
RUSTDOCFLAGS="-D rustdoc::broken_intra_doc_links -D rustdoc::redundant_explicit_links" \
    cargo doc --no-deps --document-private-items --all-features --quiet

if ! command -v cargo-deny >/dev/null 2>&1; then
    # Fail closed: a supply-chain gate that skips when it cannot check is
    # the "allow because we could not verify" anti-pattern. Install the
    # tool, or push with --no-verify in a genuine emergency.
    echo "pre-push: cargo-deny is not installed — the supply-chain gate cannot run" >&2
    echo "pre-push: install it, then push again:" >&2
    echo "    cargo install cargo-deny --locked" >&2
    exit 1
fi

echo "pre-push: running cargo deny check"
cargo deny check

echo "pre-push: gate passed"
