DEV Community

Gareth M.
Gareth M.

Posted on • Updated on

Building a version control system

Intro

As developers, we use version control systems everyday. Most are probably with Git or Subversion (SVN). Since it's easy to use these tools without thinking about how they work, I decided to try and make my own version control system.

How version control works

A version control system keeps track of the changes (versions) of your code. Whenever you make a commit, the current version of the file(s) are compared to the previous version, and a new "commit" is made. Each control system has various ways of doing this. For example, Git uses a snapshot system, where a completely new copy of every modified file is stored in a commit object [1]. Other systems like SVN (and my project) record the differences between files over time.

About zren

When a zren repository is created, a base directory is made with a copy of every file at that moment. When the first commit is made, it compares the current version of each file with the base version. For every commit after that, the "image" of the current file is first built off of applying each commit to the base file chronologically. Then, the current file is compared to the reconstruction of the previous version.

Changefiles

To generate the changefiles, the most recent version of the file is compared with the current version being committed. Every character that is different is stored in a linked list. Places that are the same are given a placeholder, but will not be included in the file itself. When it comes time to write the actual changefile, the linked list is iterated through, and only the different characters and the number of spaces in between each change is recorded. Hex codes, 0x16 for characters and 0x15 for space counts are used so the program knows how to read it back and reconstruct the linked list.

Other features

Since this is just a project I started a few weeks ago, it obviously won't have all the features of a system like Git or SVN. Most importantly, this program currently only keeps track of files locally, since there is no server component for a remote repository. Some of the basic features seen in Git, such as logging and an ignore-file, are present.

Branching

New branches are created with the rollback command, along with its own directory for changefiles and a separate log file. Branch names are autogenerated with the prefix sub- plus the first 8 characters of the source commit id. Users can switch between branches using the checkout command.

Project repo: https://github.com/gbafana25/zren
Dev. streams: https://youtube.com/@gbafana25

[1] https://www.git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F

Top comments (0)