From 8237c1c4d72e6257e82c25ece76c2edf6d2c1bd0 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Fri, 5 Aug 2016 15:40:17 -0600 Subject: [PATCH] move git config commands to getGitConfigs() --- config/config.go | 66 ++++++------------------------------------- config/git_fetcher.go | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/config/config.go b/config/config.go index 7bf23ea6..a36e7aa9 100644 --- a/config/config.go +++ b/config/config.go @@ -4,8 +4,6 @@ package config import ( "fmt" - "os" - "path/filepath" "strconv" "strings" "sync" @@ -65,13 +63,13 @@ type Configuration struct { IsDebuggingHttp bool IsLoggingStats bool - loading sync.Mutex // guards initialization of gitConfig and remotes - origConfig map[string]string - remotes []string - extensions map[string]Extension - fetchPruneConfig *FetchPruneConfig - manualEndpoint *Endpoint - parsedNetrc netrcfinder + loading sync.Mutex // guards initialization of gitConfig and remotes + origConfig map[string]string + remotes []string + extensions map[string]Extension + fetchPruneConfig *FetchPruneConfig + manualEndpoint *Endpoint + parsedNetrc netrcfinder } func New() *Configuration { @@ -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 diff --git a/config/git_fetcher.go b/config/git_fetcher.go index bb80fcb3..ae41a64d 100644 --- a/config/git_fetcher.go +++ b/config/git_fetcher.go @@ -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 {