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
|
package config
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tr"
)
type GitFetcher struct {
vmu sync.RWMutex
vals map[string][]string
}
func readGitConfig(configs ...*git.ConfigurationSource) (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
// We don't need to change the case of the key here,
// since Git will already have canonicalized it for us.
key, val := 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.Fprintln(os.Stderr, tr.Tr.Get("warning: These `git config` values clash:"))
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 := strings.Join(parts[1:len(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.Fprint(os.Stderr, tr.Tr.Get("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, except for the middle part of a
// three-part key, 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[g.caseFoldKey(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 (g *GitFetcher) caseFoldKey(key string) string {
parts := strings.Split(key, ".")
last := len(parts) - 1
// We check for 3 or more parts here because if the middle part is a
// URL, it may have dots in it. We'll downcase the part before the first
// dot and after the last dot, but preserve the piece in the middle,
// which may be a branch name, remote, or URL, all of which are
// case-sensitive. This is the algorithm Git uses to canonicalize its
// keys.
if len(parts) < 3 {
return strings.ToLower(key)
}
return strings.Join([]string{
strings.ToLower(parts[0]),
strings.Join(parts[1:last], "."),
strings.ToLower(parts[last]),
}, ".")
}
func keyIsUnsafe(key string) bool {
for _, safe := range safeKeys {
if safe == key {
return false
}
}
return true
}
var safeKeys = []string{
"lfs.allowincompletepush",
"lfs.fetchexclude",
"lfs.fetchinclude",
"lfs.gitprotocol",
"lfs.locksverify",
"lfs.pushurl",
"lfs.skipdownloaderrors",
"lfs.url",
}
|