#!/usr/bin/env sh

set -e

zero_object_id=0000000000000000000000000000000000000000

while IFS=' ' read -r local_ref local_object_id remote_ref remote_object_id
do
    if [ "$remote_ref" != "refs/heads/main" ]; then
        continue
    fi

    if [ "$local_object_id" = "$zero_object_id" ]; then
        echo "拒绝推送：禁止删除远端 main。" >&2
        exit 1
    fi

    if [ "$remote_object_id" = "$zero_object_id" ]; then
        continue
    fi

    if ! git merge-base --is-ancestor "$remote_object_id" "$local_object_id"; then
        echo "拒绝推送：远端 main 只允许快进更新，禁止强制推送。" >&2
        exit 1
    fi
done
