|
| 1 | +# Copilot Instructions for gitbackup |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +`gitbackup` is a command-line tool written in Go that backs up Git repositories from GitHub, GitLab, and Bitbucket. It supports two operation modes: |
| 6 | +1. Creating clones of Git repositories (all services) |
| 7 | +2. Creating user migrations using GitHub's Migration API (GitHub only) |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +- **Main entry point**: `main.go` - Initializes configuration, validates it, creates clients, and dispatches to appropriate handlers |
| 12 | +- **Configuration**: `config.go` and `options.go` - Defines command-line flags and application configuration |
| 13 | +- **Client creation**: `client.go` - Creates service-specific API clients (GitHub, GitLab, Bitbucket) |
| 14 | +- **Repository handling**: `repositories.go` - Fetches repository lists from different services |
| 15 | +- **Backup operations**: `backup.go` - Handles git clone and update operations |
| 16 | +- **GitHub-specific features**: |
| 17 | + - `github.go` - GitHub API interactions |
| 18 | + - `github_create_user_migration.go` - Creates and downloads user migrations |
| 19 | + - `github_list_user_migrations.go` - Lists available migrations |
| 20 | + - `user_data.go` - Migration-related utilities |
| 21 | + |
| 22 | +## Coding Standards |
| 23 | + |
| 24 | +### Go Conventions |
| 25 | +- Follow standard Go formatting (use `gofmt`) |
| 26 | +- Use Go 1.24 features and syntax |
| 27 | +- Keep functions focused and single-purpose |
| 28 | +- Use descriptive variable names (e.g., `backupDir`, `ignorePrivate`) |
| 29 | + |
| 30 | +### Error Handling |
| 31 | +- Return errors, don't panic (except for fatal initialization errors) |
| 32 | +- Use `log.Fatal()` or `log.Fatalf()` for unrecoverable errors in main execution path |
| 33 | +- Provide context in error messages |
| 34 | + |
| 35 | +### Testing |
| 36 | +- Write table-driven tests where appropriate |
| 37 | +- Use test helpers for setup/teardown (see `repositories_test.go`) |
| 38 | +- Mock external dependencies (HTTP servers for API testing) |
| 39 | +- Store expected test outputs in `testdata/` directory with `.golden` files |
| 40 | +- Run tests with `go test` (no additional flags needed) |
| 41 | + |
| 42 | +### Code Style |
| 43 | +- Use global variables sparingly (mainly for dependency injection in tests) |
| 44 | +- Inject dependencies for testability (e.g., `execCommand`, `appFS`) |
| 45 | +- Use meaningful struct names with context (e.g., `appConfig`, not just `config`) |
| 46 | +- Document exported functions and types |
| 47 | +- Use constants for magic numbers and strings |
| 48 | + |
| 49 | +## Common Patterns |
| 50 | + |
| 51 | +### Service Detection |
| 52 | +```go |
| 53 | +var knownServices = map[string]string{ |
| 54 | + "github": "github.com", |
| 55 | + "gitlab": "gitlab.com", |
| 56 | + "bitbucket": "bitbucket.org", |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Command-Line Flags |
| 61 | +- Generic flags: `-service`, `-backupdir`, `-bare`, `-ignore-private`, `-ignore-fork` |
| 62 | +- Service-specific flags use prefixes: `-github.*`, `-gitlab.*` |
| 63 | +- Boolean flags default to `false` unless specified |
| 64 | + |
| 65 | +### Concurrency |
| 66 | +- Use `sync.WaitGroup` for concurrent git operations |
| 67 | +- Limit concurrent clones with `MaxConcurrentClones` constant |
| 68 | +- Always defer `wg.Done()` at function start |
| 69 | + |
| 70 | +### File Operations |
| 71 | +- Use `afero.Fs` interface for file system operations (enables testing) |
| 72 | +- Use `path.Join()` for path construction |
| 73 | +- Check if directories/files exist before operations |
| 74 | + |
| 75 | +## Testing Requirements |
| 76 | + |
| 77 | +### Running Tests |
| 78 | +```bash |
| 79 | +go test # Run all tests |
| 80 | +go test -v # Run with verbose output |
| 81 | +``` |
| 82 | + |
| 83 | +### Test Structure |
| 84 | +- Unit tests in `*_test.go` files alongside source |
| 85 | +- Use `httptest.NewServer()` for mocking API endpoints |
| 86 | +- Set environment variables for tokens in test setup |
| 87 | +- Clean up resources in teardown functions |
| 88 | + |
| 89 | +### Golden Files |
| 90 | +- CLI output tests use golden files in `testdata/` |
| 91 | +- Platform-specific golden files: `TestName.golden.windows` for Windows |
| 92 | +- Update golden files when intentionally changing output |
| 93 | + |
| 94 | +## Building |
| 95 | + |
| 96 | +```bash |
| 97 | +go build # Build binary |
| 98 | +``` |
| 99 | + |
| 100 | +The resulting binary will be named `gitbackup` (or `gitbackup.exe` on Windows). |
| 101 | + |
| 102 | +## Environment Variables |
| 103 | + |
| 104 | +- `GITHUB_TOKEN` - GitHub personal access token |
| 105 | +- `GITLAB_TOKEN` - GitLab personal access token |
| 106 | +- `BITBUCKET_USERNAME` - Bitbucket username |
| 107 | +- `BITBUCKET_PASSWORD` - Bitbucket app password |
| 108 | + |
| 109 | +## Git Operations |
| 110 | + |
| 111 | +- Clone operations use `git clone` command |
| 112 | +- Bare clones use `git clone --mirror` |
| 113 | +- Updates use `git pull` (normal) or `git remote update --prune` (bare) |
| 114 | +- Support both SSH and HTTPS cloning (via `-use-https-clone` flag) |
| 115 | + |
| 116 | +## Important Notes |
| 117 | + |
| 118 | +- The tool is designed for personal backup, not as a backup service |
| 119 | +- Repository namespaces (user/org) are preserved in backup directory structure |
| 120 | +- Private repositories can be excluded with `-ignore-private` |
| 121 | +- Forked repositories can be excluded with `-ignore-fork` |
| 122 | +- GitHub migrations download `.tar.gz` archives containing full repository data |
| 123 | + |
| 124 | +## When Making Changes |
| 125 | + |
| 126 | +1. **Maintain backward compatibility** - Don't break existing command-line flags |
| 127 | +2. **Update tests** - Add or modify tests for any new functionality |
| 128 | +3. **Update README** - Document new flags or features |
| 129 | +4. **Test on multiple platforms** - CI runs on Linux, macOS, and Windows |
| 130 | +5. **Follow existing patterns** - Match the style and structure of similar code |
| 131 | +6. **Update golden files** - Regenerate if CLI output changes intentionally |
0 commit comments