#!/bin/sh
# prepare-commit-msg — AG-03 trailer enforcement.
#
# Guarantees the human ``Signed-off-by`` trailer on EVERY commit, including
# ``git commit -m`` (which bypasses the ``.gitmessage`` template wired by
# AG-18). Idempotent — ``--if-exists doNothing`` never duplicates a trailer
# the author already wrote.
#
# Scope: only the *constant* human signoff is added here (derived from
# ``git config user.{name,email}``). The ``Co-Authored-By`` trailer names the
# drafting agent + context size, which the committing agent adds itself — a
# hook cannot know which model authored the change.
#
# Two supported wirings (mutually exclusive — see
# docs/recipes/standardized-git-config.md §4):
#   plain repo:      ``git config core.hooksPath .githooks``
#   pre-commit repo: a ``repo: local`` hook with
#                    ``stages: [prepare-commit-msg]`` running this script
#                    (``pre-commit install`` refuses when core.hooksPath is
#                    set, so pre-commit-framework repos use this path).
# Under pre-commit no source argument is passed; the framework exports
# PRE_COMMIT_COMMIT_MSG_SOURCE instead — fall back to it so merge/squash
# messages are skipped identically in both wirings.

msg_file="$1"
src="${2:-$PRE_COMMIT_COMMIT_MSG_SOURCE}"

# Leave git-generated merge / squash messages alone.
case "$src" in
	merge | squash) exit 0 ;;
esac

git interpret-trailers --if-exists doNothing \
	--trailer "Signed-off-by: $(git config user.name) <$(git config user.email)>" \
	--in-place "$msg_file"
