Can I `git clone` into an in-memory file system?

ยท

2 min read

Can I `git clone` into an in-memory file system?

Recently, I wanted to find ways to clone a Git repository into an in-memory file system for a toy project, as it seems I need a lot of git tools in my life.

Well the answer to the question in the title is simply: YES, you can clone a Git repository to an in-memory file system. Let us see how.

In this post I will present two ways we can accomplish this; using a temporary filesystem using facilities provided by the OS (Linux in this case) and using a Git library which has capability to clone and work with repositories in-memory (using Go). Maybe we can even combine the approaches?

Clone to a RAM storage (on Linux)

The first approach involves using the OS and relevant programs to create a temporary filesystem that runs in-memory and does not persist across reboot. Linux supports creating file system which keeps files in virtual memory. In order to accomplish our particular task, we create a tmpfs filesystem, mount it to a directory then navigate to that directory and perform the clone operation there. The basic idea would work as follows:

$ sudo mkdir /mnt/tempgit
$ sudo mount -t tmpfs -o size=50m tmpfs /mnt/tempgit
$ cd /mnt/tempgit
$ git clone https://github.com/zikani03/multiclone.git
$ ls
multiclone

Just take note that "tmpfs may use SWAP space. If your system runs out of physical RAM, files in your tmpfs partitions may be written to disk based SWAP partitions and will have to be read from disk when the file is next accessed" (source)

Clone using go-git library

This approach requires a fairly recent installation of Go in-order to use the go-git library. The go-git library is

... a highly extensible git implementation library written in pure Go.

The library has a Clone function which we can use with a storage type. Fortunately for us, one of the storage types that comes out of the box in the library is the memory storage. We can use that to perform an in-memory clone easily:

package main

import (
    git "github.com/go-git/go-git/v5"
    "github.com/go-git/go-git/v5/storage/memory"
)

func main() {
    _, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
        URL: "https://github.com/zikani03/multiclone.git",
    })
    if err != nil {
        panic(err)
    }
    // ...
}

Obviously, this approach will be easier if you are comfortable working with Go.

Why would you wanna do this?

๐Ÿš…. Using an in-memory storage should improve performance of git operations as memory is generally faster than disk. As mentioned earlier, I am working on something that might need this behavior. If you are curious about the tool, I spoke a bit about it in response to another Tweet:

Well, that's all folks.

ย