#!/bin/bash
# Refuses commit messages that carry attribution trailers. Contributors own
# their contributions (AGENTS.md, "No Co-Authored-By or tool-attribution
# trailers"); a trailer spreads that ownership onto a tool or a bystander.

msg_file="$1"

# Trailer keys in any casing, with any bullet or emoji in front.
trailers='^[^[:alnum:]]*(co-authored-by|claude-session|generated-by):'
# "Generated with/by" footers, including "Generated with <emoji> by".
footer='^[^[:alnum:]]*generated (with|by)( [^[:alnum:]]*by)? '
# A footer names a product, so a capitalized word must follow the phrase
# itself. Prose such as "generated by the protobuf compiler" stays legal.
tool='[Gg][Ee][Nn][Ee][Rr][Aa][Tt][Ee][Dd] ([Ww][Ii][Tt][Hh]|[Bb][Yy])( [^[:alnum:]]*[Bb][Yy])? [^[:alnum:]]*[A-Z]'

offending=$( {
    grep -Ein "$trailers" "$msg_file"
    grep -Ein "$footer" "$msg_file" | grep -E "$tool"
} | sort -un )

if [ -n "$offending" ]; then
    echo "commit-msg: attribution trailers are not accepted in this repository:" >&2
    printf '%s\n' "$offending" | sed 's/^/  /' >&2
    echo "Remove them and commit again (see AGENTS.md)." >&2
    exit 1
fi
