#!/usr/bin/env bash
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

push_lines="$(cat)"
has_updates=0
has_deletions=0
while read -r local_ref local_sha remote_ref remote_sha; do
  [ -z "${local_ref}" ] && continue
  if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
    has_deletions=1
  else
    has_updates=1
  fi
done <<< "$push_lines"

if [ "$has_updates" = "0" ] && [ "$has_deletions" = "1" ]; then
  echo "[pre-push] 分支删除推送，跳过本地检查"
  exit 0
fi

if [ "${SKIP_CHECKS:-0}" = "1" ]; then
  echo "[pre-push] SKIP_CHECKS=1，跳过本地检查"
  exit 0
fi

echo "[pre-push] 运行本地代码门禁..."
bash scripts/check.sh
echo "[pre-push] 本地检查通过"
