#!/bin/sh
set -e

# Reject pushes that update the remote 'main' branch. main advances only through
# a merged pull request, never a direct push. git feeds one line per ref update
# on stdin: "<local ref> <local oid> <remote ref> <remote oid>". Checked first so
# it fails fast, before the expensive test run.
# Escape hatch (emergencies only): ALLOW_MAIN_PUSH=1 git push ...
while read -r _local_ref _local_oid remote_ref _remote_oid; do
    if [ "$remote_ref" = "refs/heads/main" ] && [ "${ALLOW_MAIN_PUSH:-0}" != "1" ]; then
        echo "ERROR: Direct pushes to 'main' are not allowed." >&2
        echo "  Open a pull request instead." >&2
        echo "  Override (emergency): ALLOW_MAIN_PUSH=1 git push ..." >&2
        exit 1
    fi
done

echo "Running pre-push checks..."

# Run tests (same as CI)
echo "Running tests..."
cargo test --all-features

echo "Pre-push checks passed!"
