-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-author-rewrite
executable file
·115 lines (99 loc) · 2.85 KB
/
git-author-rewrite
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
#!/bin/bash
#|
#| Rewrites author/committer name/e-mail in commit history.
#|
#| Usage:
#|
#| git-author-rewrite OPTIONS [-- ARGUMENTS]
#|
#| Options:
#| -c, --committer rewrite committer
#| -e, --email EMAIL sets the e-mail to replace
#| -f, --force force backup overwrite
#| -n, --name NAME sets the name to replace
#| -h, --help show this help and exit
#| -ne, --new-email the new e-mail
#| -nn, --new-name the new name
#|
#| Notes:
#| - At least one name/e-mail and the its update must be provided.
#| - Anything beyond '--' argument is passed as arguments to the 'git filter'
# command.
#| - For all matches, both author/committer name and e-mail will be replaced
#| (if both are provided).
#| - To push the changes use the following command:
#|
#| git push --force --tags origin 'refs/heads/*'"
#|
#| The easiest way to use is applying this command on a bare repository and
#| then force-push it back to its origin.
#|
# Based on following sources:
#
# https://gist.github.com/octocat/0831f3fbd83ac4d46451
# https://gist.github.com/frz-dev/adf8c2c7275da1369e0cc340feda0ba0
# https://gist.github.com/octocat/0831f3fbd83ac4d46451#gistcomment-2178506
EX_ERROR=1
SCRIPT_FILE="${BASH_SOURCE[0]}"
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--committer)
COMMITTER="true"
shift
;;
-e|--email)
CURRENT_EMAIL="$2"
shift 2
;;
-n|--name)
CURRENT_NAME="$2"
shift 2
;;
-h|--help)
sed '/#|.*/ s/^#|[[:space:]]\?//p;d' < "$SCRIPT_FILE"
exit $EX_ERROR
;;
-ne|--new-email)
NEW_EMAIL="$2"
shift 2
;;
-nn|--new-name)
NEW_NAME="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Unknown option encountered: '$1'" >&2
exit $EX_ERROR
;;
*)
break
;;
esac
done
if [[ -z "$CURRENT_EMAIL" && -z "$CURRENT_NAME" ]]; then
echo "Specify name or e-mail." >&2
exit EX_ERROR
fi
if [[ -z "$NEW_EMAIL" && -z "$NEW_NAME" ]]; then
echo "Specify new name or new e-mail." >&2
exit EX_ERROR
fi
git filter-branch --env-filter "
if [ \"\$GIT_AUTHOR_NAME\" = \"${CURRENT_NAME}\" ] || [ \"\$GIT_AUTHOR_EMAIL\" = \"${CURRENT_EMAIL}\" ]
then
[ ! -z \"$NEW_NAME\" ] && export GIT_AUTHOR_NAME=\"${NEW_NAME}\"
[ ! -z \"$NEW_EMAIL\" ] && export GIT_AUTHOR_EMAIL=\"${NEW_EMAIL}\"
fi
if ( [ \"\$GIT_COMMITTER_EMAIL\" = \"${CURRENT_EMAIL}\" ] ) && [ \"$COMMITTER\" = \"true\" ] && [ ! -z \"$NEW_EMAIL\" ]
then
export GIT_COMMITTER_EMAIL=\"${NEW_EMAIL}\"
fi
if [ \"\$GIT_COMMITTER_NAME\" = \"${CURRENT_NAME}\" ] && [ \"$COMMITTER\" = \"true\" ] && [ ! -z \"$NEW_NAME\" ]
then
export GIT_COMMITTER_NAME=\"${NEW_NAME}\"
fi
" $@ --tag-name-filter cat -- --branches --tags