MyGit is a simple implementation of a version control system inspired by Git. This project aims to provide a basic understanding of how Git works under the hood by implementing core functionalities such as initializing a repository, managing objects, committing changes, and cloning repositories.
- Initialize a Repository: Create a new Git repository with the necessary directory structure.
- Manage Git Objects: Read, write, and hash Git objects (blobs, trees, commits).
- Commit Changes: Create commit objects with associated metadata.
- Clone Repositories: Clone a remote Git repository and check out files.
- Checkout Commits: Restore files from a specific commit.
To install MyGit, clone the repository and build the application:
git clone <repository-url>
cd mygit
go build -o mygit ./cmd/mygitAfter building the application, you can use it from the command line:
./mygit <command> [<args>...]init: Initializes a new Git repository.cat-file -p <hash>: Displays the content of a Git object.hash-object -w <file>: Computes the hash of a file and optionally writes it as a Git object.ls-tree --name-only <tree_hash>: Lists the files in a tree object.write-tree: Writes the current directory structure as a tree object.commit-tree <tree_sha> -p <parent_sha> -m <message>: Creates a new commit object.clone <repo-url> <dir>: Clones a remote repository into the specified directory.
codecrafters-git-go/
├── cmd/
│ └── mygit/
│ └── main.go # Main entry point and CLI handling
├── internal/
│ ├── objects/ # Git object operations
│ │ └── objects.go # Read/write objects, tree operations, commits
│ ├── pack/ # Pack file handling
│ │ └── pack.go # Pack file parsing and unpacking
│ ├── protocol/ # Git Smart HTTP protocol
│ │ └── git_http.go # HTTP communication, refs discovery
│ └── clone/ # Clone orchestration
│ └── clone.go # High-level clone workflow
└── go.mod # Go module definition
- Purpose: Handle all Git object types (blobs, trees, commits, tags)
- Key Functions:
ReadObject()/WriteObject()- Core object I/OHashObject()- Object hashing and storageCatFile()- Display object contentsLsTree()/ParseTree()- Tree operationsWriteTree()- Create tree objects from filesystemCommitTree()- Create commit objectsCheckoutTree()- Extract tree to working directory
- Purpose: Handle Git's Smart HTTP transfer protocol
- Key Functions:
DiscoverRefs()- Query repository referencesNegotiatePackfile()- Request and receive pack dataFindDefaultRef()- Determine default branch
- Types:
GitRef- Represents Git references
- Purpose: Parse and unpack Git pack files
- Key Functions:
UnpackPackfile()- Main pack file processingparsePackObject()- Individual object parsing- Delta object handling (basic framework)
- Types:
PackObject- Represents pack file objects
- Purpose: Coordinate the complete clone workflow
- Key Functions:
Clone()- Main clone functioninitializeGitRepo()- Set up repository structureupdateRefs()- Configure branches and refscheckoutWorkingTree()- Extract files to working directory
- Purpose: CLI interface and command routing
- Features:
- Command-line argument parsing
- Error handling and user feedback
- Integration of all internal packages
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.