set -euo pipefail
COMMIT_MSG_FILE="${1:-}"
COMMIT_SOURCE="${2:-}"
case "$COMMIT_SOURCE" in
merge|squash|message|commit) exit 0 ;;
esac
BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || echo "")
[ -z "$BRANCH" ] && exit 0
TICKET=$(grep -oE '[A-Z]{2,}-[0-9]+' <<<"$BRANCH" | head -1 || true)
if [ -n "$TICKET" ]; then
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
if ! grep -qF -- "$TICKET" <<<"$COMMIT_MSG"; then
printf '[%s] %s\n' "$TICKET" "$COMMIT_MSG" > "$COMMIT_MSG_FILE"
fi
fi
LOCAL_HOOK="$(git rev-parse --show-toplevel)/.git-hooks/local-prepare-commit-msg"
if [ -x "$LOCAL_HOOK" ]; then
exec "$LOCAL_HOOK" "$@"
fi