How git-lfs/git-lfs works
Project Type and Purpose
• This is a command-line extension written in Go that integrates with Git (“Git LFS”) to manage large file storage.
• It provides:
• A set of Git filter-drivers (clean/smudge/filter-process)
• CLI commands (git lfs <subcommand>)
• A transfer queue and network client to communicate with remote LFS servers
• JSON-schema‐driven APIs for batch transfers and locking
High-Level Components
a. CLI & Git Integration
• commands/ (all command_*.go): implements each git lfs <subcommand>
• run.go: general dispatch of CLI subcommands
• git/* & fs/*: wrappers around Git plumbing commands and filesystem operations
• filter drivers (lfs/gitfilter.go, lfs/gitfilter_clean.go, lfs/gitfilter_smudge.go, lfs/filter_process_scanner.go)
b. Configuration & Credentials
• config/: load env, .lfsconfig, Git config, extension points
• creds/: netrc, OS keychain, credential helpers
c. LFS Object Model
• lfs/: pointer file handling, local object store layout, hooks
• pointer.go: marshals/unmarshals .gitattributes pointers
d. Network Layer
• lfsapi/: high-level LFS API client (batch, auth, endpoint discovery)
• lfshttp/: HTTP transport, retries, proxy, SSH transport fallback
• locking/: remote lock/unlock via HTTP API
e. Transfer Queue
• tq/: request queue, parallel upload/download engine, adapter pattern for “basic”, “tus” and custom transfers
f. Utilities & Support
• tools/, subprocess/, tasklog/: generic IO, command execution, progress reporting
• errors/, filepathfilter/: error wrapping, glob pattern matching
Interactions & Data Flow
1. User runs git add / git commit / git clone …
2. Git invokes LFS filter drivers (clean/smudge or filter-process)
3. lfs/gitfilter.go converts large file → pointer file on commit, or pointer → large file on checkout
4. filter drivers enqueue transfer requests in tq/transfer_queue.go
5. config/ + creds/ determine remote endpoint & authentication
6. lfsapi.Client makes a Batch API call → get upload/download URLs
7. lfshttp.Client performs HTTP(S) or SSH transfers (with retries)
8. Completed objects cached under .git/lfs/objects/…
9. locking/api.go can optionally obtain or verify locks via HTTP endpoints
Architectural Patterns & Principles
• Modular Go packages, each with clear responsibility
• Adapter/Strategy in tq for multiple transfer modes (basic, tus, custom)
• Hook‐based integration with Git (filter‐process protocol)
• Separation of concerns: CLI vs core library vs network vs storage
• Use of interfaces to abstract HTTP client, subprocess execution, credential sources