forked from samrocketman/git-identity-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-idm
executable file
·460 lines (435 loc) · 15.9 KB
/
git-idm
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/bash
#Created by Sam Gleske
#License: MIT
#Project URL: https://github.com/samrocketman/git-identity-manager
version='0.7'
if ! git --version | awk 'BEGIN { FS="." }; $2 < 13 { exit(1) }'; then
echo 'WARNING: "git --version" is older than Git 2.13. git-idm will have unexpected behavior.' >&2
fi
function echo_version() {
echo "git idm v${version}" >&2
}
function remove_tracked_identities() {
git config --global --list |
grep -i "includeif\.gitdir:.*\.idm=${id}" |
sed "s/\\.idm=${id}\$//" |
sed 's/^includeif/includeIf/' |
while read -r section; do
run_command git config --global --remove-section "${section}"
done
if [ -f ~/".gitconfig_idm_${id}" ]; then
run_command rm -f ~/".gitconfig_idm_${id}"
fi
}
function check_identity_exists() {
if ! list_identities | grep "^${id}\$" > /dev/null; then
echo "ERROR: ${id} is not a valid identity." >&2
return 1
fi
return 0
}
function list_identities() {
git config --global --list |
awk '
BEGIN {
FS="."
}
$1 == "gitidm" && section != $2 {
section=$2
print $2
}
'
}
function usage() {
local cmd="${0##*/git-}"
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:
Basic commands for add, list, use, and remove.
git ${cmd} add jcool --name "Joe Cool" --email "joe@example.com" --key ~/.ssh/id_rsa
git ${cmd} list
git ${cmd} use jcool
git ${cmd} remove jcool
Auto-switching identities based on a tracked directory path of cloned
repositories.
git ${cmd} track jcool --directory ~/git/personal
git ${cmd} track work --directory ~/git/work
List directories automatically tracked.
git ${cmd} list jcool --tracked
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 identities. ls for short. Alternately list track
directories.
remove - Remove a single identity or all identities. rm for short.
track - Tracks a directory to automatically switch Git identities.
This is a convenience option which auto-switches identity based
on the path of your cloned project.
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.
--signing-key SIGNING_KEY - (optional) add a gpg signing key.
--sign-comits SIGN_COMMITS (optional) whether or not to sign commits.
--ssh-command SSH_COMMAND - Customize the SSH command ignoring --key.
list:
Has no options.
list: (alternate usage)
ID - Identity which will be used to track.
--tracked - List directories tracked by an identity for autoswitching
authorship.
use:
ID - Identity to activate.
remove:
ID - Remove the identity. If ID is "all" then all identities will be
removed.
track:
ID - Identity which will track the directory.
--directory DIR - A directory path for auto-switching identities. For
this DIR path git will automatically switch to the
specified ID for authorship. This helps prevent
accidentally using the wrong identity for
contributions for projects cloned under the DIR path.
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=""
DIRECTORY=""
LIST_TRACKED=false
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
;;
--directory)
if [ ! -d "$2" ]; then
echo 'ERROR: --directory must have a valid directory following it.' >&2
exit 1
fi
DIRECTORY="${2%/}/"
shift 2
;;
--signing-key)
if [ -z "$2" ]; then
echo 'ERROR: --signing-key must have a valid key following it.' >&2
exit 1
fi
SIGNING_KEY="$2"
shift 2
;;
--sign-commits)
SIGN_COMMITS=true
shift
;;
--tracked)
LIST_TRACKED=true
shift
;;
-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)"
SIGNING_KEY="$(git config --global --get gitidm."${ACTIVE_ID}".signingKey)"
REAL_SIGNING_KEY="$(git config --global --get user.signingkey)"
SIGN_COMMITS="$(git config --global --get gitidm."${ACTIVE_ID}".signCommits)"
REAL_SIGN_COMMITS="$(git config --global --get commit.gpgsign)"
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 [ ! "${SIGNING_KEY}" = "${REAL_SIGNING_KEY}" ]; then
echo "WARNING: user.signingKey '${SIGNING_KEY}' does not match the one used by the identity." >&2
DISCREPENCY=true
fi
if [ ! "${SIGN_COMMITS}" = "${REAL_SIGN_COMMITS}" ]; then
echo "WARNING: commit.gpgsign '${SIGN_COMMITS}' 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 echo "${id}" | grep -o '[^-_A-Za-z0-9]' > /dev/null; then
echo "ERROR: Identity name '${id}' contains invalid characters. Identity names are limited to uppercase, lowercase, underscore, and hyphen characters." >&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
if [ -n "${SIGNING_KEY}" ]; then
git config --global gitidm."${id}".signingKey "${SIGNING_KEY}"
fi
if [ -n "${SIGN_COMMITS}" ]; then
git config --global gitidm."${id}".signCommits "${SIGN_COMMITS}"
fi
;;
help)
usage
;;
list|ls)
if [ "${LIST_TRACKED}" = true ]; then
echo "${id} identity will automatically apply to the following directories:"
git config --global --list | grep "includeif\.gitdir:.*\.idm=${id}" | sed "s/^includeif\\.gitdir:\\(.*\\)\\.idm=${id}\$/ \\1/"
else
git config --global --list | print_identities
fi
;;
remove|rm)
if [ -z "${id}" ]; then
echo 'ERROR: No identity selected for removal.'
exit 1
fi
if [ "${id}" = all ]; then
list_identities |
while read -r x; do
"$0" remove "$x"
done
else
remove_tracked_identities
run_command git config --global --remove-section gitidm."${id}"
echo "Removed identity ${id}."
fi
;;
track)
if ! check_identity_exists; then
echo 'ERROR: git idm track <identity> --directory <directory> must have a valid identity. See "git idm ls".' >&2
exit 1
fi
if [ -z "${DIRECTORY:-}" ]; then
echo 'ERROR: git idm track <identity> --directory <directory> requires --directory option to be passed. See "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)"
SIGNING_KEY="$(git config --global --get gitidm."${id}".signingKey)"
SIGN_COMMITS="$(git config --global --get gitidm."${id}".signCommits)"
if [ -n "${NAME}" ]; then
run_command git config --file ~/".gitconfig_idm_${id}" user.name "${NAME}"
fi
if [ -n "${EMAIL}" ]; then
run_command git config --file ~/".gitconfig_idm_${id}" user.email "${EMAIL}"
fi
if [ -n "${SSH_COMMAND}" ]; then
run_command git config --file ~/".gitconfig_idm_${id}" core.sshCommand "${SSH_COMMAND}"
fi
if [ -n "${SIGNING_KEY}" ]; then
run_command git config --file ~/".gitconfig_idm_${id}" user.signingkey "${SIGNING_KEY}"
fi
if [ -n "${SIGN_COMMITS}" ]; then
run_command git config --file ~/".gitconfig_idm_${id}" commit.gpgsign "${SIGN_COMMITS}"
fi
run_command git config --global includeIf."gitdir:${DIRECTORY}".idm "${id}"
run_command git config --global includeIf."gitdir:${DIRECTORY}".path ~/".gitconfig_idm_${id}"
;;
uninstall)
"$0" remove all
#throw away the exit code because we don't care
run_command git config --global --unset user.activeidm || true
echo >&2
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)"
SIGNING_KEY="$(git config --global --get gitidm."${id}".signingKey)"
SIGN_COMMITS="$(git config --global --get gitidm."${id}".signCommits)"
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
if [ -n "${SIGNING_KEY}" ];
then
run_command git config --global user.signingkey "${SIGNING_KEY}"
else
run_command git config --global --unset user.signingkey
fi
if [ -n "${SIGN_COMMITS}" ];
then
run_command git config --global commit.gpgsign "${SIGN_COMMITS}"
else
run_command git config --global --unset commit.gpgsign
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