| 1 |
# gitconfig for Go |
| 2 |
|
| 3 |
[](http://godoc.org/github.com/gopasspw/gitconfig) |
| 4 |
[](https://goreportcard.com/report/github.com/gopasspw/gitconfig) |
| 5 |
[](https://opensource.org/licenses/MIT) |
| 6 |
|
| 7 |
A pure Go library for reading and writing Git configuration files without depending on the `git` CLI. This library is particularly useful when: |
| 8 |
|
| 9 |
- Git might not be installed or available |
| 10 |
- You need consistent config parsing across platforms |
| 11 |
- You want to avoid breaking changes when Git's behavior changes |
| 12 |
- You need fine-grained control over config scopes and precedence |
| 13 |
|
| 14 |
Originally developed to support [gopass](https://github.com/gopasspw/gopass), this library aims for full Git compatibility while maintaining a simple, clean API. |
| 15 |
|
| 16 |
## Features |
| 17 |
|
| 18 |
- ✅ **Multi-scope support** - System, global, local, worktree, and environment configs |
| 19 |
- ✅ **Include directives** - Basic `[include]` and `[includeIf]` support (gitdir conditions) |
| 20 |
- ✅ **Round-trip preservation** - Maintains comments, whitespace, and formatting when writing |
| 21 |
- ✅ **Cross-platform** - Works on Linux, macOS, Windows, and other Unix systems |
| 22 |
- ✅ **Customizable** - Override config paths and environment prefixes for your application |
| 23 |
- ✅ **Pure Go** - No CGo dependencies, easy cross-compilation |
| 24 |
- ✅ **Well-tested** - Comprehensive test coverage including edge cases |
| 25 |
|
| 26 |
## Quick Start |
| 27 |
|
| 28 |
```go |
| 29 |
package main |
| 30 |
|
| 31 |
import ( |
| 32 |
"fmt" |
| 33 |
"github.com/gopasspw/gitconfig" |
| 34 |
) |
| 35 |
|
| 36 |
func main() { |
| 37 |
// Load all config scopes (system, global, local) |
| 38 |
cfg := gitconfig.New() |
| 39 |
if err := cfg.LoadAll("."); err != nil { |
| 40 |
panic(err) |
| 41 |
} |
| 42 |
|
| 43 |
// Read a configuration value |
| 44 |
name, ok := cfg.Get("user.name") |
| 45 |
if ok { |
| 46 |
fmt.Printf("User name: %s\n", name) |
| 47 |
} |
| 48 |
|
| 49 |
// Write a configuration value (to local scope by default) |
| 50 |
cfg.Set("user.email", "[email protected]") |
| 51 |
if err := cfg.Write(); err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
} |
| 55 |
``` |
| 56 |
|
| 57 |
See the [examples/](examples/) directory for more detailed usage patterns. |
| 58 |
|
| 59 |
## Reference Documentation |
| 60 |
|
| 61 |
The reference for this implementation is the [Git config documentation](https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html). |
| 62 |
|
| 63 |
## Installation |
| 64 |
|
| 65 |
```bash |
| 66 |
go get github.com/gopasspw/gitconfig |
| 67 |
``` |
| 68 |
|
| 69 |
## Usage |
| 70 |
|
| 71 |
### Loading Configuration |
| 72 |
|
| 73 |
Use `gitconfig.LoadAll` with an optional workspace argument to process configuration from these locations in order (later ones take precedence): |
| 74 |
|
| 75 |
1. **System** - `/etc/gitconfig` |
| 76 |
2. **Global** - `$XDG_CONFIG_HOME/git/config` or `~/.gitconfig` |
| 77 |
3. **Local** - `<workdir>/.git/config` |
| 78 |
4. **Worktree** - `<workdir>/.git/config.worktree` |
| 79 |
5. **Environment** - `GIT_CONFIG_{COUNT,KEY,VALUE}` environment variables |
| 80 |
|
| 81 |
```go |
| 82 |
cfg := gitconfig.New() |
| 83 |
if err := cfg.LoadAll("/path/to/repo"); err != nil { |
| 84 |
log.Fatal(err) |
| 85 |
} |
| 86 |
|
| 87 |
// Read from unified config (respects precedence) |
| 88 |
value, ok := cfg.Get("core.editor") |
| 89 |
``` |
| 90 |
|
| 91 |
### Reading Values |
| 92 |
|
| 93 |
```go |
| 94 |
// Get single value (returns last matching value) |
| 95 |
editor, ok := cfg.Get("core.editor") |
| 96 |
if !ok { |
| 97 |
editor = "vi" // default |
| 98 |
} |
| 99 |
|
| 100 |
// Get all values for a key (for multi-valued config) |
| 101 |
remotes, ok := cfg.GetAll("remote.origin.fetch") |
| 102 |
|
| 103 |
// Read from specific scope |
| 104 |
email := cfg.GetGlobal("user.email") |
| 105 |
autocrlf := cfg.GetLocal("core.autocrlf") |
| 106 |
``` |
| 107 |
|
| 108 |
### Writing Values |
| 109 |
|
| 110 |
```go |
| 111 |
// Write to default scope (local) |
| 112 |
cfg.Set("user.name", "Jane Doe") |
| 113 |
cfg.Set("user.email", "[email protected]") |
| 114 |
|
| 115 |
// Write to specific scope |
| 116 |
cfg.SetGlobal("core.editor", "vim") |
| 117 |
cfg.SetSystem("core.autocrlf", "false") |
| 118 |
|
| 119 |
// Save changes |
| 120 |
if err := cfg.Write(); err != nil { |
| 121 |
log.Fatal(err) |
| 122 |
} |
| 123 |
``` |
| 124 |
|
| 125 |
### Scope-Specific Operations |
| 126 |
|
| 127 |
```go |
| 128 |
// Load only a specific config file |
| 129 |
localCfg, err := gitconfig.LoadConfig("/path/to/repo/.git/config") |
| 130 |
if err != nil { |
| 131 |
log.Fatal(err) |
| 132 |
} |
| 133 |
|
| 134 |
// Work with single scope |
| 135 |
localCfg.Set("branch.main.remote", "origin") |
| 136 |
localCfg.Write() |
| 137 |
``` |
| 138 |
|
| 139 |
## Customization for Your Application |
| 140 |
|
| 141 |
Applications like `gopass` can easily customize file paths and environment variable prefixes: |
| 142 |
|
| 143 |
```go |
| 144 |
cfg := gitconfig.New() |
| 145 |
|
| 146 |
// Customize config file locations |
| 147 |
cfg.SystemConfig = "/etc/gopass/config" |
| 148 |
cfg.GlobalConfig = "~/.config/gopass/config" |
| 149 |
cfg.LocalConfig = ".gopass-config" |
| 150 |
cfg.WorktreeConfig = "" // Disable worktree config |
| 151 |
|
| 152 |
// Customize environment variable prefix |
| 153 |
cfg.EnvPrefix = "GOPASS_CONFIG" // Uses GOPASS_CONFIG_COUNT, etc. |
| 154 |
|
| 155 |
// For testing: prevent accidentally overwriting real configs |
| 156 |
cfg.NoWrites = true |
| 157 |
|
| 158 |
// Load with custom settings |
| 159 |
cfg.LoadAll(".") |
| 160 |
``` |
| 161 |
|
| 162 |
### Customization Options |
| 163 |
|
| 164 |
- **SystemConfig** - Path to system-wide configuration file |
| 165 |
- **GlobalConfig** - Path to user's global configuration (set to `""` to disable) |
| 166 |
- **LocalConfig** - Filename for repository-local config |
| 167 |
- **WorktreeConfig** - Filename for worktree-specific config (or `""` to disable) |
| 168 |
- **EnvPrefix** - Prefix for environment variables (e.g., `MYAPP_CONFIG`) |
| 169 |
- **NoWrites** - Set to true to prevent Write() from modifying files (useful for testing) |
| 170 |
|
| 171 |
## Advanced Features |
| 172 |
|
| 173 |
### Include Directives |
| 174 |
|
| 175 |
The library supports Git's `[include]` and `[includeIf]` directives: |
| 176 |
|
| 177 |
```ini |
| 178 |
# .git/config |
| 179 |
[include] |
| 180 |
path = /path/to/common.gitconfig |
| 181 |
|
| 182 |
[includeIf "gitdir:/path/to/work/"] |
| 183 |
path = ~/.gitconfig-work |
| 184 |
``` |
| 185 |
|
| 186 |
**Supported `includeIf` conditions:** |
| 187 |
|
| 188 |
- `gitdir:` - Include if git directory matches pattern |
| 189 |
- `gitdir/i:` - Case-insensitive gitdir match |
| 190 |
|
| 191 |
**Current limitations:** |
| 192 |
|
| 193 |
- Other conditional types (onbranch, hasconfig) are not yet supported |
| 194 |
- Relative paths in includes are resolved from the config file's directory |
| 195 |
|
| 196 |
### Subsections |
| 197 |
|
| 198 |
Access subsections using dot notation: |
| 199 |
|
| 200 |
```go |
| 201 |
// Set remote URL |
| 202 |
cfg.Set("remote.origin.url", "https://github.com/user/repo.git") |
| 203 |
|
| 204 |
// Set branch tracking |
| 205 |
cfg.Set("branch.main.remote", "origin") |
| 206 |
cfg.Set("branch.main.merge", "refs/heads/main") |
| 207 |
|
| 208 |
// Access subsections |
| 209 |
url, _ := cfg.Get("remote.origin.url") |
| 210 |
``` |
| 211 |
|
| 212 |
### Multi-valued Keys |
| 213 |
|
| 214 |
Some config keys can have multiple values: |
| 215 |
|
| 216 |
```go |
| 217 |
// Add multiple fetch refspecs |
| 218 |
cfg.Set("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*") |
| 219 |
cfg.Set("remote.origin.fetch", "+refs/pull/*/head:refs/remotes/origin/pr/*") |
| 220 |
|
| 221 |
// Retrieve all values |
| 222 |
fetchSpecs, ok := cfg.GetAll("remote.origin.fetch") |
| 223 |
// fetchSpecs = ["+refs/heads/*:refs/remotes/origin/*", "+refs/pull/*/head:refs/remotes/origin/pr/*"] |
| 224 |
``` |
| 225 |
|
| 226 |
## Known Limitations |
| 227 |
|
| 228 |
Current implementation has the following known limitations: |
| 229 |
|
| 230 |
- **Bare boolean values** - Keys without values (bare booleans) are not supported |
| 231 |
- **Worktree support** - Only partial worktree config support |
| 232 |
- **includeIf conditions** - Only `gitdir` and `gitdir/i` are supported |
| 233 |
- **URL matching** - `url.<base>.insteadOf` patterns are not yet implemented |
| 234 |
- **Multivar operations** - No special handling for replacing specific multivar instances |
| 235 |
- **Whitespace preservation** - Insignificant whitespace is not always perfectly preserved |
| 236 |
|
| 237 |
These limitations reflect the primary use case supporting [gopass](https://github.com/gopasspw/gopass). Contributions to address these are welcome! |
| 238 |
|
| 239 |
## Project Documentation |
| 240 |
|
| 241 |
- **[ARCHITECTURE.md](ARCHITECTURE.md)** - Design decisions and internal architecture |
| 242 |
- **[CONTRIBUTING.md](CONTRIBUTING.md)** - Development guidelines and how to contribute |
| 243 |
- **[CHANGELOG.md](CHANGELOG.md)** - Version history and release notes |
| 244 |
- **[examples/](examples/)** - Runnable code examples demonstrating various features |
| 245 |
|
| 246 |
## Versioning and Compatibility |
| 247 |
|
| 248 |
This library aims to support the latest stable release of Git. We currently do not provide semantic versioning guarantees but aim to maintain backwards compatibility where possible. |
| 249 |
|
| 250 |
**Compatibility goals:** |
| 251 |
|
| 252 |
- Parse any valid Git config file correctly |
| 253 |
- Preserve config file structure when writing |
| 254 |
- Handle edge cases gracefully (malformed input, missing files, etc.) |
| 255 |
|
| 256 |
## Contributing |
| 257 |
|
| 258 |
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for: |
| 259 |
|
| 260 |
- Development setup instructions |
| 261 |
- Code style guidelines |
| 262 |
- Testing requirements |
| 263 |
- Pull request process |
| 264 |
|
| 265 |
### Quick Development Setup |
| 266 |
|
| 267 |
```bash |
| 268 |
# Clone the repository |
| 269 |
git clone https://github.com/gopasspw/gitconfig.git |
| 270 |
cd gitconfig |
| 271 |
|
| 272 |
# Run tests |
| 273 |
make test |
| 274 |
|
| 275 |
# Run code quality checks |
| 276 |
make codequality |
| 277 |
|
| 278 |
# Format code |
| 279 |
make fmt |
| 280 |
``` |
| 281 |
|
| 282 |
## Testing |
| 283 |
|
| 284 |
Run the full test suite: |
| 285 |
|
| 286 |
```bash |
| 287 |
# Run all tests |
| 288 |
go test ./... |
| 289 |
|
| 290 |
# Run with verbose output |
| 291 |
go test -v ./... |
| 292 |
|
| 293 |
# Run with coverage |
| 294 |
go test -cover ./... |
| 295 |
|
| 296 |
# Generate a coverage report |
| 297 |
go test -coverprofile=coverage.out ./... |
| 298 |
go tool cover -func=coverage.out | tail -1 |
| 299 |
|
| 300 |
# Current coverage (2026-02-17): 89.9% |
| 301 |
|
| 302 |
# Run specific test |
| 303 |
go test -run TestLoadConfig |
| 304 |
``` |
| 305 |
|
| 306 |
## License and Credit |
| 307 |
|
| 308 |
This package is licensed under the [MIT License](https://opensource.org/licenses/MIT). |
| 309 |
|
| 310 |
This repository is maintained to support the needs of [gopass](https://github.com/gopasspw/gopass), a password manager for the command line. We aim to make it universally useful for all Go projects that need Git config parsing. |
| 311 |
|
| 312 |
**Maintainers:** |
| 313 |
|
| 314 |
- Primary development and maintenance for gopass integration |
| 315 |
|
| 316 |
**Contributing:** |
| 317 |
Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) before submitting pull requests. |
| 318 |
|
| 319 |
## Support |
| 320 |
|
| 321 |
- **Issues**: [GitHub Issues](https://github.com/gopasspw/gitconfig/issues) |
| 322 |
- **Documentation**: [GoDoc](https://godoc.org/github.com/gopasspw/gitconfig) |
| 323 |
- **Examples**: [examples/](examples/) directory in this repository |