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? it is a random gpt suggestion which looked snappy to me.


๐Ÿš€ Installation ๐Ÿš€

If you are interested to see how this works, you can start by installing zyra using one of the following ways.

1. Using pip

Link to the package

pip install zyra-tool

After you are done using zyra, you can uninstall it using:

pip uninstall zyra-tool

So that your glorious storage is not eaten up.

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

3. Clone from github

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

Here is the source code zyratool in github.

Now you can use zyra like a command in your terminal

โš ๏ธ 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 start running commands. 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 files are changing inside. (if you are interested)

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

Just like git, zyra also stores data by compressing and hashing objects. There are four objects mainly - blob, tree, commit and tag.

Example: Let's see how a file one.txt with contents "Hi there" is stored:

At a high level, zyra compresses file contents and stores them as objects inside a folder in the repo (named as .git, though any name could be used). The .git name is a convention many editors and tools recognize, which is why zyra uses it.

one.txt with contents Hi there

  1. Zyra computes the content's 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 because we are trying to store contents of a file
  4. Compresses with zlib.

Example: b'blob8 Hi there'

  1. Also, computes 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.

A brief gpt explanation on what's the difference between these objects:

  • Blob โ€” stores raw file contents (data), addressed by its SHA.
  • Tree โ€” records directory entries (names, modes) and points to blobs/subtrees.
  • Commit โ€” snapshot pointing to a tree with metadata (parent(s), author, message).
  • Tag โ€” named reference (lightweight or annotated) pointing to another object with optional message/metadata.

Relationships between objects

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

Issues Reporting and Contributions

While playing around with zyra you might be bombarded with errors if you run commands with unexpected arguments etc. While I've tried my best to cover edge cases and provide error messages, if there are any cases where I've missed, kindly open an issue on github.


Thank you for reading

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.12.tar.gz (20.8 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.12-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file zyra_tool-1.0.12.tar.gz.

File metadata

  • Download URL: zyra_tool-1.0.12.tar.gz
  • Upload date:
  • Size: 20.8 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.12.tar.gz
Algorithm Hash digest
SHA256 0d179dac95e27237221bd75751447fca78187644b90120d956e001178047fd6b
MD5 74752117a9e257f2d4e104b77cde43d8
BLAKE2b-256 de217bca6859fde158bda124a5131abefcacbb4b07cd1770f6f7e9b9abc51e3c

See more details on using hashes here.

File details

Details for the file zyra_tool-1.0.12-py3-none-any.whl.

File metadata

  • Download URL: zyra_tool-1.0.12-py3-none-any.whl
  • Upload date:
  • Size: 24.5 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 2b8f0feb9b92e3cc0e2322cb8e7309368d213da4a1ad0808a7ad24e43824fa91
MD5 cb38722d98ec2e00c0e6fc4fe1844aab
BLAKE2b-256 9b294a725c71c187a8fe1ce37ef322bdb5fd8846550ef814316f9fc83256a878

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