#!/usr/bin/env bash
# Reject commit messages containing a GitHub closing keyword next to an issue
# reference. This is the *prevention* half of the guard: a commit pushed
# directly to `main` never passes through a PR, so CI can only notice it after
# the issue has already closed. See scripts/check_closing_keywords.py.
#
# Install:  ./scripts/install-hooks.sh
# Bypass:   ALLOW_CLOSING_KEYWORD=1 git commit ...
set -euo pipefail

repo_root="$(git rev-parse --show-toplevel)"
checker="$repo_root/scripts/check_closing_keywords.py"

# Never block a commit because the guard itself is missing (e.g. checking out
# an older revision). Fail open here; CI is the backstop.
[ -f "$checker" ] || exit 0

# Only ask the checker to apply git's comment/scissors cleanup when git will
# actually perform it. Under `whitespace` or `verbatim`, `#` lines survive into
# the stored message and GitHub parses them, so they must be scanned.
cleanup="$(git config --get commit.cleanup || echo default)"
case "$cleanup" in
  default | strip) git_comments="--git-comments" ;;
  scissors)        git_comments="--git-comments" ;;
  *)               git_comments="" ;;
esac

# shellcheck disable=SC2086
python3 "$checker" "$1" $git_comments \
  --label "commit message" \
  --bypass-hint "ALLOW_CLOSING_KEYWORD=1 git commit ..."
