A minimal implementation of Git core functionality built as part of the CodeCrafters Git challenge.
git init- Initialize a new Git repositorygit cat-file- Display object contentsgit hash-object- Compute object hash and storegit ls-tree- List tree contentsgit write-tree- Create tree from working directorygit commit-tree- Create commit objectsgit clone- Clone remote repositories via Smart HTTP protocol
graph TD
A[Git Commands] --> B[Object Layer]
A --> C[Command Layer]
B --> D[Blob Objects]
B --> E[Tree Objects]
B --> F[Commit Objects]
B --> G[Packfile Parser]
C --> H[Init Command]
C --> I[Cat File Command]
C --> J[Hash Object Command]
C --> K[Ls Tree Command]
C --> L[Write Tree Command]
C --> M[Commit Tree Command]
C --> N[Clone Command]
N --> O[HTTP Protocol]
N --> P[Packfile Unpacking]
N --> Q[Checkout Process]
O --> R[Discovery Phase]
O --> S[Negotiation Phase]
P --> T[Delta Compression]
P --> U[Object Storage]
Q --> V[Tree Traversal]
Q --> W[File Creation]
- Objects stored in
.git/objects/with SHA-1 based directory structure - Zlib compression for efficient storage
- Standard Git object format:
type size\0content
- Smart HTTP protocol for remote repository access
- Pkt-line format for network communication
- Packfile parsing with REF_DELTA support
- Delta compression for efficient data transfer
- Recursive tree checkout to working directory
- Discovery: GET
/info/refs?service=git-upload-pack - Negotiation: POST
/git-upload-packwith want/have lines - Transfer: Receive packfile data
- Unpacking: Parse and store objects
- Checkout: Create working directory files
# Build the project
go build -o bin/gitx app/main.go
# Initialize repository
./bin/gitx init
# Add and commit files
./bin/gitx hash-object -w file.txt
./bin/gitx write-tree
./bin/gitx commit-tree <tree-sha> -p <parent-sha> -m "commit message"
# Clone remote repository
./bin/gitx clone https://github.com/user/repo.git target-dir├── app/
│ └── main.go # Main entry point
├── commands/
│ ├── init.go # Git init implementation
│ ├── cat_file.go # Git cat-file implementation
│ ├── hash_object.go # Git hash-object implementation
│ ├── ls_tree.go # Git ls-tree implementation
│ ├── write_tree.go # Git write-tree implementation
│ ├── commit_tree.go # Git commit-tree implementation
│ └── clone.go # Git clone implementation
├── object/
│ ├── blob.go # Blob object handling
│ ├── tree.go # Tree object handling
│ ├── commit.go # Commit object handling
│ ├── packfile.go # Packfile parsing and delta compression
│ └── checkout.go # Working directory checkout
└── README.md