Forge

Jessie / forge public

Self-hosted git - made by qwen 3.8 27b

commit: 0c9688e9

Branches

main
BranchScenario.cs 2.7 kB · 59 lines RawHistory
using Forge.Data;

namespace Forge.Tests.Support;

/// <summary>
/// A pushed repository with main and a branch "feature/x". The feature branch has two commits:
/// it edits line two of a.txt and adds b.txt. Main can be made to diverge, with or without a conflict.
/// </summary>
public sealed record BranchScenario(ForgeHost Host, Repository Repo, string Work, string Url, string MainSha, string FeatureSha)
{
    public string Name => Repo.Name;

    public static async Task<BranchScenario> CreateAsync(
        ForgeHost host,
        string name,
        bool diverge = false,
        bool conflict = false,
        Visibility visibility = Visibility.Public)
    {
        var user = await host.CreateUserAsync();
        var repo = await host.CreateRepoAsync(user, name, visibility);
        var token = await host.CreateTokenAsync(user, canWrite: true);
        var url = host.GitUrl("jessie", name, token);
        var work = host.NewDir("work");
        var git = host.Git;

        await git.CreateWorkRepoAsync(work, fileName: "a.txt", content: "one\ntwo\nthree\n");
        await git.MustAsync(work, "checkout", "-b", "feature/x");
        await File.WriteAllTextAsync(Path.Combine(work, "a.txt"), "one\nTWO from feature\nthree\n");
        await File.WriteAllTextAsync(Path.Combine(work, "b.txt"), "new file\n");
        await git.MustAsync(work, "add", "-A");
        await git.MustAsync(work, "commit", "-m", "Feature work");
        await File.WriteAllTextAsync(Path.Combine(work, "b.txt"), "new file\nsecond line\n");
        await git.MustAsync(work, "commit", "-am", "More feature work");
        var featureSha = await git.MustAsync(work, "rev-parse", "HEAD");

        await git.MustAsync(work, "checkout", "main");
        if (diverge)
        {
            var (file, content) = conflict ? ("a.txt", "one\nTWO from main\nthree\n") : ("c.txt", "only on main\n");
            await File.WriteAllTextAsync(Path.Combine(work, file), content);
            await git.MustAsync(work, "add", "-A");
            await git.MustAsync(work, "commit", "-m", "Main moves on");
        }

        var mainSha = await git.MustAsync(work, "rev-parse", "HEAD");
        await git.MustAsync(work, "push", url, "main", "feature/x");
        return new BranchScenario(host, repo, work, url, mainSha, featureSha);
    }

    /// <summary>Fetches the server's branches as server/* into the working repository, then runs git there.</summary>
    public async Task<string> ServerAsync(params string[] args)
    {
        await Host.Git.MustAsync(Work, "fetch", "--prune", Url, "+refs/heads/*:refs/remotes/server/*");
        return await Host.Git.MustAsync(Work, args);
    }

    public string Page(string relative) => $"/jessie/{Name}/{relative}";
}

Keyboard shortcuts

/
Search repositories
s
Search file names and code
y
Open the permalink of the file being viewed
?
Show this help