forked from git-lfs/git-lfs
/
git_fetcher.go
212 lines (175 loc) · 4.71 KB
/
git_fetcher.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
package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/git-lfs/git-lfs/git"
)
type GitFetcher struct {
vmu sync.RWMutex
vals map[string][]string
}
type GitConfig struct {
Lines []string
OnlySafeKeys bool
}
func NewGitConfig(gitconfiglines string, onlysafe bool) *GitConfig {
return &GitConfig{
Lines: strings.Split(gitconfiglines, "\n"),
OnlySafeKeys: onlysafe,
}
}
func ReadGitConfig(configs ...*GitConfig) (gf *GitFetcher, extensions map[string]Extension, uniqRemotes map[string]bool) {
vals := make(map[string][]string)
ignored := make([]string, 0)
extensions = make(map[string]Extension)
uniqRemotes = make(map[string]bool)
for _, gc := range configs {
uniqKeys := make(map[string]string)
for _, line := range gc.Lines {
pieces := strings.SplitN(line, "=", 2)
if len(pieces) < 2 {
continue
}
allowed := !gc.OnlySafeKeys
key, val := strings.ToLower(pieces[0]), pieces[1]
if origKey, ok := uniqKeys[key]; ok {
if ShowConfigWarnings && len(vals[key]) > 0 && vals[key][len(vals[key])-1] != val && strings.HasPrefix(key, gitConfigWarningPrefix) {
fmt.Fprintf(os.Stderr, "WARNING: These git config values clash:\n")
fmt.Fprintf(os.Stderr, " git config %q = %q\n", origKey, vals[key])
fmt.Fprintf(os.Stderr, " git config %q = %q\n", pieces[0], val)
}
} else {
uniqKeys[key] = pieces[0]
}
parts := strings.Split(key, ".")
if len(parts) == 4 && parts[0] == "lfs" && parts[1] == "extension" {
// prop: lfs.extension.<name>.<prop>
name := parts[2]
prop := parts[3]
ext := extensions[name]
ext.Name = name
switch prop {
case "clean":
if gc.OnlySafeKeys {
ignored = append(ignored, key)
continue
}
ext.Clean = val
case "smudge":
if gc.OnlySafeKeys {
ignored = append(ignored, key)
continue
}
ext.Smudge = val
case "priority":
allowed = true
p, err := strconv.Atoi(val)
if err == nil && p >= 0 {
ext.Priority = p
}
}
extensions[name] = ext
} else if len(parts) > 1 && parts[0] == "remote" {
if gc.OnlySafeKeys && (len(parts) == 3 && parts[2] != "lfsurl") {
ignored = append(ignored, key)
continue
}
allowed = true
remote := parts[1]
uniqRemotes[remote] = remote == "origin"
} else if len(parts) > 2 && parts[len(parts)-1] == "access" {
allowed = true
}
if !allowed && keyIsUnsafe(key) {
ignored = append(ignored, key)
continue
}
vals[key] = append(vals[key], val)
}
}
if len(ignored) > 0 {
fmt.Fprintf(os.Stderr, "WARNING: These unsafe lfsconfig keys were ignored:\n\n")
for _, key := range ignored {
fmt.Fprintf(os.Stderr, " %s\n", key)
}
}
gf = &GitFetcher{vals: vals}
return
}
// Get implements the Fetcher interface, and returns the value associated with
// a given key and true, signaling that the value was present. Otherwise, an
// empty string and false will be returned, signaling that the value was
// absent.
//
// Map lookup by key is case-insensitive, as per the .gitconfig specification.
//
// Get is safe to call across multiple goroutines.
func (g *GitFetcher) Get(key string) (val string, ok bool) {
all := g.GetAll(key)
if len(all) == 0 {
return "", false
}
return all[len(all)-1], true
}
func (g *GitFetcher) GetAll(key string) []string {
g.vmu.RLock()
defer g.vmu.RUnlock()
return g.vals[strings.ToLower(key)]
}
func (g *GitFetcher) All() map[string][]string {
newmap := make(map[string][]string)
g.vmu.RLock()
defer g.vmu.RUnlock()
for key, values := range g.vals {
for _, value := range values {
newmap[key] = append(newmap[key], value)
}
}
return newmap
}
func getGitConfigs() (sources []*GitConfig) {
if lfsconfig := getFileGitConfig(".lfsconfig"); lfsconfig != nil {
sources = append(sources, lfsconfig)
}
globalList, err := git.Config.List()
if err == nil {
sources = append(sources, NewGitConfig(globalList, false))
} else {
fmt.Fprintf(os.Stderr, "Error reading git config: %s\n", err)
}
return
}
func getFileGitConfig(basename string) *GitConfig {
fullname := filepath.Join(LocalWorkingDir, basename)
if _, err := os.Stat(fullname); err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", basename, err)
}
return nil
}
lines, err := git.Config.ListFromFile(fullname)
if err == nil {
return NewGitConfig(lines, true)
}
fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", basename, err)
return nil
}
func keyIsUnsafe(key string) bool {
for _, safe := range safeKeys {
if safe == key {
return false
}
}
return true
}
var safeKeys = []string{
"lfs.fetchexclude",
"lfs.fetchinclude",
"lfs.gitprotocol",
"lfs.pushurl",
"lfs.url",
}