// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later using System.Text.Json; using System.Text.RegularExpressions; using Microsoft.Extensions.Options; namespace Progmasoft.Progmaweb.Api.Git; /// Where the read-only mirrors of git.progmasoft.com are and how they are read. internal sealed class GitOptions { /// The section of the configuration. public const string Section = "Git"; /// The host the mirrors are shown on. public string Host { get; set; } = "git.progmasoft.com"; /// The directory of the bare mirrors, <name>.git each. public string MirrorsPath { get; set; } = "/srv/git/mirrors"; /// The catalog: the sections, the repositories in them and their descriptions. public string CatalogPath { get; set; } = "/etc/git-mirror/catalog.json"; /// The Git program. public string GitPath { get; set; } = "/usr/bin/git"; } /// One repository of the catalog. /// Its name, the same on GitHub and in the mirrors. /// What it is. internal sealed record GitCatalogRepository(string Name, string Description); /// One section of the catalog. /// Its heading. /// Its repositories, in the order shown. internal sealed record GitCatalogSection(string Name, IReadOnlyList Repositories); /// The repositories git.progmasoft.com shows, read from the catalog file. /// /// The file is ops/git/catalog.json, installed beside the list git-mirror reads; a repository that is in /// the catalog but has no mirror is not shown. The file is read again when it changes. /// /// Where the catalog is. internal sealed partial class GitCatalog(IOptions options) { /// How the catalog file is read: property names in any case. private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; /// Guards . private readonly Lock gate = new(); /// The catalog as last read, with the time its file was written then. private (DateTime Written, IReadOnlyList Sections)? cached; /// Matches a repository name: what GitHub allows, without a leading dot. /// The compiled expression. [GeneratedRegex("^[A-Za-z0-9_-][A-Za-z0-9._-]{0,99}$", RegexOptions.CultureInvariant)] private static partial Regex NamePattern(); /// Whether a text is a repository name. /// The text. /// when it is one. public static bool IsName(string name) => NamePattern().IsMatch(name) && !name.EndsWith(".git", StringComparison.Ordinal); /// The sections, with only the repositories that have a mirror. /// The sections; empty when there is no catalog. public IReadOnlyList Sections() { string path = options.Value.CatalogPath; DateTime written = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue; lock (gate) { if (cached is { } known && known.Written == written) { return known.Sections; } IReadOnlyList sections = written == DateTime.MinValue ? [] : Read(path); cached = (written, sections); return sections; } } /// One repository by name, when the catalog has it and its mirror exists. /// The name. /// The repository and the path of its mirror, or . public (GitCatalogRepository Repository, string MirrorPath)? Find(string name) { if (!IsName(name)) { return null; } foreach (GitCatalogSection section in Sections()) { foreach (GitCatalogRepository repository in section.Repositories) { if (string.Equals(repository.Name, name, StringComparison.Ordinal)) { string mirror = MirrorOf(repository.Name); return Directory.Exists(mirror) ? (repository, mirror) : null; } } } return null; } /// The path of a repository's mirror. /// A valid name. /// The path. public string MirrorOf(string name) => Path.Combine(options.Value.MirrorsPath, name + ".git"); /// Reads and checks the catalog file. /// The file. /// Its sections. /// The file is empty or names something that is not a repository name. private static IReadOnlyList Read(string path) { using FileStream stream = File.OpenRead(path); GitCatalogFile file = JsonSerializer.Deserialize(stream, JsonOptions) ?? throw new InvalidDataException($"{path} is empty."); foreach (GitCatalogSection section in file.Sections) { foreach (GitCatalogRepository repository in section.Repositories) { if (!IsName(repository.Name)) { throw new InvalidDataException($"{path} names a repository '{repository.Name}' that is not a name."); } } } return file.Sections; } /// The shape of the catalog file. /// The sections. private sealed record GitCatalogFile(IReadOnlyList Sections); }