OPC # 0001: Gitea services
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -17,10 +17,13 @@ public static class GitEndpoints
|
||||
// GET /api/git/log?grep=OPC+%23+0001&limit=50&repo=all
|
||||
// repo can be "all" (default) or a named key from Git:Repos (e.g. "Clarity", "OPC", "Gateway").
|
||||
// All responses include a repoKey field so the caller knows which repo each commit came from.
|
||||
// GET /api/git/log?grep=...&limit=25&page=1&repo=all
|
||||
// page is 1-based. Each repo contributes up to limit*page commits before merge-sort+skip/take.
|
||||
private static IResult GetLog(
|
||||
IConfiguration config,
|
||||
string? grep = null,
|
||||
int limit = 50,
|
||||
int limit = 25,
|
||||
int page = 1,
|
||||
string repo = "all")
|
||||
{
|
||||
var repos = repo == "all"
|
||||
@@ -56,12 +59,13 @@ public static class GitEndpoints
|
||||
if (!string.IsNullOrWhiteSpace(grep))
|
||||
query = query.Where(c => c.Message.Contains(grep, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
foreach (var c in query.Take(limit))
|
||||
foreach (var c in query.Take(limit * page))
|
||||
bucket.Add((c.Author.When, repoKey, ToGitCommit(r, c)));
|
||||
}
|
||||
|
||||
var commits = bucket
|
||||
.OrderByDescending(x => x.When)
|
||||
.Skip((page - 1) * limit)
|
||||
.Take(limit)
|
||||
.Select(x => new
|
||||
{
|
||||
@@ -79,41 +83,48 @@ public static class GitEndpoints
|
||||
}
|
||||
|
||||
// GET /api/git/commits/{hash}
|
||||
// Searches all registered repos — hash may belong to any of the three repos.
|
||||
private static IResult GetCommit(string hash, IConfiguration config)
|
||||
{
|
||||
var repoPath = ResolveRepo(config);
|
||||
if (repoPath is null)
|
||||
return Results.Problem("Could not locate a git repository.");
|
||||
var repos = ResolveAllRepos(config);
|
||||
if (repos.Count == 0)
|
||||
return Results.Problem("Could not locate any git repositories.");
|
||||
|
||||
using var repo = new Repository(repoPath);
|
||||
var commit = repo.Lookup<Commit>(hash);
|
||||
if (commit is null) return Results.NotFound();
|
||||
|
||||
var parentTree = commit.Parents.FirstOrDefault()?.Tree;
|
||||
var changes = repo.Diff.Compare<TreeChanges>(parentTree, commit.Tree);
|
||||
var patch = repo.Diff.Compare<Patch>(parentTree, commit.Tree);
|
||||
|
||||
var files = changes.Select(c => new
|
||||
foreach (var (_, repoPath) in repos)
|
||||
{
|
||||
path = c.Path,
|
||||
oldPath = c.OldPath,
|
||||
status = c.Status.ToString(),
|
||||
additions = patch[c.Path]?.LinesAdded ?? 0,
|
||||
deletions = patch[c.Path]?.LinesDeleted ?? 0,
|
||||
patch = patch[c.Path]?.Patch ?? string.Empty,
|
||||
}).ToList();
|
||||
if (!Directory.Exists(repoPath)) continue;
|
||||
using var repo = new Repository(repoPath);
|
||||
var commit = repo.Lookup<Commit>(hash);
|
||||
if (commit is null) continue;
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
hash = commit.Sha,
|
||||
shortHash = commit.Sha[..7],
|
||||
author = commit.Author.Name,
|
||||
email = commit.Author.Email,
|
||||
date = commit.Author.When.ToString("yyyy-MM-dd HH:mm:ss zzz"),
|
||||
subject = commit.MessageShort,
|
||||
body = commit.Message,
|
||||
files,
|
||||
});
|
||||
var parentTree = commit.Parents.FirstOrDefault()?.Tree;
|
||||
var changes = repo.Diff.Compare<TreeChanges>(parentTree, commit.Tree);
|
||||
var patch = repo.Diff.Compare<Patch>(parentTree, commit.Tree);
|
||||
|
||||
var files = changes.Select(c => new
|
||||
{
|
||||
path = c.Path,
|
||||
oldPath = c.OldPath,
|
||||
status = c.Status.ToString(),
|
||||
additions = patch[c.Path]?.LinesAdded ?? 0,
|
||||
deletions = patch[c.Path]?.LinesDeleted ?? 0,
|
||||
patch = patch[c.Path]?.Patch ?? string.Empty,
|
||||
}).ToList();
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
hash = commit.Sha,
|
||||
shortHash = commit.Sha[..7],
|
||||
author = commit.Author.Name,
|
||||
email = commit.Author.Email,
|
||||
date = commit.Author.When.ToString("yyyy-MM-dd HH:mm:ss zzz"),
|
||||
subject = commit.MessageShort,
|
||||
body = commit.Message,
|
||||
files,
|
||||
});
|
||||
}
|
||||
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
// GET /api/git/branches
|
||||
|
||||
Reference in New Issue
Block a user