using Forge.Data;
namespace Forge.Tests.Support;
///
/// 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.
///
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 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);
}
/// Fetches the server's branches as server/* into the working repository, then runs git there.
public async Task 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}";
}