-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgit-set-author
More file actions
executable file
·81 lines (77 loc) · 2.48 KB
/
git-set-author
File metadata and controls
executable file
·81 lines (77 loc) · 2.48 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
#!/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>
committer=
preserve_committer_date=
while getopts "cp" opt; do
case "$opt" in
c) committer=1 ;;
p) preserve_committer_date=1 ;;
\?) echo "Unknown option: -$opt" >&2
exit 1
esac
done
shift $((OPTIND - 1))
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
name="$(git show -s --format='%an' "$ref")"
email="$(git show -s --format='<%ae>' "$ref")"
author="$name <$email>"
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
if [ -z "$committer" ]; then
committer_flags=()
else
committer_flags=(-c user.name="$name" -c user.email="$email")
fi
cmd=(git "${committer_flags[@]}" commit --amend --no-edit --allow-empty --author "$author")
echo "Running: ${cmd[*]}" >&2
"${cmd[@]}"