Skip to main content

Git in the JupyterLite terminal

· 5 min read
Ian Thomas
Scientific Software Developer at QuantStack
Sandrine Pataut
Scientific Software Developer at QuantStack
Johan Mabille
Technical Director at QuantStack

At QuantStack we are creating a complete ecosystem for running programming languages in a web browser based on JupyterLite and WebAssembly. You can already execute code in Python, C++, R and other languages interactively on desktop or mobile, without having to install software packages locally on your computer.

A complete ecosystem needs all the tools that are important for software development which includes version control. Hence we are pleased to announce git2cpp, a git implementation compiled to WebAssembly that runs entirely within the browser from the JupyterLite terminal. Here it is in action:

note

You can try out the same workflow yourself at https://jupyterlite.github.io/terminal. Note that an alias is set by default so that you can type git rather than the full git2cpp.

What is git2cpp?​

git2cpp is an Open Source C++ project developed by QuantStack that provides a Command-Line Interface to git functionality. Designed primarily for the browser, it is compiled to WebAssembly, packaged on Emscripten-forge, and used in the JupyterLite terminal. Native builds for POSIX-compliant systems such as Linux and macOS are also supported.

Under the hood git2cpp uses libgit2, an Open Source C library that implements git core methods. Libgit2 is an established, reliable and well-maintained library used in many software projects, including in production at GitHub.

tip

To check the functionality supported by git2cpp use git --help and git <subcommand> --help in the usual manner.

Browser limitations​

git2cpp can work with remote repositories such as those hosted on GitHub and GitLab using the usual clone, fetch and push subcommands. However, web browsers protect users with a security mechanism called Cross-Origin Resource Sharing (CORS). A web page may only make requests to another site if that site explicitly allows it by adding special headers to its responses. Most git servers, including GitHub and GitLab, do not add these headers, so a git clone that works fine in a terminal on your computer fails with a CORS error when run in a browser.

The solution is to use a CORS proxy server running outside the browser on a real computer. Rather than sending a request directly to the git server, git2cpp sends it to the proxy, which forwards it to the git server. As this request comes from outside the browser it is not subject to CORS restrictions. The proxy then adds the required CORS headers to the response before passing it back to git2cpp.

Setting up a CORS proxy for git2cpp is described in the git2cpp documentation, but there is a much easier way.

Notebook.link includes its own CORS proxy server for git requests so accessing remote servers using git2cpp works out of the box. This makes it the easiest way to try out git2cpp.

The video below shows a complete git clone, branch and commit workflow. First the Git Add-on is set up with the name and email address to use for git commits. Then a new Project is created with the Git Add-on enabled. Finally, in a terminal in that Project, a git clone from GitHub works straight away using the built-in CORS proxy.

Private repositories​

Private repositories work in Notebook.link too.

The next video shows a workflow that clones a private repository, creates and modifies a new branch, and then pushes the new branch back to the remote repository. Accessing a private repository requires authentication, typically using an access token such as a GitHub or GitLab Personal Access Token. The first time a token is needed, Notebook.link prompts you to enter it, and if the clone succeeds the token is securely stored for future use. So the git push at the end succeeds without having to enter the token again.

note

For step-by-step walkthroughs of these workflows see Git in the terminal in the Notebook.link documentation.

Try it out​

git2cpp is available today in both the JupyterLite terminal and Notebook.link. The easiest way to use it is via Notebook.link, as it handles both the CORS proxy and securely storing access tokens for you.

If you find that git2cpp is missing your favourite subcommands or flags, please open an issue about it in the git2cpp GitHub repository.

Acknowledgements​

git2cpp is developed at QuantStack by Ian Thomas, Sandrine Pataut and Johan Mabille.

We are grateful to the maintainers and contributors of the many other projects that this builds on, especially libgit2, Emscripten-forge and JupyterLite.