#!/usr/bin/env bash
# Pre-push: run repo mise checks on every non-empty push.
# `mise run test` stays the canonical full-suite command for CI / pre-release.
#
# Portable to bash 3.2 (macOS /bin/bash): no `mapfile`, no `declare -A`, etc.
set -e
set -o pipefail

# Unset git env inherited from the hook context. Tests in this repo invoke
# git subprocesses with cwd for isolation; GIT_DIR overrides cwd.
GIT_DIR_SAVED="${GIT_DIR-}"
unset GIT_DIR

trap '[ -n "$GIT_DIR_SAVED" ] && export GIT_DIR="$GIT_DIR_SAVED" || unset GIT_DIR' EXIT

# Pick a base: upstream if we have one, else the previous commit.
if upstream=$(git rev-parse --verify --quiet '@{u}' 2>/dev/null); then
  base=$(git merge-base "$upstream" HEAD)
elif base=$(git rev-parse --verify --quiet 'HEAD~1' 2>/dev/null); then
  :
else
  # No upstream, no parent — nothing to compare against.
  exit 0
fi

if [ -z "$(git diff --name-only --diff-filter=ACMR "$base...HEAD")" ]; then
  exit 0
fi

mise run format-check
mise run lint

# testmon forceselect requires a warm .testmondata from a prior full-suite
# run. Without it, testmon selects zero tests and passes silently — defeating
# the purpose of the pre-push gate. Fall back to the full suite when the
# data file is missing or empty.
if [ -s .testmondata ]; then
  mise run test-affected
else
  echo "pre-push: .testmondata missing or empty — running full suite"
  mise run test
fi
