#!/usr/bin/env bash
#
# Pre-push hook: the last gate before commits leave the machine. Scans the
# message and the added lines of every commit each pushed ref would publish,
# including commits made with --no-verify or before the other hooks existed.
#
# Install with scripts/install_hooks.sh.
set -uo pipefail
repo_root="$(git rev-parse --show-toplevel)" || exit 1
remote="$1"
zero="0000000000000000000000000000000000000000"
status=0
while read -r _local_ref local_sha _remote_ref remote_sha; do
    [ "$local_sha" = "$zero" ] && continue   # deleting a ref publishes nothing
    if [ "$remote_sha" = "$zero" ]; then
        # New branch: everything not already on the remote.
        range="$local_sha --not --remotes=$remote"
    else
        range="$remote_sha..$local_sha"
    fi
    "$repo_root/scripts/check_portable.sh" --range "$range" || status=1
done
exit $status
