Making Git Clone
This week was spent in learning by building and I made a prototype of Git using Javascript.
Here is the summary of it.
Github Link
Overview
The script provides basic Git-like operations, such as:
Initializing a Git-like repository with a .git directory.
Handling file hashing and object storage in a way similar to Git’s object storage.
Building and committing trees to model directory structures.
Commands Implemented
init: Sets up a .git directory structure with objects and refs folders and creates a HEAD file pointing to the main branch.
cat-file: Reads and decompresses a Git object given its SHA-1 hash, displaying its contents.
hash-object: Hashes a file, creates a compressed version, and stores it in the .git/objects folder.
ls-tree: Lists the contents of a tree object, similar to the git ls-tree command.
write-tree: Writes the current directory structure into a tree object and stores it.
commit-tree: Creates a new commit object, linking it to a tree object and a parent commit, and stores it in the Git object storage.
Functionality Breakdown
1. Git Directory Initialization (init):
Creates .git, .git/objects, and .git/refs directories.
Sets HEAD to reference the main branch.
2. File Hashing and Object Storage (hash-object):
Reads a file, hashes its contents using SHA-1, and compresses it using zlib.
Stores the compressed object in .git/objects using the first two characters of the SHA-1 hash to create a subdirectory.
3. Viewing Object Content (cat-file):
- Decompresses a stored object from .git/objects and prints its content.
4. Tree Management (ls-tree and write-tree):
ls-tree: Lists the files and directories in a tree object.
write-tree: Recursively traverses the current directory, hashing files and building a tree object. Stores the tree object in .git/objects.
5. Commit Creation (commit-tree):
Constructs a commit object containing metadata (author, committer, timestamp) and a reference to the tree object and parent commit.
Compresses and stores the commit object in .git/objects.
That's all for this week folks.
Stay tuned for more.