| 1 |
// Package gitconfig implements a pure Go parser of Git SCM config files. The support |
| 2 |
// is currently not matching git exactly, e.g. includes, urlmatches and multivars are currently |
| 3 |
// not supported. And while we try to preserve the original file a much as possible |
| 4 |
// when writing we currently don't exactly retain (insignificant) whitespaces. |
| 5 |
// |
| 6 |
// The reference for this implementation is https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html |
| 7 |
// |
| 8 |
// # Usage |
| 9 |
// |
| 10 |
// Use gitconfig.LoadAll with an optional workspace argument to process configuration |
| 11 |
// input from these locations in order (i.e. the later ones take precedence): |
| 12 |
// |
| 13 |
// - `system` - /etc/gitconfig |
| 14 |
// - `global` - `$XDG_CONFIG_HOME/git/config` or `~/.gitconfig` |
| 15 |
// - `local` - `<workdir>/config` |
| 16 |
// - `worktree` - `<workdir>/config.worktree` |
| 17 |
// - `command` - GIT_CONFIG_{COUNT,KEY,VALUE} environment variables |
| 18 |
// |
| 19 |
// Note: We do not support parsing command line flags directly, but one |
| 20 |
// can use the SetEnv method to set flags from the command line in the config. |
| 21 |
// |
| 22 |
// # Customization |
| 23 |
// |
| 24 |
// `gopass` and other users of this package can easily customize file and environment |
| 25 |
// names by utilizing the exported variables from the Configs struct: |
| 26 |
// |
| 27 |
// - SystemConfig - Path to system-wide config (e.g., /etc/gitconfig) |
| 28 |
// - GlobalConfig - Path to user config (e.g., ~/.gitconfig) or "" to disable |
| 29 |
// - LocalConfig - Per-repository config name (e.g., .git/config) |
| 30 |
// - WorktreeConfig - Per-worktree config name (e.g., .git/config.worktree) |
| 31 |
// - EnvPrefix - Environment variable prefix (defaults to GIT_CONFIG) |
| 32 |
// |
| 33 |
// Note: For tests users will want to set `NoWrites = true` to avoid overwriting |
| 34 |
// their real configs. |
| 35 |
// |
| 36 |
// # Examples |
| 37 |
// |
| 38 |
// ## Loading and Reading Configuration |
| 39 |
// |
| 40 |
// Basic reading from all scopes (respects precedence): |
| 41 |
// |
| 42 |
// cfg := gitconfig.New() |
| 43 |
// cfg.LoadAll(".") |
| 44 |
// value := cfg.Get("user.name") |
| 45 |
// fmt.Println(value) // Reads from highest priority scope available |
| 46 |
// |
| 47 |
// ## Reading from Specific Scopes |
| 48 |
// |
| 49 |
// Access configuration from a specific scope: |
| 50 |
// |
| 51 |
// cfg := gitconfig.New() |
| 52 |
// cfg.LoadAll(".") |
| 53 |
// local := cfg.GetLocal("core.editor") |
| 54 |
// global := cfg.GetGlobal("user.email") |
| 55 |
// system := cfg.GetSystem("core.pager") |
| 56 |
// |
| 57 |
// ## Customization for Other Applications |
| 58 |
// |
| 59 |
// Configure for a different application (like gopass): |
| 60 |
// |
| 61 |
// cfg := gitconfig.New() |
| 62 |
// cfg.SystemConfig = "/etc/gopass/config" |
| 63 |
// cfg.GlobalConfig = "" |
| 64 |
// cfg.LocalConfig = ".gopass-config" |
| 65 |
// cfg.EnvPrefix = "GOPASS_CONFIG" |
| 66 |
// cfg.LoadAll(".") |
| 67 |
// notifications := cfg.Get("core.notifications") |
| 68 |
// |
| 69 |
// ## Writing Configuration |
| 70 |
// |
| 71 |
// Modify and persist changes: |
| 72 |
// |
| 73 |
// cfg, _ := gitconfig.LoadConfig(".git/config") |
| 74 |
// cfg.Set("user.name", "John Doe") |
| 75 |
// cfg.Set("user.email", "[email protected]") |
| 76 |
// cfg.Write() // Persist changes to disk |
| 77 |
// |
| 78 |
// ## Scope-Specific Writes |
| 79 |
// |
| 80 |
// Write to specific scopes in multi-scope configs: |
| 81 |
// |
| 82 |
// cfg := gitconfig.New() |
| 83 |
// cfg.LoadAll(".") |
| 84 |
// cfg.SetLocal("core.autocrlf", "true") // Write to .git/config |
| 85 |
// cfg.SetGlobal("user.signingkey", "...") // Write to ~/.gitconfig |
| 86 |
// cfg.SetSystem("core.pager", "less") // Write to /etc/gitconfig |
| 87 |
// |
| 88 |
// ## Error Handling |
| 89 |
// |
| 90 |
// Use errors.Is to detect common error categories: |
| 91 |
// |
| 92 |
// if err := cfg.Set("invalid", "value"); err != nil { |
| 93 |
// if errors.Is(err, gitconfig.ErrInvalidKey) { |
| 94 |
// // handle invalid key |
| 95 |
// } |
| 96 |
// } |
| 97 |
// |
| 98 |
// if err := cfgs.SetLocal("core.editor", "vim"); err != nil { |
| 99 |
// if errors.Is(err, gitconfig.ErrWorkdirNotSet) { |
| 100 |
// // call LoadAll or provide a workdir |
| 101 |
// } |
| 102 |
// } |
| 103 |
// |
| 104 |
// # Versioning and Compatibility |
| 105 |
// |
| 106 |
// We aim to support the latest stable release of Git only. |
| 107 |
// Currently we do not provide any backwards compatibility |
| 108 |
// and semantic versioning. |
| 109 |
// |
| 110 |
// # Known limitations |
| 111 |
// |
| 112 |
// * Worktree support is only partial |
| 113 |
// * Bare boolean values are not supported (e.g. a setting were only the key is present) |
| 114 |
// * includeIf suppport is only partial, i.e. we only support the gitdir option |
| 115 |
package gitconfig |