forked from samrocketman/git-identity-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-idm
More file actions
executable file
·303 lines (286 loc) · 9.35 KB
/
Copy pathgit-idm
File metadata and controls
executable file
·303 lines (286 loc) · 9.35 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
#Created by Sam Gleske
#License: MIT
#Project URL: https://github.com/samrocketman/git-identity-manager
version='0.6'
if ! git --version | awk 'BEGIN { FS="." }; $2 < 10 { exit(1) }'; then
echo 'WARNING: "git --version" is older than Git 2.10. git-idm will have unexpected behavior.' >&2
fi
function echo_version() {
echo "git idm v${version}" >&2
}
function usage() {
cat >&2 <<EOF
Git identity manager (git idm). It allows you to switch between git identities
for user, name, and SSH private key used for authoring and publishing git
commits.
Synopsis: git ${0##*/git-} COMMAND [ID] [OPTIONS]
Example usage:
git ${0##*/git-} add jcool --name "Joe Cool" --email "joe@example.com" --key ~/.ssh/id_rsa
git ${0##*/git-} list
git ${0##*/git-} use jcool
git ${0##*/git-} remove jcool
Commands:
active - Display the identity currently used by git idm.
add - Add or update an identity. --name, --email, and --key are
required when adding an identity for the first time only.
list - List identites. ls for short.
remove - Remove a single identity or all identities. rm for short.
uninstall - Removes all git idm data from global gitconfig. All git idm
identities will be removed. This will not affect settings not
related to git idm.
use - Actively use an identity for git authorship and publishing.
version - Output version information.
Command options:
active has no options
add:
ID - The first argument is the identity ID.
--name NAME - Name of the associated identity.
--email EMAIL - Email of the associated identity.
--key SSH_KEY - SSH private key of the associated identity.
--ssh-command SSH_COMMAND - Customize the SSH command ignoring --key.
list has no options.
use:
ID - Identity to activate.
remove:
ID - Remove the identity. If ID is "all" then all identities will be
removed.
uninstall has no options
EOF
echo_version
exit 1
}
function print_identities() {
awk '
BEGIN {
FS="."
}
$1 == "gitidm" && section != $2 {
section=$2
print $2
}
$1 == "gitidm" {
output=" "$3
for(i = 4; i <= NF; i++) {
output=output"."$i
}
print output
}
'
}
function run_command() {
case $4 in
user.name|user.email|core.sshCommand)
echo "$1 $2 $3 $4 \"$5\""
;;
*)
echo "$*"
;;
esac
"$@"
}
function identity_exists() {
git config --global --list | grep "^gitidm\\.${id}\\." &> /dev/null
}
function check_ssh_agent() {
if ! ssh-add -l | grep -F -- "$1" &> /dev/null; then
echo "WARNING: $1 has not been added to ssh-agent. To fix run: ssh-add '$1'" >&2
fi
}
#################
# OPTION HANDLING
#################
# command (required in all cases)
comm="$1"
shift
# identity ID (optional in some cases)
id="$1"
shift
NAME=""
EMAIL=""
SSH_KEY=""
SSH_COMMAND=""
if [ -z "${comm}" ]; then
echo "ERROR: command not specified. See 'git idm help'."
exit 1
fi
if [[ "${comm}" = "-h" || "${id}" = "-h" ]]; then
usage
fi
if [[ "${comm}" = "-v" || "${id}" = "-v" || "${comm}" = "--version" || "${id}" = "--version" ]]; then
echo_version
exit
fi
# parse additional arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--name)
NAME="$2"
shift 2
;;
--email)
EMAIL="$2"
shift 2
;;
--key)
if [ ! -r "$2" ]; then
echo "ERROR: Private key ${2} does not exist." >&2
exit 1
fi
if [ -n "${SSH_COMMAND}" ]; then
echo 'ERROR: --key conflicts with --ssh-command. Use one or the other.' >&2
exit 1
fi
SSH_KEY="$2"
SSH_COMMAND="ssh -i $2 -o IdentitiesOnly=yes -F /dev/null"
shift 2
;;
--ssh-command)
if [ -n "${SSH_KEY}" ]; then
echo 'ERROR: --ssh-command conflicts with --key. Use one or the other.' >&2
exit 1
fi
SSH_COMMAND="$2"
shift 2
;;
-h)
usage
;;
--version|-v)
echo_version
exit
;;
*)
echo "ERROR: option $1 not recognized. See 'git idm help'." >&2
exit 1
;;
esac
done
###################
# RUN COMMAND LOGIC
###################
case "${comm}" in
active)
ACTIVE_ID="$(git config --global --get user.activeidm)"
NAME="$(git config --global --get gitidm."${ACTIVE_ID}".name)"
REAL_NAME="$(git config --global --get user.name)"
EMAIL="$(git config --global --get gitidm."${ACTIVE_ID}".email)"
REAL_EMAIL="$(git config --global --get user.email)"
SSH_COMMAND="$(git config --global --get gitidm."${ACTIVE_ID}".sshCommand)"
REAL_SSH_COMMAND="$(git config --global --get core.sshCommand)"
SSH_KEY="$(git config --global --get gitidm."${ACTIVE_ID}".sshKey)"
DISCREPENCY=false
if [ -n "${ACTIVE_ID}" ]; then
git config --global --list | grep "^gitidm\\.${ACTIVE_ID}\\." | print_identities
check_ssh_agent "${SSH_KEY}"
if [ ! "${NAME}" = "${REAL_NAME}" ]; then
echo "WARNING: user.name '${REAL_NAME}' does not match the one used by the identity." >&2
DISCREPENCY=true
fi
if [ ! "${EMAIL}" = "${REAL_EMAIL}" ]; then
echo "WARNING: user.email '${REAL_EMAIL}' does not match the one used by the identity." >&2
DISCREPENCY=true
fi
if [ ! "${SSH_COMMAND}" = "${REAL_SSH_COMMAND}" ]; then
echo "WARNING: core.sshCommand '${REAL_SSH_COMMAND}' does not match the one used by the identity." >&2
DISCREPENCY=true
fi
if [ "${DISCREPENCY}" = true ]; then
echo "ERROR: found above discrepencies with identity. To fix run 'git idm use ${ACTIVE_ID}'." >&2
exit 1
fi
else
echo 'No identites are active.'
fi
;;
add)
if [ "${id}" = all ]; then
echo 'ERROR: May not add an identity with the ID "all". This is a reserved ID.' >&2
exit 1
fi
if ! identity_exists && [[ -z "${NAME}" || -z "${EMAIL}" || ( -z "${SSH_KEY}" && -z "${SSH_COMMAND}" ) ]]; then
echo 'ERROR: options --name, --email, and --key or --ssh-command are required when adding an identity for the first time.' >&2
exit 1
fi
if [ -n "${NAME}" ]; then
git config --global gitidm."${id}".name "${NAME}"
fi
if [ -n "${EMAIL}" ]; then
git config --global gitidm."${id}".email "${EMAIL}"
fi
if [ -n "${SSH_KEY}" ]; then
git config --global gitidm."${id}".sshKey "${SSH_KEY}"
check_ssh_agent "${SSH_KEY}"
fi
if [ -n "${SSH_COMMAND}" ]; then
git config --global gitidm."${id}".sshCommand "${SSH_COMMAND}"
fi
;;
help)
usage
;;
list|ls)
git config --global --list | print_identities
;;
remove|rm)
if [ -z "${id}" ]; then
echo 'ERROR: No identity selected for removal.'
exit 1
fi
if [ "${id}" = all ]; then
git config --global --list |
awk '
BEGIN {
FS="."
}
$1 == "gitidm" && section != $2 {
section=$2
print $2
}
' |
while read -r x; do
"$0" remove "$x"
done
else
git config --global --remove-section gitidm."${id}"
echo "Removed identity ${id}."
fi
;;
uninstall)
"$0" remove all
#throw away the exit code because we don't care
run_command git config --global --unset user.activeidm || true
echo 'To complete uninstallation run the following command:' >&2
echo "rm \"$0\"" >&2
;;
use)
if ! identity_exists; then
echo "ERROR: Identity ${id} does not exist. See 'git idm list' or 'git idm help'." >&2
exit 1
fi
NAME="$(git config --global --get gitidm."${id}".name)"
EMAIL="$(git config --global --get gitidm."${id}".email)"
SSH_COMMAND="$(git config --global --get gitidm."${id}".sshCommand)"
SSH_KEY="$(git config --global --get gitidm."${id}".sshKey)"
if [ -n "${NAME}" ]; then
run_command git config --global user.name "${NAME}"
fi
if [ -n "${EMAIL}" ]; then
run_command git config --global user.email "${EMAIL}"
fi
if [ -n "${SSH_COMMAND}" ]; then
run_command git config --global core.sshCommand "${SSH_COMMAND}"
fi
run_command git config --global user.activeidm "${id}"
if [ -n "${SSH_KEY}" ]; then
check_ssh_agent "${SSH_KEY}"
fi
;;
version)
echo_version
;;
*)
echo "ERROR: Command ${comm} not found. See 'git idm help'." >&2
exit 1
;;
esac