DEV Community

Cover image for mini-git: a small Git in Rust that real git can read
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

mini-git: a small Git in Rust that real git can read

Git is the tool most developers use the most and understand the least. We memorise commands, and when something goes wrong we copy a fix from a search result. Yet the model underneath is small: a few kinds of objects, each named by the hash of its content, and some files that point at them. Once you see that model, checkout, branch and log stop being magic.

The best way I know to see it is to write it. mini-git is a small Git implementation in Rust that reads and writes real git loose objects: zlib-compressed and SHA-1 addressed. Its binary is called mgit. Repositories created by mgit can be opened by git, and mgit can read repositories created by git, as long as the objects are loose. This post covers what I built and what it teaches about Git.

What I built

The object format. object.rs implements the encoding every Git object uses: a header "<type> <len>\0" followed by the body. The object id is the SHA-1 of those bytes, and the file on disk is the same bytes compressed with zlib (through the flate2 crate). The file also contains:

  • tree serialization with git's sort order, where directories compare as if their name ended with /;
  • commit parsing and serialization;
  • signature and date formatting.

The repository. repo.rs implements what sits around the objects:

  • the .git layout;
  • the loose object store, where an object with id abcdef… lives at objects/ab/cdef…;
  • refs and the symbolic HEAD;
  • revision resolution: full ids, short ids, branch names and HEAD;
  • write-tree, built straight from the working directory;
  • a first-parent log;
  • checkout, which diffs the current tree and the target tree, and refuses to clobber uncommitted changes unless you pass --force.

The commands. init, hash-object [-w] [-t type], cat-file (-p|-t|-s|-e), write-tree, commit-tree <tree> [-p parent]... -m msg, commit -m msg, log, branch, checkout [--force] and rev-parse. The author and committer come from GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL (and the committer equivalents), falling back to $USER.

How to use it

Clone the repository and build it with Cargo:

git clone https://github.com/AbdeldjalilChougui/mini-git
cd mini-git
cargo build --release
alias mgit=target/release/mgit
Enter fullscreen mode Exit fullscreen mode

Then create a repository with mgit and inspect it with real git:

mgit init demo && cd demo
echo hello > a.txt
mgit hash-object -w a.txt        # same id as `git hash-object a.txt`
mgit commit -m "first"
git log --oneline                # real git reads it
mgit log
mgit branch feature && mgit checkout feature
Enter fullscreen mode Exit fullscreen mode

The line to pay attention to is git log --oneline. The commit was written by mgit, and Git itself lists it. Nothing was converted or exported: both tools read the same files under .git.

To look inside, use cat-file. -t prints the type of an object, -s its size, -p its content in readable form, and -e only checks that it exists:

mgit cat-file -t HEAD
mgit cat-file -p HEAD
Enter fullscreen mode Exit fullscreen mode

The commit you get back names a tree. cat-file -p on that tree lists a.txt with the id of its blob, the same id that hash-object printed earlier.

What it teaches

Git is a content-addressed store. A blob is the content of a file. A tree is a list of names, modes and object ids. A commit is a tree id, parent ids, an author, a committer and a message. Each one is stored under the hash of its own bytes. Two files with the same content are the same blob, stored once.

A branch is a file with one id in it. Creating a branch writes a ref. HEAD is usually a symbolic ref that names the current branch. A commit on that branch writes a new commit object and moves the ref. That is the whole mechanism.

Byte-exact details decide whether git accepts you. The object id is a hash, so "almost the same bytes" is a different object. The tree sort order is the classic example: entries are sorted by name, but a directory compares as if it had a trailing /. Get that wrong and mgit write-tree produces a different id than git write-tree for the same folder, and git fsck complains. The header length, the date format in signatures and the newline at the end of a commit message have the same all-or-nothing nature.

Checkout is a diff between two trees. Switching branches does not rewrite the working directory from zero. It compares the current tree with the target tree and touches only what differs. The careful part is refusing to overwrite files that have uncommitted changes, which is why --force exists.

Compatibility is the best test. With a real implementation next to you, you do not have to wonder whether your understanding is right. You ask Git.

That last point is how the project is tested. cargo test runs unit tests plus tests/git_compat.rs, which drives the mgit binary and checks with real git (git fsck --strict, git log, git cat-file, git write-tree) that the objects are byte-for-byte compatible, in both directions. Those tests skip themselves if git is not installed.

Things to know

This is a learning project. The README lists what is not supported:

  • No index. There is no staging area, so there is no add. commit snapshots the whole working tree.
  • No packfiles. Objects packed by git gc cannot be read. mgit reads a repository made by git only while its objects are loose.
  • No merges, remotes or tags.
  • No .gitignore. Only .git itself is skipped.
  • No symlinks or submodules.
  • log follows first parents only.

So do not point it at a repository you care about. Use it on a scratch folder, next to real Git, to compare.

Why I built it

I wanted to stop treating Git as a list of commands and understand the data model the commands operate on. Articles explain that model well, but the understanding stays shallow until you have to produce an object that git fsck --strict accepts. The compatibility requirement was the point of the exercise: it turns every wrong assumption into a failing test instead of a vague feeling.

It also makes a compact reference. The object code and the repository code are two files, and the limits are stated openly, so the repository can be read from start to end by someone who wants to see how the pieces of .git fit together.

Get the code

The source and the compatibility tests are on GitHub: https://github.com/AbdeldjalilChougui/mini-git. It's MIT licensed. Issues and pull requests are welcome.


Originally published at abdeldjalilchougui.biz.

Top comments (0)