#!/usr/bin/env bash
# Versioned git commit-msg hook — the English-only rule, applied to the COMMIT MESSAGE.
#
# Activate ONCE per clone (git will not auto-run versioned hooks, by design — security):
#   git config core.hooksPath hooks
# (Or let the toolbelt's global dispatcher do it: it runs hooks/<name> and hooks/<name>.d/*.)
#
# Why this surface exists, and why it was the last one to get a wall.
# CONTRIBUTING.md and CLAUDE.md have always said commit messages are English. Nothing checked it,
# and the result is measurable and permanent: 7 commit subjects on `main` are in Spanish, on a
# public repository, in the history of a tool whose whole promise is that the repo is portable.
# A file gate cannot catch this — the message is not a file — so it needs its own hook.
#
# It runs BEFORE the commit exists, which is the only moment the message is still cheap to fix:
# afterwards it takes an `--amend` (or, once pushed and merged, it is simply permanent).
#
# `--git-comments` drops git's own template lines (`#…`), which are written in the machine's locale
# and would otherwise fail the gate on text the author never wrote, and stops at the `>8` scissors
# so a `--verbose` commit does not get its own diff judged as prose.
#
# Bypass (discouraged): git commit --no-verify
set -euo pipefail
# shellcheck source=/dev/null
. "$(git rev-parse --show-toplevel)/tools/darnlang_ref.sh"

command -v uvx >/dev/null 2>&1 || { echo "lang commit-msg: uvx not on PATH -> SKIPPING." >&2; exit 0; }

# RESOLVE FIRST, JUDGE SECOND, and never conflate the two. A uv resolution failure (offline,
# unreachable ref) exits 1 exactly like a real finding, so a single combined call cannot tell "the
# tool could not start" from "your message is wrong".
#
# The first version of this guard grepped the output for a `darnlang:` line. That worked, and it was
# the wrong contract: a reviewer proved it by renaming the prefix upstream, whereupon the hook threw
# away a REAL finding and reported a skip. Depending on wording when a numeric exit-code contract
# exists is how a gate dies quietly. So: a cheap `--help` decides whether the tool is reachable, and
# after that every exit code belongs to darnlang.
#
# `set +e` is required or errexit aborts before the guard runs -- which is how the first attempt at
# this still failed closed in a sibling repo.
set +e
uvx --from "$DARNLANG_REF" darnlang --help >/dev/null 2>&1
resolved=$?
set -e
if [ "$resolved" -ne 0 ]; then
  echo "lang commit-msg: darnlang could not be resolved (offline? bad ref?) -> SKIPPING." >&2
  echo "  CI still gates this. A clean commit does NOT prove the message was judged." >&2
  exit 0
fi

# `--show-text` locally: the reader here is you, and "line 1 is wrong" without the words is a gate
# you argue with instead of fix. CI keeps it off, because its logs are public.
exec uvx --from "$DARNLANG_REF" darnlang prose "$1" --git-comments --show-text \
     --label "commit message"
