# git-lfs/git-lfs architecture

> Architecture diagram of the GitHub repository git-lfs/git-lfs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/git-lfs/git-lfs
- Repository: https://github.com/git-lfs/git-lfs

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    GitCore("Git Core"):::external
    LFSServer("LFS Server"):::external

    %% CLI Layer
    subgraph "Git LFS CLI"
        subgraph "Commands & Dispatch"
            Commands["commands/"]:::internal
            Run["run.go"]:::internal
        end
        subgraph "Git Integration"
            GitPlumbing["git/"]:::internal
            FSOps["fs/"]:::internal
            FilterCore["lfs/gitfilter.go"]:::internal
            FilterClean["lfs/gitfilter_clean.go"]:::internal
            FilterSmudge["lfs/gitfilter_smudge.go"]:::internal
            FilterProcess["git/filter_process_scanner.go"]:::internal
        end
    end

    %% Configuration & Credentials
    Config["config/"]:::internal
    Creds["creds/"]:::internal

    %% Local Storage
    LocalStore[".git/lfs/objects & pointer files"]:::cylinder

    %% Transfer Queue
    subgraph "Transfer Queue"
        TQ["tq/"]:::internal
        TQCore["tq/transfer_queue.go"]:::internal
    end

    %% Network Layer
    subgraph "Network Layer"
        LFSAPI["lfsapi/"]:::internal
        HTTPTransport["lfshttp/"]:::internal
        LockingMod["locking/"]:::internal
    end

    %% Utilities & Support
    subgraph "Utilities & Support"
        Tools["tools/"]:::internal
        Subproc["subprocess/"]:::internal
        TaskLog["tasklog/"]:::internal
        Errors["errors/"]:::internal
        FilePathFilter["filepathfilter/"]:::internal
    end

    %% LFS Server Sub-services
    subgraph "LFS Server"
        BatchAPI("Batch API"):::internal
        LockAPI("Locking API"):::internal
    end

    %% Connections
    GitCore -->|"git lfs subcommand"| Commands
    GitCore -->|"hook registration"| Run

    GitCore -->|"Git filter process"| FilterCore
    FilterCore -->|"clean/smudge"| LocalStore
    FilterCore -->|"enqueue"| TQCore
    FilterClean -->|"clean"| LocalStore
    FilterSmudge -->|"smudge"| LocalStore
    FilterProcess -->|"filter-process"| LocalStore

    TQCore -->|"parallel workers"| LFSAPI
    Config -->|"determine endpoint"| LFSAPI
    Creds -->|"auth info"| LFSAPI
    LFSAPI -->|"Batch API (JSON)"| HTTPTransport
    LockingMod -->|"Lock/Unlock API"| HTTPTransport
    HTTPTransport -->|"HTTP/SSH transfer"| LFSServer

    LFSServer -->|"Batch API"| BatchAPI
    LFSServer -->|"Lock API"| LockAPI

    %% Click Events
    click Commands "https://github.com/git-lfs/git-lfs/tree/main/commands/"
    click Run "https://github.com/git-lfs/git-lfs/blob/main/commands/run.go"
    click GitPlumbing "https://github.com/git-lfs/git-lfs/tree/main/git/"
    click FSOps "https://github.com/git-lfs/git-lfs/tree/main/fs/"
    click FilterCore "https://github.com/git-lfs/git-lfs/blob/main/lfs/gitfilter.go"
    click FilterClean "https://github.com/git-lfs/git-lfs/blob/main/lfs/gitfilter_clean.go"
    click FilterSmudge "https://github.com/git-lfs/git-lfs/blob/main/lfs/gitfilter_smudge.go"
    click FilterProcess "https://github.com/git-lfs/git-lfs/blob/main/git/filter_process_scanner.go"
    click Config "https://github.com/git-lfs/git-lfs/tree/main/config/"
    click Creds "https://github.com/git-lfs/git-lfs/tree/main/creds/"
    click TQ "https://github.com/git-lfs/git-lfs/tree/main/tq/"
    click TQCore "https://github.com/git-lfs/git-lfs/blob/main/tq/transfer_queue.go"
    click LFSAPI "https://github.com/git-lfs/git-lfs/tree/main/lfsapi/"
    click HTTPTransport "https://github.com/git-lfs/git-lfs/tree/main/lfshttp/"
    click LockingMod "https://github.com/git-lfs/git-lfs/tree/main/locking/"

    %% Styles
    classDef external fill:#FFE599,stroke:#000000,stroke-width:1px;
    classDef internal fill:#D9EAD3,stroke:#38761D,stroke-width:1px;
    classDef cylinder fill:#C9DAF8,stroke:#1155CC,stroke-width:1px;
```
