-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathchange-git-author.sh
More file actions
executable file
·220 lines (189 loc) · 6.89 KB
/
Copy pathchange-git-author.sh
File metadata and controls
executable file
·220 lines (189 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# Git提交用户名和邮箱修改脚本
# 同时修改 author 和 committer,支持本地配置和历史重写
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_usage() {
cat << EOF
用法: ./change-git-author.sh [选项]
选项:
-h, --help 显示此帮助信息
-n, --name NAME 新的用户名
-e, --email EMAIL 新的邮箱
-on, --old-name NAME 旧的用户名(仅改写匹配该名字的提交)
-oe, --old-email EMAIL 旧的邮箱(仅改写匹配该邮箱的提交)
-r, --rewrite 改写历史提交(author + committer 均会修改)
-l, --local 仅改变本地配置(默认,仅影响未来的提交)
示例:
# 修改当前仓库配置(仅影响未来的提交)
./change-git-author.sh -n "张三" -e "zhangsan@example.com"
# 改写所有历史提交的 author 和 committer
./change-git-author.sh -n "张三" -e "zhangsan@example.com" -r
# 只改写旧邮箱匹配的历史提交
./change-git-author.sh -oe "old@example.com" -n "新名字" -e "new@example.com" -r
EOF
}
# 初始化变量
NEW_NAME=""
NEW_EMAIL=""
OLD_NAME=""
OLD_EMAIL=""
REWRITE_HISTORY=false
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_usage
exit 0
;;
-n|--name)
NEW_NAME="$2"
shift 2
;;
-e|--email)
NEW_EMAIL="$2"
shift 2
;;
-on|--old-name)
OLD_NAME="$2"
shift 2
;;
-oe|--old-email)
OLD_EMAIL="$2"
shift 2
;;
-r|--rewrite)
REWRITE_HISTORY=true
shift
;;
-l|--local)
REWRITE_HISTORY=false
shift
;;
*)
echo -e "${RED}未知选项: $1${NC}"
print_usage
exit 1
;;
esac
done
# 验证是否在git仓库中
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo -e "${RED}错误: 不在git仓库中${NC}"
exit 1
fi
# 验证必需参数
if [ -z "$NEW_NAME" ] || [ -z "$NEW_EMAIL" ]; then
echo -e "${RED}错误: 必须指定新的用户名(-n)和邮箱(-e)${NC}"
print_usage
exit 1
fi
# 验证邮箱格式
if ! [[ "$NEW_EMAIL" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo -e "${RED}错误: 邮箱格式不正确: $NEW_EMAIL${NC}"
exit 1
fi
echo -e "${YELLOW}===== Git提交信息修改 =====${NC}"
echo "新用户名: $NEW_NAME"
echo "新邮箱: $NEW_EMAIL"
if [ "$REWRITE_HISTORY" = true ]; then
echo -e "${YELLOW}模式: 改写历史提交(author + committer)${NC}"
# 有旧值时只改写匹配的提交,否则改写全部
if [ -n "$OLD_NAME" ] || [ -n "$OLD_EMAIL" ]; then
[ -n "$OLD_NAME" ] && echo "旧用户名: $OLD_NAME"
[ -n "$OLD_EMAIL" ] && echo "旧邮箱: $OLD_EMAIL"
FILTER_MODE="match"
else
echo -e "${YELLOW}未指定旧值,将改写全部提交${NC}"
FILTER_MODE="all"
fi
echo -e "${RED}警告: 这将改写仓库的提交历史,强烈建议执行前备份仓库!${NC}"
read -p "确定要继续吗? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "操作已取消"
exit 1
fi
# 导出变量供 --env-filter 子 shell 使用
export OLD_NAME OLD_EMAIL NEW_NAME NEW_EMAIL FILTER_MODE
if command -v git-filter-repo &>/dev/null; then
# ---- 优先使用现代工具 git-filter-repo ----
echo -e "${YELLOW}使用 git-filter-repo 改写...${NC}"
CALLBACK=$(mktemp /tmp/git-fr-callback-XXXXXX.py)
if [ "$FILTER_MODE" = "all" ]; then
cat > "$CALLBACK" << PYEOF
import os
new_name = os.environ['NEW_NAME'].encode()
new_email = os.environ['NEW_EMAIL'].encode()
def commit_callback(commit):
commit.author_name = new_name
commit.author_email = new_email
commit.committer_name = new_name
commit.committer_email = new_email
PYEOF
else
cat > "$CALLBACK" << PYEOF
import os
old_name = os.environ.get('OLD_NAME', '').encode()
old_email = os.environ.get('OLD_EMAIL', '').encode()
new_name = os.environ['NEW_NAME'].encode()
new_email = os.environ['NEW_EMAIL'].encode()
def commit_callback(commit):
if (old_name and commit.author_name == old_name) or \
(old_email and commit.author_email == old_email):
commit.author_name = new_name
commit.author_email = new_email
if (old_name and commit.committer_name == old_name) or \
(old_email and commit.committer_email == old_email):
commit.committer_name = new_name
commit.committer_email = new_email
PYEOF
fi
git-filter-repo --commit-callback "$(cat "$CALLBACK")" --force
rm -f "$CALLBACK"
else
# ---- 回退到 git filter-branch ----
echo -e "${YELLOW}未检测到 git-filter-repo,回退到 git filter-branch...${NC}"
if [ "$FILTER_MODE" = "all" ]; then
git filter-branch -f --env-filter '
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
' --tag-name-filter cat -- --all
else
git filter-branch -f --env-filter '
MATCH_AUTHOR=false
MATCH_COMMITTER=false
[ -n "$OLD_NAME" ] && [ "$GIT_AUTHOR_NAME" = "$OLD_NAME" ] && MATCH_AUTHOR=true
[ -n "$OLD_EMAIL" ] && [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] && MATCH_AUTHOR=true
[ -n "$OLD_NAME" ] && [ "$GIT_COMMITTER_NAME" = "$OLD_NAME" ] && MATCH_COMMITTER=true
[ -n "$OLD_EMAIL" ] && [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] && MATCH_COMMITTER=true
if [ "$MATCH_AUTHOR" = true ]; then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
if [ "$MATCH_COMMITTER" = true ]; then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --all
fi
fi
echo -e "${GREEN}✓ 历史提交已改写(author 和 committer 均已更新)${NC}"
echo -e "${YELLOW}提示: 推送远程仓库需执行 git push --force${NC}"
else
echo -e "${YELLOW}模式: 修改本地配置(仅影响未来的提交)${NC}"
git config user.name "$NEW_NAME"
git config user.email "$NEW_EMAIL"
echo -e "${GREEN}✓ 本地配置已更新${NC}"
fi
# 显示当前配置
echo -e "\n${YELLOW}当前本地配置:${NC}"
echo "用户名: $(git config user.name)"
echo "邮箱: $(git config user.email)"
echo -e "\n${GREEN}完成!${NC}"