using System.Net; using System.Text; using Forge.Data; using Forge.Features.History; using Forge.Features.SmartHttp; using Forge.Tests.Support; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Forge.Tests; public class DiffRowTests { [Fact] public void Compare_marks_what_lies_between_the_common_start_and_end() { var (old, @new) = DiffRows.Compare(" var total = count + 1;", " var total = count + 20;"); Assert.Equal(new ChangeRange(24, 25), old); Assert.Equal(new ChangeRange(24, 26), @new); // A pure insertion leaves an empty range on the old side. (old, @new) = DiffRows.Compare("call(a);", "call(a, b);"); Assert.Equal(new ChangeRange(6, 6), old); Assert.Equal(new ChangeRange(6, 9), @new); } [Fact] public void Compare_gives_up_when_only_indentation_is_shared() { Assert.Equal((null, null), DiffRows.Compare(" left", " north")); Assert.Equal((null, null), DiffRows.Compare("same", "same")); Assert.Equal((null, null), DiffRows.Compare("abc", "xyz")); } [Fact] public void Pair_lines_up_removed_and_added_lines() { var hunk = new Git.DiffHunk { Header = "@@ -1,4 +1,4 @@" }; hunk.Lines.AddRange( [ new(Git.DiffLineKind.Context, 1, 1, "keep"), new(Git.DiffLineKind.Deleted, 2, null, "old a"), new(Git.DiffLineKind.Deleted, 3, null, "old b"), new(Git.DiffLineKind.Added, null, 2, "new a"), new(Git.DiffLineKind.NoNewline, null, null, ""), new(Git.DiffLineKind.Context, 4, 3, "end"), ]); var rows = DiffRows.Pair(hunk); Assert.Equal(4, rows.Count); Assert.Equal(("keep", "keep"), (rows[0].Left!.Line.Text, rows[0].Right!.Line.Text)); Assert.Equal(("old a", "new a"), (rows[1].Left!.Line.Text, rows[1].Right!.Line.Text)); Assert.NotNull(rows[1].Left!.Change); Assert.Equal("old b", rows[2].Left!.Line.Text); Assert.Null(rows[2].Right); Assert.Equal("end", rows[3].Right!.Line.Text); } } public class PushGuardTests { private static byte[] Request(params string[] commands) { var body = new MemoryStream(); foreach (var command in commands) { body.Write(SmartHttpEndpoints.PktLine(command)); } body.Write("0000PACK...rest of the push"u8); return body.ToArray(); } [Fact] public async Task Command_list_is_read_and_the_stream_replays_everything() { var a = new string('a', 40); var b = new string('b', 40); var request = Request($"{a} {b} refs/heads/main\0report-status side-band-64k\n", $"{b} {a} refs/tags/v1\n"); var (refs, consumed, understood) = await PushGuard.ReadCommandsAsync(new MemoryStream(request), default); Assert.True(understood); Assert.Equal(["refs/heads/main", "refs/tags/v1"], refs); Assert.EndsWith("0000", Encoding.ASCII.GetString(consumed)); BranchProtection[] rules = [new() { Pattern = "main" }, new() { Pattern = "release/*", BlockPush = true }]; var (input, options) = await PushGuard.InspectAsync(new MemoryStream(request), rules, default); Assert.Equal( ["-c", "receive.hideRefs=refs/heads/release/", "-c", "receive.denyNonFastForwards=true", "-c", "receive.denyDeletes=true"], options); var replayed = new MemoryStream(); await input.CopyToAsync(replayed); Assert.Equal(request, replayed.ToArray()); } [Fact] public async Task Pushes_that_miss_protected_branches_are_left_alone_and_garbage_fails_closed() { var a = new string('a', 40); BranchProtection[] rules = [new() { Pattern = "main" }]; var (_, options) = await PushGuard.InspectAsync(new MemoryStream(Request($"{a} {a} refs/heads/topic\n")), rules, default); Assert.Empty(options); (_, options) = await PushGuard.InspectAsync(new MemoryStream("zzzz not pkt-lines"u8.ToArray()), rules, default); Assert.Contains("receive.denyNonFastForwards=true", options); } [Theory] [InlineData("main", "main", true)] [InlineData("main", "main2", false)] [InlineData("main", "feature/main", false)] [InlineData("release/*", "release/1.0", true)] [InlineData("release/*", "release/1.0/hotfix", true)] [InlineData("release/*", "release", false)] [InlineData("release/*", "releases/1.0", false)] public void Protection_patterns_match_names_and_prefixes(string pattern, string branch, bool expected) => Assert.Equal(expected, BranchRules.Matches(pattern, branch)); } public class AdvancedMergeTests(ForgeHost host) : IClassFixture { private Task DbAsync(Func> query) => host.WithScopeAsync(sp => query(sp.GetRequiredService())); [Fact] public async Task Diff_has_a_file_index_side_by_side_layout_and_within_line_changes() { var s = await BranchScenario.CreateAsync(host, "diff-view", diverge: true); using var client = host.CreateClient(); var url = s.Page("compare/main...feature/x"); var unified = await client.GetStringAsync(url); Assert.Contains("href=\"#diff-0\"", unified); Assert.Contains("id=\"diff-1\"", unified); Assert.DoesNotContain("class=\"diff split\"", unified); // "two" became "TWO from feature": nothing but indentation-free text in common, so the whole line is the change. Assert.Contains($"href=\"/jessie/diff-view/blob/{s.FeatureSha}/b.txt\"", unified); Assert.Contains("/lib/highlight/highlight.min.js", unified); Assert.Contains("href=\"" + url + "?view=split\"", unified); var split = await client.GetStringAsync(url + "?view=split"); Assert.Contains("class=\"diff split\"", split); Assert.Contains("TWO from feature", split); // The other options keep the chosen layout. Assert.Contains("href=\"" + url + "?view=split&w=1\"", split); Assert.Contains("href=\"/jessie/diff-view/compare/main..feature/x?view=split\"", split); // Two dots compare the tips directly, so main's own file shows up (as removed); three dots leave it out. var direct = await client.GetStringAsync(s.Page("compare/main..feature/x")); Assert.Contains("only on main", direct); Assert.DoesNotContain("only on main", unified); } [Fact] public async Task Commit_page_marks_the_changed_part_of_a_line_and_can_ignore_whitespace() { var s = await BranchScenario.CreateAsync(host, "intraline"); await File.WriteAllTextAsync(Path.Combine(s.Work, "calc.cs"), "class Calc\n{\n int total = count + 1;\n}\n"); await host.Git.MustAsync(s.Work, "add", "-A"); await host.Git.MustAsync(s.Work, "commit", "-m", "Add calc"); await File.WriteAllTextAsync(Path.Combine(s.Work, "calc.cs"), "class Calc\n{\n int total = count + 20;\n}\n"); await host.Git.MustAsync(s.Work, "commit", "-am", "Change the increment"); var changed = await host.Git.MustAsync(s.Work, "rev-parse", "HEAD"); await File.WriteAllTextAsync(Path.Combine(s.Work, "calc.cs"), "class Calc\n{\n int total = count + 20;\n}\n"); await host.Git.MustAsync(s.Work, "commit", "-am", "Reindent"); var reindented = await host.Git.MustAsync(s.Work, "rev-parse", "HEAD"); await host.Git.MustAsync(s.Work, "push", s.Url, "main"); using var client = host.CreateClient(); var page = await client.GetStringAsync(s.Page($"commit/{changed}")); Assert.Contains("data-lang=\"csharp\"", page); Assert.Contains("1", page); Assert.Contains("20", page); Assert.Contains("data-cs=\"24\" data-ce=\"26\"", page); var withWhitespace = await ForgeHost.TextAsync(await client.GetAsync(s.Page($"commit/{reindented}"))); Assert.Contains("1 changed file", withWhitespace); var ignoring = await ForgeHost.TextAsync(await client.GetAsync(s.Page($"commit/{reindented}?w=1"))); Assert.Contains("0 changed files", ignoring); } [Fact] public async Task Rebase_replays_the_commits_on_top_of_the_base_keeping_their_authors() { var s = await BranchScenario.CreateAsync(host, "rebase", diverge: true); using var owner = await host.CreateSignedInClientAsync(); var merged = await ForgeHost.PostFormAsync(owner, s.Page("compare/main...feature/x"), "merge", new() { ["Method"] = "rebase" }); Assert.Equal(HttpStatusCode.Redirect, merged.StatusCode); var log = (await s.ServerAsync("log", "--format=%s|%an|%cn|%P", "server/main")).Split('\n'); Assert.Equal(4, log.Length); Assert.StartsWith("More feature work|Test Author|jessie|", log[0]); Assert.StartsWith("Feature work|Test Author|jessie|", log[1]); Assert.Equal($"Main moves on|Test Author|Test Author|{await host.Git.MustAsync(s.Work, "rev-parse", s.MainSha + "~1")}", log[2]); // Linear: every commit has exactly one parent, and the feature's first commit now sits on main's tip. Assert.EndsWith("|" + s.MainSha, log[1]); Assert.All(log.Take(3), line => Assert.DoesNotContain(' ', line.Split('|')[3])); Assert.Equal("new file\nsecond line", await host.Git.MustAsync(s.Work, "show", "server/main:b.txt")); Assert.Equal("only on main", await host.Git.MustAsync(s.Work, "show", "server/main:c.txt")); // The feature branch itself is not rewritten. Assert.Equal(s.FeatureSha, await host.Git.MustAsync(s.Work, "rev-parse", "server/feature/x")); } [Fact] public async Task Commits_can_be_reverted_and_cherry_picked_from_their_page() { var s = await BranchScenario.CreateAsync(host, "apply", diverge: true); using var owner = await host.CreateSignedInClientAsync(); using var anonymous = host.CreateClient(); var featureFirst = await host.Git.MustAsync(s.Work, "rev-parse", s.FeatureSha + "~1"); // Cherry-pick the feature's first commit onto main. var picked = await ForgeHost.PostFormAsync(owner, s.Page($"commit/{featureFirst}"), "apply-commit", new() { ["Branch"] = "main", ["Action"] = "pick" }); Assert.Equal(HttpStatusCode.Redirect, picked.StatusCode); Assert.Equal("Feature work|Test Author|jessie", await s.ServerAsync("log", "-1", "--format=%s|%an|%cn", "server/main")); Assert.Contains($"(cherry picked from commit {featureFirst})", await host.Git.MustAsync(s.Work, "log", "-1", "--format=%B", "server/main")); Assert.Equal("new file", await host.Git.MustAsync(s.Work, "show", "server/main:b.txt")); var pickedSha = await host.Git.MustAsync(s.Work, "rev-parse", "server/main"); // Picking it again changes nothing and says so. var again = await ForgeHost.PostFormAsync(owner, s.Page($"commit/{featureFirst}"), "apply-commit", new() { ["Branch"] = "main", ["Action"] = "pick" }); Assert.Contains("already contains this change", await ForgeHost.TextAsync(again)); // Revert it. var reverted = await ForgeHost.PostFormAsync(owner, s.Page($"commit/{pickedSha}"), "apply-commit", new() { ["Branch"] = "main", ["Action"] = "revert" }); Assert.Equal(HttpStatusCode.Redirect, reverted.StatusCode); Assert.Equal("Revert \"Feature work\"", await s.ServerAsync("log", "-1", "--format=%s", "server/main")); Assert.Equal("a.txt\nc.txt", await host.Git.MustAsync(s.Work, "ls-tree", "--name-only", "server/main")); Assert.Equal("one\ntwo\nthree", await host.Git.MustAsync(s.Work, "show", "server/main:a.txt")); // A change that does not apply cleanly is refused with the files named. var conflicting = await BranchScenario.CreateAsync(host, "apply-conflict", diverge: true, conflict: true); var first = await host.Git.MustAsync(conflicting.Work, "rev-parse", conflicting.FeatureSha + "~1"); var refused = await ForgeHost.PostFormAsync(owner, conflicting.Page($"commit/{first}"), "apply-commit", new() { ["Branch"] = "main", ["Action"] = "pick" }); var refusedHtml = await ForgeHost.TextAsync(refused); Assert.Contains("cannot be applied automatically", refusedHtml); Assert.Contains("a.txt", refusedHtml); Assert.Equal(conflicting.MainSha, await conflicting.ServerAsync("rev-parse", "server/main")); // Visitors get neither the form nor the effect. Assert.DoesNotContain("apply-commit", await anonymous.GetStringAsync(s.Page($"commit/{s.FeatureSha}"))); var sneaky = await ForgeHost.PostFormAsync(anonymous, s.Page($"commit/{s.FeatureSha}"), "apply-commit", new() { ["Branch"] = "main", ["Action"] = "pick" }); Assert.False(sneaky.IsSuccessStatusCode); } [Fact] public async Task Conflicts_are_resolved_in_the_browser() { var s = await BranchScenario.CreateAsync(host, "resolve", diverge: true, conflict: true); using var owner = await host.CreateSignedInClientAsync(); using var anonymous = host.CreateClient(); var url = s.Page("conflicts/main...feature/x"); Assert.Contains("href=\"/jessie/resolve/conflicts/main...feature/x\"", await owner.GetStringAsync(s.Page("compare/main...feature/x"))); Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync(url)).StatusCode); var page = await ForgeHost.TextAsync(await owner.GetAsync(url)); Assert.Contains("a.txt", page); Assert.Contains("<<<<<<< ", page); Assert.Contains("TWO from main", page); Assert.Contains("TWO from feature", page); Dictionary Form(string choice, string text = "") => new() { ["base_sha"] = s.MainSha, ["head_sha"] = s.FeatureSha, ["choice_0"] = choice, ["text_0"] = text, ["message"] = "", }; // Leftover markers are refused. var leftover = await ForgeHost.PostFormAsync(owner, url, "resolve", Form("edit", "one\r\n<<<<<<< main\r\nTWO\r\n>>>>>>> feature\r\nthree\r\n")); Assert.Contains("still contains conflict markers", await ForgeHost.TextAsync(leftover)); // A stale page (the branch moved since) is refused. var stale = Form("ours"); stale["base_sha"] = s.FeatureSha; Assert.Contains("changed in the meantime", await ForgeHost.TextAsync(await ForgeHost.PostFormAsync(owner, url, "resolve", stale))); Assert.Equal(s.MainSha, await s.ServerAsync("rev-parse", "server/main")); // An edited resolution: browsers send CRLF, the file keeps its LF endings. var resolved = await ForgeHost.PostFormAsync(owner, url, "resolve", Form("edit", "one\r\nTWO from both\r\nthree\r\n")); Assert.Equal(HttpStatusCode.Redirect, resolved.StatusCode); Assert.Equal($"{s.MainSha} {s.FeatureSha}", await s.ServerAsync("log", "-1", "--format=%P", "server/main")); var bytes = await host.Git.RunAsync(s.Work, "cat-file", "blob", "server/main:a.txt"); Assert.Equal("one\nTWO from both\nthree\n", bytes.StdOut); // The non-conflicting part of the merge came along. Assert.Equal("new file\nsecond line", await host.Git.MustAsync(s.Work, "show", "server/main:b.txt")); // Taking one side wholesale. var theirs = await BranchScenario.CreateAsync(host, "resolve-theirs", diverge: true, conflict: true); var taken = await ForgeHost.PostFormAsync(owner, theirs.Page("conflicts/main...feature/x"), "resolve", new() { ["base_sha"] = theirs.MainSha, ["head_sha"] = theirs.FeatureSha, ["choice_0"] = "theirs", ["message"] = "Take the feature's version", }); Assert.Equal(HttpStatusCode.Redirect, taken.StatusCode); Assert.Equal("Take the feature's version", await theirs.ServerAsync("log", "-1", "--format=%s", "server/main")); Assert.Equal("one\nTWO from feature\nthree", await host.Git.MustAsync(theirs.Work, "show", "server/main:a.txt")); } [Fact] public async Task Pull_request_goes_from_open_to_merged_with_comments() { var s = await BranchScenario.CreateAsync(host, "pulls", diverge: true); using var owner = await host.CreateSignedInClientAsync(); using var anonymous = host.CreateClient(); var compare = s.Page("compare/main...feature/x"); // Opened from the compare page. var opened = await ForgeHost.PostFormAsync(owner, compare, "open-pull", new() { ["Title"] = "Add the feature", ["Description"] = "Adds **b.txt**.\n\n", }); Assert.Equal("/jessie/pulls/pull/1", opened.Headers.Location?.PathAndQuery); // A second one for the same branches is refused, and the compare page points at the first. var duplicate = await ForgeHost.PostFormAsync(owner, s.Page("compare/main..feature/x"), "open-pull", new() { ["Title"] = "Again" }); Assert.False(duplicate.IsSuccessStatusCode && (await ForgeHost.TextAsync(duplicate)).Contains("Again")); Assert.Contains("href=\"/jessie/pulls/pull/1\"", await owner.GetStringAsync(compare)); // Everyone who can read the repository can read the pull request. var page = await ForgeHost.TextAsync(await anonymous.GetAsync(s.Page("pull/1"))); Assert.Contains("Add the feature", page); Assert.Contains("b.txt", page); Assert.DoesNotContain("", page); Assert.Contains("More feature work", page); Assert.Contains("TWO from feature", page); Assert.Contains("2 commits", page); Assert.DoesNotContain("name=\"Method\"", page); Assert.Contains("Add the feature", await anonymous.GetStringAsync(s.Page("pulls"))); // Only the owner comments. await ForgeHost.PostFormAsync(anonymous, s.Page("pull/1"), "comment", new() { ["Body"] = "drive-by" }); var commented = await ForgeHost.PostFormAsync(owner, s.Page("pull/1"), "comment", new() { ["Body"] = "Looks *good*." }); var commentedHtml = await ForgeHost.TextAsync(commented); Assert.Contains("good", commentedHtml); Assert.Contains("1 comment", commentedHtml); Assert.DoesNotContain("drive-by", commentedHtml); // Close and reopen. var closed = await ForgeHost.PostFormAsync(owner, s.Page("pull/1"), "pull-state", new() { ["Action"] = "close" }); Assert.DoesNotContain("name=\"Method\"", await ForgeHost.TextAsync(closed)); Assert.Equal(PullRequestStatus.Closed, (await DbAsync(db => db.PullRequests.SingleAsync(p => p.RepoId == s.Repo.Id))).Status); Assert.Contains("Add the feature", await anonymous.GetStringAsync(s.Page("pulls?state=closed"))); // A closed pull request still shows what it proposed. Assert.Contains("TWO from feature", await anonymous.GetStringAsync(s.Page("pull/1"))); var reopened = await ForgeHost.PostFormAsync(owner, s.Page("pull/1"), "pull-state", new() { ["Action"] = "reopen" }); Assert.Contains("name=\"Method\"", await ForgeHost.TextAsync(reopened)); // Merge with a squash and delete the branch. var merged = await ForgeHost.PostFormAsync(owner, s.Page("pull/1"), "merge", new() { ["Method"] = "squash", ["Message"] = "Add the feature (#1)", ["DeleteBranch"] = "true", }); var mergedHtml = await ForgeHost.TextAsync(merged); Assert.Contains("Merged into main.", mergedHtml); Assert.DoesNotContain("name=\"Method\"", mergedHtml); var pull = await DbAsync(db => db.PullRequests.SingleAsync(p => p.RepoId == s.Repo.Id)); Assert.Equal(PullRequestStatus.Merged, pull.Status); Assert.Equal(s.FeatureSha, pull.HeadSha); Assert.Equal(pull.MergeCommitSha, await s.ServerAsync("rev-parse", "server/main")); Assert.Equal("Add the feature (#1)", await host.Git.MustAsync(s.Work, "log", "-1", "--format=%s", "server/main")); Assert.DoesNotContain("server/feature/x", await host.Git.MustAsync(s.Work, "branch", "-r")); // The branch is gone, but the merged pull request keeps its commits and diff. var after = await anonymous.GetStringAsync(s.Page("pull/1")); Assert.Contains("More feature work", after); Assert.Contains("TWO from feature", after); // Title and description can be corrected afterwards; visitors cannot do it. await ForgeHost.PostFormAsync(anonymous, s.Page("pull/1"), "edit-pull", new() { ["Title"] = "Hijacked" }); var edited = await ForgeHost.PostFormAsync(owner, s.Page("pull/1"), "edit-pull", new() { ["Title"] = "Add the feature, renamed", ["Description"] = "New *text*." }); var editedHtml = await ForgeHost.TextAsync(edited); Assert.Contains("Add the feature, renamed", editedHtml); Assert.Contains("text", editedHtml); Assert.DoesNotContain("Hijacked", editedHtml); // Private repositories keep their pull requests to themselves. var secret = await BranchScenario.CreateAsync(host, "pulls-private", visibility: Visibility.Private); await ForgeHost.PostFormAsync(owner, secret.Page("compare/main...feature/x"), "open-pull", new() { ["Title"] = "Secret" }); Assert.Equal(HttpStatusCode.OK, (await owner.GetAsync(secret.Page("pull/1"))).StatusCode); Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync(secret.Page("pull/1"))).StatusCode); Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync(secret.Page("pulls"))).StatusCode); Assert.Equal(HttpStatusCode.NotFound, (await owner.GetAsync(s.Page("pull/99"))).StatusCode); } [Fact] public async Task Pull_request_notices_a_merge_made_with_git() { var s = await BranchScenario.CreateAsync(host, "pulls-external", diverge: true); using var owner = await host.CreateSignedInClientAsync(); await ForgeHost.PostFormAsync(owner, s.Page("compare/main...feature/x"), "open-pull", new() { ["Title"] = "Merged elsewhere" }); await host.Git.MustAsync(s.Work, "merge", "--no-ff", "-m", "Merge locally", "feature/x"); await host.Git.MustAsync(s.Work, "push", s.Url, "main"); Assert.Contains("Merged into main.", await ForgeHost.TextAsync(await owner.GetAsync(s.Page("pull/1")))); Assert.Equal(PullRequestStatus.Merged, (await DbAsync(db => db.PullRequests.SingleAsync(p => p.RepoId == s.Repo.Id))).Status); } [Fact] public async Task Conflicting_pull_request_is_merged_through_the_conflict_page() { var s = await BranchScenario.CreateAsync(host, "pulls-conflict", diverge: true, conflict: true); using var owner = await host.CreateSignedInClientAsync(); await ForgeHost.PostFormAsync(owner, s.Page("compare/main...feature/x"), "open-pull", new() { ["Title"] = "Needs a hand" }); var page = await owner.GetStringAsync(s.Page("pull/1")); Assert.Contains("These branches have conflicts.", page); Assert.Contains("href=\"/jessie/pulls-conflict/conflicts/main...feature/x\"", page); Assert.DoesNotContain("name=\"Method\"", page); await ForgeHost.PostFormAsync(owner, s.Page("conflicts/main...feature/x"), "resolve", new() { ["base_sha"] = s.MainSha, ["head_sha"] = s.FeatureSha, ["choice_0"] = "ours", }); var pull = await DbAsync(db => db.PullRequests.SingleAsync(p => p.RepoId == s.Repo.Id)); Assert.Equal(PullRequestStatus.Merged, pull.Status); Assert.Equal(pull.MergeCommitSha, await s.ServerAsync("rev-parse", "server/main")); Assert.Equal("one\nTWO from main\nthree", await host.Git.MustAsync(s.Work, "show", "server/main:a.txt")); } [Fact] public async Task Protected_branch_refuses_force_pushes_and_deletion_but_not_ordinary_work() { var s = await BranchScenario.CreateAsync(host, "protected"); using var owner = await host.CreateSignedInClientAsync(); var settings = s.Page("settings/branches"); var invalid = await ForgeHost.PostFormAsync(owner, settings, "add-rule", new() { ["Pattern"] = "--force" }); Assert.Contains("This is not a valid branch name.", await ForgeHost.TextAsync(invalid)); var added = await ForgeHost.PostFormAsync(owner, settings, "add-rule", new() { ["Pattern"] = "main" }); Assert.Contains("main", await ForgeHost.TextAsync(added)); // An ordinary push still goes through. await File.WriteAllTextAsync(Path.Combine(s.Work, "d.txt"), "more\n"); await host.Git.MustAsync(s.Work, "add", "-A"); await host.Git.MustAsync(s.Work, "commit", "-m", "Normal work"); await host.Git.MustAsync(s.Work, "push", s.Url, "main"); var tip = await host.Git.MustAsync(s.Work, "rev-parse", "HEAD"); // Rewriting history on it does not, and neither does deleting it. await host.Git.MustAsync(s.Work, "commit", "--amend", "-m", "Rewritten"); var forced = await host.Git.RunAsync(s.Work, "push", "--force", s.Url, "main"); Assert.False(forced.Ok); Assert.Contains("non-fast-forward", forced.StdErr); await host.Git.MustAsync(s.Work, "push", s.Url, "feature/x:refs/heads/spare"); Assert.False((await host.Git.RunAsync(s.Work, "push", s.Url, "--delete", "main")).Ok); Assert.Equal(tip, await s.ServerAsync("rev-parse", "server/main")); // Branches that are not protected behave as before, in a push of their own. await host.Git.MustAsync(s.Work, "push", "--force", s.Url, "main:refs/heads/spare"); await host.Git.MustAsync(s.Work, "push", s.Url, "--delete", "spare"); // The rule can be removed again. var ruleId = (await DbAsync(db => db.BranchProtections.SingleAsync(r => r.RepoId == s.Repo.Id))).Id; await ForgeHost.PostFormAsync(owner, settings, "remove-rule", new() { ["RuleId"] = ruleId.ToString() }); await host.Git.MustAsync(s.Work, "push", "--force", s.Url, "main"); using var anonymous = host.CreateClient(); Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync(settings)).StatusCode); } [Fact] public async Task Branch_closed_to_pushes_changes_only_through_the_web() { var s = await BranchScenario.CreateAsync(host, "web-only"); using var owner = await host.CreateSignedInClientAsync(); await ForgeHost.PostFormAsync(owner, s.Page("settings/branches"), "add-rule", new() { ["Pattern"] = "release/*", ["BlockPush"] = "true" }); await ForgeHost.PostFormAsync(owner, s.Page("branches"), "create-branch", new() { ["NewName"] = "release/1.0", ["From"] = "main" }); // Git may not push to it, even fast-forward… var pushed = await host.Git.RunAsync(s.Work, "push", s.Url, "feature/x:refs/heads/release/1.0"); Assert.False(pushed.Ok); Assert.Contains("hidden ref", pushed.StdErr); Assert.Equal(s.MainSha, await s.ServerAsync("rev-parse", "server/release/1.0")); // …the web may not delete it… var deleted = await ForgeHost.PostFormAsync(owner, s.Page("branches"), "delete-branch", new() { ["DeleteName"] = "release/1.0" }); Assert.Contains("This branch is protected and cannot be deleted.", await ForgeHost.TextAsync(deleted)); // …but a merge on the web moves it, and other branches still take pushes. var merged = await ForgeHost.PostFormAsync(owner, s.Page("compare/release/1.0...feature/x"), "merge", new() { ["Method"] = "ff" }); Assert.Equal(HttpStatusCode.Redirect, merged.StatusCode); Assert.Equal(s.FeatureSha, await s.ServerAsync("rev-parse", "server/release/1.0")); await host.Git.MustAsync(s.Work, "push", s.Url, "feature/x:refs/heads/main"); } }