#!/usr/bin/env bash
# Local push gate (platform-gincommon's .githooks/pre-push, ported): Trivy CVE scan of the
# example service image, the same check as ci.yml's "Trivy CVE scan" job (make scan). Builds the
# Docker image, so it needs Docker running and a local trivy (brew install trivy), and takes a
# few minutes on a cold cache.
# Skip once with `git push --no-verify` or SKIP_TRIVY=1 git push. A push that only deletes
# remote refs (git push origin --delete <branch>) is skipped: nothing is uploaded, so there is no
# image to check.
set -euo pipefail

if [ "${SKIP_TRIVY:-}" = "1" ]; then
  echo "[pre-push] SKIP_TRIVY=1 — skipping Trivy scan"
  exit 0
fi

# stdin: one "<local ref> <local oid> <remote ref> <remote oid>" line per ref;
# a deletion has an all-zero local oid.
pushes_commits=0
while read -r _local_ref local_oid _remote_ref _remote_oid; do
  case "$local_oid" in
    *[!0]*) pushes_commits=1 ;;
  esac
done
if [ "$pushes_commits" = "0" ]; then
  echo "[pre-push] only deleting remote refs — skipping Trivy scan"
  exit 0
fi

if ! docker info >/dev/null 2>&1; then
  echo "[pre-push] Docker is not running; start it, or push with --no-verify / SKIP_TRIVY=1 to skip the Trivy scan." >&2
  exit 1
fi

if ! command -v trivy >/dev/null 2>&1; then
  echo "[pre-push] trivy is not installed (brew install trivy); install it, or push with --no-verify / SKIP_TRIVY=1 to skip the Trivy scan." >&2
  exit 1
fi

echo "[pre-push] running trivy..."
make --no-print-directory scan
