From ecb64c048179fbc6086e7eff23136802720c8972 Mon Sep 17 00:00:00 2001 From: Wes Morgan Date: Mon, 6 Apr 2020 10:32:06 -0600 Subject: Add Merged config ...for reading and writing global (~/.git/config) and reading system (/etc/gitconfig) configs in addition to local repo config --- plumbing/format/config/merged.go | 215 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 plumbing/format/config/merged.go (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/merged.go b/plumbing/format/config/merged.go new file mode 100644 index 0000000..3afb063 --- /dev/null +++ b/plumbing/format/config/merged.go @@ -0,0 +1,215 @@ +package config + +type Scope int + +const ( + LocalScope Scope = iota + GlobalScope + SystemScope +) + +type ScopedConfigs map[Scope]*Config + +type Merged struct { + scopedConfigs ScopedConfigs +} + +type MergedSection struct { + backingSection *Section +} + +type MergedSubsection struct { + backingSubsection *Subsection +} + +type MergedSubsections []*MergedSubsection + +func NewMerged() *Merged { + cfg := &Merged{ + scopedConfigs: make(ScopedConfigs), + } + for s := LocalScope; s <= SystemScope; s++ { + cfg.scopedConfigs[s] = New() + } + + return cfg +} + +func (m *Merged) ResetScopedConfig(scope Scope) { + m.scopedConfigs[scope] = New() +} + +func (m *Merged) ScopedConfig(scope Scope) *Config { + return m.scopedConfigs[scope] +} + +func (m *Merged) LocalConfig() *Config { + return m.ScopedConfig(LocalScope) +} + +func (m *Merged) GlobalConfig() *Config { + return m.ScopedConfig(GlobalScope) +} + +func (m *Merged) SystemConfig() *Config { + return m.ScopedConfig(SystemScope) +} + +// Config.Section creates the section if it doesn't exist, which is not +// what we want in here. +func (c *Config) hasSection(name string) bool { + sections := c.Sections + var found bool + + for _, s := range sections { + if s.IsName(name) { + found = true + break + } + } + + return found +} + +func (m *Merged) Section(name string) *MergedSection { + var mergedSection *MergedSection + + for s := SystemScope; s >= LocalScope; s-- { + if m.scopedConfigs[s].hasSection(name) { + sec := m.scopedConfigs[s].Section(name) + if mergedSection == nil { + mergedSection = NewMergedSection(sec) + } + + if mergedSection.Options() == nil { + mergedSection.backingSection.Options = sec.Options + } else { + for _, o := range sec.Options { + mergedSection.backingSection.SetOption(o.Key, o.Value) + } + } + + if mergedSection.Subsections() == nil { + mergedSection.backingSection.Subsections = sec.Subsections + } else { + for _, ss := range sec.Subsections { + if mergedSection.HasSubsection(ss.Name) { + for _, o := range ss.Options { + mergedSection.backingSection.Subsection(ss.Name).SetOption(o.Key, o.Value) + } + } else { + mergedSection.backingSection.Subsections = append(mergedSection.backingSection.Subsections, ss) + } + } + } + } + } + + if mergedSection != nil { + mergedSection.backingSection.Name = name + } + + return mergedSection +} + +func (m *Merged) AddOption(scope Scope, section string, subsection string, key string, value string) *Config { + return m.ScopedConfig(scope).AddOption(section, subsection, key, value) +} + +func (m *Merged) SetOption(scope Scope, section string, subsection string, key string, value string) *Config { + return m.ScopedConfig(scope).SetOption(section, subsection, key, value) +} + +func (m *Merged) RemoveSection(scope Scope, name string) *Config { + return m.ScopedConfig(scope).RemoveSection(name) +} + +func (m *Merged) RemoveSubsection(scope Scope, section string, subsection string) *Config { + return m.ScopedConfig(scope).RemoveSubsection(section, subsection) +} + +func copyOptions(os Options) Options { + copiedOptions := make(Options, 0) + + for _, o := range os { + copiedOptions = append(copiedOptions, o) + } + + return copiedOptions +} + +func copySubsections(ss Subsections) Subsections { + copiedSubsections := make(Subsections, 0) + + for _, ss := range ss { + copiedSubsections = append(copiedSubsections, &Subsection{ + Name: ss.Name, + Options: copyOptions(ss.Options), + }) + } + + return copiedSubsections +} + +func NewMergedSection(backing *Section) *MergedSection { + return &MergedSection{ + backingSection: &Section{ + Name: backing.Name, + Options: copyOptions(backing.Options), + Subsections: copySubsections(backing.Subsections), + }, + } +} + +func (ms *MergedSection) Name() string { + return ms.backingSection.Name +} + +func (ms *MergedSection) IsName(name string) bool { + return ms.backingSection.IsName(name) +} + +func (ms *MergedSection) Options() []*Option { + return ms.backingSection.Options +} + +func (ms *MergedSection) Option(key string) string { + return ms.backingSection.Option(key) +} + +func (ms *MergedSection) Subsections() MergedSubsections { + mss := make(MergedSubsections, 0) + for _, ss := range ms.backingSection.Subsections { + mss = append(mss, NewMergedSubsection(ss)) + } + return mss +} + +func (ms *MergedSection) Subsection(name string) *MergedSubsection { + return NewMergedSubsection(ms.backingSection.Subsection(name)) +} + +func (ms *MergedSection) HasSubsection(name string) bool { + return ms.backingSection.HasSubsection(name) +} + +func NewMergedSubsection(backing *Subsection) *MergedSubsection { + return &MergedSubsection{backingSubsection: backing} +} + +func (mss *MergedSubsection) Name() string { + return mss.backingSubsection.Name +} + +func (mss *MergedSubsection) IsName(name string) bool { + return mss.backingSubsection.IsName(name) +} + +func (mss *MergedSubsection) Options() []*Option { + return mss.backingSubsection.Options +} + +func (mss *MergedSubsection) Option(key string) string { + return mss.backingSubsection.Option(key) +} + -- cgit From 231a188e4899cc8a76fd0ca914b9fa5fe9b3175b Mon Sep 17 00:00:00 2001 From: Wes Morgan Date: Mon, 6 Apr 2020 11:20:56 -0600 Subject: Add funcs to set global & local config in Merged --- plumbing/format/config/merged.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/merged.go b/plumbing/format/config/merged.go index 3afb063..52f4b68 100644 --- a/plumbing/format/config/merged.go +++ b/plumbing/format/config/merged.go @@ -55,6 +55,14 @@ func (m *Merged) SystemConfig() *Config { return m.ScopedConfig(SystemScope) } +func (m *Merged) SetLocalConfig(c *Config) { + m.scopedConfigs[LocalScope] = c +} + +func (m *Merged) SetGlobalConfig(c *Config) { + m.scopedConfigs[GlobalScope] = c +} + // Config.Section creates the section if it doesn't exist, which is not // what we want in here. func (c *Config) hasSection(name string) bool { -- cgit From 8df0dec7e676c1281c46834614c68fb09697bf45 Mon Sep 17 00:00:00 2001 From: Wes Morgan Date: Tue, 7 Apr 2020 07:58:58 -0600 Subject: Document some things in format/config/merged.go --- plumbing/format/config/merged.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/merged.go b/plumbing/format/config/merged.go index 52f4b68..a44f3b8 100644 --- a/plumbing/format/config/merged.go +++ b/plumbing/format/config/merged.go @@ -1,7 +1,13 @@ package config +// Scope defines which configuration we're reading / writing. Git uses +// a local config file in each repo (./.git/config) which corresponds +// with LocalScope here. It's values have the highest priority (i.e. they +// override those in the other two scopes). Next is the global config which +// could also be described as a user config since it lives in the +// ~/.gitconfig file. Last and lowest priority is the system config which +// resides in the /etc/gitconfig file. type Scope int - const ( LocalScope Scope = iota GlobalScope @@ -10,14 +16,22 @@ const ( type ScopedConfigs map[Scope]*Config +// Merged defines a read-only view of the priority-merged config params +// so that you can access the effective settings in the same way git does. +// For example, if a user has defined a user.name value in ~/.gitconfig but +// a different one in the current repo's ./.git/config file, this code: +// config.Merged.Section("user").Option("name") will give you the value +// from ./.git/config. type Merged struct { scopedConfigs ScopedConfigs } +// MergedSection is a read-only Section for merged config views. type MergedSection struct { backingSection *Section } +// MergedSubsection is a read-only Subsection for merged config views. type MergedSubsection struct { backingSubsection *Subsection } @@ -39,26 +53,39 @@ func (m *Merged) ResetScopedConfig(scope Scope) { m.scopedConfigs[scope] = New() } +// ScopedConfig allows accessing specific backing *Config instances. func (m *Merged) ScopedConfig(scope Scope) *Config { return m.scopedConfigs[scope] } +// LocalConfig allows accessing the local (i.e. ./.git/config) backing +// *Config instance. func (m *Merged) LocalConfig() *Config { return m.ScopedConfig(LocalScope) } +// GlobalConfig allows accessing the global (i.e. ~/.gitconfig) backing +// *Config instance. func (m *Merged) GlobalConfig() *Config { return m.ScopedConfig(GlobalScope) } +// SystemConfig allows accessing the system (i.e. /etc/gitconfig) backing +// *Config instance. func (m *Merged) SystemConfig() *Config { return m.ScopedConfig(SystemScope) } +// SetLocalConfig allows updating the local (i.e. ./.git/config) config. If you +// call config.SetConfig(...) on the containing top-level config instance after +// this, your new local config params will be written to ./.git/config. func (m *Merged) SetLocalConfig(c *Config) { m.scopedConfigs[LocalScope] = c } +// SetGlobalConfig allows updating the global (i.e. ~/.gitconfig) config. If you +// call config.SetConfig(...) on the containing top-level config instance after +// this, your new global config params will be written to ~/.gitconfig. func (m *Merged) SetGlobalConfig(c *Config) { m.scopedConfigs[GlobalScope] = c } @@ -79,6 +106,9 @@ func (c *Config) hasSection(name string) bool { return found } +// Section returns a read-only *MergedSection view of the config in which +// params are merged in the same order as git itself: +// Local overrides global overrides system. func (m *Merged) Section(name string) *MergedSection { var mergedSection *MergedSection @@ -120,18 +150,26 @@ func (m *Merged) Section(name string) *MergedSection { return mergedSection } +// AddOption works just like config.AddOption except that it takes a Scope as its first argument. +// This defines which config scope (local, global, or system) this option should be added to. func (m *Merged) AddOption(scope Scope, section string, subsection string, key string, value string) *Config { return m.ScopedConfig(scope).AddOption(section, subsection, key, value) } +// SetOption works just like config.SetOption except that it takes a Scope as its first argument. +// This defines which config scope (local, global, or system) this option should be set in. func (m *Merged) SetOption(scope Scope, section string, subsection string, key string, value string) *Config { return m.ScopedConfig(scope).SetOption(section, subsection, key, value) } +// RemoveSection works just like config.RemoveSection except that it takes a Scope as its first argument. +// This defines which config scope (local, global, or system) the section should be removed from. func (m *Merged) RemoveSection(scope Scope, name string) *Config { return m.ScopedConfig(scope).RemoveSection(name) } +// RemoveSubsection works just like config.RemoveSubsection except that it takes a Scope as its first argument. +// This defines which config scope (local, global, or system) the subsection should be removed from. func (m *Merged) RemoveSubsection(scope Scope, section string, subsection string) *Config { return m.ScopedConfig(scope).RemoveSubsection(section, subsection) } -- cgit