-
Notifications
You must be signed in to change notification settings - Fork 10
/
git-idm
executable file
·555 lines (521 loc) · 19.1 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/bin/bash
#Created by Sam Gleske
#License: MIT
#Project URL: https://github.com/samrocketman/git-identity-manager
version='0.11'
if ! git --version | awk 'BEGIN { FS="." }; $2 < 18 { exit(1) }'; then
echo 'WARNING: "git --version" is older than Git 2.18. git-idm will have unexpected behavior.' >&2
fi
function initial_setup() {
if [ ! -f ~/.gitconfig ]; then
touch ~/.gitconfig
fi
if ! { git config --global -l | grep '^user\.' || grep -F '[user]' ~/.gitconfig; } > /dev/null; then
echo '[user]' >> ~/.gitconfig
fi
if ! { git config --global -l | grep '^include\.' || grep -F '[include]' ~/.gitconfig; } > /dev/null; then
echo '[include]' >> ~/.gitconfig
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
}
'
}
# TODO: remove migration 5 releases after v0.9.
function migrate_active_identities() {
local stored_id=""
local migrate=false
for x in $(list_identities); do
if [ -f ~/".gitconfig_idm_${x}" ]; then
stored_id="$(git config --file ~/".gitconfig_idm_${x}" --get user.activeidm)"
if [ -n "${stored_id}" ]; then
break
fi
git config --file ~/".gitconfig_idm_${x}" user.activeidm "${x}"
fi
done
}
# TODO: remove migration 5 releases after v0.10.
function migrate_global_config() {
if git config -f ~/.gitconfig --get core.sshCommand > /dev/null; then
run_command git config -f ~/.gitconfig --unset core.sshCommand
fi
if git config -f ~/.gitconfig --get user.signingkey > /dev/null; then
run_command git config -f ~/.gitconfig --unset user.signingkey
fi
if git config -f ~/.gitconfig --get commit.gpgsign; then
run_command git config -f ~/.gitconfig --unset commit.gpgsign
fi
}
function no_ssh_key_warning() {
local SSH_COMMAND="$(git config --global --get gitidm."${1}".sshCommand)"
if [ -z "${SSH_COMMAND:-}" ]; then
echo >&2
echo "WARNING: ${1} identity has no SSH key." >&2
echo ' Copy an SSH key from another identity with copy-key-to command.' >&2
echo ' See "git idm help" for usage.' >&2
fi
}
function add_identity_file() {
if [ -f ~/".gitconfig_idm_${1}" ]; then
return
fi
local NAME="$(git config --global --get gitidm."${1}".name)"
local EMAIL="$(git config --global --get gitidm."${1}".email)"
local SSH_COMMAND="$(git config --global --get gitidm."${1}".sshCommand)"
local SIGNING_KEY="$(git config --global --get gitidm."${1}".signingKey)"
local SIGN_COMMITS="$(git config --global --get gitidm."${1}".signCommits)"
if [ -n "${NAME}" ]; then
run_command git config --file ~/".gitconfig_idm_${1}" user.name "${NAME}"
fi
if [ -n "${EMAIL}" ]; then
run_command git config --file ~/".gitconfig_idm_${1}" user.email "${EMAIL}"
fi
if [ -n "${SSH_COMMAND}" ]; then
run_command git config --file ~/".gitconfig_idm_${1}" core.sshCommand "${SSH_COMMAND}"
fi
if [ -n "${SIGNING_KEY}" ]; then
run_command git config --file ~/".gitconfig_idm_${1}" user.signingkey "${SIGNING_KEY}"
fi
if [ -n "${SIGN_COMMITS}" ]; then
run_command git config --file ~/".gitconfig_idm_${1}" commit.gpgsign "${SIGN_COMMITS}"
fi
run_command git config --file ~/".gitconfig_idm_${1}" user.activeidm "${1}"
}
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 "[email protected]" --key ~/.ssh/id_rsa
git ${cmd} list
git ${cmd} use jcool
git ${cmd} remove jcool
Adding with a GPG key and signing each commit
git ${cmd} add jcool --name "Joe Cool" --email "[email protected]" --key ~/.ssh/id_rsa --signing-key 1AA11AAA111A1AAA --sign-commits
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
Copy SSH key from another identity.
git ${cmd} copy-key-to jcool --from-id work
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.
copy-key-to - Copy an SSH key from another identity.
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-commits SIGN_COMMITS (optional) whether or not to sign commits.
--ssh-command SSH_COMMAND - Customize the SSH command ignoring --key.
copy-key-to:
ID - Identity to copy an SSH key to.
--from-id ID - The source identity to copy the SSH key from.
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() {
local args=( "$@" )
case $4 in
user.name|user.email|core.sshCommand)
msg=( "$1" "$2" "$3" "$4" \""$5"\" )
echo "${msg[@]} $*"
;;
*)
echo "$*"
;;
esac
"${args[@]}"
}
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=""
FROM_ID=""
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
;;
--from-id)
FROM_ID="$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)
# TODO: remove migration 5 or 6 releases after v0.9.
migrate_active_identities
ACTIVE_ID="$(git config --get user.activeidm)"
GLOBAL_ACTIVE_ID="$(git config --global --get user.activeidm)"
NAME="$(git config --global --get gitidm."${ACTIVE_ID}".name)"
REAL_NAME="$(git config --get user.name)"
EMAIL="$(git config --global --get gitidm."${ACTIVE_ID}".email)"
REAL_EMAIL="$(git config --get user.email)"
SSH_COMMAND="$(git config --global --get gitidm."${ACTIVE_ID}".sshCommand)"
REAL_SSH_COMMAND="$(git config --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 --get user.signingkey)"
SIGN_COMMITS="$(git config --global --get gitidm."${ACTIVE_ID}".signCommits)"
REAL_SIGN_COMMITS="$(git config --get commit.gpgsign)"
DISCREPENCY=false
if [ -n "${ACTIVE_ID}" ]; then
if [ ! "${ACTIVE_ID}" = "${GLOBAL_ACTIVE_ID}" ]; then
cat >&2 <<EOF
NOTE: identity '${ACTIVE_ID}' has overridden '${GLOBAL_ACTIVE_ID}'. See also
'git idm list ${ACTIVE_ID} --tracked' for full list of tracked directories
for this identity.
EOF
fi
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
no_ssh_key_warning "${ACTIVE_ID}"
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
initial_setup
if [ -n "${NAME}" ]; then
run_command git config --global gitidm."${id}".name "${NAME}"
fi
if [ -n "${EMAIL}" ]; then
run_command git config --global gitidm."${id}".email "${EMAIL}"
fi
if [ -n "${SSH_KEY}" ]; then
run_command git config --global gitidm."${id}".sshKey "${SSH_KEY}"
check_ssh_agent "${SSH_KEY}"
fi
if [ -n "${SSH_COMMAND}" ]; then
run_command git config --global gitidm."${id}".sshCommand "${SSH_COMMAND}"
fi
if [ -n "${SIGNING_KEY}" ]; then
run_command git config --global gitidm."${id}".signingKey "${SIGNING_KEY}"
fi
if [ -n "${SIGN_COMMITS}" ]; then
run_command git config --global gitidm."${id}".signCommits "${SIGN_COMMITS}"
fi
add_identity_file "${id}"
no_ssh_key_warning "${id}"
;;
copy-key-to)
initial_setup
SSH_KEY="$(git config --global --get gitidm."${FROM_ID}".sshKey)"
SSH_COMMAND="$(git config --global --get gitidm."${FROM_ID}".sshCommand)"
if [ -z "${SSH_KEY:-}" ] || [ -z "${SSH_COMMAND:-}" ]; then
echo "ERROR: ${FROM_ID} identity has no SSH key." >&2
echo ' See "git idm ls" or "git idm help".' >&2
exit 1
fi
run_command git config --global gitidm."${id}".sshKey "${SSH_KEY}"
check_ssh_agent "${SSH_KEY}"
;;
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
initial_setup
add_identity_file "${id}"
run_command git config --global includeIf."gitdir:${DIRECTORY}".idm "${id}"
run_command git config --global includeIf."gitdir:${DIRECTORY}".path ~/".gitconfig_idm_${id}"
no_ssh_key_warning "${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
initial_setup
NAME="$(git config --global --get gitidm."${id}".name)"
EMAIL="$(git config --global --get gitidm."${id}".email)"
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 ! git config --global --get include.path '_idm_' > /dev/null; then
run_command git config --global --add include.path ~/".gitconfig_idm_${id}"
else
run_command git config --global --replace-all include.path ~/".gitconfig_idm_${id}" '_idm_'
fi
migrate_global_config
run_command git config --global user.activeidm "${id}"
if [ -n "${SSH_KEY}" ]; then
check_ssh_agent "${SSH_KEY}"
fi
add_identity_file "${id}"
no_ssh_key_warning "${id}"
;;
version)
echo_version
;;
*)
echo "ERROR: Command ${comm} not found. See 'git idm help'." >&2
exit 1
;;
esac