From a12a0a2c8d2849ea6a8ba446b258b0294f91c5a8 Mon Sep 17 00:00:00 2001 From: Christian Miller Date: Tue, 26 Jul 2022 05:36:04 +0000 Subject: [PATCH] examples: add config example. Fixes #523 * previously an example existed for working with `config.Config`, but changes in implementation made the example not usable. (see #75) * this mostly puts back the original example with some modifications to make it work with current implementation --- _examples/config/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 _examples/config/main.go diff --git a/_examples/config/main.go b/_examples/config/main.go new file mode 100644 index 000000000..b47259965 --- /dev/null +++ b/_examples/config/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + + "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 + +func main() { + Info("git init") + r, err := git.PlainInit(".", false) + CheckIfError(err) + + // Load the configuration + cfg, err := r.Config() + CheckIfError(err) + + Info("worktree is %s", cfg.Core.Worktree) + + // Set basic local config params + cfg.Remotes["origin"] = &config.RemoteConfig{ + Name: "origin", + URLs: []string{"https://github.com/git-fixtures/basic.git"}, + } + + Info("origin remote: %+v", cfg.Remotes["origin"]) + + cfg.User.Name = "Local name" + + Info("custom.name is %s", cfg.User.Name) + + // In order to save the config file, you need to call SetConfig + // After calling this go to .git/config and see the custom.name added and the changes to the remote + r.Storer.SetConfig(cfg) +}