GitDotNet 0.1.33-alpha

This is a prerelease version of GitDotNet.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package GitDotNet --version 0.1.33-alpha
                    
NuGet\Install-Package GitDotNet -Version 0.1.33-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GitDotNet" Version="0.1.33-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GitDotNet" Version="0.1.33-alpha" />
                    
Directory.Packages.props
<PackageReference Include="GitDotNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GitDotNet --version 0.1.33-alpha
                    
#r "nuget: GitDotNet, 0.1.33-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=GitDotNet&version=0.1.33-alpha&prerelease
                    
Install GitDotNet as a Cake Addin
#tool nuget:?package=GitDotNet&version=0.1.33-alpha&prerelease
                    
Install GitDotNet as a Cake Tool

GitDotNet is a .NET library designed to read Git repositories in a fully native .NET environment. It is optimized for minimal memory footprint and efficient data retrieval from repositories.

It also supports writing, but it doesn't write itself: it uses git fast-import to write objects and refs to the repository. This guarantees best writing performance, safe git data writing, and avoid having to reinvent the wheel (object compression...).

Features

  • Supports .NET 8 and 9. Note that .NET 9 is highly recommended because it uses zlib-ng which is much faster.
  • Read and parse Git objects (commits, trees, blobs, tags, index...) natively in .NET.
  • Write using the git fast-import feature for high efficiency and reliability.
  • Use of memory-mapped files for high-performance .git repository data reading.
  • Asynchronous methods for efficient data retrieval.
  • Minimal memory footprint with lazy loading of data and usage of data streaming.
  • Comparison of git commits and trees, along with renaming detection and git patch creation.

<details> <summary>Detailed status...</summary> As per high-level git features, the following is the current status of the project:

  • clone: GitConnection.Create(path, isBare)
  • fetch: connection.Branches["main"].Fetch()
  • blame
  • push
  • reset
  • status
  • commit/trees diff (including renaming detection): connection.CompareAsync("HEAD~10", "HEAD")
  • merge
    • blobs
    • trees
    • commits
  • rebase
  • commit await connection.CommitAsync("main", c => c.AddOrUpdate("test.txt", Encoding.UTF8.GetBytes("foo")), connection.CreateCommit(...))
  • worktree checkout
  • worktree stream
  • read history: connection.GetLogAsync("HEAD~1", LogOptions.Default with { ... }), await (foreach commint in connection.Branches["fix/bug"])
  • .NET native reading of objects: connection.GetAsync<BlobEntry>("1aad9b571c0b84031191ab76e06fae4ba1f981bc")
  • .NET native reading of .git/index: connection.Index.GetEntriesAsync()
  • writing of objects (uses fast-import): connection.CommitAsync("main", c => c.AddOrUpdate("test.txt", Encoding.UTF8.GetBytes("foo")), connection.CreateCommit(...))
  • writing of .git/index
  • reading of git configuration: connection.Config.GetProperty("user", "email")
  • writing of git configuration

Note that the main purpose of DotNetGit is to provide high speed reading. Writing can be done through commands. </details>

Installation

To install GitDotNet, add the GitDotNet or GitDotNet.Microsoft.Extensions.DependencyInjection NuGet package to your project.

Usage

  1. Add GitDotNet dependencies to your IServiceCollection using the AddGitDotNet extension method. This will inject ConnectionProvider and other required services.
  2. Use the Objects.GetAsync method to retrieve Git objects by their hash.
  3. Use the provided methods to parse and display the content of commits, trees, blobs, and tags.
using Microsoft.Extensions.DependencyInjection;
using GitDotNet;

var services = new ServiceCollection()
    .AddMemoryCache(o => o.SizeLimit = 10_000_000) // Each entry size is always 1
    .AddGitDotNet();
var provider = services.BuildServiceProvider().GetRequiredService<GitConnectionProvider>();

using var connection = await provider("path/to/repo");
var commit = await connection.Branches["main"].Tip;
Console.WriteLine($"Commit Message: {commit.Message}");

var tree = await commit.GetTreeAsync();

Benchmarks

GitDotNet vs. LibGit2Sharp: reading random blobs of size from 1kB to 3kB

Method Runtime Mean Error StdDev Ratio RatioSD Gen0 Gen1 Gen2 Allocated Alloc Ratio
GitDotNet .NET 9.0 266.5 ns 5.21 ns 6.95 ns 1.00 0.04 0.0243 - - 464 B 1.00
GitDotNet .NET 8.0 322.1 ns 4.07 ns 3.61 ns 1.21 0.03 0.0243 - - 464 B 1.00
LibGit2Sharp .NET 9.0 23,787.7 ns 428.43 ns 379.79 ns 1.00 0.02 0.1221 0.0305 - 2384 B 1.00

GitDotNet vs. native git 2.47.1.windows.1: archiving git repository

Method Mean Error StdDev
GitDotNet 6.200 s 2.676 s 0.1467 s
git archive -o result.zip HEAD 7.357 s 6.893 s 0.3778 s

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on GitDotNet:

Package Downloads
GitDotNet.Microsoft.Extensions.DependencyInjection

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.35-alpha 116 4/2/2025
0.1.34-alpha 110 4/2/2025
0.1.33-alpha 113 4/1/2025
0.1.32-alpha 107 4/1/2025
0.1.31-alpha 111 4/1/2025
0.1.30-alpha 129 3/31/2025
0.1.29-alpha 119 3/31/2025
0.1.28-alpha 116 3/31/2025
0.1.26-alpha 119 3/31/2025
0.1.24-alpha 102 3/30/2025
0.1.17-alpha 97 3/28/2025
0.1.6-alpha 444 3/25/2025