zyra: A version control system that is built from scratch in python
Project description
๐ฑ zyra ๐ฑ
Zyra is a version control system built from scratch in Python.
Why the name zyra? The name zyra is snappy and inspired by plants. In League of Legends, Zyra is a plant-themed champion. Since Git also uses tree structures (objects, roots, branches), this felt like an acceptable analogy for me.
Table of Contents
- โ๏ธ High level basics of how a version control system like git operates its storage using objects
- Where are the files stored?
- Object Relationships
- ๐ Installation ๐
- ๐ Quick start ๐
- โก Complex example
- ๐ ๏ธ Command Reference
- Challenges Faced
- Why Git?
High level basics of how a version control system like git operates its storage using objects
At its core, Zyra follows the same architecture as Git.
- Everything is stored in objects.
- There are four types of objects:
- Blob โ Stores file contents.
- Tree โ Represents the entire working directory (contains items - leaves (blob or tree)).
- Reference:
common/tree/tree_obj.py
- Reference:
- Commit โ Represents a snapshot (tree's sha, parentโs sha, commit message, etc.).
- Tag โ Human-readable tags for objects. These are basically other names given to commits so that its easier to reference.
- An index file is used for staging. The entire metadata is stored in this binary file
- Reference:
/stage
- Reference:
Where are the files stored?
Zyra stores data by compressing and hashing objects like git.
Example: Let's see how a file one.txt with contents "Hi there" is stored:
- Zyra computes its size:
len("Hi there") = 8. - Converts the contents into a binary string
- Format: b'{object_type}{size}\x00{content}'. The object type is blob in this case
- Compresses with zlib.
Example: b'blob8 Hi there'
-
Also, compute the SHA1 hash of b'{object_type}{size}\x00{content}'.
-
Store the zlib compressed binary file in:
.git/objects/<sha[0:2]>/<sha[2:]>
More details: common/objects.py
Very similarly tree, commit and tag objects are also stored.
Object Relationships
Files โ Blobs โ Tree
file1.txt file2.txt
โ โ
โผ โผ
(blob) (blob)
\ /
โผ โผ
โโโโโโโโโโโโโ
โ Tree โ
โโโโโโโโโโโโโ
Commit Structure
โโโโโโโโโโโโโ
โ Commit โโโโโบ This object is stored in the path of its own sha
โ-----------โ
โ tree: sha โโโโโบ (Tree)
โ parent: โโโโโบ (Prev Commit)
โ message โ
โโโโโโโโโโโโโ
Tag Reference
(Tag) โโโโบ (Blob)
โ
โผ
(Commit)
Overall Flow:
File โ Blob โ Tree โ Commit
๐ Installation ๐
If you are interested to see how this works, you need to install zyra using one of the following approaches
Using Docker
Make sure you have docker installed already.
# Clone the repo
git clone https://github.com/karthik11135/zyra.git
# Change your directory
cd zyra
# Build the dockerfile
docker build --no-cache -t zyra .
# Run interactively with volume mounted to your current path
docker run -it -v $(pwd):/app zyra
Or with Docker Compose:
git clone https://github.com/karthik11135/zyra.git
cd zyra
docker compose up run zyracli
Manual way
If you donโt have Docker, you can create an executable directly:
git clone https://github.com/karthik11135/zyra.git
cd zyra
pip install -r requirements.txt
chmod +x zyra.py
alias zyra=./zyra.py
Now you can use zyra like a command
โ ๏ธ Ensure youโre in the same directory to run commands. Usage:
zyra <subcommand>
๐ Quick start ๐
Once you have installed and have an interactive shell, you can test it out. The folders you create will be created in your current directory because of the volume mount (if you are using docker).
mkdir example
cd example
touch ex1.txt ex2.txt
zyra init
zyra add ex1.txt ex2.txt
zyra status
zyra commit -m "first commit"
Congratulations ๐ You just created your first commit with my zyra.
โก Complex example
mkdir example
cd example
touch ex1.txt ex2.txt
(add some txt)
zyra init
zyra add ex1.txt
zyra commit -m "master commit"
zyra all-commits
zyra create-branch dev
zyra switch dev
zyra add ex2.txt
zyra commit -m "dev commit"
zyra all-commits
zyra b-commits (branch specific commits)
(Change the text of ex2.txt)
zyra add ex2.txt
zyra commit -m "dev second commit"
zyra b-commits
zyra switch master
๐ ๏ธ Command Reference
(See /cmds/commands.py for implementation details)
While running these commands unhide your .git folder to see how things are changing inside it.
| Command | Description | Example |
|---|---|---|
| init | Initializes an empty repository and creates a master branch. | zyra init |
| cat-file | Displays content of an object (blob, commit, tag, tree). | zyra cat-file <sha> |
| hash-object | Computes the hash of a file and optionally writes it. | zyra hash-object -w <file> |
| log | Displays commit history from a given commit. | zyra log <commit_sha> |
| checkout | Checks out a commit/tree into a directory. | zyra checkout <commit_sha> <dir> |
| show-ref | Lists references (branches, tags, etc.). | zyra show-ref |
| tag | Creates a tag or lists existing tags. | zyra tag -a <tag_name> <sha> |
| rev-parse | Resolves a reference or object to its SHA. | zyra rev-parse <ref> |
| status | Shows current repo status (branch, staged changes, etc.). | zyra status |
| rm | Removes files from staging/working directory. | zyra rm <file> |
| add | Adds files to staging. | zyra add <file> |
| commit | Creates a commit with staged changes. | zyra commit -m "msg" |
| all-commits | Lists all commit objects in the repo. | zyra all-commits |
| branch | Shows all branches and highlights the current one. | zyra branch |
| switch | Switches to another branch. | zyra switch <branch> |
| create-branch | Creates a branch and updates HEAD. | zyra create-branch <branch> |
| b-commits | Displays all commits in the current branch. | zyra b-commits |
Challenges Faced
- Writing the staging area logic.
- Understanding Gitโs branching model (solved by using
.git/branchesalongside/refs/heads). - Managing file metadata, which is verbose (much of it unused).
- Converting the index file โ tree SHA was tricky.
Why Git?
Git was referenced often because Zyra follows the same architecture, so understanding Git internals was crucial to implementing Zyra.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zyra-tool-1.0.8.tar.gz.
File metadata
- Download URL: zyra-tool-1.0.8.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
663abb63a547c2514baab926bdad582d130e862b2b966779cfb8a7d58fced900
|
|
| MD5 |
08d433bccc86d23d559662923666d49c
|
|
| BLAKE2b-256 |
77f2c013f3c765f85f23fcc3755517692d299d6f1a4d183bc20cd07bb65be9b4
|
File details
Details for the file zyra_tool-1.0.8-py3-none-any.whl.
File metadata
- Download URL: zyra_tool-1.0.8-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5347b9097e07736238ffe1ee77256e373ba9dacca6d220dd9659e3bce8ee9c2
|
|
| MD5 |
30400b3661d398e171f8b6bd6e08c44e
|
|
| BLAKE2b-256 |
9332a4cc45812f678f03a505138c61489a2c7cbf6fe8a2d921e3015c1a9b43b6
|