forked from git-lfs/git-lfs
/
config.go
509 lines (426 loc) · 14.1 KB
/
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
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
// Package config collects together all configuration settings
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
package config
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync"
"github.com/ThomsonReutersEikon/go-ntlm/ntlm"
"github.com/bgentry/go-netrc/netrc"
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/tools"
"github.com/rubyist/tracerx"
)
var (
Config = New()
ShowConfigWarnings = false
defaultRemote = "origin"
gitConfigWarningPrefix = "lfs."
)
// FetchPruneConfig collects together the config options that control fetching and pruning
type FetchPruneConfig struct {
// The number of days prior to current date for which (local) refs other than HEAD
// will be fetched with --recent (default 7, 0 = only fetch HEAD)
FetchRecentRefsDays int `git:"lfs.fetchrecentrefsdays"`
// Makes the FetchRecentRefsDays option apply to remote refs from fetch source as well (default true)
FetchRecentRefsIncludeRemotes bool `git:"lfs.fetchrecentremoterefs"`
// number of days prior to latest commit on a ref that we'll fetch previous
// LFS changes too (default 0 = only fetch at ref)
FetchRecentCommitsDays int `git:"lfs.fetchrecentcommitsdays"`
// Whether to always fetch recent even without --recent
FetchRecentAlways bool `git:"lfs.fetchrecentalways"`
// Number of days added to FetchRecent*; data outside combined window will be
// deleted when prune is run. (default 3)
PruneOffsetDays int `git:"lfs.pruneoffsetdays"`
// Always verify with remote before pruning
PruneVerifyRemoteAlways bool `git:"lfs.pruneverifyremotealways"`
// Name of remote to check for unpushed and verify checks
PruneRemoteName string `git:"lfs.pruneremotetocheck"`
}
type Configuration struct {
// Os provides a `*Environment` used to access to the system's
// environment through os.Getenv. It is the point of entry for all
// system environment configuration.
Os Environment
// Git provides a `*Environment` used to access to the various levels of
// `.gitconfig`'s. It is the point of entry for all Git environment
// configuration.
Git Environment
gitConfig map[string]string
CurrentRemote string
NtlmSession ntlm.ClientSession
envVars map[string]string
envVarsMutex sync.Mutex
IsTracingHttp bool
IsDebuggingHttp bool
IsLoggingStats bool
loading sync.Mutex // guards initialization of gitConfig and remotes
remotes []string
extensions map[string]Extension
manualEndpoint *Endpoint
parsedNetrc netrcfinder
urlAliasesMap map[string]string
urlAliasMu sync.Mutex
}
func New() *Configuration {
c := &Configuration{
Os: EnvironmentOf(NewOsFetcher()),
CurrentRemote: defaultRemote,
envVars: make(map[string]string),
}
c.Git = &gitEnvironment{config: c}
c.IsTracingHttp = c.Os.Bool("GIT_CURL_VERBOSE", false)
c.IsDebuggingHttp = c.Os.Bool("LFS_DEBUG_HTTP", false)
c.IsLoggingStats = c.Os.Bool("GIT_LOG_STATS", false)
return c
}
// Values is a convenience type used to call the NewFromValues function. It
// specifies `Git` and `Env` maps to use as mock values, instead of calling out
// to real `.gitconfig`s and the `os.Getenv` function.
type Values struct {
// Git and Os are the stand-in maps used to provide values for their
// respective environments.
Git, Os map[string]string
}
// NewFrom returns a new `*config.Configuration` that reads both its Git
// and Enviornment-level values from the ones provided instead of the actual
// `.gitconfig` file or `os.Getenv`, respectively.
//
// This method should only be used during testing.
func NewFrom(v Values) *Configuration {
return &Configuration{
Os: EnvironmentOf(mapFetcher(v.Os)),
Git: EnvironmentOf(mapFetcher(v.Git)),
gitConfig: v.Git,
envVars: make(map[string]string, 0),
}
}
// Unmarshal unmarshals the *Configuration in context into all of `v`'s fields,
// according to the following rules:
//
// Values are marshaled according to the given key and environment, as follows:
// type T struct {
// Field string `git:"key"`
// Other string `os:"key"`
// }
//
// If an unknown environment is given, an error will be returned. If there is no
// method supporting conversion into a field's type, an error will be returned.
// If no value is associated with the given key and environment, the field will
// // only be modified if there is a config value present matching the given
// key. If the field is already set to a non-zero value of that field's type,
// then it will be left alone.
//
// Otherwise, the field will be set to the value of calling the
// appropriately-typed method on the specified environment.
func (c *Configuration) Unmarshal(v interface{}) error {
into := reflect.ValueOf(v)
if into.Kind() != reflect.Ptr {
return fmt.Errorf("lfs/config: unable to parse non-pointer type of %T", v)
}
into = into.Elem()
for i := 0; i < into.Type().NumField(); i++ {
field := into.Field(i)
sfield := into.Type().Field(i)
key, env, err := c.parseTag(sfield.Tag)
if err != nil {
return err
}
if env == nil {
continue
}
var val interface{}
switch sfield.Type.Kind() {
case reflect.String:
var ok bool
val, ok = env.Get(key)
if !ok {
val = field.String()
}
case reflect.Int:
val = env.Int(key, int(field.Int()))
case reflect.Bool:
val = env.Bool(key, field.Bool())
default:
return fmt.Errorf(
"lfs/config: unsupported target type for field %q: %v",
sfield.Name, sfield.Type.String())
}
if val != nil {
into.Field(i).Set(reflect.ValueOf(val))
}
}
return nil
}
// parseTag returns the key, environment, and optional error assosciated with a
// given tag. It will return the XOR of either the `git` or `os` tag. That is to
// say, a field tagged with EITHER `git` OR `os` is valid, but pone tagged with
// both is not.
//
// If neither field was found, then a nil environment will be returned.
func (c *Configuration) parseTag(tag reflect.StructTag) (key string, env Environment, err error) {
git, os := tag.Get("git"), tag.Get("os")
if len(git) != 0 && len(os) != 0 {
return "", nil, errors.New("lfs/config: ambiguous tags")
}
if len(git) != 0 {
return git, c.Git, nil
}
if len(os) != 0 {
return os, c.Os, nil
}
return
}
// GitRemoteUrl returns the git clone/push url for a given remote (blank if not found)
// the forpush argument is to cater for separate remote.name.pushurl settings
func (c *Configuration) GitRemoteUrl(remote string, forpush bool) string {
if forpush {
if u, ok := c.Git.Get("remote." + remote + ".pushurl"); ok {
return u
}
}
if u, ok := c.Git.Get("remote." + remote + ".url"); ok {
return u
}
if err := git.ValidateRemote(remote); err == nil {
return remote
}
return ""
}
// Manually set an Endpoint to use instead of deriving from Git config
func (c *Configuration) SetManualEndpoint(e Endpoint) {
c.manualEndpoint = &e
}
func (c *Configuration) Endpoint(operation string) Endpoint {
if c.manualEndpoint != nil {
return *c.manualEndpoint
}
if operation == "upload" {
if url, ok := c.Git.Get("lfs.pushurl"); ok {
return NewEndpointWithConfig(url, c)
}
}
if url, ok := c.Git.Get("lfs.url"); ok {
return NewEndpointWithConfig(url, c)
}
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
if endpoint := c.RemoteEndpoint(c.CurrentRemote, operation); len(endpoint.Url) > 0 {
return endpoint
}
}
return c.RemoteEndpoint(defaultRemote, operation)
}
func (c *Configuration) ConcurrentTransfers() int {
if c.NtlmAccess("download") {
return 1
}
uploads := 3
if v, ok := c.Git.Get("lfs.concurrenttransfers"); ok {
n, err := strconv.Atoi(v)
if err == nil && n > 0 {
uploads = n
}
}
return uploads
}
// BasicTransfersOnly returns whether to only allow "basic" HTTP transfers.
// Default is false, including if the lfs.basictransfersonly is invalid
func (c *Configuration) BasicTransfersOnly() bool {
return c.Git.Bool("lfs.basictransfersonly", false)
}
// TusTransfersAllowed returns whether to only use "tus.io" HTTP transfers.
// Default is false, including if the lfs.tustransfers is invalid
func (c *Configuration) TusTransfersAllowed() bool {
return c.Git.Bool("lfs.tustransfers", false)
}
func (c *Configuration) BatchTransfer() bool {
return c.Git.Bool("lfs.batch", true)
}
func (c *Configuration) NtlmAccess(operation string) bool {
return c.Access(operation) == "ntlm"
}
// PrivateAccess will retrieve the access value and return true if
// the value is set to private. When a repo is marked as having private
// access, the http requests for the batch api will fetch the credentials
// before running, otherwise the request will run without credentials.
func (c *Configuration) PrivateAccess(operation string) bool {
return c.Access(operation) != "none"
}
// Access returns the access auth type.
func (c *Configuration) Access(operation string) string {
return c.EndpointAccess(c.Endpoint(operation))
}
// SetAccess will set the private access flag in .git/config.
func (c *Configuration) SetAccess(operation string, authType string) {
c.SetEndpointAccess(c.Endpoint(operation), authType)
}
func (c *Configuration) FindNetrcHost(host string) (*netrc.Machine, error) {
c.loading.Lock()
defer c.loading.Unlock()
if c.parsedNetrc == nil {
n, err := c.parseNetrc()
if err != nil {
return nil, err
}
c.parsedNetrc = n
}
return c.parsedNetrc.FindMachine(host), nil
}
// Manually override the netrc config
func (c *Configuration) SetNetrc(n netrcfinder) {
c.parsedNetrc = n
}
func (c *Configuration) EndpointAccess(e Endpoint) string {
key := fmt.Sprintf("lfs.%s.access", e.Url)
if v, ok := c.Git.Get(key); ok && len(v) > 0 {
lower := strings.ToLower(v)
if lower == "private" {
return "basic"
}
return lower
}
return "none"
}
func (c *Configuration) SetEndpointAccess(e Endpoint, authType string) {
c.loadGitConfig()
tracerx.Printf("setting repository access to %s", authType)
key := fmt.Sprintf("lfs.%s.access", e.Url)
// Modify the config cache because it's checked again in this process
// without being reloaded.
switch authType {
case "", "none":
git.Config.UnsetLocalKey("", key)
c.loading.Lock()
delete(c.gitConfig, strings.ToLower(key))
c.loading.Unlock()
default:
git.Config.SetLocal("", key, authType)
c.loading.Lock()
c.gitConfig[strings.ToLower(key)] = authType
c.loading.Unlock()
}
}
func (c *Configuration) FetchIncludePaths() []string {
patterns, _ := c.Git.Get("lfs.fetchinclude")
return tools.CleanPaths(patterns, ",")
}
func (c *Configuration) FetchExcludePaths() []string {
patterns, _ := c.Git.Get("lfs.fetchexclude")
return tools.CleanPaths(patterns, ",")
}
func (c *Configuration) RemoteEndpoint(remote, operation string) Endpoint {
if len(remote) == 0 {
remote = defaultRemote
}
// Support separate push URL if specified and pushing
if operation == "upload" {
if url, ok := c.Git.Get("remote." + remote + ".lfspushurl"); ok {
return NewEndpointWithConfig(url, c)
}
}
if url, ok := c.Git.Get("remote." + remote + ".lfsurl"); ok {
return NewEndpointWithConfig(url, c)
}
// finally fall back on git remote url (also supports pushurl)
if url := c.GitRemoteUrl(remote, operation == "upload"); url != "" {
return NewEndpointFromCloneURLWithConfig(url, c)
}
return Endpoint{}
}
func (c *Configuration) Remotes() []string {
c.loadGitConfig()
return c.remotes
}
// GitProtocol returns the protocol for the LFS API when converting from a
// git:// remote url.
func (c *Configuration) GitProtocol() string {
if value, ok := c.Git.Get("lfs.gitprotocol"); ok {
return value
}
return "https"
}
func (c *Configuration) Extensions() map[string]Extension {
c.loadGitConfig()
return c.extensions
}
// SortedExtensions gets the list of extensions ordered by Priority
func (c *Configuration) SortedExtensions() ([]Extension, error) {
return SortExtensions(c.Extensions())
}
func (c *Configuration) AllGitConfig() map[string]string {
c.loadGitConfig()
return c.gitConfig
}
func (c *Configuration) urlAliases() map[string]string {
c.urlAliasMu.Lock()
defer c.urlAliasMu.Unlock()
if c.urlAliasesMap == nil {
c.urlAliasesMap = make(map[string]string)
prefix := "url."
suffix := ".insteadof"
for gitkey, gitval := range c.AllGitConfig() {
if strings.HasPrefix(gitkey, prefix) && strings.HasSuffix(gitkey, suffix) {
if _, ok := c.urlAliasesMap[gitval]; ok {
fmt.Fprintf(os.Stderr, "WARNING: Multiple 'url.*.insteadof' keys with the same alias: %q\n", gitval)
}
c.urlAliasesMap[gitval] = gitkey[len(prefix) : len(gitkey)-len(suffix)]
}
}
}
return c.urlAliasesMap
}
// ReplaceUrlAlias returns a url with a prefix from a `url.*.insteadof` git
// config setting. If multiple aliases match, use the longest one.
// See https://git-scm.com/docs/git-config for Git's docs.
func (c *Configuration) ReplaceUrlAlias(rawurl string) string {
var longestalias string
aliases := c.urlAliases()
for alias, _ := range aliases {
if !strings.HasPrefix(rawurl, alias) {
continue
}
if longestalias < alias {
longestalias = alias
}
}
if len(longestalias) > 0 {
return aliases[longestalias] + rawurl[len(longestalias):]
}
return rawurl
}
func (c *Configuration) FetchPruneConfig() FetchPruneConfig {
f := &FetchPruneConfig{
FetchRecentRefsDays: 7,
FetchRecentRefsIncludeRemotes: true,
PruneOffsetDays: 3,
PruneRemoteName: "origin",
}
if err := c.Unmarshal(f); err != nil {
panic(err.Error())
}
return *f
}
func (c *Configuration) SkipDownloadErrors() bool {
return c.Os.Bool("GIT_LFS_SKIP_DOWNLOAD_ERRORS", false) || c.Git.Bool("lfs.skipdownloaderrors", false)
}
// loadGitConfig is a temporary measure to support legacy behavior dependent on
// accessing properties set by ReadGitConfig, namely:
// - `c.extensions`
// - `c.uniqRemotes`
// - `c.gitConfig`
//
// Since the *gitEnvironment is responsible for setting these values on the
// (*config.Configuration) instance, we must call that method, if it exists.
//
// loadGitConfig returns a bool returning whether or not `loadGitConfig` was
// called AND the method did not return early.
func (c *Configuration) loadGitConfig() bool {
if g, ok := c.Git.(*gitEnvironment); ok {
return g.loadGitConfig()
}
return false
}