-
Notifications
You must be signed in to change notification settings - Fork 34
/
git-nym
executable file
·41 lines (36 loc) · 1.14 KB
/
git-nym
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
#!/bin/bash
# NYM=... - part of *unique* key name of any ~/.ssh/id_*
# NYM_CLEAN=flag - if set, uses "clean" ssh - without control/agent sockets
# XXX: with git-2.3, also possible with GIT_SSH_COMMAND var
[[ -n "$NYM_RUN" ]] && {
extras=
[[ -n "$NYM_CLEAN" ]] && {
extras=( -o ControlPath=none -o ControlMaster=no )
export SSH_AGENT_PID= SSH_AUTH_SOCK=
}
exec ssh "${extras[@]}" -o PasswordAuthentication=no -i "$NYM" "$@"
}
[[ -z "$NYM" ]] && { echo >&2 "No NYM env var set"; exit 1; }
[[ ! -e "${NYM}" ]] && {
p="${HOME}/.ssh/${NYM}"
[[ -e "$p" ]] && export NYM_RUN=true NYM=$p
[[ -z "${NYM_RUN}" ]] && {
for t in rsa ecdsa ed25519; do
p="${HOME}/.ssh/id_${t}__${NYM}"
[[ ! -e "$p" ]] && continue
export NYM_RUN=true NYM=$p
break
done
}
[[ -z "${NYM_RUN}" ]] && {
readarray -t ids < <(ls -1 "$HOME"/.ssh/id_* | grep -F "$NYM" | grep -v '\.pub$')
[[ ${#ids[@]} -gt 1 ]] && {
echo >&2 "More than one ID matching NYM ('${NYM}'): ${ids[@]}"
exit 2; }
[[ ${#ids[@]} -le 0 ]] && {
echo >&2 "Failed to find ID matching NYM ('${NYM}'): ${ids[@]}"
exit 2; }
export NYM_RUN=true NYM=${ids[0]}
}
}
GIT_SSH=$0 exec git "$@"