| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | ) |
| 13 | |
| 14 | type GitFetcher struct { |
| 15 | vmu sync.RWMutex |
| 16 | vals map[string][]string |
| 17 | } |
| 18 | |
| 19 | func readGitConfig(configs ...*git.ConfigurationSource) (gf *GitFetcher, extensions map[string]Extension, uniqRemotes map[string]bool) { |
| 20 | vals := make(map[string][]string) |
| 21 | ignored := make([]string, 0) |
| 22 | |
| 23 | extensions = make(map[string]Extension) |
| 24 | uniqRemotes = make(map[string]bool) |
| 25 | |
| 26 | for _, gc := range configs { |
| 27 | uniqKeys := make(map[string]string) |
| 28 | |
| 29 | for _, line := range gc.Lines { |
| 30 | pieces := strings.SplitN(line, "=", 2) |
| 31 | if len(pieces) < 2 { |
| 32 | continue |
| 33 | } |
| 34 | |
| 35 | allowed := !gc.OnlySafeKeys |
| 36 | |
| 37 | // We don't need to change the case of the key here, |
| 38 | // since Git will already have canonicalized it for us. |
| 39 | key, val := pieces[0], pieces[1] |
| 40 | |
| 41 | if origKey, ok := uniqKeys[key]; ok { |
| 42 | if ShowConfigWarnings && len(vals[key]) > 0 && vals[key][len(vals[key])-1] != val && strings.HasPrefix(key, gitConfigWarningPrefix) { |
| 43 | fmt.Fprintln(os.Stderr, tr.Tr.Get("warning: These `git config` values clash:")) |
| 44 | fmt.Fprintf(os.Stderr, " git config %q = %q\n", origKey, vals[key]) |
| 45 | fmt.Fprintf(os.Stderr, " git config %q = %q\n", pieces[0], val) |
| 46 | } |
| 47 | } else { |
| 48 | uniqKeys[key] = pieces[0] |
| 49 | } |
| 50 | |
| 51 | parts := strings.Split(key, ".") |
| 52 | if len(parts) == 4 && parts[0] == "lfs" && parts[1] == "extension" { |
| 53 | // prop: lfs.extension.<name>.<prop> |
| 54 | name := parts[2] |
| 55 | prop := parts[3] |
| 56 | |
| 57 | ext := extensions[name] |
| 58 | ext.Name = name |
| 59 | |
| 60 | switch prop { |
| 61 | case "clean": |
| 62 | if gc.OnlySafeKeys { |
| 63 | ignored = append(ignored, key) |
| 64 | continue |
| 65 | } |
| 66 | ext.Clean = val |
| 67 | case "smudge": |
| 68 | if gc.OnlySafeKeys { |
| 69 | ignored = append(ignored, key) |
| 70 | continue |
| 71 | } |
| 72 | ext.Smudge = val |
| 73 | case "priority": |
| 74 | allowed = true |
| 75 | p, err := strconv.Atoi(val) |
| 76 | if err == nil && p >= 0 { |
| 77 | ext.Priority = p |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | extensions[name] = ext |
| 82 | } else if len(parts) > 1 && parts[0] == "remote" { |
| 83 | if gc.OnlySafeKeys && (len(parts) == 3 && parts[2] != "lfsurl") { |
| 84 | ignored = append(ignored, key) |
| 85 | continue |
| 86 | } |
| 87 | |
| 88 | allowed = true |
| 89 | remote := strings.Join(parts[1:len(parts)-1], ".") |
| 90 | uniqRemotes[remote] = remote == "origin" |
| 91 | } else if len(parts) > 2 && parts[len(parts)-1] == "access" { |
| 92 | allowed = true |
| 93 | } |
| 94 | |
| 95 | if !allowed && keyIsUnsafe(key) { |
| 96 | ignored = append(ignored, key) |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | vals[key] = append(vals[key], val) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if len(ignored) > 0 { |
| 105 | fmt.Fprint(os.Stderr, tr.Tr.Get("warning: These unsafe '.lfsconfig' keys were ignored:"), "\n\n") |
| 106 | for _, key := range ignored { |
| 107 | fmt.Fprintf(os.Stderr, " %s\n", key) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | gf = &GitFetcher{vals: vals} |
| 112 | |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | // Get implements the Fetcher interface, and returns the value associated with |
| 117 | // a given key and true, signaling that the value was present. Otherwise, an |
| 118 | // empty string and false will be returned, signaling that the value was |
| 119 | // absent. |
| 120 | // |
| 121 | // Map lookup by key is case-insensitive, except for the middle part of a |
| 122 | // three-part key, as per the .gitconfig specification. |
| 123 | // |
| 124 | // Get is safe to call across multiple goroutines. |
| 125 | func (g *GitFetcher) Get(key string) (val string, ok bool) { |
| 126 | all := g.GetAll(key) |
| 127 | |
| 128 | if len(all) == 0 { |
| 129 | return "", false |
| 130 | } |
| 131 | return all[len(all)-1], true |
| 132 | } |
| 133 | |
| 134 | func (g *GitFetcher) GetAll(key string) []string { |
| 135 | g.vmu.RLock() |
| 136 | defer g.vmu.RUnlock() |
| 137 | |
| 138 | return g.vals[g.caseFoldKey(key)] |
| 139 | } |
| 140 | |
| 141 | func (g *GitFetcher) All() map[string][]string { |
| 142 | newmap := make(map[string][]string) |
| 143 | |
| 144 | g.vmu.RLock() |
| 145 | defer g.vmu.RUnlock() |
| 146 | |
| 147 | for key, values := range g.vals { |
| 148 | for _, value := range values { |
| 149 | newmap[key] = append(newmap[key], value) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return newmap |
| 154 | } |
| 155 | |
| 156 | func (g *GitFetcher) caseFoldKey(key string) string { |
| 157 | parts := strings.Split(key, ".") |
| 158 | last := len(parts) - 1 |
| 159 | |
| 160 | // We check for 3 or more parts here because if the middle part is a |
| 161 | // URL, it may have dots in it. We'll downcase the part before the first |
| 162 | // dot and after the last dot, but preserve the piece in the middle, |
| 163 | // which may be a branch name, remote, or URL, all of which are |
| 164 | // case-sensitive. This is the algorithm Git uses to canonicalize its |
| 165 | // keys. |
| 166 | if len(parts) < 3 { |
| 167 | return strings.ToLower(key) |
| 168 | } |
| 169 | |
| 170 | return strings.Join([]string{ |
| 171 | strings.ToLower(parts[0]), |
| 172 | strings.Join(parts[1:last], "."), |
| 173 | strings.ToLower(parts[last]), |
| 174 | }, ".") |
| 175 | } |
| 176 | |
| 177 | func keyIsUnsafe(key string) bool { |
| 178 | for _, safe := range safeKeys { |
| 179 | if safe == key { |
| 180 | return false |
| 181 | } |
| 182 | } |
| 183 | return true |
| 184 | } |
| 185 | |
| 186 | var safeKeys = []string{ |
| 187 | "lfs.allowincompletepush", |
| 188 | "lfs.fetchexclude", |
| 189 | "lfs.fetchinclude", |
| 190 | "lfs.gitprotocol", |
| 191 | "lfs.locksverify", |
| 192 | "lfs.pushurl", |
| 193 | "lfs.skipdownloaderrors", |
| 194 | "lfs.url", |
| 195 | } |