-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgit-set-id
More file actions
executable file
·67 lines (60 loc) · 1.25 KB
/
git-set-id
File metadata and controls
executable file
·67 lines (60 loc) · 1.25 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
#!/usr/bin/env bash
#
# Set git config user.{name,email}:
#
# $ git set-id [--global] [<name> <email>]
#
# If <name>/<email> are omitted, they will be inferred from the HEAD commit of the current Git repository:
#
# $ git set-id [--global]
usage() {
echo "Usage: $0 [-g] [-c]" >&2
echo "Usage: $0 [-g] [-c] <email | ref>" >&2
echo "Usage: $0 [-g] [-c] <name> <email>" >&2
exit 1
}
name_fmt=an
email_fmt=ae
global=()
while getopts "gc" opt; do
case "$opt" in
c) name_fmt=cn; email_fmt=ce ;;
g) global=("--global") ;;
\?) exit 1 ;;
esac
done
shift $((OPTIND - 1))
name() {
git log -n1 "--format=%$name_fmt" "$@"
}
email() {
git log -n1 "--format=%$email_fmt" "$@"
}
if [ $# -eq 0 ]; then
name="$(name)"
email="$(email)"
elif [ $# -eq 1 ]; then
arg="$1"; shift
if [[ "$arg" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
email="$arg"
name=
else
name="$(name "$arg")"
email="$(email "$arg")"
fi
elif [ $# -eq 2 ]; then
name="$1"; shift
email="$1"; shift
else
usage
fi
run() {
echo "Running: $*" >&2
"$@"
}
if [ -z "$name" ]; then
run git config "${global[@]}" user.email "$email"
else
run git config "${global[@]}" user.name "$name"
run git config "${global[@]}" user.email "$email"
fi