diff options
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)) |