Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

#39 - Add C# implementation (reverse engineer) of git http-backend. #351

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 110 additions & 39 deletions Pyro.Api/Pyro/Endpoints/GitBackendEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,129 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the GPL-3.0 license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Mvc;
using Pyro.Extensions;
using Pyro.Infrastructure;
using Pyro.Services;

namespace Pyro.Endpoints;

internal static class GitBackendEndpoints
{
public static IEndpointRouteBuilder MapGitBackendEndpoints(this IEndpointRouteBuilder app)
public static IEndpointRouteBuilder MapGitBackendEndpoint(this IEndpointRouteBuilder app)
{
var options = app.ServiceProvider.GetRequiredService<IOptions<GitOptions>>();
if (options.Value.UseNativeGitBackend)
return app.MapNativeGitBackendEndpoints();

return app.MapCsharpBackendEndpoint();
}

private static IEndpointRouteBuilder MapNativeGitBackendEndpoints(this IEndpointRouteBuilder app)
{
app
.Map("/{repositoryName}.git/{**path}", async (
GitBackend backend,
string repositoryName,
CancellationToken cancellationToken) =>
await backend.Handle(repositoryName, cancellationToken))
var group = app
.MapGroup("/{repositoryName}.git")
.RequireAuthorization(pb => pb
.AddAuthenticationSchemes(AuthExtensions.BasicAuthenticationScheme)
.RequireAuthenticatedUser());

return app;
}

private static IEndpointRouteBuilder MapCsharpBackendEndpoint(this IEndpointRouteBuilder app)
{
var group = app.MapGroup("/{repositoryName}.git");

group.MapGet("/HEAD", () => { throw new NotImplementedException(); });
group.MapGet("/info/refs", () => { throw new NotImplementedException(); });
group.MapGet("/info/alternates", () => { throw new NotImplementedException(); });
group.MapGet("/info/http-alternates", () => { throw new NotImplementedException(); });
group.MapGet("/info/packs", () => { throw new NotImplementedException(); });
group.MapGet("/objects/{hash1:regex([[0-9a-f]]{{2}})}/{hash2:regex([[0-9a-f]]{{38}})}", () => { throw new NotImplementedException(); });
group.MapGet("/objects/{hash1:regex([[0-9a-f]]{{2}})}/{hash2:regex([[0-9a-f]]{{62}})}", () => { throw new NotImplementedException(); });
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{40}})}.pack", () => { throw new NotImplementedException(); });
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{64}})}.pack", () => { throw new NotImplementedException(); });
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{40}})}.idx", () => { throw new NotImplementedException(); });
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{64}})}.idx", () => { throw new NotImplementedException(); });
group.MapGet("/HEAD", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetHead(repositoryName, cancellationToken);
});
group.MapGet("/info/refs", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetInfoRefs(repositoryName, cancellationToken);
});
group.MapGet("/info/alternates", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetInfoAlternates(repositoryName, cancellationToken);
});
group.MapGet("/info/http-alternates", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetInfoHttpAlternates(repositoryName, cancellationToken);
});
group.MapGet("/info/packs", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetInfoPacks(repositoryName, cancellationToken);
});
group.MapGet("/objects/{hash1:regex([[0-9a-f]]{{2}})}/{hash2:regex([[0-9a-f]]{{38}})}", async (
[FromRoute] string repositoryName,
[FromRoute] string hash1,
[FromRoute] string hash2,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjects(repositoryName, hash1, hash2, cancellationToken);
});
group.MapGet("/objects/{hash1:regex([[0-9a-f]]{{2}})}/{hash2:regex([[0-9a-f]]{{62}})}", async (
[FromRoute] string repositoryName,
[FromRoute] string hash1,
[FromRoute] string hash2,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjects(repositoryName, hash1, hash2, cancellationToken);
});
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{40}})}.pack", async (
[FromRoute] string repositoryName,
[FromRoute] string hash,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjectsPack(repositoryName, hash, cancellationToken);
});
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{64}})}.pack", async (
[FromRoute] string repositoryName,
[FromRoute] string hash,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjectsPack(repositoryName, hash, cancellationToken);
});
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{40}})}.idx", async (
[FromRoute] string repositoryName,
[FromRoute] string hash,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjectsPackIdx(repositoryName, hash, cancellationToken);
});
group.MapGet("/objects/pack/pack-{hash:regex([[0-9a-f]]{{64}})}.idx", async (
[FromRoute] string repositoryName,
[FromRoute] string hash,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GetObjectsPackIdx(repositoryName, hash, cancellationToken);
});

group.MapPost("/git-upload-pack", () => { throw new NotImplementedException(); });
group.MapPost("/git-upload-archive", () => { throw new NotImplementedException(); });
group.MapPost("/git-receive-pack", () => { throw new NotImplementedException(); });
group.MapPost("/git-upload-pack", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GitUploadPack(repositoryName, cancellationToken);
});
group.MapPost("/git-upload-archive", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GitUploadArchive(repositoryName, cancellationToken);
});
group.MapPost("/git-receive-pack", async (
[FromRoute] string repositoryName,
[FromServices] NativeGitBackend gitBackend,
CancellationToken cancellationToken) =>
{
await gitBackend.GitReceivePack(repositoryName, cancellationToken);
});

return app;
}
Expand Down
2 changes: 1 addition & 1 deletion Pyro.Api/Pyro/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
options => options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents)
.RequireAuthorization();

app.MapGitBackendEndpoints();
app.MapGitBackendEndpoint();
app.MapOpenApi();
app.MapScalarApiReference();

Expand Down
2 changes: 1 addition & 1 deletion Pyro.Api/Pyro/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IHostApplicationBuilder AddPyroInfrastructure(this IHostApplicatio
services
.Configure<ServiceOptions>(configuration.GetRequiredSection(ServiceOptions.Name))
.AddSingleton<IValidateOptions<ServiceOptions>, ServiceOptions>()
.AddScoped<GitBackend>()
.AddScoped<NativeGitBackend>()
.AddHostedService<OutboxMessageProcessing>()
.Configure<OutboxMessageProcessingOptions>(
configuration
Expand Down
135 changes: 0 additions & 135 deletions Pyro.Api/Pyro/Services/GitBackend.cs

This file was deleted.

Loading
Loading