Writing Git in Rust

Notes around the implementation of a small part of Git in Rust. Full implementation in this repository.

Note that most of the implementation’s details will be snipped. If you want to see the full implementation and try it out on your machine, check out the repository.

Structuring the Project

The challenge proceeded in steps and each step was about implementing what they call “plumbing commands” of Git. So, to make the job of adding a new command in every step easier, I split the project into three parts:

  • The internal implementations
  • Command Line Interface
  • Wrapper functions to be called by the CLI

Now doing this lets us make the fn main() what is called a single-line main function.

mod cli;
mod clone;
mod commands;
mod objects;
mod packfile;
mod utils;

use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    cli::CLI::run().await
}

The blob, tree, and commit modules are for Git objects‚ we’ll come back to these later. The cli.rs defines the CLI and the commands.rs defines the functions that sort of wrap together everything into convenience functions.

Also note, the #[tokio::main] is because we are using Tokio’s asynchronous runtime. Tokio is the most popular asynchronous runtime for Rust.

Next

Move on to [[git/cli]] to continue reading about the implementation details.