Skip to content

Commit 88bc3d1

Browse files
chore: x
1 parent e8bda8a commit 88bc3d1

File tree

1 file changed

+103
-30
lines changed

1 file changed

+103
-30
lines changed

strip-claude-coauthor.sh

Lines changed: 103 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/bin/bash
22

3-
# Script to strip Claude co-author lines from commit messages on current branch
4-
# Usage: ./strip-claude-coauthor.sh [base_branch] [--dry-run]
3+
# Safe script to strip Claude co-author lines ONLY from commits authored by the current user
4+
# This version includes additional safety checks to avoid modifying others' commits
55

66
set -e
77

8+
# Get current user info
9+
USER_EMAIL=$(git config user.email)
10+
USER_NAME=$(git config user.name)
11+
12+
if [ -z "$USER_EMAIL" ] || [ -z "$USER_NAME" ]; then
13+
echo "Error: Git user.email and user.name must be configured"
14+
exit 1
15+
fi
16+
17+
echo "Current user: $USER_NAME <$USER_EMAIL>"
18+
819
# Parse arguments
920
DRY_RUN=false
1021
BASE_BRANCH_ARG=""
@@ -60,11 +71,9 @@ get_pr_base_branch() {
6071

6172
# Determine base branch
6273
if [ -n "$BASE_BRANCH_ARG" ]; then
63-
# Use provided base branch
6474
BASE_BRANCH="$BASE_BRANCH_ARG"
6575
echo "Using provided base branch: $BASE_BRANCH"
6676
elif PR_BASE=$(get_pr_base_branch "$CURRENT_BRANCH"); then
67-
# Use base branch from PR
6877
BASE_BRANCH="$PR_BASE"
6978
echo "Detected PR base branch: $BASE_BRANCH"
7079
else
@@ -90,40 +99,76 @@ fi
9099
echo "Merge base: $MERGE_BASE"
91100
echo "Base branch: $BASE_BRANCH"
92101

93-
# Check if there are any commits to rewrite
94-
COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..$CURRENT_BRANCH)
102+
# Get all commits on current branch (excluding merge commits)
103+
ALL_COMMITS=$(git rev-list --no-merges $MERGE_BASE..$CURRENT_BRANCH)
95104

96-
if [ "$COMMIT_COUNT" -eq 0 ]; then
97-
echo "No commits to rewrite on current branch"
105+
if [ -z "$ALL_COMMITS" ]; then
106+
echo "No commits to process on current branch"
98107
exit 0
99108
fi
100109

101-
echo "Found $COMMIT_COUNT commits to process"
110+
TOTAL_COMMITS=$(echo "$ALL_COMMITS" | wc -l)
111+
echo "Found $TOTAL_COMMITS total non-merge commits on branch"
102112

103-
# Show detailed commit information
113+
# Filter to only YOUR commits
104114
echo ""
105-
echo "=== COMMITS TO BE MODIFIED ==="
106-
git log --oneline --no-merges $MERGE_BASE..$CURRENT_BRANCH
115+
echo "=== FILTERING TO YOUR COMMITS ONLY ==="
116+
YOUR_COMMITS=""
117+
YOUR_COMMIT_COUNT=0
107118

108-
# Check which commits actually contain Claude co-author lines
119+
while IFS= read -r commit_hash; do
120+
if [ -z "$commit_hash" ]; then continue; fi
121+
122+
# Get commit author email and name
123+
commit_author_email=$(git log --format="%ae" -n 1 "$commit_hash")
124+
commit_author_name=$(git log --format="%an" -n 1 "$commit_hash")
125+
126+
# Only include commits authored by current user
127+
if [ "$commit_author_email" = "$USER_EMAIL" ] && [ "$commit_author_name" = "$USER_NAME" ]; then
128+
YOUR_COMMITS="$YOUR_COMMITS$commit_hash"$'\n'
129+
YOUR_COMMIT_COUNT=$((YOUR_COMMIT_COUNT + 1))
130+
echo "$commit_hash $(git log --format=%s -n 1 "$commit_hash")"
131+
else
132+
echo "$commit_hash $(git log --format=%s -n 1 "$commit_hash") [Author: $commit_author_name <$commit_author_email>] - SKIPPED"
133+
fi
134+
done <<< "$ALL_COMMITS"
135+
136+
if [ $YOUR_COMMIT_COUNT -eq 0 ]; then
137+
echo "No commits authored by you found on this branch"
138+
exit 0
139+
fi
140+
141+
# Check which of YOUR commits contain Claude co-author lines
109142
echo ""
110-
echo "=== COMMITS WITH CLAUDE CO-AUTHOR LINES ==="
111-
CLAUDE_COMMITS=0
143+
echo "=== YOUR COMMITS WITH CLAUDE CO-AUTHOR LINES ==="
144+
CLAUDE_COMMITS=""
145+
CLAUDE_COMMIT_COUNT=0
146+
112147
while IFS= read -r commit_hash; do
148+
if [ -z "$commit_hash" ]; then continue; fi
149+
113150
commit_msg=$(git log --format=%B -n 1 "$commit_hash")
114-
if echo "$commit_msg" | grep -q -E "(🤖 Generated with \[Claude Code\]|Co-Authored-By: Claude|Co-authored-by: Claude)"; then
115-
echo "$commit_hash $(git log --format=%s -n 1 "$commit_hash")"
116-
CLAUDE_COMMITS=$((CLAUDE_COMMITS + 1))
151+
if echo "$commit_msg" | grep -q -E "(🤖 Generated with \\[Claude Code\\]|Co-Authored-By: Claude|Co-authored-by: Claude)"; then
152+
CLAUDE_COMMITS="$CLAUDE_COMMITS$commit_hash"$'\n'
153+
CLAUDE_COMMIT_COUNT=$((CLAUDE_COMMIT_COUNT + 1))
154+
echo "📝 $commit_hash $(git log --format=%s -n 1 "$commit_hash")"
117155
fi
118-
done < <(git rev-list --no-merges $MERGE_BASE..$CURRENT_BRANCH)
156+
done <<< "$YOUR_COMMITS"
119157

120158
echo ""
121-
echo "=== SUMMARY ==="
159+
echo "=== SAFETY SUMMARY ==="
160+
echo "Current user: $USER_NAME <$USER_EMAIL>"
122161
echo "Current branch: $CURRENT_BRANCH"
123162
echo "Base branch: $BASE_BRANCH"
124-
echo "Merge base: $MERGE_BASE"
125-
echo "Total commits on branch: $COMMIT_COUNT"
126-
echo "Commits with Claude co-author lines: $CLAUDE_COMMITS"
163+
echo "Total commits on branch: $TOTAL_COMMITS"
164+
echo "YOUR commits on branch: $YOUR_COMMIT_COUNT"
165+
echo "YOUR commits with Claude co-author: $CLAUDE_COMMIT_COUNT"
166+
echo "Other authors' commits: $((TOTAL_COMMITS - YOUR_COMMIT_COUNT)) (will be PRESERVED)"
167+
168+
if [ $CLAUDE_COMMIT_COUNT -eq 0 ]; then
169+
echo "No commits authored by you contain Claude co-author lines"
170+
exit 0
171+
fi
127172

128173
if [ "$DRY_RUN" = true ]; then
129174
echo ""
@@ -132,6 +177,16 @@ if [ "$DRY_RUN" = true ]; then
132177
exit 0
133178
fi
134179

180+
echo ""
181+
echo "⚠️ WARNING: This will rewrite git history for commits authored by you only"
182+
echo "Other authors' commits will be preserved unchanged"
183+
read -p "Are you sure you want to proceed? (y/N): " -n 1 -r
184+
echo
185+
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
186+
echo "Aborted"
187+
exit 1
188+
fi
189+
135190
# Check for unstaged changes
136191
if ! git diff-index --quiet HEAD --; then
137192
echo "Stashing unstaged changes..."
@@ -147,18 +202,36 @@ if git show-ref --verify --quiet refs/original/refs/heads/$CURRENT_BRANCH; then
147202
git update-ref -d refs/original/refs/heads/$CURRENT_BRANCH
148203
fi
149204

150-
# Run filter-branch to strip Claude co-author lines
151-
echo "Rewriting commit messages..."
152-
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --msg-filter '
153-
sed "/🤖 Generated with \\[Claude Code\\]/d; /Co-Authored-By: Claude/d; /Co-authored-by: Claude/d"
154-
' $MERGE_BASE..$CURRENT_BRANCH
205+
# Create a safer filter that only modifies commits by the current user
206+
echo "Rewriting commit messages for YOUR commits only..."
207+
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --msg-filter "
208+
# Get current commit hash from environment
209+
commit_author_email=\$(git log --format='%ae' -n 1 \$GIT_COMMIT 2>/dev/null || echo '')
210+
commit_author_name=\$(git log --format='%an' -n 1 \$GIT_COMMIT 2>/dev/null || echo '')
211+
212+
# Only modify commits by current user
213+
if [ \"\$commit_author_email\" = \"$USER_EMAIL\" ] && [ \"\$commit_author_name\" = \"$USER_NAME\" ]; then
214+
# Strip Claude co-author lines for current user's commits
215+
sed '/🤖 Generated with \\\\[Claude Code\\\\]/d; /Co-Authored-By: Claude/d; /Co-authored-by: Claude/d'
216+
else
217+
# Preserve other authors' commit messages unchanged
218+
cat
219+
fi
220+
" $MERGE_BASE..$CURRENT_BRANCH
155221

156-
echo "Successfully stripped Claude co-author lines from $COMMIT_COUNT commits"
222+
echo "Successfully processed $YOUR_COMMIT_COUNT of your commits (out of $TOTAL_COMMITS total)"
223+
echo "Stripped Claude co-author lines from $CLAUDE_COMMIT_COUNT commits"
157224

158225
# Restore stashed changes if any
159226
if [ "$STASHED" = true ]; then
160227
echo "Restoring stashed changes..."
161228
git stash pop
162229
fi
163230

164-
echo "Done! Use 'git push --force-with-lease origin $CURRENT_BRANCH' to update remote"
231+
echo ""
232+
echo "✅ Done! Git history rewritten safely:"
233+
echo " - Only YOUR commits were modified"
234+
echo " - Other authors' commits were preserved unchanged"
235+
echo " - Merge commits were not touched"
236+
echo ""
237+
echo "Use 'git push --force-with-lease origin $CURRENT_BRANCH' to update remote"

0 commit comments

Comments
 (0)