Sitemap

Connecting to a git repository using libgit2sharp

3 min readSep 16, 2021

--

Press enter or click to view image in full size

It may seem trivial but maybe you need to connect to remote git repository using an SDK. Maybe you have the need to consume data from a git repository in some way. If you think about it for minute a git repository is as much a source of data as a database (SQL or NoSQL). In microservice language the concept of designing small services for an application you have the notion of “sovereignty” of data. It basically means each service in your application has its own set of data. Each service in your application could use different ways of connecting to and storing data. Using the aforementioned knowledge its fair to say a git repository is a data storage mechanism. git repositories can contain a whole lot of different source files — including Markdown. Markdown has become ubiquitous with GitHub — most repositories contain README.md files. Markdown files are far more useful than just being a README. They are widely used in Azure DevOps Wikis. In fact most of the major git source control providers integrate Markdown into the source code browsers. If you create a Markdown document GitHub renders it to HTML.

A collection of Markdown documents is a source of data. A collection of Markdown documents hosted in a remote git repository is a form of cloud-hosted data.

What if you wanted to say use a git repository to store Markdown documents? What if you wanted to publish or render the Markdown documents to a web-page? I enjoy writing Markdown documents using Visual Studio Code. Primarily for its extensive range of plugins. It also suits my development workflow. During the day I work with Visual Studio as well as Visual Studio Code. I enjoy Visual Studio Code’s editor experience. More than I feel I would enjoy a browser-based WYSIWYG editors. Most blogging platforms these days provide basic editors, in the browser. Like Medium.

Imagine designing an application allowing you to write Markdown documents, commit it to git, parsing it to HTML and finally publishing it to a website. One of the microservices could have the sole responsibility of connecting to a git repository to process the Markdown files. It would need to connect to a git repository to retrieve the Markdown files. It could do some extra domain specific processing on the files. The processed files can then be consumed by another service in another way.

Get Fredrik Erasmus’s stories in your inbox

Join Medium for free to get updates from this writer.

Connecting to a git repository using code is fairly easy. One library in particular, libgit2sharp makes it super easy to connect to a remote git repository.

I created an xunit test project to get started. Its always a good way to start exploring an SDK using a test project. It allows you to mess around — fail and break things until you get to usable solution. It also helps you shape your code into models. Often coming up with models upfront becomes brittle too soon. Using a test to understand how an SDK works is a great way to design an application. Unit tests are also a great way to see how things work in another project. libgit2sharp has tests. It becomes easy to understand how things work by looking at the tests.

Press enter or click to view image in full size

In the code above you clone the repository before connecting to it. In the specific example I connected the Azure Devops Wiki for the project. In the assertion I check a list of branches stored in a local variable expectedBranches, an array of strings, with a list of branches from the repo. I then checked the file location I specified in the Repository constructor to see the repository cloned. I could see the files from the cloned repository!

Awesome!

--

--

Fredrik Erasmus
Fredrik Erasmus

Written by Fredrik Erasmus

Software Imagineer. Eager to learn new things. Focused on solving problems. Mainly focused on ASP.NET Core, C#, Azure.