gitconfig is a Go library to read and edit git configuration the way git
does.
- same result as git: same parsing, same value types (bool, int, path),
tested against the
gitbinary. - every source: system, global, local and worktree files,
GIT_CONFIG_*variables,includeandincludeIf. Each value says which file and line it came from. - lossless edits: an edit changes only the lines it touches; comments and layout are kept.
- safe alongside git: writes take git's lock, so a concurrent
git configand this library never overwrite each other. - safe on hostile repositories: untrusted repositories are refused, as
git refuses them (
safe.directory).
Where this library deliberately differs from git, and why, is listed in COMPATIBILITY.md.
go get github.com/git-bug/gitconfig
package main
import (
"fmt"
"log"
"github.com/git-bug/gitconfig"
)
func main() {
// Find the repository and where git reads its configuration from.
env, err := gitconfig.Discover(".")
if err != nil {
log.Fatal(err) // ErrNotRepository, or ErrUntrusted as git refuses it
}
// Read the effective configuration: system, global, local, worktree
// and command scopes, with includes, as git would use it.
cfg, err := env.Load()
if err != nil {
log.Fatal(err)
}
if e, ok := cfg.Get("user.name"); ok {
fmt.Printf("user.name = %s (%s)\n", e.Value, e.Origin.File)
}
// Edit the repository's config. Comments and layout are kept, and the
// file is not touched if the value is already set.
err = env.UpdateScope(gitconfig.ScopeLocal, func(f *gitconfig.File) error {
return f.Set("core.editor", "vim")
})
if err != nil {
log.Fatal(err)
}
}MIT, see LICENSE. This is a clean room implementation: no code is derived from git, see CLEAN-ROOM.md. It is meant to interoperate with git.