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 /_examples | |
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 '_examples')
-rw-r--r-- | _examples/config/main.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/_examples/config/main.go b/_examples/config/main.go new file mode 100644 index 0000000..22dc447 --- /dev/null +++ b/_examples/config/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + format "github.com/go-git/go-git/v5/plumbing/format/config" + + "github.com/go-git/go-git/v5/config" +) + +// Example of how to: +// - Access basic local (i.e. ./.git/config) configuration params +// - Set basic local config params +// - Set custom local config params +// - Access custom local config params +// - Set global config params +// - Access global & system config params + +func main() { + // Open this repository + // Info("git init") + // r, err := git.Init(memory.NewStorage(), nil) + Info("open local git repo") + r, err := git.PlainOpen(".") + CheckIfError(err) + + // Load the configuration + cfg, err := r.Config() + CheckIfError(err) + + // Get core local config params + if cfg.Core.IsBare { + Info("repo is bare") + } else { + Info("repo is not bare") + } + + Info("worktree is %s", cfg.Core.Worktree) + + // Set basic local config params + cfg.Remotes["origin"] = &config.RemoteConfig{ + Name: "origin", + URLs: []string{"git@github.com:mcuadros/go-git.git"}, + } + + Info("origin remote: %+v", cfg.Remotes["origin"]) + + // Set local custom config param + cfg.Merged.LocalConfig().AddOption("custom", format.NoSubsection, "name", "Local Name") + + // Set global config param (~/.gitconfig) + cfg.Merged.GlobalConfig().AddOption("custom", format.NoSubsection, "name", "Global Name") + + // Get custom config param (merged in the same way git does: system -> global -> local) + Info("custom.name is %s", cfg.Merged.Section("custom").Option("name")) + + // Get system config params (/etc/gitconfig) + systemSections := cfg.Merged.SystemConfig().Sections + for _, ss := range systemSections { + Info("System section: %s", ss.Name) + for _, o := range ss.Options { + Info("\tOption: %s = %s", o.Key, o.Value) + } + } +} |