Building Git in Elixir — Part 2 (Store Trees & Initial Commit)

Meraj Molla
ITNEXT
Published in
6 min readMar 10, 2021

--

In the previous article of this series, I focused on initializing repository and storing blobs for file content. In this article, I will focus on storing trees and first commit.

A Bit of Theory

As explained in previous article, a tree stores certain information about the stored blobs. It is one more level of indirection above blobs for easier lookup. If we examine Git’s objects database using git cat-file command we can see —

Here, commit 88e38705fdbd3608cddbe904b67c731f3234c45b is a tree and as we can see it stores a list of entries. Each entry contains the file’s mode (100644), the word blob, the object ID for entry and file’s name.

Likewise, a git commit is stored as below —

Here, commit 6554ab08c8844fe9c85346a7f2bf021614e2db93 stores information about pointer to a tree, author and committer information, timestamp, and the commit message.

To populate the author name and email, there are multiple ways but typically a ~/.gitconfig is used which looks like —

But in this article, we will pass these information via environment variables which I will explain later.

Focus of This Article

In this article, focus is to replicate the above behaviour and implement —

  • git commit — such that it can store trees and save initial commit along with commit message. Also, HEAD file inside .git will be updated to store commit ID for initial commit.

Elixir Code Walkthrough

The source code for this article is available from — https://github.com/imeraj/elixir_git

Particularly, code for this article is available at this commit —

It contains a README file on how to build the executable and use git init and updated commit commands.

Some Code Refactoring

--

--