diff options
author | Wes Morgan <wesmorgan@icloud.com> | 2020-04-06 10:32:06 -0600 |
---|---|---|
committer | Wes Morgan <wesmorgan@icloud.com> | 2020-04-06 10:32:06 -0600 |
commit | ecb64c048179fbc6086e7eff23136802720c8972 (patch) | |
tree | 5fba37333c2dcabccadee73f2c152f6b5df46fab /storage/filesystem/config.go | |
parent | 8fddd7abcc436d77e9f7449a7b7aa15ee13f7c60 (diff) | |
download | go-git-ecb64c048179fbc6086e7eff23136802720c8972.tar.gz |
Add Merged config
...for reading and writing global (~/.git/config) and reading system (/etc/gitconfig) configs in addition to local repo config
Diffstat (limited to 'storage/filesystem/config.go')
-rw-r--r-- | storage/filesystem/config.go | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go index 01b35b4..dbdce54 100644 --- a/storage/filesystem/config.go +++ b/storage/filesystem/config.go @@ -5,6 +5,7 @@ import ( "os" "github.com/go-git/go-git/v5/config" + format "github.com/go-git/go-git/v5/plumbing/format/config" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" "github.com/go-git/go-git/v5/utils/ioutil" ) @@ -13,10 +14,11 @@ type ConfigStorage struct { dir *dotgit.DotGit } -func (c *ConfigStorage) Config() (conf *config.Config, err error) { +func (c *ConfigStorage) Config() (*config.Config, error) { cfg := config.NewConfig() - f, err := c.dir.Config() + // local config (./.git/config) + f, err := c.dir.LocalConfig() if err != nil { if os.IsNotExist(err) { return cfg, nil @@ -29,13 +31,55 @@ func (c *ConfigStorage) Config() (conf *config.Config, err error) { b, err := stdioutil.ReadAll(f) if err != nil { + return cfg, err + } + + if err = cfg.UnmarshalScoped(format.LocalScope, b); err != nil { + return cfg, err + } + + // global config (~/.gitconfig) + f, err = c.dir.GlobalConfig() + if err != nil { + if os.IsNotExist(err) { + return cfg, nil + } + return nil, err } - if err = cfg.Unmarshal(b); err != nil { + defer ioutil.CheckClose(f, &err) + + b, err = stdioutil.ReadAll(f) + if err != nil { + return cfg, err + } + + if err = cfg.UnmarshalScoped(format.GlobalScope, b); err != nil { + return cfg, err + } + + // system config (/etc/gitconfig) + f, err = c.dir.SystemConfig() + if err != nil { + if os.IsNotExist(err) { + return cfg, nil + } + return nil, err } + defer ioutil.CheckClose(f, &err) + + b, err = stdioutil.ReadAll(f) + if err != nil { + return cfg, err + } + + if err = cfg.UnmarshalScoped(format.SystemScope, b); err != nil { + return cfg, err + } + return cfg, err } @@ -44,7 +88,7 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) { return err } - f, err := c.dir.ConfigWriter() + f, err := c.dir.LocalConfigWriter() if err != nil { return err } |