diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-04-07 23:18:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-07 23:18:13 +0200 |
commit | 3127ad9a44a2ee935502816065dfe39f494f583d (patch) | |
tree | c5b7892e36ba31890e5c60571082deacf255bca7 /config/config.go | |
parent | 73c52edaad2dae256be61bd1dbbab08e1092f58e (diff) | |
parent | 68bffab4d6fa84260cc4287f979a6feceaf92ff8 (diff) | |
download | go-git-3127ad9a44a2ee935502816065dfe39f494f583d.tar.gz |
Merge pull request #20 from quorumcontrol/feature/other-configs
plumbing: format, config.Merged to allow access to local and global config
Diffstat (limited to 'config/config.go')
-rw-r--r-- | config/config.go | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/config/config.go b/config/config.go index bec35b0..2b427cb 100644 --- a/config/config.go +++ b/config/config.go @@ -66,6 +66,9 @@ type Config struct { // preserve the parsed information from the original format, to avoid // dropping unsupported fields. Raw *format.Config + // Merged contains the raw form of how git views the system (/etc/gitconfig), + // global (~/.gitconfig), and local (./.git/config) config params. + Merged *format.Merged } // NewConfig returns a new empty Config. @@ -74,9 +77,11 @@ func NewConfig() *Config { Remotes: make(map[string]*RemoteConfig), Submodules: make(map[string]*Submodule), Branches: make(map[string]*Branch), - Raw: format.New(), + Merged: format.NewMerged(), } + config.Raw = config.Merged.LocalConfig() + config.Pack.Window = DefaultPackWindow return config @@ -129,25 +134,38 @@ const ( // Unmarshal parses a git-config file and stores it. func (c *Config) Unmarshal(b []byte) error { + return c.UnmarshalScoped(format.LocalScope, b) +} + +func (c *Config) UnmarshalScoped(scope format.Scope, b []byte) error { r := bytes.NewBuffer(b) d := format.NewDecoder(r) - c.Raw = format.New() - if err := d.Decode(c.Raw); err != nil { - return err - } + c.Merged.ResetScopedConfig(scope) - c.unmarshalCore() - if err := c.unmarshalPack(); err != nil { + if err := d.Decode(c.Merged.ScopedConfig(scope)); err != nil { return err } - unmarshalSubmodules(c.Raw, c.Submodules) - if err := c.unmarshalBranches(); err != nil { - return err + if scope == format.LocalScope { + c.Raw = c.Merged.LocalConfig() + + c.unmarshalCore() + if err := c.unmarshalPack(); err != nil { + return err + } + unmarshalSubmodules(c.Raw, c.Submodules) + + if err := c.unmarshalBranches(); err != nil { + return err + } + + if err := c.unmarshalRemotes(); err != nil { + return err + } } - return c.unmarshalRemotes() + return nil } func (c *Config) unmarshalCore() { @@ -218,7 +236,7 @@ func (c *Config) unmarshalBranches() error { } // Marshal returns Config encoded as a git-config file. -func (c *Config) Marshal() ([]byte, error) { +func (c *Config) MarshalScope(scope format.Scope) ([]byte, error) { c.marshalCore() c.marshalPack() c.marshalRemotes() @@ -226,13 +244,18 @@ func (c *Config) Marshal() ([]byte, error) { c.marshalBranches() buf := bytes.NewBuffer(nil) - if err := format.NewEncoder(buf).Encode(c.Raw); err != nil { + cfg := c.Merged.ScopedConfig(scope) + if err := format.NewEncoder(buf).Encode(cfg); err != nil { return nil, err } return buf.Bytes(), nil } +func (c *Config) Marshal() ([]byte, error) { + return c.MarshalScope(format.LocalScope) +} + func (c *Config) marshalCore() { s := c.Raw.Section(coreSection) s.SetOption(bareKey, fmt.Sprintf("%t", c.Core.IsBare)) |