3 // gitEnvironment is an implementation of the Environment which wraps the legacy
4 // behavior or `*config.Configuration.loadGitConfig()`.
6 // It is functionally equivelant to call `cfg.loadGitConfig()` before calling
7 // methods on the Environment type.
8 type gitEnvironment struct {
9 // git is the Environment which gitEnvironment wraps.
11 // config is the *Configuration instance which is mutated by
16 // Get is shorthand for calling the loadGitConfig, and then returning
18 func (g *gitEnvironment) Get(key string) (val string, ok bool) {
24 // Get is shorthand for calling the loadGitConfig, and then returning
25 // `g.git.GetAll(key)`.
26 func (g *gitEnvironment) GetAll(key string) []string {
29 return g.git.GetAll(key)
32 // Get is shorthand for calling the loadGitConfig, and then returning
33 // `g.git.Bool(key, def)`.
34 func (g *gitEnvironment) Bool(key string, def bool) (val bool) {
37 return g.git.Bool(key, def)
40 // Get is shorthand for calling the loadGitConfig, and then returning
41 // `g.git.Int(key, def)`.
42 func (g *gitEnvironment) Int(key string, def int) (val int) {
45 return g.git.Int(key, def)
48 // All returns a copy of all the key/value pairs for the current git config.
49 func (g *gitEnvironment) All() map[string][]string {
55 // loadGitConfig reads and parses the .gitconfig by calling ReadGitConfig. It
56 // also sets values on the configuration instance `g.config`.
58 // If loadGitConfig has already been called, this method will bail out early,
59 // and return false. Otherwise it will preform the entire parse and return true.
61 // loadGitConfig is safe to call across multiple goroutines.
62 func (g *gitEnvironment) loadGitConfig() bool {
63 g.config.loading.Lock()
64 defer g.config.loading.Unlock()
70 gf, extensions, uniqRemotes := ReadGitConfig(getGitConfigs()...)
72 g.git = EnvironmentOf(gf)
74 g.config.extensions = extensions
76 g.config.remotes = make([]string, 0, len(uniqRemotes))
77 for remote, isOrigin := range uniqRemotes {
81 g.config.remotes = append(g.config.remotes, remote)