forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_config.go
307 lines (249 loc) · 6.49 KB
/
url_config.go
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
package config
import (
"fmt"
"net/url"
"regexp"
"strings"
)
type URLConfig struct {
git Environment
}
func NewURLConfig(git Environment) *URLConfig {
if git == nil {
git = EnvironmentOf(make(mapFetcher))
}
return &URLConfig{
git: git,
}
}
// Get retrieves a `http.{url}.{key}` for the given key and urls, following the
// rules in https://git-scm.com/docs/git-config#Documentation/git-config.txt-httplturlgt.
// The value for `http.{key}` is returned as a fallback if no config keys are
// set for the given urls.
func (c *URLConfig) Get(prefix, rawurl, key string) (string, bool) {
if c == nil {
return "", false
}
key = strings.ToLower(key)
prefix = strings.ToLower(prefix)
if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
return v[len(v)-1], true
}
return c.git.Get(strings.Join([]string{prefix, key}, "."))
}
func (c *URLConfig) GetAll(prefix, rawurl, key string) []string {
if c == nil {
return nil
}
key = strings.ToLower(key)
prefix = strings.ToLower(prefix)
if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
return v
}
return c.git.GetAll(strings.Join([]string{prefix, key}, "."))
}
func (c *URLConfig) Bool(prefix, rawurl, key string, def bool) bool {
s, _ := c.Get(prefix, rawurl, key)
return Bool(s, def)
}
func (c *URLConfig) getAll(prefix, rawurl, key string) []string {
type urlMatch struct {
key string // The full configuration key
hostScore int // A score indicating the strength of the host match
pathScore int // A score indicating the strength of the path match
userMatch int // Whether we matched on a username. 1 for yes, else 0
}
searchURL, err := url.Parse(rawurl)
if err != nil {
return nil
}
config := c.git.All()
re := regexp.MustCompile(fmt.Sprintf(`\A%s\.(\S+)\.%s\z`, prefix, key))
bestMatch := urlMatch{
key: "",
hostScore: 0,
pathScore: 0,
userMatch: 0,
}
for k := range config {
// Ensure we're examining the correct type of key and parse out the URL
matches := re.FindStringSubmatch(k)
if matches == nil {
continue
}
configURL, err := url.Parse(matches[1])
if err != nil {
continue
}
match := urlMatch{
key: k,
}
// Rule #1: Scheme must match exactly
if searchURL.Scheme != configURL.Scheme {
continue
}
// Rule #2: Hosts must match exactly, or through wildcards. More exact
// matches should take priority over wildcard matches
match.hostScore = compareHosts(searchURL.Hostname(), configURL.Hostname())
if match.hostScore == 0 {
continue
}
if match.hostScore < bestMatch.hostScore {
continue
}
// Rule #3: Port Number must match exactly
if portForURL(searchURL) != portForURL(configURL) {
continue
}
// Rule #4: Configured path must match exactly, or as a prefix of
// slash-delimited path elements
match.pathScore = comparePaths(searchURL.Path, configURL.Path)
if match.pathScore == 0 {
continue
}
// Rule #5: Username must match exactly if present in the config.
// If not present, config matches on any username but with lower
// priority than an exact username match.
if configURL.User != nil {
if searchURL.User == nil {
continue
}
if searchURL.User.Username() != configURL.User.Username() {
continue
}
match.userMatch = 1
}
// Now combine our various scores to determine if we have found a best
// match. Host score > path score > user score
if match.hostScore > bestMatch.hostScore {
bestMatch = match
continue
}
if match.pathScore > bestMatch.pathScore {
bestMatch = match
continue
}
if match.pathScore == bestMatch.pathScore && match.userMatch > bestMatch.userMatch {
bestMatch = match
continue
}
}
if bestMatch.key == "" {
return nil
}
return c.git.GetAll(bestMatch.key)
}
func portForURL(u *url.URL) string {
port := u.Port()
if port != "" {
return port
}
switch u.Scheme {
case "http":
return "80"
case "https":
return "443"
case "ssh":
return "22"
default:
return ""
}
}
// compareHosts compares a hostname with a configuration hostname to determine
// a match. It returns an integer indicating the strength of the match, or 0 if
// the two hostnames did not match.
func compareHosts(searchHostname, configHostname string) int {
searchHost := strings.Split(searchHostname, ".")
configHost := strings.Split(configHostname, ".")
if len(searchHost) != len(configHost) {
return 0
}
score := len(searchHost) + 1
for i, subdomain := range searchHost {
if configHost[i] == "*" {
score--
continue
}
if subdomain != configHost[i] {
return 0
}
}
return score
}
// comparePaths compares a path with a configuration path to determine a match.
// It returns an integer indicating the strength of the match, or 0 if the two
// paths did not match.
func comparePaths(rawSearchPath, rawConfigPath string) int {
f := func(c rune) bool {
return c == '/'
}
searchPath := strings.FieldsFunc(rawSearchPath, f)
configPath := strings.FieldsFunc(rawConfigPath, f)
if len(searchPath) < len(configPath) {
return 0
}
// Start with a base score of 1, so we return something above 0 for a
// zero-length path
score := 1
for i, element := range configPath {
searchElement := searchPath[i]
if element == searchElement {
score += 2
continue
}
if isDefaultLFSUrl(searchElement, searchPath, i+1) {
if searchElement[0:len(searchElement)-4] == element {
// Since we matched without the `.git` prefix, only add one
// point to the score instead of 2
score++
continue
}
}
return 0
}
return score
}
func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, nil
}
return c.hosts(u), c.paths(u.Path)
}
func (c *URLConfig) hosts(u *url.URL) []string {
hosts := make([]string, 0, 1)
if u.User != nil {
hosts = append(hosts, fmt.Sprintf("%s://%s@%s", u.Scheme, u.User.Username(), u.Host))
}
hosts = append(hosts, fmt.Sprintf("%s://%s", u.Scheme, u.Host))
return hosts
}
func (c *URLConfig) paths(path string) []string {
pLen := len(path)
if pLen <= 2 {
return nil
}
end := pLen
if strings.HasSuffix(path, slash) {
end--
}
return strings.Split(path[1:end], slash)
}
const (
gitExt = ".git"
infoPart = "info"
lfsPart = "lfs"
slash = "/"
)
func isDefaultLFSUrl(path string, parts []string, index int) bool {
if len(path) < 5 {
return false // shorter than ".git"
}
if !strings.HasSuffix(path, gitExt) {
return false
}
if index > len(parts)-2 {
return false
}
return parts[index] == infoPart && parts[index+1] == lfsPart
}