-
Notifications
You must be signed in to change notification settings - Fork 6
/
git-set-author
executable file
·70 lines (66 loc) · 2.27 KB
/
git-set-author
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
#!/usr/bin/env bash
#
# Update the author of the last commit.
#
# Usage:
#
# # 0 args ⟹ read author name and email from Git config
# git set-author [-p|--preserve-committer-date]
#
# # If 1 arg is provided:
# # - If it contains a space, treat it as a name
# # - If it matches an existing "ref" (branch/tag/SHA), copy author info from the corresponding commit
# # - Otherwise, treat it as an email address
# git set-author [-p|--preserve-committer-date] <name|email|ref>
#
# # 2 args ⟹ name, email
# git set-author [-p|--preserve-committer-date] <name> <email>
preserve_committer_date=
case "$1" in
-p | --preserve-committer-date)
preserve_committer_date=1
shift
;;
esac
if [ $# -eq 0 ]; then
name="$(git config user.name)"
email="$(git config user.email)"
author="$$name <$email>"
echo "Setting author: $author" >&2
elif [ $# -eq 1 ]; then
if [[ "$1" == *' '* ]]; then
# If the argument has more than one word, assume it's the author's name.
name="$1"; shift
email="$(git log -1 --format=%ae)"
author="$name <$email>"
echo "Updating author name: $author" >&2
elif git cat-file -t "$1" &>/dev/null; then
# Otherwise, check whether it's a Git "ref", and copy that author name/email if so
ref="$1"; shift
author="$(git show -s --format='%an <%ae>' "$ref")"
echo "Updating author from commit $ref: $author" >&2
else
# Otherwise, assume we're updating the author's email address
email="$1"; shift
name="$(git log -1 --format=%an)"
author="$name <$email>"
echo "Updating author email: $author" >&2
fi
elif [ $# -eq 2 ]; then
name="$1"; shift
email="$1"; shift
author="$name <$email>"
echo "Setting author: $author" >&2
else
echo "Usage: git set-author [-p|--preserve-committer-date] [name <email>]" >&2
echo "Usage: git set-author [-p|--preserve-committer-date] <name> <email>" >&2
exit 1
fi
if [ "$preserve_committer_date" ]; then
GIT_COMMITTER_DATE="$(git log -1 --format=%cd)"
export GIT_COMMITTER_DATE
echo "Preserving committer date: $GIT_COMMITTER_DATE" >&2
fi
cmd=(git commit --amend --no-edit --allow-empty --author "$author")
echo "Running: ${cmd[*]}" >&2
"${cmd[@]}"