Skip to main content

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

At its core, Zyra follows the same architecture as Git.

  1. Everything is stored in objects.
  2. 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
    • 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.
  3. An index file is used for staging. The entire metadata is stored in this binary file
    • Reference: /stage

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:

  1. Zyra computes its size: len("Hi there") = 8.
  2. Converts the contents into a binary string
  3. Format: b'{object_type}{size}\x00{content}'. The object type is blob in this case
  4. Compresses with zlib.

Example: b'blob8 Hi there'

  1. Also, compute the SHA1 hash of b'{object_type}{size}\x00{content}'.

  2. 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

  1. Writing the staging area logic.
  2. Understanding Gitโ€™s branching model (solved by using .git/branches alongside /refs/heads).
  3. Managing file metadata, which is verbose (much of it unused).
  4. 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

zyra-tool-1.0.8.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zyra_tool-1.0.8-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

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

Hashes for zyra-tool-1.0.8.tar.gz
Algorithm Hash digest
SHA256 663abb63a547c2514baab926bdad582d130e862b2b966779cfb8a7d58fced900
MD5 08d433bccc86d23d559662923666d49c
BLAKE2b-256 77f2c013f3c765f85f23fcc3755517692d299d6f1a4d183bc20cd07bb65be9b4

See more details on using hashes here.

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

Hashes for zyra_tool-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d5347b9097e07736238ffe1ee77256e373ba9dacca6d220dd9659e3bce8ee9c2
MD5 30400b3661d398e171f8b6bd6e08c44e
BLAKE2b-256 9332a4cc45812f678f03a505138c61489a2c7cbf6fe8a2d921e3015c1a9b43b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page