| 1 |
# Development Guide |
| 2 |
|
| 3 |
This guide covers the development workflow, architecture decisions, and implementation details for contributors working on the gitconfig library. |
| 4 |
|
| 5 |
## Table of Contents |
| 6 |
|
| 7 |
- [Getting Started](#getting-started) |
| 8 |
- [Project Architecture](#project-architecture) |
| 9 |
- [Development Workflow](#development-workflow) |
| 10 |
- [Code Organization](#code-organization) |
| 11 |
- [Testing Strategy](#testing-strategy) |
| 12 |
- [Common Development Tasks](#common-development-tasks) |
| 13 |
- [Debugging Tips](#debugging-tips) |
| 14 |
- [Performance Considerations](#performance-considerations) |
| 15 |
- [Release Process](#release-process) |
| 16 |
|
| 17 |
## Getting Started |
| 18 |
|
| 19 |
### Prerequisites |
| 20 |
|
| 21 |
- **Go**: 1.24 or later |
| 22 |
- **golangci-lint**: For code quality checks |
| 23 |
- **make**: For build automation |
| 24 |
- **git**: For version control |
| 25 |
|
| 26 |
### Initial Setup |
| 27 |
|
| 28 |
```bash |
| 29 |
# Clone the repository |
| 30 |
git clone https://github.com/gopasspw/gitconfig.git |
| 31 |
cd gitconfig |
| 32 |
|
| 33 |
# Run tests to verify setup |
| 34 |
make test |
| 35 |
|
| 36 |
# Run code quality checks |
| 37 |
make codequality |
| 38 |
|
| 39 |
# Format code |
| 40 |
make fmt |
| 41 |
``` |
| 42 |
|
| 43 |
The project should build and all tests should pass on a fresh clone. |
| 44 |
|
| 45 |
## Project Architecture |
| 46 |
|
| 47 |
### Core Components |
| 48 |
|
| 49 |
The library is organized into three main components: |
| 50 |
|
| 51 |
#### 1. Config (Single Scope) |
| 52 |
|
| 53 |
**File**: `config.go` |
| 54 |
|
| 55 |
Handles a single configuration file with format preservation: |
| 56 |
|
| 57 |
```go |
| 58 |
type Config struct { |
| 59 |
path string // File path |
| 60 |
parsed map[string]string // Parsed key-value pairs |
| 61 |
raw []string // Original file lines |
| 62 |
} |
| 63 |
``` |
| 64 |
|
| 65 |
**Key design decision**: Maintains both `parsed` (for quick lookups) and `raw` (for format preservation) representations. When writing, the library updates the raw representation to preserve comments, whitespace, and structure. |
| 66 |
|
| 67 |
**Methods**: |
| 68 |
|
| 69 |
- `LoadConfig(path)` - Load from file |
| 70 |
- `Get(key)` / `GetAll(key)` - Read values |
| 71 |
- `Set(key, value)` - Write values |
| 72 |
- `Write()` - Persist changes |
| 73 |
|
| 74 |
#### 2. Configs (Multi-Scope) |
| 75 |
|
| 76 |
**File**: `configs.go` |
| 77 |
|
| 78 |
Manages multiple configuration scopes with precedence: |
| 79 |
|
| 80 |
```go |
| 81 |
type Configs struct { |
| 82 |
env *Config // Environment variables (highest precedence) |
| 83 |
worktree *Config // Worktree-specific config |
| 84 |
local *Config // Repository config |
| 85 |
global *Config // User config |
| 86 |
system *Config // System-wide config |
| 87 |
preset *Config // Built-in defaults (lowest precedence) |
| 88 |
} |
| 89 |
``` |
| 90 |
|
| 91 |
**Precedence order**: env > worktree > local > global > system > preset |
| 92 |
|
| 93 |
**Methods**: |
| 94 |
|
| 95 |
- `LoadAll(workdir)` - Load all scopes |
| 96 |
- `Get(key)` - Read from combined config (respects precedence) |
| 97 |
- `GetLocal(key)`, `GetGlobal(key)`, etc. - Scope-specific reads |
| 98 |
- `Set(key, value)` - Write to local scope (default) |
| 99 |
- `SetLocal()`, `SetGlobal()`, etc. - Scope-specific writes |
| 100 |
|
| 101 |
#### 3. Utilities |
| 102 |
|
| 103 |
**File**: `utils.go` |
| 104 |
|
| 105 |
Helper functions for parsing and formatting: |
| 106 |
|
| 107 |
- `parseKey(key)` - Extract section, subsection, key from dotted notation |
| 108 |
- `trim(lines)` - Clean whitespace |
| 109 |
- `unescapeValue(val)` - Handle escape sequences |
| 110 |
- `globMatch(pattern, path)` - Pattern matching for includeIf |
| 111 |
|
| 112 |
### Data Flow |
| 113 |
|
| 114 |
```text |
| 115 |
┌─────────────────────────────────────────────────────┐ |
| 116 |
│ User API Call (Get/Set) │ |
| 117 |
└──────────────────┬──────────────────────────────────┘ |
| 118 |
│ |
| 119 |
▼ |
| 120 |
┌─────────────────────────────────────────────────────┐ |
| 121 |
│ Configs (Multi-scope coordinator) │ |
| 122 |
│ - Checks each scope in precedence order │ |
| 123 |
│ - Returns first match for reads │ |
| 124 |
│ - Routes writes to appropriate scope │ |
| 125 |
└──────────────────┬──────────────────────────────────┘ |
| 126 |
│ |
| 127 |
▼ |
| 128 |
┌─────────────────────────────────────────────────────┐ |
| 129 |
│ Config (Single file handler) │ |
| 130 |
│ - Parses file into map + raw lines │ |
| 131 |
│ - Handles includes recursively │ |
| 132 |
│ - Updates both map and raw on changes │ |
| 133 |
└──────────────────┬──────────────────────────────────┘ |
| 134 |
│ |
| 135 |
▼ |
| 136 |
┌─────────────────────────────────────────────────────┐ |
| 137 |
│ File I/O │ |
| 138 |
│ - Read: os.ReadFile │ |
| 139 |
│ - Write: os.WriteFile with preservation │ |
| 140 |
└─────────────────────────────────────────────────────┘ |
| 141 |
``` |
| 142 |
|
| 143 |
## Development Workflow |
| 144 |
|
| 145 |
### Making Changes |
| 146 |
|
| 147 |
1. **Create a feature branch** |
| 148 |
|
| 149 |
```bash |
| 150 |
git checkout -b feature/your-feature-name |
| 151 |
``` |
| 152 |
|
| 153 |
2. **Make your changes** |
| 154 |
- Follow existing code style |
| 155 |
- Add tests for new functionality |
| 156 |
- Update documentation |
| 157 |
|
| 158 |
3. **Run quality checks** |
| 159 |
|
| 160 |
```bash |
| 161 |
make fmt # Format code |
| 162 |
make test # Run tests |
| 163 |
make codequality # Lint checks |
| 164 |
``` |
| 165 |
|
| 166 |
4. **Commit with conventional commits** |
| 167 |
|
| 168 |
```bash |
| 169 |
git commit -m "feat: add support for X" |
| 170 |
git commit -m "fix: handle edge case Y" |
| 171 |
git commit -m "docs: update readme for Z" |
| 172 |
``` |
| 173 |
|
| 174 |
5. **Push and create PR** |
| 175 |
|
| 176 |
```bash |
| 177 |
git push origin feature/your-feature-name |
| 178 |
``` |
| 179 |
|
| 180 |
### Commit Message Format |
| 181 |
|
| 182 |
Follow [Conventional Commits](https://www.conventionalcommits.org/): |
| 183 |
|
| 184 |
- `feat:` - New feature |
| 185 |
- `fix:` - Bug fix |
| 186 |
- `docs:` - Documentation changes |
| 187 |
- `test:` - Test additions or changes |
| 188 |
- `refactor:` - Code restructuring |
| 189 |
- `style:` - Formatting, whitespace |
| 190 |
- `chore:` - Build, dependencies |
| 191 |
|
| 192 |
## Code Organization |
| 193 |
|
| 194 |
### File Structure |
| 195 |
|
| 196 |
```text |
| 197 |
gitconfig/ |
| 198 |
├── config.go # Single config file handling |
| 199 |
├── configs.go # Multi-scope coordination |
| 200 |
├── utils.go # Helper functions |
| 201 |
├── gitconfig.go # Default settings for git |
| 202 |
├── gitconfig_windows.go # Windows-specific code |
| 203 |
├── gitconfig_others.go # Unix-specific code |
| 204 |
├── doc.go # Package documentation |
| 205 |
├── config_test.go # Config tests |
| 206 |
├── configs_test.go # Configs tests |
| 207 |
├── utils_test.go # Utility tests |
| 208 |
├── config_errors_test.go # Error handling tests |
| 209 |
├── config_parse_errors_test.go # Parse error tests |
| 210 |
├── config_include_errors_test.go # Include tests |
| 211 |
├── config_edge_cases_test.go # Edge case tests |
| 212 |
├── examples/ # Usage examples |
| 213 |
├── ARCHITECTURE.md # Design documentation |
| 214 |
├── CONTRIBUTING.md # Contribution guidelines |
| 215 |
├── CONFIG_FORMAT.md # Format reference |
| 216 |
└── DEVELOPMENT.md # This file |
| 217 |
``` |
| 218 |
|
| 219 |
### Naming Conventions |
| 220 |
|
| 221 |
- **Exported functions**: Start with capital letter, use PascalCase |
| 222 |
- `LoadConfig()`, `Get()`, `SetGlobal()` |
| 223 |
|
| 224 |
- **Unexported functions**: Start with lowercase, use camelCase |
| 225 |
- `parseKey()`, `getEffectiveIncludes()` |
| 226 |
|
| 227 |
- **Test functions**: `Test<FunctionName>` |
| 228 |
- `TestLoadConfig()`, `TestGet()` |
| 229 |
|
| 230 |
- **Types**: PascalCase |
| 231 |
- `Config`, `Configs` |
| 232 |
|
| 233 |
- **Variables**: camelCase (short names acceptable for local variables) |
| 234 |
- `cfg`, `key`, `value` |
| 235 |
|
| 236 |
### Code Style Guidelines |
| 237 |
|
| 238 |
1. **Function length**: Keep functions focused and under ~50 lines |
| 239 |
2. **Comments**: |
| 240 |
- All exported functions need godoc comments |
| 241 |
- Complex logic needs inline comments |
| 242 |
- Use complete sentences with periods |
| 243 |
3. **Error handling**: Always check and handle errors explicitly |
| 244 |
4. **Testing**: Use table-driven tests with `t.Run()` and `t.Parallel()` |
| 245 |
|
| 246 |
## Testing Strategy |
| 247 |
|
| 248 |
### Test Organization |
| 249 |
|
| 250 |
Tests are organized by component and purpose: |
| 251 |
|
| 252 |
- **`config_test.go`**: Basic Config functionality |
| 253 |
- **`configs_test.go`**: Multi-scope behavior and precedence |
| 254 |
- **`utils_test.go`**: Utility function tests |
| 255 |
- **`config_errors_test.go`**: File system and general error handling |
| 256 |
- **`config_parse_errors_test.go`**: Malformed syntax handling |
| 257 |
- **`config_include_errors_test.go`**: Include directive edge cases |
| 258 |
- **`config_edge_cases_test.go`**: Unusual but valid configurations |
| 259 |
|
| 260 |
### Writing Tests |
| 261 |
|
| 262 |
Use table-driven tests: |
| 263 |
|
| 264 |
```go |
| 265 |
func TestFunctionName(t *testing.T) { |
| 266 |
t.Parallel() |
| 267 |
|
| 268 |
testCases := []struct { |
| 269 |
name string |
| 270 |
input string |
| 271 |
expected string |
| 272 |
wantErr bool |
| 273 |
}{ |
| 274 |
{ |
| 275 |
name: "basic case", |
| 276 |
input: "value", |
| 277 |
expected: "value", |
| 278 |
wantErr: false, |
| 279 |
}, |
| 280 |
// ... more cases |
| 281 |
} |
| 282 |
|
| 283 |
for _, tc := range testCases { |
| 284 |
t.Run(tc.name, func(t *testing.T) { |
| 285 |
t.Parallel() |
| 286 |
|
| 287 |
result, err := FunctionName(tc.input) |
| 288 |
|
| 289 |
if tc.wantErr { |
| 290 |
require.Error(t, err) |
| 291 |
} else { |
| 292 |
require.NoError(t, err) |
| 293 |
assert.Equal(t, tc.expected, result) |
| 294 |
} |
| 295 |
}) |
| 296 |
} |
| 297 |
} |
| 298 |
``` |
| 299 |
|
| 300 |
### Testing Best Practices |
| 301 |
|
| 302 |
1. **Use temp directories**: Always use `t.TempDir()` for file operations |
| 303 |
2. **Parallel tests**: Add `t.Parallel()` unless tests modify global state |
| 304 |
3. **Assertions**: Use `testify/assert` and `testify/require` |
| 305 |
- `require`: For critical checks (test cannot continue if fails) |
| 306 |
- `assert`: For non-critical checks |
| 307 |
4. **Coverage**: Aim for >80% coverage, especially for new code |
| 308 |
5. **Error paths**: Test both success and failure scenarios |
| 309 |
|
| 310 |
### Running Tests |
| 311 |
|
| 312 |
```bash |
| 313 |
# Run all tests |
| 314 |
go test ./... |
| 315 |
|
| 316 |
# Run with verbose output |
| 317 |
go test -v ./... |
| 318 |
|
| 319 |
# Run specific test |
| 320 |
go test -run TestLoadConfig |
| 321 |
|
| 322 |
# Run with coverage |
| 323 |
go test -cover ./... |
| 324 |
|
| 325 |
# Generate coverage report |
| 326 |
go test -coverprofile=coverage.out ./... |
| 327 |
go tool cover -html=coverage.out |
| 328 |
|
| 329 |
# Current coverage (2026-02-17): 89.9% |
| 330 |
``` |
| 331 |
|
| 332 |
## Common Development Tasks |
| 333 |
|
| 334 |
### Adding a New Configuration Method |
| 335 |
|
| 336 |
1. **Determine scope** (Config or Configs) |
| 337 |
2. **Add method**: |
| 338 |
|
| 339 |
```go |
| 340 |
// GetBool retrieves a boolean value from config. |
| 341 |
func (c *Configs) GetBool(key string) (bool, bool) { |
| 342 |
val, ok := c.Get(key) |
| 343 |
if !ok { |
| 344 |
return false, false |
| 345 |
} |
| 346 |
// Parse boolean |
| 347 |
return parseBool(val), true |
| 348 |
} |
| 349 |
``` |
| 350 |
|
| 351 |
3. **Add tests**: |
| 352 |
|
| 353 |
```go |
| 354 |
func TestGetBool(t *testing.T) { |
| 355 |
t.Parallel() |
| 356 |
// Test cases... |
| 357 |
} |
| 358 |
``` |
| 359 |
|
| 360 |
4. **Update documentation** in `doc.go` if user-facing |
| 361 |
|
| 362 |
### Adding Support for a New Section Type |
| 363 |
|
| 364 |
1. **Understand Git behavior** (test with actual git) |
| 365 |
2. **Update parser** in `config.go` if syntax changes needed |
| 366 |
3. **Add test cases** with real-world examples |
| 367 |
4. **Document** in `CONFIG_FORMAT.md` |
| 368 |
|
| 369 |
### Handling a New Include Type |
| 370 |
|
| 371 |
1. **Check Git documentation** for semantics |
| 372 |
2. **Add condition parser** in `getConditionalIncludes()` |
| 373 |
3. **Add tests** in `config_include_errors_test.go` |
| 374 |
4. **Update** `CONFIG_FORMAT.md` with examples |
| 375 |
|
| 376 |
### Cross-Platform Considerations |
| 377 |
|
| 378 |
Platform-specific code goes in: |
| 379 |
|
| 380 |
- `gitconfig_windows.go` - Windows (`//go:build windows`) |
| 381 |
- `gitconfig_others.go` - Unix/Linux (`//go:build !windows`) |
| 382 |
|
| 383 |
Example: |
| 384 |
|
| 385 |
```go |
| 386 |
// gitconfig_windows.go |
| 387 |
//go:build windows |
| 388 |
|
| 389 |
package gitconfig |
| 390 |
|
| 391 |
func getDefaultSystemConfig() string { |
| 392 |
return "C:\\ProgramData\\Git\\config" |
| 393 |
} |
| 394 |
``` |
| 395 |
|
| 396 |
## Debugging Tips |
| 397 |
|
| 398 |
### Debugging Tests |
| 399 |
|
| 400 |
```bash |
| 401 |
# Run single test with verbose output |
| 402 |
go test -v -run TestSpecificTest |
| 403 |
|
| 404 |
# Add debug prints in tests |
| 405 |
t.Logf("Config state: %+v", cfg) |
| 406 |
|
| 407 |
# Use delve debugger |
| 408 |
dlv test -- -test.run TestSpecificTest |
| 409 |
``` |
| 410 |
|
| 411 |
### Debugging File Parsing |
| 412 |
|
| 413 |
Add temporary debug output: |
| 414 |
|
| 415 |
```go |
| 416 |
func (c *Config) Load() error { |
| 417 |
// ... parsing logic |
| 418 |
|
| 419 |
// Debug: print parsed state |
| 420 |
for k, v := range c.parsed { |
| 421 |
fmt.Printf("DEBUG: %s = %s\n", k, v) |
| 422 |
} |
| 423 |
|
| 424 |
// ... rest of function |
| 425 |
} |
| 426 |
``` |
| 427 |
|
| 428 |
### Common Issues |
| 429 |
|
| 430 |
**Issue**: Tests fail with permission errors |
| 431 |
|
| 432 |
- **Solution**: Ensure using `t.TempDir()` for test files |
| 433 |
- **Solution**: Check file permissions in test setup |
| 434 |
|
| 435 |
**Issue**: Include tests fail inconsistently |
| 436 |
|
| 437 |
- **Solution**: Check for absolute vs relative path handling |
| 438 |
- **Solution**: Verify include depth limits |
| 439 |
|
| 440 |
**Issue**: Write operations don't preserve format |
| 441 |
|
| 442 |
- **Solution**: Check that `raw` slice is being updated |
| 443 |
- **Solution**: Verify line matching logic in write operations |
| 444 |
|
| 445 |
## Performance Considerations |
| 446 |
|
| 447 |
### Parsing Performance |
| 448 |
|
| 449 |
- **Current**: File is fully parsed on load |
| 450 |
- **Optimization**: Consider lazy loading for large configs |
| 451 |
- **Benchmark**: Add benchmarks before optimizing |
| 452 |
|
| 453 |
### Memory Usage |
| 454 |
|
| 455 |
- **Trade-off**: We keep both `parsed` map and `raw` lines |
| 456 |
- **Benefit**: Enables format preservation |
| 457 |
- **Cost**: ~2x memory of parsed-only approach |
| 458 |
- **Acceptable**: Config files are typically small (<100KB) |
| 459 |
|
| 460 |
### Include Performance |
| 461 |
|
| 462 |
- **Current**: Includes are loaded recursively |
| 463 |
- **Watch for**: Circular includes (we have depth limits) |
| 464 |
- **Future**: Could cache included files |
| 465 |
|
| 466 |
### Benchmarking |
| 467 |
|
| 468 |
Create benchmarks for performance-critical code: |
| 469 |
|
| 470 |
```go |
| 471 |
func BenchmarkParseKey(b *testing.B) { |
| 472 |
for i := 0; i < b.N; i++ { |
| 473 |
parseKey("section.subsection.key") |
| 474 |
} |
| 475 |
} |
| 476 |
``` |
| 477 |
|
| 478 |
Run benchmarks: |
| 479 |
|
| 480 |
```bash |
| 481 |
go test -bench=. -benchmem |
| 482 |
``` |
| 483 |
|
| 484 |
## Release Process |
| 485 |
|
| 486 |
### Version Numbering |
| 487 |
|
| 488 |
The project does use semantic versioning. |
| 489 |
|
| 490 |
### Release Checklist |
| 491 |
|
| 492 |
1. **Update CHANGELOG.md** |
| 493 |
|
| 494 |
- Add new version section |
| 495 |
- List all changes since last release |
| 496 |
- Categorize: Added, Changed, Deprecated, Removed, Fixed, Security |
| 497 |
|
| 498 |
2. **Run full test suite** |
| 499 |
|
| 500 |
```bash |
| 501 |
make test |
| 502 |
make codequality |
| 503 |
``` |
| 504 |
|
| 505 |
3. **Test cross-compilation** |
| 506 |
|
| 507 |
```bash |
| 508 |
make crosscompile |
| 509 |
``` |
| 510 |
|
| 511 |
4. **Update documentation** |
| 512 |
|
| 513 |
- Ensure README is current |
| 514 |
- Check godoc examples |
| 515 |
- Verify all links work |
| 516 |
|
| 517 |
5. **Tag release** |
| 518 |
|
| 519 |
```bash |
| 520 |
git tag -a v0.x.y -m "Release v0.x.y" |
| 521 |
git push origin v0.x.y |
| 522 |
``` |
| 523 |
|
| 524 |
6. **Announce** |
| 525 |
- Update dependent projects (gopass) |
| 526 |
|
| 527 |
## Additional Resources |
| 528 |
|
| 529 |
- **Architecture**: See [ARCHITECTURE.md](ARCHITECTURE.md) for design decisions |
| 530 |
- **Contributing**: See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines |
| 531 |
- **Format**: See [CONFIG_FORMAT.md](CONFIG_FORMAT.md) for Git config format details |
| 532 |
- **Examples**: See [examples/](examples/) for usage examples |
| 533 |
- **Git Docs**: <https://git-scm.com/docs/git-config> |
| 534 |
|
| 535 |
## Getting Help |
| 536 |
|
| 537 |
- **Issues**: Open an issue on GitHub for bugs or questions |
| 538 |
- **Discussions**: Use GitHub Discussions for general questions |
| 539 |
- **Gopass**: This library primarily supports gopass; consult gopass maintainers for integration questions |
| 540 |
|
| 541 |
## Maintainer Notes |
| 542 |
|
| 543 |
### Code Review Checklist |
| 544 |
|
| 545 |
When reviewing PRs: |
| 546 |
|
| 547 |
- [ ] Tests added for new functionality |
| 548 |
- [ ] Tests pass and coverage doesn't decrease |
| 549 |
- [ ] Code follows existing style |
| 550 |
- [ ] Godoc added for exported functions |
| 551 |
- [ ] CHANGELOG updated if user-facing change |
| 552 |
- [ ] Cross-platform considerations addressed |
| 553 |
- [ ] No breaking changes (or clearly documented) |