forked from git-lfs/git-lfs
/
config.go
201 lines (164 loc) · 3.94 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
package lfs
import (
"fmt"
"github.com/github/git-lfs/git"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
)
type Configuration struct {
CurrentRemote string
gitConfig map[string]string
remotes []string
httpClient *http.Client
redirectingHttpClient *http.Client
isTracingHttp bool
loading sync.Mutex
}
type Endpoint struct {
Url string
SshUserAndHost string
SshPath string
}
var (
Config = NewConfig()
httpPrefixRe = regexp.MustCompile("\\Ahttps?://")
defaultRemote = "origin"
)
func NewConfig() *Configuration {
c := &Configuration{
CurrentRemote: defaultRemote,
isTracingHttp: len(os.Getenv("GIT_CURL_VERBOSE")) > 0,
}
return c
}
func ObjectUrl(endpoint Endpoint, oid string) (*url.URL, error) {
u, err := url.Parse(endpoint.Url)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "objects")
if len(oid) > 0 {
u.Path = path.Join(u.Path, oid)
}
return u, nil
}
func (c *Configuration) Endpoint() Endpoint {
if url, ok := c.GitConfig("lfs.url"); ok {
return Endpoint{Url: url}
}
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
if endpoint := c.RemoteEndpoint(c.CurrentRemote); len(endpoint.Url) > 0 {
return endpoint
}
}
return c.RemoteEndpoint(defaultRemote)
}
func (c *Configuration) ConcurrentUploads() int {
uploads := 3
if v, ok := c.GitConfig("lfs.concurrentuploads"); ok {
n, err := strconv.Atoi(v)
if err == nil {
uploads = n
}
}
return uploads
}
func (c *Configuration) RemoteEndpoint(remote string) Endpoint {
if len(remote) == 0 {
remote = defaultRemote
}
if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok {
return Endpoint{Url: url}
}
if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
endpoint := Endpoint{Url: url}
if !httpPrefixRe.MatchString(url) {
pieces := strings.SplitN(url, ":", 2)
hostPieces := strings.SplitN(pieces[0], "@", 2)
if len(hostPieces) < 2 {
endpoint.Url = "<unknown>"
return endpoint
}
endpoint.SshUserAndHost = pieces[0]
endpoint.SshPath = pieces[1]
endpoint.Url = fmt.Sprintf("https://%s/%s", hostPieces[1], pieces[1])
}
if path.Ext(url) == ".git" {
endpoint.Url += "/info/lfs"
} else {
endpoint.Url += ".git/info/lfs"
}
return endpoint
}
return Endpoint{}
}
func (c *Configuration) Remotes() []string {
c.loadGitConfig()
return c.remotes
}
func (c *Configuration) GitConfig(key string) (string, bool) {
c.loadGitConfig()
value, ok := c.gitConfig[strings.ToLower(key)]
return value, ok
}
func (c *Configuration) SetConfig(key, value string) {
c.loadGitConfig()
c.gitConfig[key] = value
}
func (c *Configuration) ObjectUrl(oid string) (*url.URL, error) {
return ObjectUrl(c.Endpoint(), oid)
}
type AltConfig struct {
Remote map[string]*struct {
Media string
}
Media struct {
Url string
}
}
func (c *Configuration) loadGitConfig() {
c.loading.Lock()
defer c.loading.Unlock()
if c.gitConfig != nil {
return
}
uniqRemotes := make(map[string]bool)
c.gitConfig = make(map[string]string)
var output string
listOutput, err := git.Config.List()
if err != nil {
panic(fmt.Errorf("Error listing git config: %s", err))
}
fileOutput, err := git.Config.ListFromFile()
if err != nil {
panic(fmt.Errorf("Error listing git config from file: %s", err))
}
output = fileOutput + "\n" + listOutput
lines := strings.Split(output, "\n")
for _, line := range lines {
pieces := strings.SplitN(line, "=", 2)
if len(pieces) < 2 {
continue
}
key := strings.ToLower(pieces[0])
c.gitConfig[key] = pieces[1]
keyParts := strings.Split(key, ".")
if len(keyParts) > 1 && keyParts[0] == "remote" {
remote := keyParts[1]
uniqRemotes[remote] = remote == "origin"
}
}
c.remotes = make([]string, 0, len(uniqRemotes))
for remote, isOrigin := range uniqRemotes {
if isOrigin {
continue
}
c.remotes = append(c.remotes, remote)
}
}