#!/bin/sh # atgc-change-id-hook v2 # # Stamp a `Change-Id:` trailer on a commit that has none. The id names the # commit across an amend or a rebase. Tangled reads per-change identity from a # `change-id` commit header, not this trailer. The trailer is written when the # commit is made rather than bolted on by rewriting the branch. # # A commit made under a known agent harness also gets an `Assisted-by:` # trailer, `[:]`, e.g. `claude-code@2.1.281:claude-opus-5-5`. # `git config atgc.assistedByTrailer false`, per repo or with --global, turns # that off; the Change-Id is stamped either way. # # Installed by `atgc repo configure`. Deleting this file is a supported way # to turn it off; atgc reinstalls it only when the slot is empty. # # Nothing here prompts, and nothing here fails a commit: every path that # cannot produce an id leaves the message exactly as it found it and exits 0. msg="$1" [ -n "$msg" ] || exit 0 [ -f "$msg" ] || exit 0 # Already carries one. Also covers a commit being amended, whose trailer is # already in the message being re-edited: neither trailer is added then, since # the agent doing the amend is not the one that made the commit. if grep -qi '^Change-Id:[[:space:]]*I' "$msg"; then exit 0 fi # A fixup or squash belongs to the commit it names, and will be folded into # it: an id of its own would be one more id than there are changes. if head -n 1 "$msg" | grep -qE '^(fixup|squash|amend)! '; then exit 0 fi # An empty message aborts the commit. Stamping one would turn that abort into # a commit whose entire message is a trailer. if [ -z "$(sed -e 's/#.*//' -e '/^[[:space:]]*$/d' "$msg")" ]; then exit 0 fi # Unique, not reproducible: author, time, pid and the message itself. Cut to # the 40 hex digits a change-id is written with, since a repository using # sha256 hashes would otherwise hand back 64. id=$( { git var GIT_AUTHOR_IDENT 2>/dev/null echo "$$" date +%s 2>/dev/null cat "$msg" } | git hash-object --stdin 2>/dev/null | cut -c1-40 ) [ -n "$id" ] || exit 0 # A switch reads the way atgc reads its own: unset, empty, 0, false, no and # off are off, in any case. on() { case $(printf '%s' "$1" | tr '[:upper:]' '[:lower:]') in '' | 0 | false | no | off) return 1 ;; esac return 0 } # A value goes into the trailer only if it is plain: trimmed, and made of # letters, digits and `._@/+-`. Anything else prints nothing, as if unset. plain() { v=$(printf '%s' "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') case $v in '' | *[!A-Za-z0-9._@/+-]*) return 0 ;; esac printf '%s' "$v" } # The agent, `name[@version]`. `AI_AGENT` is shared across tools; Claude Code # writes it as `claude-code_2-1-281_agent`. The rest are per-harness, and each # is set in the commands its harness runs and named in its own documentation. # Claude Code's `CLAUDECODE` is not used: its IDE extensions set that in a # person's terminal too. agent=$(plain "${AI_AGENT:-}") case $agent in claude-code_*_agent) version=${agent#claude-code_} agent=claude-code@$(printf '%s' "${version%_agent}" | sed 's/-/./g') ;; esac if [ -z "$agent" ]; then if on "${CLAUDE_CODE_CHILD_SESSION:-}"; then agent=claude-code elif on "${GEMINI_CLI:-}"; then agent=gemini-cli fi fi # The model the agent is running, when something hands it over. # `ATGC_AGENT_MODEL` is for any harness or wrapper to set; the vibescrobble # launcher sets `VIBESCROBBLE_MODEL`. A harness's own model setting is a # default, not what is running, so none is read. model=$(plain "${ATGC_AGENT_MODEL:-}") [ -n "$model" ] || model=$(plain "${VIBESCROBBLE_MODEL:-}") assisted= if [ -n "$agent" ] && [ "$(git config --bool --get atgc.assistedByTrailer 2>/dev/null)" != false ]; then assisted=$agent${model:+:$model} fi # `interpret-trailers` places them in the trailer block, after a Signed-off-by # and before nothing, and knows about comments and the scissors line — all # the reasons not to append a line by hand here. `doNothing` keeps a trailer # the message already has. set -- --trailer "Change-Id: I$id" [ -z "$assisted" ] || set -- --trailer "Assisted-by: $assisted" "$@" git interpret-trailers --if-exists doNothing "$@" --in-place "$msg" 2>/dev/null exit 0