prepare-commit-msgβ’2.98 kB
#!/bin/bash
# prepare-commit-msg hook to remove Claude Code and AI attribution messages
# Place this file in .git/hooks/prepare-commit-msg and make executable with: chmod +x .git/hooks/prepare-commit-msg
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
# Skip if not a normal commit message (e.g. merge, squash, etc.)
if [ "$COMMIT_SOURCE" != "" ] && [ "$COMMIT_SOURCE" != "message" ]; then
exit 0
fi
# Store the original content of the commit message
ORIGINAL_COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# Save it for pre-commit hook to reference
echo "$ORIGINAL_COMMIT_MSG" > "$HOME/.git_commit_msg.txt"
# Patterns to check for (using fixed strings, not regex)
ATTRIBUTION_PATTERNS=(
"Generated with Claude"
"Generated with Claude Code"
"Generated with [Claude Code]"
"π€ Generated with Claude Code"
"π€ Generated with [Claude Code]"
"Co-Authored-By: Claude"
"noreply@anthropic.com"
"Generated by AI"
"AI-generated"
"Generated by Claude"
"π€ Generated"
"Created with Claude"
)
# Initialize a flag to check if we found any attributions
FOUND_ATTRIBUTION=false
CLEANED_MSG=""
# First, check for the standard Claude attribution block at the end
STANDARD_ATTRIBUTION_BLOCK=$'π€ Generated with [Claude Code](https://claude.ai/code)\n\nCo-Authored-By: Claude <noreply@anthropic.com>'
if [[ "$ORIGINAL_COMMIT_MSG" == *"$STANDARD_ATTRIBUTION_BLOCK"* ]]; then
echo -e "${YELLOW}Warning:${NC} Removed standard Claude attribution block"
CLEANED_MSG="${ORIGINAL_COMMIT_MSG//$STANDARD_ATTRIBUTION_BLOCK/}"
FOUND_ATTRIBUTION=true
else
# If no standard block, process the message line by line for other patterns
while IFS= read -r line; do
modified_line="$line"
# Check if the line contains any attribution pattern
for pattern in "${ATTRIBUTION_PATTERNS[@]}"; do
if [[ "$line" == *"$pattern"* ]]; then
FOUND_ATTRIBUTION=true
echo -e "${YELLOW}Warning:${NC} Removed AI attribution: '${YELLOW}$pattern${NC}'"
# Remove the pattern from the line
modified_line="${modified_line//$pattern/}"
fi
done
# Keep the line (with patterns removed if any were found)
if [ -n "$modified_line" ]; then
CLEANED_MSG="${CLEANED_MSG}${modified_line}
"
fi
done <<< "$ORIGINAL_COMMIT_MSG"
fi
# Only modify the file if attributions were found
if [ "$FOUND_ATTRIBUTION" = true ]; then
echo -e "Automatically removing attribution messages..."
# Trim trailing newlines
CLEANED_MSG=$(echo "$CLEANED_MSG" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
# If the cleaned message is empty, provide a placeholder
if [ -z "$CLEANED_MSG" ]; then
echo "Please provide a meaningful commit message" > "$COMMIT_MSG_FILE"
else
echo "$CLEANED_MSG" > "$COMMIT_MSG_FILE"
fi
echo -e "${GREEN}β
Attribution messages have been removed. Please review the commit message.${NC}"
fi
exit 0