Language: EN

csharp-gitreader

Analyze Git Repositories in C# with GitReader

GitReader is an open-source library for C# that allows us to efficiently access and analyze Git repositories without the need to depend on the installation of git.exe on the system.

This library is ideal for developers who need to programmatically interact with Git repositories, providing a simple and straightforward API to access repository information.

Features of GitReader,

  • No external dependencies: Does not require git.exe to operate.
  • Direct access to Git repositories: Provides an API to read and analyze the structure and content of repositories.
  • Cross-platform: Works on Windows, macOS, and Linux.
  • Efficient and fast: Designed for quick and efficient access to repository data.

GitReader is Open Source and all its code and documentation are available in the project repository on GitHub - kekyo/GitReader.

Installing GitReader

To start using GitReader in your .NET project, you first need to install the library via NuGet. You can do this using the NuGet Package Manager in Visual Studio or by using the NuGet console.

Install-Package GitReader

How to Use GitReader

Once you have installed GitReader, you can start using it to access and analyze Git repositories. Below are several examples illustrating how to interact with Git repositories using GitReader.

These are just two simple examples of two operations. Many (many) more functions can be performed with the repo. Check the project documentation, which includes many examples.

Opening a Repository

This example shows how to open a Git repository and obtain basic information about it.

using GitReader;
using GitReader.Structures;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Path to the Git repository
        string repoPath = "path/to/repository/.git";
        
        // Open the repository
        using var repository = await Repository.Factory.OpenStructureAsync(repoPath);
        
		if (repository.Head is Branch head)
		{
		    Console.WriteLine($"Name: {head.Name}");
		
		    // Get the commit that this HEAD points to:
		    Commit commit = await head.GetHeadCommitAsync();
		
		    Console.WriteLine($"Hash: {commit.Hash}");
		    Console.WriteLine($"Author: {commit.Author}");
		    Console.WriteLine($"Committer: {commit.Committer}");
		    Console.WriteLine($"Subject: {commit.Subject}");
		    Console.WriteLine($"Body: {commit.Body}");
		}
    }
}

Getting a Specific Commit

In this example, a specific commit is retrieved.

using GitReader;
using GitReader.Structures;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Path to the Git repository
        string repoPath = "path/to/repository/.git";
        
        // Open the repository
        using var repository = await Repository.Factory.OpenAsync(repoPath);
        
		if (await repository.GetCommitAsync(
		    "1205dc34ce48bda28fc543daaf9525a9bb6e6d10") is Commit commit)
		{
		    Console.WriteLine($"Hash: {commit.Hash}");
		    Console.WriteLine($"Author: {commit.Author}");
		    Console.WriteLine($"Committer: {commit.Committer}");
		    Console.WriteLine($"Subject: {commit.Subject}");
		    Console.WriteLine($"Body: {commit.Body}");
		}
    }
}