move git config commands to getGitConfigs()
This commit is contained in:
parent
ee687d74b0
commit
8237c1c4d7
@ -4,8 +4,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -446,55 +444,7 @@ func (c *Configuration) loadGitConfig() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var sources []*GitConfig
|
||||
|
||||
lfsconfig := filepath.Join(LocalWorkingDir, ".lfsconfig")
|
||||
if _, err := os.Stat(lfsconfig); err == nil {
|
||||
lines, err := git.Config.ListFromFile(lfsconfig)
|
||||
if err == nil {
|
||||
sources = append(sources, &GitConfig{
|
||||
Lines: strings.Split(lines, "\n"),
|
||||
OnlySafeKeys: true,
|
||||
})
|
||||
} else {
|
||||
if !os.IsNotExist(err) {
|
||||
fmt.Fprintf(os.Stderr, "Error reading .lfsconfig: %s\n", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gitconfig := filepath.Join(LocalWorkingDir, ".gitconfig")
|
||||
if _, err := os.Stat(gitconfig); err == nil {
|
||||
if ShowConfigWarnings {
|
||||
expected := ".lfsconfig"
|
||||
fmt.Fprintf(os.Stderr, "WARNING: Reading LFS config from %q, not %q. Rename to %q before Git LFS v2.0 to remove this warning.\n",
|
||||
filepath.Base(gitconfig), expected, expected)
|
||||
}
|
||||
|
||||
lines, err := git.Config.ListFromFile(gitconfig)
|
||||
if err == nil {
|
||||
sources = append(sources, &GitConfig{
|
||||
Lines: strings.Split(lines, "\n"),
|
||||
OnlySafeKeys: true,
|
||||
})
|
||||
} else {
|
||||
if !os.IsNotExist(err) {
|
||||
fmt.Fprintf(os.Stderr, "Error reading .gitconfig: %s\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
globalList, err := git.Config.List()
|
||||
if err == nil {
|
||||
sources = append(sources, &GitConfig{
|
||||
Lines: strings.Split(globalList, "\n"),
|
||||
OnlySafeKeys: false,
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error reading git config: %s\n", err)
|
||||
}
|
||||
|
||||
gf, extensions, uniqRemotes := ReadGitConfig(sources...)
|
||||
gf, extensions, uniqRemotes := ReadGitConfig(getGitConfigs()...)
|
||||
|
||||
c.Git = EnvironmentOf(gf)
|
||||
c.gitConfig = gf.vals // XXX TERRIBLE
|
||||
|
@ -3,9 +3,12 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/github/git-lfs/git"
|
||||
)
|
||||
|
||||
type GitFetcher struct {
|
||||
@ -18,6 +21,13 @@ type GitConfig struct {
|
||||
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)
|
||||
|
||||
@ -107,6 +117,46 @@ func (g *GitFetcher) Get(key string) (val string) {
|
||||
return g.vals[key]
|
||||
}
|
||||
|
||||
func getGitConfigs() (sources []*GitConfig) {
|
||||
if lfsconfig := getFileGitConfig(".lfsconfig"); lfsconfig != nil {
|
||||
sources = append(sources, lfsconfig)
|
||||
} else {
|
||||
if gitconfig := getFileGitConfig(".gitconfig"); gitconfig != nil {
|
||||
if ShowConfigWarnings {
|
||||
fmt.Fprintf(os.Stderr, "WARNING: Reading LFS config from .gitconfig, not .lfsconfig. Rename to .lfsconfig before Git LFS v2.0 to remove this warning.\n")
|
||||
}
|
||||
sources = append(sources, gitconfig)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user