aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/config/main.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-04-07 23:18:13 +0200
committerGitHub <noreply@github.com>2020-04-07 23:18:13 +0200
commit3127ad9a44a2ee935502816065dfe39f494f583d (patch)
treec5b7892e36ba31890e5c60571082deacf255bca7 /_examples/config/main.go
parent73c52edaad2dae256be61bd1dbbab08e1092f58e (diff)
parent68bffab4d6fa84260cc4287f979a6feceaf92ff8 (diff)
downloadgo-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 '_examples/config/main.go')
-rw-r--r--_examples/config/main.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/_examples/config/main.go b/_examples/config/main.go
new file mode 100644
index 0000000..6ccc9f6
--- /dev/null
+++ b/_examples/config/main.go
@@ -0,0 +1,69 @@
+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"])
+
+ // NOTE: The examples below show advanced usage of the config.Merged system, which should
+ // only be used as a last resort if the basic data defined on the Config struct don't
+ // suffice for what you're trying to do.
+
+ // Set local custom config param
+ cfg.Merged.AddOption(format.LocalScope, "custom", format.NoSubsection, "name", "Local Name")
+
+ // Set global config param (~/.gitconfig)
+ cfg.Merged.AddOption(format.GlobalScope, "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)
+ }
+ }
+}