#!/usr/bin/env bash
# Mirror the gates the CI workflow enforces on every push, before they
# travel to GitHub. Bypass with `git push --no-verify` if you really
# need to (e.g. pushing a WIP branch for someone to look at).
set -euo pipefail

# Skip CI only when every ref in the push is a deletion (zero sha).
# A mixed push (e.g. `git push origin new-branch :old-branch`) must
# still run CI for the new commits.
zero=0000000000000000000000000000000000000000
non_deletion=0
while read -r _local_ref local_sha _remote_ref _remote_sha; do
    if [ "$local_sha" != "$zero" ]; then
        non_deletion=1
    fi
done

if [ "$non_deletion" -eq 0 ]; then
    exit 0
fi

cd "$(git rev-parse --show-toplevel)"
exec just ci
