Skip to content

taham8875/gitx

Repository files navigation

Git Implementation in Go

A minimal implementation of Git core functionality built as part of the CodeCrafters Git challenge.

Features

  • git init - Initialize a new Git repository
  • git cat-file - Display object contents
  • git hash-object - Compute object hash and store
  • git ls-tree - List tree contents
  • git write-tree - Create tree from working directory
  • git commit-tree - Create commit objects
  • git clone - Clone remote repositories via Smart HTTP protocol

Architecture

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]
Loading

Implementation Details

Object Storage

  • Objects stored in .git/objects/ with SHA-1 based directory structure
  • Zlib compression for efficient storage
  • Standard Git object format: type size\0content

Clone Implementation

  • 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

Network Protocol

  1. Discovery: GET /info/refs?service=git-upload-pack
  2. Negotiation: POST /git-upload-pack with want/have lines
  3. Transfer: Receive packfile data
  4. Unpacking: Parse and store objects
  5. Checkout: Create working directory files

Usage

# 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

Project Structure

├── 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

About

A minimal Git implementation in Go that supports some plumbing commands like `cat-file`, `hash-object`, `commit-tree`, and two porcelain commands like `init` and `clone`.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors