Search
Close this search box.

Git Blog

Releasing the Power of Git

GitKraken and NodeGit logos

A Look at NodeGit and libgit2

The Jet Engine Powering GitKraken

One common question we hear from folks trying out the GitKraken Git GUI for the first time is “how do those Undo and Redo buttons work?” If you’re used to only using the CLI, or a GUI that simply runs Git CLI commands in the background, this might seem like some kind of weird voodoo. 

The real tech behind this, and all of GitKraken’s awesomeness, is not magic, but open source technology. The same open source technology powering many other Git projects. It’s called NodeGit and we’re super excited to tell you all about it. 

Make your Git workflow faster and safer with GitKraken’s undo and redo buttons powered by NodeGit!

Git without the Command Line

Deep in the back of the official git-scm.com manual, you will find a section called “Plumbing and Porcelain”. This chapter breaks down how the Git software functions internally (the ‘plumbing’) and how people and code interact with it (the ‘porcelain’). Often, when developing functionality for Git, the software will execute child processes that run the Git ‘porcelain’ API and parse the results of the standard output stream. 

This can be incredibly inefficient. When executing the CLI command, Git needs to interact with the underlying repository database, and the software above it has to reinterpret the retrieved results through text parsing. 

Fortunately, we can accomplish Git-based version control without relying directly on Git’s software to make it happen. Git provides both a set of protocols and standards for how to manipulate the file system and track changes over time and a pre-packaged set of CLI commands that use those protocols. 

By protocol, for those who might be new to computer science, we mean something like Simple Mail Transfer Protocol  (SMTP). The protocol itself is not email, but how email is sent between computers. So no matter if you prefer Outlook, iCloud, Gmail, or even AOL, you can send and receive email because all tools use this same standard. 

So how exactly is GitKraken able to leverage Git without the CLI? 

libgit2 – an Open Source Library for Git

This is where libgit2 comes in. libgit2 is a popular open source library for interacting with Git, but as a fresh implementation with no requirement on Git plumbing or porcelain commands. The project describes itself as “A portable, pure C implementation of the Git core methods provided as a re-entrant linkable library with a solid API, allowing you to write native speed custom Git applications in any language that supports C bindings.” 

It is an awesome project but most developers we know don’t particularly care for working in C.

If you have never worked with the C language directly, you are not alone. It is a really efficient and powerful language. But, like C++ that came afterward, it is “very expert friendly” but not very user friendly. This is why so many developers prefer to work in languages like Python, PHP, and JavaScript, including Node. 

What about Node? 

At GitKraken, we love Node! Our legendary cross-platform Git GUI, available for free download, is delivered across platforms as an Electron application. To use libgit2 efficiently in Node, we need a way to address the same APIs without needing to rewrite and maintain the entire library in Node. This is where the hero of our story comes into the picture.

NodeGit For The Win

NodeGit is an open source project with the mission to provide the Node bindings to the libgit2 project. In a nutshell, NodeGit manages a much smaller set of template files that parse the documentation of libgit2 to produce code that Node can access. That is a pretty dense explanation, so let’s take a step back.

If you look at the codebase for libgit2, there are over 100,000 lines of code. Let’s look at why this is so massive. 

NodeGit code parsing libgit2 docs

Each Git command actually requires sets of little steps. For example, git add takes 3 basic steps:

  1. compute the SHA-1 based on the file content.
  2. Store the contents of the file in the repository database, located at .git /objects
  3. Register the contents of the file(s) to .git /index 

As you can imagine, doing very complex tasks like Git rebase or Git cherry pick can have a lot more steps, especially when you consider the dozens and dozens of options for each command. 

Trying to manually rewrite all of this code from C to Node manually, and then attempt to update it by hand with every update, would be a huge undertaking. So, instead, NodeGit maintains a much smaller codebase, around 6,000 lines, that can look up and parse the documentation of libgit2 to produce code that we can run in Node instead of C. The new code makes the exact same API calls in the correct order to accomplish tasks like Git pull or Git commit when you drag and drop them in the GitKraken interface. 

Why this is such a big deal for GitKraken is that it allows us to do things like inserting our own functions in the middle of any of these calls. This is what lets us give users an Undo button or any of the other cool unique features of the GitKraken Git GUI

Undo and Redo buttons in action in GitKRaken

The Future Is Kraken

We are very proud to be the maintainers of this project that is being leveraged by so many software products around the world. NodeGit is letting us do some pretty awesome stuff in GitKraken, and gives us the ability to do even cooler stuff moving ahead. We are proud to stand behind the open source community as well. Ultimately, we hope to help Git improve and help every single Git user on the planet manage their work in a more transparent, reliable, and productive way. 

If you haven’t already checked out what the GitKraken Git GUI has to offer, now is your chance.

“Thank you @GitKraken for the Undo feature. Accidentally discarded all of my changes but now they are back again 😁 ” – @offset337

GitKraken will protect you from yourself.

Like this post? Share it!

Read More Articles

Make Git Easier, Safer &
More Powerful

with GitKraken
Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.