-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-credential-subpath
More file actions
executable file
·202 lines (179 loc) · 6.26 KB
/
git-credential-subpath
File metadata and controls
executable file
·202 lines (179 loc) · 6.26 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
#!/bin/bash
#
# A credential helper to look up username based on path components.
#
# git credentials are able to be configured to provide a username for a host
# name or a full URL path (if the `credential.useHttpPath` config is set). For
# sites like like `github.com`, you may want to use a different username for
# different sub-paths, as the first path component designates an organisation.
# If you have a work and personal GitHub identity, you may want to select that
# identity based on GitHub organisation. It is not possible to do that directly
# with the configuration used by `gitcredentials(7)`.
#
# This script operates as a git credential helper to sit between git and the
# real credential helper you want to use (called the chained helper in this
# script). It takes the host and path and searches your git config for a
# username by walking the path component by component. If the full path
# matches multiple config entries, the most specific one is selected. The
# username from that configuration is passed to the chained helper so it can
# provide the credentials for the correct username.
#
# This helper requires that the config setting `credential.useHttpPath` is
# true, however it effectively disables that for the credential helper it
# chains to, so that helper will not request or store a password per path
# (which is rarely what you want - you want to be able to select the username
# per path and the password per host/username pair).
#
# If you do need to have the chained helper see the path, you can set the
# config option `credential.subpath.passPath` to true, but beware that it will
# ask for your password for every repository even if it knows the host and
# username for another repo.
# Config command reference:
# git config --global credential.helper subpath store
# git config --global credential.useHttpPath true
# git config --global credential.subpath.passPath true
# git config --global credential.https://github.com/org.username octocat
#-----------------------------------------------------------------------------
usage() {
printf 'Usage: %s {<options>...} <chain> <get|store|erase>\n' "${0##*/}"
printf ' where <chain> is the credential helper to chain to.\n'
printf 'Available options:\n'
printf ' -d print debugging input/output (warning will output password)\n'
printf ' -o arg pass <arg> to the chained helper (multiple allowed)\n'
}
#-----------------------------------------------------------------------------
main() {
set -euo pipefail
declare -A attrs
declare -a attr_order
declare -a chain_args
debug=false
parse_args "$@"
[[ "${cmd}" =~ (erase|get|store) ]] || {
usage >&2
exit 1
}
read_attrs
# We do not alter the username for `set` operations as that provides a
# pair of (username, password) that belong toegether and that pair has
# been returned from a previous `get` operation.
if [[ "${cmd}" =~ (erase|get) ]]; then set_username_from_context; fi
config_has_pass_path || unset 'attrs[path]'
emit_attrs | run_next_helper "${cmd}"
}
#-----------------------------------------------------------------------------
read_attrs() {
debug '%s: attributes read from git:\n' "${cmd}"
while read -r attrval; do
debug ' %s\n' "${attrval}"
local key="${attrval%%=*}"
local val="${attrval#*=}"
if [[ "${key}" == "${attrval}" ]]; then
# No = in attrval line. skip it
continue
fi
attrs["${key}"]="${val}"
attr_order+=("${key}")
done
debug '\n'
}
#-----------------------------------------------------------------------------
emit_attrs() {
for key in "${attr_order[@]}"; do
if [[ -z "${attrs[${key}]+x}" ]]; then
continue
fi
printf '%s=%s\n' "${key}" "${attrs[${key}]}"
done
}
#-----------------------------------------------------------------------------
run_next_helper() {
local cmd="$1"
debug_pipe 'running "git-credential-%s %s %s" with attributes: \n' \
"${chained_helper}" "${chain_args[*]}" "${cmd}" |
"git-credential-${chained_helper}" "${chain_args[@]}" "${cmd}" |
debug_pipe 'attributes returned from %s.%s\n' "${chained_helper}" "${cmd}"
}
#-----------------------------------------------------------------------------
set_username_from_context() {
# Starting at the shortest path, iterating to the longest based on slash
# separated components of ${attrs[path]}, look up the git config for a
# credential username. The username of a longer path will override that of
# a shorter one.
declare -a path_components path_paths
local url
# Split path attribute on slashes, and prepend the protocol://host
IFS=/ read -ra path_paths <<<"${attrs[path]-}"
path_components=("" "${attrs[protocol]}://${attrs[host]}" "${path_paths[@]}")
for component in "${path_components[@]}"; do
url="${url:+${url}/}${component}"
set_username "${url}"
done
}
#-----------------------------------------------------------------------------
set_username() {
local key="${1:+$1.}" username
if username=$(git config --get "credential.${key}username"); then
attrs[username]="${username}"
fi
}
#-----------------------------------------------------------------------------
config_has_pass_path() {
local passPath
passPath=$(git config --get credential.subpath.passPath) &&
[[ "${passPath}" == 'true' ]]
}
#-----------------------------------------------------------------------------
parse_args() {
OPTSTRING=':do:'
while getopts "${OPTSTRING}" opt; do
case "${opt}" in
d)
debug=true
;;
o)
chain_args+=("${OPTARG}")
;;
\?)
printf 'Invalid option: -%s\n\n' "${OPTARG}" >&2
usage >&2
exit 1
;;
:)
printf 'Option -%s requires an argument\n\n' "${OPTARG}" >&2
usage >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Process remaining in "$@"
if (($# != 2)); then
usage >&2
exit 1
fi
chained_helper="$1"
cmd="$2"
}
#-----------------------------------------------------------------------------
debug() {
"${debug}" || return 0
# shellcheck disable=SC2059
# we expect a format string as the first arg
printf "$@" >&2
}
debug_pipe() {
local logged=false
while read -r line; do
# delay logging prefix message until we read the first line from stdin
"${logged}" || {
debug "$@"
logged=true
}
printf '%s\n' "${line}"
debug ' %s\n' "${line}"
done
debug '\n'
}
#-----------------------------------------------------------------------------
[[ "$(caller)" != 0\ * ]] || main "$@"