aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/config/main.go
blob: 22dc447f167a001b41f2a911b7738656fc354983 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
		}
	}
}