-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh
More file actions
executable file
·34 lines (30 loc) · 1.15 KB
/
Copy pathgh
File metadata and controls
executable file
·34 lines (30 loc) · 1.15 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
#!/usr/bin/env bash
# Pick the GitHub token matching the repo's git identity.
#
# gh's active account is global state in ~/.config/gh/hosts.yml, so `gh auth
# switch` races with concurrent sessions. Per-repo git identity already resolves
# correctly via the includeIf blocks in ~/.gitconfig, and each account's git
# user.name equals its GitHub login, so that name selects the token directly.
#
# Falls through to gh's own active account whenever the mapping is unavailable:
# outside a repo, with no user.name, or when no token is stored for that login.
set -uo pipefail
real_gh=""
while IFS= read -r candidate; do
if [ "$candidate" != "$(realpath "$0")" ]; then
real_gh="$candidate"
break
fi
done < <(type -aP gh | xargs -r -n1 realpath 2>/dev/null)
if [ -z "$real_gh" ]; then
echo "gh wrapper: no real gh found on PATH" >&2
exit 127
fi
if [ -z "${GH_TOKEN:-}" ] && [ -z "${GITHUB_TOKEN:-}" ]; then
login="$(git config user.name 2>/dev/null || true)"
if [ -n "$login" ]; then
token="$("$real_gh" auth token --user "$login" 2>/dev/null || true)"
[ -n "$token" ] && export GH_TOKEN="$token"
fi
fi
exec "$real_gh" "$@"