|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# prepare-commit-msg — stamp Claude co-author trailer on Claude-driven commits. |
| 5 | +# |
| 6 | +# Why this exists: |
| 7 | +# commit.template (.gitmessage) only fires when git opens an editor. |
| 8 | +# `git commit -m "..."` and HEREDOC variants (used by Claude Code and |
| 9 | +# scripts) bypass it entirely. That is why commits after 2026-03-30 |
| 10 | +# stopped carrying the "Co-authored-by: Claude" line even though |
| 11 | +# .gitmessage still declared the intent. |
| 12 | +# |
| 13 | +# This hook runs on every commit path, strips any existing Claude |
| 14 | +# co-author line (in any historical variant — "Claude", "Claude Opus 4.6", |
| 15 | +# "Claude Sonnet 4.6"), then appends the canonical trailer. Result: |
| 16 | +# exactly one Claude trailer in a consistent form across the log. |
| 17 | +# |
| 18 | +# Scope: |
| 19 | +# Only stamps when Claude is clearly the driver. Detection is via the |
| 20 | +# CLAUDECODE=1 environment variable, which Claude Code sets in the env |
| 21 | +# of every shell command it runs (including `git commit`). A commit you |
| 22 | +# run yourself in your own terminal has no CLAUDECODE set and is left |
| 23 | +# untouched — your solo commits stay honestly attributed to you alone. |
| 24 | +# |
| 25 | +# This is a stronger honesty guarantee than the old .gitmessage template, |
| 26 | +# which stamped every editor-driven commit regardless of who was typing. |
| 27 | +# |
| 28 | +# Skipped commit sources: |
| 29 | +# - merge : git is assembling a merge commit message; don't stamp. |
| 30 | +# - squash : git is assembling a squash message from other commits. |
| 31 | +# - commit : this is an --amend or cherry-pick reusing an existing |
| 32 | +# message. The trailer is already on the source commit |
| 33 | +# (if it should be). Leave it alone so amends don't mutate |
| 34 | +# history unexpectedly. |
| 35 | +# |
| 36 | +# Install (one-time per clone — already set repo-wide): |
| 37 | +# git config core.hooksPath .githooks |
| 38 | + |
| 39 | +COMMIT_MSG_FILE="$1" |
| 40 | +COMMIT_SOURCE="${2:-}" |
| 41 | + |
| 42 | +# Only stamp when Claude Code is driving this commit. |
| 43 | +if [[ "${CLAUDECODE:-}" != "1" ]]; then |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +case "$COMMIT_SOURCE" in |
| 48 | + merge|squash|commit) exit 0 ;; |
| 49 | +esac |
| 50 | + |
| 51 | +TRAILER="Co-authored-by: Claude <noreply@anthropic.com>" |
| 52 | + |
| 53 | +TMP=$(mktemp) |
| 54 | +trap 'rm -f "$TMP"' EXIT |
| 55 | + |
| 56 | +# Strip any existing Claude co-author line. Character classes (not the /I |
| 57 | +# flag) because BSD sed on macOS does not support case-insensitive match. |
| 58 | +# Matches: "Co-authored-by: Claude", "co-authored-by: Claude Opus 4.6", |
| 59 | +# "Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>", etc. |
| 60 | +sed -E '/^[Cc]o-[Aa]uthored-[Bb]y:[[:space:]]*Claude.*/d' "$COMMIT_MSG_FILE" > "$TMP" |
| 61 | + |
| 62 | +# Append the canonical trailer. interpret-trailers handles the blank-line |
| 63 | +# separator between body and trailer block per git convention. Using |
| 64 | +# addIfDifferent as belt-and-suspenders — we already stripped Claude lines |
| 65 | +# above, so this will always add ours; the flag just guards against future |
| 66 | +# edits to the strip regex. |
| 67 | +git interpret-trailers \ |
| 68 | + --if-exists addIfDifferent \ |
| 69 | + --trailer "$TRAILER" \ |
| 70 | + "$TMP" > "$COMMIT_MSG_FILE" |
0 commit comments