#!/usr/bin/env bash
# Per-push SHA gate: for each pushed ref, the working tree must be at the pushed tip and
# clean, and every gate the pushed commits can affect must pass. Backstop against
# --no-verify. See tools/hooks/pre-commit, "What the gate builds", for why a dirty tree
# is refused rather than worked around: the gates build the working tree, so the working
# tree has to be the thing being pushed.
#
# The file list comes from the commits (`git log --name-only`), never from a `git diff`
# against the working tree. For a ref the remote does not have yet, remote_sha is all
# zeros; `git diff --name-only "$local_sha"` then diffs that commit against the working
# tree, which on a clean tree is empty, and a brand new branch would push with no gate at
# all. `--not --remotes` gives the commits that are not already on a remote-tracking
# branch, which is the set this push is actually adding.
#
# tools/check_no_rom.sh runs once, unconditionally, before the loop. It is a second of
# work and the IP rule has no "only when the diff looks relevant" clause.
set -e
ROOT="$(git rev-parse --show-toplevel)"
cd "$ROOT"

tools/check_no_rom.sh

push_failed=0
while read -r local_ref local_sha remote_ref remote_sha; do
    [[ "$local_sha" =~ ^0+$ ]] && continue
    # No 2>/dev/null and no `|| true` on the git call: an object this clone does not
    # have, or a broken repository, must fail the push instead of reading as "nothing
    # build-sensitive". set -e makes the assignment below do that.
    if [[ "$remote_sha" =~ ^0+$ ]]; then
        CHANGED=$(git log --format= --name-only "$local_sha" --not --remotes)
    else
        CHANGED=$(git log --format= --name-only "$remote_sha..$local_sha")
    fi
    BUILD_SENSITIVE=$(grep -E '^(src/|spc/|tools/|config/|Makefile$)' <<<"$CHANGED" || true)
    RECOMP_TOUCHED=$(grep -E '^(recomp/|config/recomp|tools/recomp_verify\.py$)' <<<"$CHANGED" || true)
    APP_TOUCHED=$(grep -E '^(recomp/|config/assets\.txt$)' <<<"$CHANGED" || true)
    [[ -z "$BUILD_SENSITIVE" && -z "$RECOMP_TOUCHED" && -z "$APP_TOUCHED" ]] && continue
    if [[ ! -f baserom/DREAM.sfc || ! -x tools/bin/asar ]]; then
        echo "pre-push: no baserom/DREAM.sfc or tools/bin/asar: cannot verify $local_ref, refusing" >&2
        push_failed=1; continue
    fi
    if [[ "$(git rev-parse HEAD)" != "$local_sha" ]]; then
        echo "pre-push: working tree HEAD is not the pushed tip of $local_ref; check it out and retry" >&2
        push_failed=1; continue
    fi
    DIRTY=$(git status --porcelain --untracked-files=no)
    UNTRACKED=$(git ls-files -o --exclude-standard -- src spc tools config Makefile recomp)
    if [[ -n "$DIRTY" || -n "$UNTRACKED" ]]; then
        echo "pre-push: the working tree is not the commit being pushed, so the gates below" >&2
        echo "          would verify content $local_ref does not carry. Refusing." >&2
        [[ -n "$DIRTY" ]]     && sed 's/^/    /' <<<"$DIRTY" >&2
        [[ -n "$UNTRACKED" ]] && { echo "  untracked under a gated path:" >&2; sed 's/^/    /' <<<"$UNTRACKED" >&2; }
        push_failed=1; continue
    fi
    if [[ -n "$BUILD_SENSITIVE" ]]; then
        echo "pre-push: make check on $local_ref ..."
        if ! make -s check >/dev/null; then
            echo "pre-push: SHA-1 GATE FAILED on $local_ref: refusing push." >&2
            push_failed=1; continue
        fi
    fi
    # The lockstep gates are a release checklist (README.md "Releasing"), not a hook.
    if ! python3 tools/progress.py --check >/dev/null; then
        echo "pre-push: docs/progress.json is stale: run tools/progress.py and commit." >&2
        push_failed=1; continue
    fi
    echo "pre-push: gate OK on $local_ref"
done
exit $push_failed
