Pmad.Git.HttpServer
0.1.112
dotnet add package Pmad.Git.HttpServer --version 0.1.112
NuGet\Install-Package Pmad.Git.HttpServer -Version 0.1.112
<PackageReference Include="Pmad.Git.HttpServer" Version="0.1.112" />
<PackageVersion Include="Pmad.Git.HttpServer" Version="0.1.112" />
<PackageReference Include="Pmad.Git.HttpServer" />
paket add Pmad.Git.HttpServer --version 0.1.112
#r "nuget: Pmad.Git.HttpServer, 0.1.112"
#:package Pmad.Git.HttpServer@0.1.112
#addin nuget:?package=Pmad.Git.HttpServer&version=0.1.112
#tool nuget:?package=Pmad.Git.HttpServer&version=0.1.112
Pmad.Git.HttpServer
Pmad.Git.HttpServer is an ASP.NET Core library that lets git synchronize with a server side stored local repository using the Git Smart HTTP protocol.
Getting Started
- Add a reference to
Pmad.Git.HttpServerin your ASP.NET Core application. - Register the endpoints inside
Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
});
var app = builder.Build();
app.MapGitSmartHttp();
app.Run();
With the sample above, git clients can clone/fetch repositories stored under /srv/git using the Smart HTTP endpoints (/git/{repository}.git/info/refs, /git/{repository}.git/git-upload-pack).
Configuration
GitSmartHttpOptions controls behavior:
RepositoryRoot: Absolute path containing repositories. Mandatory.EnableUploadPack: Allowsgit-upload-pack(fetch/clone). Enabled by default.EnableReceivePack: Allowsgit-receive-pack(push). Disabled by default.Agent: String advertised to clients (shown bygit clone --verbose).AuthorizeAsync: Optional callback to allow/deny access per request. Receives the operation type (Read or Write) to distinguish between fetch/clone and push operations. By default, only read operations are allowed.RepositoryNameNormalizer: Optional callback to sanitize or transform repository names before file-system access. Applied after the.gitsuffix is removed from the repository name.RepositoryResolver: Callback to resolve the repository name from the HTTP context. By default, extracts therepositoryroute parameter.OnReceivePackCompleted: Optional callback invoked after a push operation whenever at least one reference update succeeds (including partial success). The list passed to the callback contains only the successfully updated references. This allows the host application to perform cache invalidation or trigger other post-push actions.RepositoryNameValidator: Optional callback to validate repository names for security. By default only allows alphanumeric characters, hyphens, underscores, and forward slashes (no leading, trailing, or repeated slashes). Host applications can override this to allow additional characters.
Enabling Push Operations
By default, push operations are restricted for security. To enable push, you need to:
- Set
EnableReceivePack = true - Provide an
AuthorizeAsynccallback that allows write operations for authorized users
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
options.EnableReceivePack = true;
options.AuthorizeAsync = async (context, repositoryName, operation, cancellationToken) =>
{
// Allow read for everyone
if (operation == GitOperation.Read)
return true;
// Only allow write for authenticated users
return context.User.Identity?.IsAuthenticated == true;
};
});
Push Notification
When push operations are enabled, you can be notified when a push completes (including partial success) to invalidate application caches or trigger webhooks:
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
options.EnableReceivePack = true;
options.OnReceivePackCompleted = async (context, repositoryName, updatedReferences) =>
{
// Log the push
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Repository {Repository} received push updating {Count} references",
repositoryName, updatedReferences.Count);
// Invalidate cache
var cache = context.RequestServices.GetRequiredService<IMyRepositoryCache>();
await cache.InvalidateAsync(repositoryName);
// Trigger webhook
foreach (var reference in updatedReferences)
{
logger.LogInformation(" Updated: {Reference}", reference);
}
};
});
Custom Repository Resolution
By default, MapGitSmartHttp expects a route with a {repository} parameter. You can customize this to use multiple parameters, no parameters (for single-repository hosting), or any custom logic:
Multiple Parameters
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
options.RepositoryResolver = context =>
{
var org = context.Request.RouteValues["organization"]?.ToString();
var repo = context.Request.RouteValues["repository"]?.ToString();
return string.IsNullOrEmpty(org) || string.IsNullOrEmpty(repo)
? null
: $"{org}/{repo}";
};
});
app.MapGitSmartHttp("/git/{organization}/{repository}.git");
Single Repository (No Parameters)
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
options.RepositoryResolver = context => "my-repo";
});
app.MapGitSmartHttp("/git");
From Query String or Header
builder.Services.AddGitSmartHttp(options =>
{
options.RepositoryRoot = "/srv/git";
options.RepositoryResolver = context =>
{
// Try query string first, then header
return context.Request.Query["repo"].FirstOrDefault()
?? context.Request.Headers["X-Git-Repository"].FirstOrDefault();
};
});
app.MapGitSmartHttp("/git");
Features
- Smart HTTP compatibility for Git clients (advertise, upload-pack, receive-pack).
- SHA-1 and SHA-256 repository support through
Pmad.Git.LocalRepositories. - No dependency on the
gitCLI at runtime. - Extensible authorization through user-provided callbacks.
| Product | Versions 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Pmad.Git.LocalRepositories (>= 0.1.112)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.