╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const obj = xit.object; 6 const hash = xit.hash; 7 const rf = xit.ref; 8 const fork = @import("../../fork.zig"); 9 const pch = @import("../../patch.zig"); 10 const push = @import("../../push.zig"); 11 const ui = @import("../../ui.zig"); 12 13 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 14 const Repo = rp.Repo(.xit, repo_opts); 15 const author = evt.CommitAuthor{ .name = "alice", .email = "alice@example.test" }; 16 17 test "branch patch lifecycle" { 18 try testBranchPatch(.git, .sha1); 19 try testBranchPatch(.git, .sha256); 20 try testBranchPatch(.xit, .sha1); 21 try testBranchPatch(.xit, .sha256); 22 } 23 24 fn testBranchPatch(comptime kind: rp.RepoKind, comptime hash_kind: hash.HashKind) !void { 25 const io = std.testing.io; 26 const allocator = std.testing.allocator; 27 const opts: rp.RepoOpts(kind) = .{ .is_test = true, .hash = hash_kind }; 28 var temp = std.testing.tmpDir(.{}); 29 defer temp.cleanup(); 30 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 31 defer allocator.free(path); 32 var repo = try rp.Repo(kind, opts).init(io, allocator, .{ .path = path }); 33 defer repo.deinit(io, allocator); 34 var arena = std.heap.ArenaAllocator.init(allocator); 35 defer arena.deinit(); 36 37 // create two branches with a shared base 38 { 39 const file = try repo.core.work_dir.createFile(io, "base.txt", .{}); 40 defer file.close(io); 41 try file.writeStreamingAll(io, "base\n"); 42 } 43 try repo.add(io, allocator, &.{"base.txt"}); 44 const base = try repo.commit(io, allocator, .{ .message = "base" }); 45 try repo.addBranch(io, .{ .name = "feature" }); 46 const id = [_]u8{7} ** evt.event_id_size; 47 const patch = evt.Patch{ .title = "branch patch", .tags = "feature", .description = "existing branch", .source_branch = "feature", .target_branch = "master" }; 48 49 // the same branch cannot be both source and target 50 { 51 var invalid = patch; 52 invalid.target_branch = "feature"; 53 try std.testing.expectError(error.SameBranch, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), invalid, null, author)); 54 try std.testing.expectEqual(null, try repo.readRef(io, evt.events_ref)); 55 } 56 57 // distinct branches at the same commit can still create a patch 58 try pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), patch, null, author); 59 const first = (try evt.readFromRepo(evt.Patch, kind, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 60 const first_revision = first.event.revision orelse return error.NotFound; 61 try std.testing.expectEqualStrings(&base, first_revision.source_oid); 62 try std.testing.expectEqualStrings(&base, &(try repo.readRef(io, .{ .kind = .head, .name = "master" }) orelse return error.NotFound)); 63 64 // an unchanged refresh does not add events 65 const event_tip = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 66 try pch.refreshBranches(.local, kind, opts, io, allocator, &repo, null, null); 67 try std.testing.expectEqualStrings(&event_tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 68 69 // retargeting to the source leaves the patch and revision unchanged 70 try std.testing.expectError(error.SameBranch, evt.Patch.update(.local, kind, opts, io, allocator, &repo, &id, .{ .fields = .{ 71 .title = patch.title, 72 .tags = patch.tags, 73 .description = patch.description, 74 .target_branch = "feature", 75 } }, author)); 76 try std.testing.expectEqualStrings(&event_tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 77 78 // a source update replaces the revision, not the patch metadata 79 const next = try repo.commitAtRef(io, allocator, .{ .message = "next feature" }, null, .{ .kind = .head, .name = "feature" }); 80 try pch.refreshBranches(.local, kind, opts, io, allocator, &repo, null, null); 81 const updated = (try evt.readFromRepo(evt.Patch, kind, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 82 const revision = updated.event.revision orelse return error.NotFound; 83 try std.testing.expectEqualStrings(&next, revision.source_oid); 84 try std.testing.expect(!std.mem.eql(u8, &first_revision.id, &revision.id)); 85 try std.testing.expectEqualStrings(patch.title, updated.event.title); 86 87 // changing the target captures a fresh revision, without changing the source 88 try repo.addBranch(io, .{ .name = "other-target" }); 89 try evt.Patch.update(.local, kind, opts, io, allocator, &repo, &id, .{ .fields = .{ 90 .title = patch.title, 91 .tags = patch.tags, 92 .description = patch.description, 93 .target_branch = "other-target", 94 } }, author); 95 const retargeted = (try evt.readFromRepo(evt.Patch, kind, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 96 try std.testing.expectEqualStrings("other-target", retargeted.event.target_branch); 97 try std.testing.expectEqualStrings("feature", retargeted.event.source_branch orelse return error.NotFound); 98 try std.testing.expect(retargeted.event.revision != null); 99 100 // clearing the revision must not let a stale edit overwrite a newer patch 101 var stale_edit = updated.event; 102 stale_edit.revision = null; 103 const before_stale = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 104 try std.testing.expectError(error.PatchOutOfDate, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), stale_edit, updated.event, author)); 105 try std.testing.expectEqualStrings(&before_stale, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 106 107 // an unrelated target leaves the patch and its revision intact 108 _ = try repo.commitAtRef(io, allocator, .{ .message = "unrelated", .parent_oids = &.{} }, null, .{ .kind = .head, .name = "unrelated" }); 109 const before_edit = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 110 try std.testing.expectError(error.UnrelatedBranches, evt.Patch.update(.local, kind, opts, io, allocator, &repo, &id, .{ .fields = .{ 111 .title = "edited title", 112 .tags = patch.tags, 113 .description = patch.description, 114 .target_branch = "unrelated", 115 } }, author)); 116 try std.testing.expectEqualStrings(&before_edit, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 117 118 // missing branches fail without writing events 119 var invalid = patch; 120 invalid.source_branch = "missing"; 121 try std.testing.expectError(error.InvalidSourceBranch, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), invalid, retargeted.event, author)); 122 invalid = patch; 123 invalid.target_branch = "missing"; 124 try std.testing.expectError(error.InvalidTargetBranch, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), invalid, retargeted.event, author)); 125 126 // removing the patch leaves its branch intact 127 try evt.remove(.local, .repo, kind, opts, io, allocator, &repo, &id, .patch, author); 128 try pch.refreshBranches(.local, kind, opts, io, allocator, &repo, null, null); 129 const removed = (try evt.readFromRepo(evt.Patch, kind, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 130 try std.testing.expect(removed.removed); 131 try std.testing.expectEqualStrings(&next, &(try repo.readRef(io, .{ .kind = .head, .name = "feature" }) orelse return error.NotFound)); 132 133 // an edit started before removal must not restore the patch 134 stale_edit = retargeted.event; 135 stale_edit.revision = null; 136 const before_restore = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 137 try std.testing.expectError(error.PatchOutOfDate, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), stale_edit, retargeted.event, author)); 138 try std.testing.expectEqualStrings(&before_restore, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 139 } 140 141 test "branch patches merge without a fork" { 142 try testBranchMerge(.source); 143 try testBranchMerge(.squash); 144 } 145 146 fn testBranchMerge(selection: evt.Patch.MergeRevision) !void { 147 const io = std.testing.io; 148 const allocator = std.testing.allocator; 149 const opts: rp.RepoOpts(.xit) = .{ .is_test = true, .ProgressCtx = *push.PushProgress }; 150 var temp = std.testing.tmpDir(.{}); 151 defer temp.cleanup(); 152 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 153 defer allocator.free(path); 154 var repo = try rp.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 155 defer repo.deinit(io, allocator); 156 { 157 const file = try repo.core.work_dir.createFile(io, "base.txt", .{}); 158 defer file.close(io); 159 try file.writeStreamingAll(io, "base\n"); 160 } 161 try repo.add(io, allocator, &.{"base.txt"}); 162 _ = try repo.commit(io, allocator, .{ .message = "base" }); 163 try repo.addBranch(io, .{ .name = "feature" }); 164 { 165 var selected = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "feature" } } }); 166 defer selected.deinit(); 167 const file = try repo.core.work_dir.createFile(io, "base.txt", .{}); 168 defer file.close(io); 169 try file.writeStreamingAll(io, "source\n"); 170 try repo.add(io, allocator, &.{"base.txt"}); 171 _ = try repo.commit(io, allocator, .{ .message = "edit file" }); 172 var restored = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 173 defer restored.deinit(); 174 } 175 const id = [_]u8{9} ** evt.event_id_size; 176 const id_hex = std.fmt.bytesToHex(id, .lower); 177 try pch.writeBranchPatch(.server, .xit, opts, io, allocator, &repo, id_hex, .{ 178 .title = "merge branch", 179 .description = "", 180 .tags = "", 181 .source_branch = "feature", 182 .target_branch = "master", 183 }, null, author); 184 185 // updating a branch and checking its mergeability counts as one patch 186 _ = try repo.commitAtRef(io, allocator, .{ .message = "revise feature" }, null, .{ .kind = .head, .name = "feature" }); 187 var response = xit.net_server_receive_pack.Response.init(allocator); 188 defer response.deinit(); 189 response.sideband = true; 190 var output = std.Io.Writer.Allocating.init(allocator); 191 defer output.deinit(); 192 var progress = push.PushProgress{ .response = &response, .writer = &output.writer, .label = "Updating patches" }; 193 try pch.refreshBranches(.server, .xit, opts, io, allocator, &repo, null, &progress); 194 try std.testing.expect(std.mem.indexOf(u8, output.written(), "Updating patches: 100% (1/1)\n") != null); 195 196 // closed patches skip source updates and contribute no progress 197 try evt.Patch.update(.server, .xit, opts, io, allocator, &repo, &id, .{ .status = .closed }, author); 198 const next = try repo.commitAtRef(io, allocator, .{ .message = "revise while closed" }, null, .{ .kind = .head, .name = "feature" }); 199 const closed_tip = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 200 const closed_progress_len = output.written().len; 201 try pch.refreshBranches(.server, .xit, opts, io, allocator, &repo, null, &progress); 202 try std.testing.expectEqualStrings(&closed_tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 203 try std.testing.expectEqual(closed_progress_len, output.written().len); 204 205 // reopening captures the latest source and checks both merge styles 206 try evt.Patch.update(.server, .xit, opts, io, allocator, &repo, &id, .{ .status = .open }, author); 207 var arena = std.heap.ArenaAllocator.init(allocator); 208 defer arena.deinit(); 209 const before = (try evt.readFromRepo(evt.Patch, .xit, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 210 const revision = before.event.revision orelse return error.NotFound; 211 try std.testing.expectEqualStrings(&next, revision.source_oid); 212 { 213 var moment = try repo.core.latestMoment(); 214 const result = try pch.readMergeability(opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, &id, before.event); 215 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .clean, .squash = .clean }, result); 216 } 217 218 // a diverged target produces a merge commit in both merge styles 219 { 220 const file = try repo.core.work_dir.createFile(io, "target.txt", .{}); 221 defer file.close(io); 222 try file.writeStreamingAll(io, "target only\n"); 223 } 224 try repo.add(io, allocator, &.{"target.txt"}); 225 const target_oid = try repo.commit(io, allocator, .{ .message = "advance target" }); 226 pch.refreshMergeability(opts, io, allocator, &repo, id); 227 228 // both merge styles use the stored revision, without fork data 229 try pch.merge(opts, io, allocator, path, &repo, .{ .id = id_hex, .revision = selection, .author = author, .timestamp = 100 }); 230 const merged = (try evt.readFromRepo(evt.Patch, .xit, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 231 const merged_status = switch (merged.event.status) { 232 .merged => |value| value, 233 else => return error.NotMerged, 234 }; 235 const result_oid = (try repo.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 236 try std.testing.expectEqual(selection, merged_status.revision); 237 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 238 const result = (try evt.readFromRepo(evt.PatchRev, .xit, opts, io, allocator, &arena, &repo, &result_id)) orelse return error.NotFound; 239 try std.testing.expectEqualStrings(&target_oid, result.event.base_oid); 240 try std.testing.expectEqualStrings(&result_oid, result.event.source_oid); 241 try std.testing.expect(!std.mem.eql(u8, &revision.id, &merged_status.patchrev_id)); 242 try std.testing.expectEqualDeep(before.event.revision, merged.event.revision); 243 { 244 var commits = try repo.log(io, allocator, .{ .start_oids = &.{result_oid} }); 245 defer commits.deinit(); 246 const commit = (try commits.next(allocator)) orelse return error.NotFound; 247 defer commit.deinit(); 248 const parents = commit.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 249 try std.testing.expectEqual(2, parents.len); 250 try std.testing.expectEqualStrings(&target_oid, &parents[0]); 251 try std.testing.expectEqualStrings(if (selection == .source) revision.source_oid else revision.squash_oid, &parents[1]); 252 } 253 254 // the source survives, and merged patches no longer follow it 255 _ = try repo.commitAtRef(io, allocator, .{ .message = "later" }, null, .{ .kind = .head, .name = "feature" }); 256 const tip = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 257 const progress_len = output.written().len; 258 try pch.refreshBranches(.server, .xit, opts, io, allocator, &repo, null, &progress); 259 try std.testing.expectEqual(progress_len, output.written().len); 260 try std.testing.expectEqualStrings(&tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 261 262 // retain the exact merge diff after its original commits are collected 263 const diff_route = (try ui.Repo.Patches.diffRoute("", .{ .id = &id_hex, .record = merged })) orelse return error.BadRoute; 264 const diff_before = try ui.Repo.Diff.init(.xit, opts, &arena, &repo, io, allocator, diff_route.repo_diff); 265 try std.testing.expectEqual(1, diff_before.window.hunks.len); 266 try std.testing.expectEqualStrings("base.txt", diff_before.window.hunks[0].path orelse return error.MissingPath); 267 _ = try repo.commitAtRef(io, allocator, .{ .message = "rewrite history", .parent_oids = &.{} }, null, .{ .kind = .head, .name = "master" }); 268 try repo.removeBranch(io, .{ .name = "feature" }); 269 _ = try repo.garbageCollect(io, allocator, .{}); 270 { 271 var moment = try repo.core.latestMoment(); 272 const state = rp.Repo(.xit, opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 273 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, opts).initCommit(state, io, allocator, &result_oid)); 274 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, opts).initCommit(state, io, allocator, &target_oid)); 275 } 276 const diff_after = try ui.Repo.Diff.init(.xit, opts, &arena, &repo, io, allocator, diff_route.repo_diff); 277 try std.testing.expectEqualDeep(diff_before.window, diff_after.window); 278 const files = try ui.Repo.Files.initPatchRev(.xit, opts, &arena, &repo, io, allocator, .{ .repo = "" }, &merged_status.patchrev_id, "base.txt", 0); 279 try std.testing.expectEqual(2, files.entries.len); 280 try std.testing.expectEqualStrings("base.txt", files.entries[0].name); 281 try std.testing.expectEqualStrings("source", files.entries[0].lines[0]); 282 const files_route = files.filesRoute("base.txt", 0) orelse return error.BadRoute; 283 try std.testing.expectEqualStrings(&merged_status.patchrev_id, files_route.repo_files.patchrev_id.slice()); 284 285 // opening either tab must not load a commit log from the pruned history 286 var session = ui.Session{ .io = io, .arena = &arena, .page_arena = &arena, .local = .{ .path = path, .repo_kind = .xit }, .data = .{ .host_kind = .local } }; 287 for ([_]ui.RoutablePage{ diff_route, files_route }) |route| { 288 session.data.current_page = route; 289 const page = try ui.Repo.init(&arena, &session, route); 290 const diff = switch (page.changes) { 291 .diff => |diff| diff, 292 .commits => return error.MissingDiff, 293 }; 294 try std.testing.expectEqualDeep(diff_before.window, diff.window); 295 try std.testing.expectEqualStrings(&merged_status.patchrev_id, page.files.patchrev_id orelse return error.MissingRevision); 296 } 297 } 298 299 fn commitTree( 300 io: std.Io, 301 allocator: std.mem.Allocator, 302 repo: *Repo, 303 oid: *const [hash.hexLen(repo_opts.hash)]u8, 304 ) ![hash.hexLen(repo_opts.hash)]u8 { 305 var moment = try repo.core.latestMoment(); 306 const state = Repo.State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 307 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 308 defer object.deinit(); 309 return switch (object.content) { 310 .commit => |commit| commit.tree, 311 else => error.InvalidObject, 312 }; 313 } 314 315 fn consumePatchWithRevision( 316 comptime role: evt.RepoRole, 317 io: std.Io, 318 allocator: std.mem.Allocator, 319 repo: *Repo, 320 ref: rf.Ref, 321 revision_id: [evt.event_id_size]u8, 322 revision: evt.PatchRev, 323 tree_entries: []const evt.EventTreeEntry, 324 revision_timestamp: u64, 325 patch_id: [evt.event_id_size]u8, 326 patch_value: evt.Patch, 327 patch_timestamp: u64, 328 ) !void { 329 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 330 .id = std.fmt.bytesToHex(revision_id, .lower), 331 .timestamp = revision_timestamp, 332 .author = author, 333 .tree_entries = tree_entries, 334 .event = .{ .patchrev = revision }, 335 }}); 336 337 var arena = std.heap.ArenaAllocator.init(allocator); 338 defer arena.deinit(); 339 const moment = try evt.currentMoment(repo_opts, repo); 340 const record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &revision_id)) orelse return error.NotFound; 341 var patch = patch_value; 342 patch.revision = evt.Patch.Revision.fromRecord(revision_id, record); 343 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 344 .id = std.fmt.bytesToHex(patch_id, .lower), 345 .timestamp = patch_timestamp, 346 .author = author, 347 .event = .{ .patch = patch }, 348 }}); 349 } 350 351 test "patch event conflicts, stacking, and gc" { 352 const io = std.testing.io; 353 const allocator = std.testing.allocator; 354 355 // create the temp dir 356 var temp = std.testing.tmpDir(.{}); 357 defer temp.cleanup(); 358 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 359 defer allocator.free(temp_path); 360 361 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 362 defer allocator.free(upstream_path); 363 const target_path = try std.fs.path.join(allocator, &.{ temp_path, "target" }); 364 defer allocator.free(target_path); 365 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 366 defer allocator.free(source_path); 367 368 // 369 // create the upstream repo and its base commit 370 // 371 372 const base_oid = blk: { 373 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 374 defer upstream.deinit(io, allocator); 375 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 376 defer file.close(io); 377 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 378 try upstream.add(io, allocator, &.{"main.zig"}); 379 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 380 }; 381 382 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 383 defer target.deinit(io, allocator); 384 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 385 386 // 387 // fork the target and commit the proposed change 388 // 389 390 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 391 defer source.deinit(io, allocator); 392 393 const source_oid = blk: { 394 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 395 defer file.close(io); 396 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 397 try source.add(io, allocator, &.{"feature.zig"}); 398 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 399 }; 400 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 401 402 // 403 // copy the proposed objects without creating a target ref 404 // 405 406 { 407 var source_moment = try source.core.latestMoment(); 408 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 409 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 410 defer objects.deinit(); 411 try objects.include(&source_oid); 412 try target.copyObjects(.xit, repo_opts, &objects, io, null); 413 } 414 415 // 416 // define the patch revision and patch 417 // 418 419 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 420 const patchrev_id = evt.EventWithId.randomId(prng.random()); 421 const patch_id = evt.EventWithId.randomId(prng.random()); 422 const patch = evt.Patch{ 423 .title = "add the answer", 424 .description = "adds a reusable answer constant", 425 .tags = "enhancement", 426 .target_branch = "master", 427 }; 428 const tree_entries = [_]evt.EventTreeEntry{ 429 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 430 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 431 }; 432 433 // reject invalid branches even when the event bypasses patch creation 434 { 435 const cases = [_]struct { source: ?[]const u8, target: []const u8, expected_error: anyerror }{ 436 .{ .source = "master", .target = "master", .expected_error = error.SameBranch }, 437 .{ .source = null, .target = evt.events_ref.name, .expected_error = error.InvalidTargetBranch }, 438 }; 439 for (cases) |case| { 440 var invalid = patch; 441 invalid.source_branch = case.source; 442 invalid.target_branch = case.target; 443 try std.testing.expectError(case.expected_error, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 444 .id = std.fmt.bytesToHex(patch_id, .lower), 445 .timestamp = 3, 446 .author = author, 447 .event = .{ .patch = invalid }, 448 }})); 449 try std.testing.expectEqual(null, try target.readRef(io, evt.events_ref)); 450 } 451 } 452 453 // 454 // consume the patch revision and patch 455 // 456 457 { 458 try consumePatchWithRevision( 459 .repo, 460 io, 461 allocator, 462 &target, 463 evt.events_ref, 464 patchrev_id, 465 .{ .base_oid = &base_oid, .source_oid = &source_oid, .message = patch.title }, 466 &tree_entries, 467 3, 468 patch_id, 469 patch, 470 4, 471 ); 472 } 473 474 // 475 // check the records and squash commit 476 // 477 478 var arena = std.heap.ArenaAllocator.init(allocator); 479 defer arena.deinit(); 480 const moment = try evt.currentMoment(repo_opts, &target); 481 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 482 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 483 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 484 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 485 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 486 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 487 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 488 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 489 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 490 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 491 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 492 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 493 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 494 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 495 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 496 497 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 498 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 499 { 500 var repo_moment = try target.core.latestMoment(); 501 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 502 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 503 defer patch_object.deinit(); 504 switch (patch_object.content) { 505 .commit => |commit| { 506 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 507 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 508 try std.testing.expectEqual(1, parents.len); 509 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 510 }, 511 else => return error.InvalidObject, 512 } 513 } 514 515 // 516 // update metadata without creating another patch revision 517 // 518 519 var updated_patch = patch; 520 updated_patch.revision = .{ 521 .id = std.fmt.bytesToHex(patchrev_id, .lower), 522 .squash_oid = &initial_patch_oid, 523 .source_oid = &source_oid, 524 }; 525 updated_patch.title = "add a reusable answer"; 526 { 527 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 528 .id = std.fmt.bytesToHex(patch_id, .lower), 529 .timestamp = 5, 530 .author = author, 531 .event = .{ .patch = updated_patch }, 532 }}); 533 } 534 535 _ = arena.reset(.retain_capacity); 536 const updated_moment = try evt.currentMoment(repo_opts, &target); 537 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 538 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 539 540 // 541 // branch the events history before creating competing revisions 542 // 543 544 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 545 { 546 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 547 defer result.deinit(); 548 try target.addBranch(io, .{ .name = side_events_ref.name }); 549 } 550 551 // 552 // create two competing code revisions 553 // 554 555 const source_a_oid = blk: { 556 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 557 defer file.close(io); 558 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 559 try source.add(io, allocator, &.{"feature.zig"}); 560 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 561 }; 562 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 563 const source_b_oid = blk: { 564 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 565 defer file.close(io); 566 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 567 try source.add(io, allocator, &.{"feature.zig"}); 568 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 569 }; 570 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 571 { 572 var source_moment = try source.core.latestMoment(); 573 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 574 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 575 defer objects.deinit(); 576 try objects.include(&source_b_oid); 577 try target.copyObjects(.xit, repo_opts, &objects, io, null); 578 } 579 580 // 581 // select the first revision on the events branch 582 // 583 584 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 585 const target_entries = [_]evt.EventTreeEntry{ 586 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 587 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 588 }; 589 var target_patch = updated_patch; 590 target_patch.status = .closed; 591 { 592 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 593 .base_oid = &base_oid, 594 .source_oid = &source_a_oid, 595 .message = updated_patch.title, 596 }, &target_entries, 8, patch_id, target_patch, 9); 597 } 598 599 // 600 // select the second revision on the side branch 601 // 602 603 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 604 const parent_entries = [_]evt.EventTreeEntry{ 605 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 606 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 607 }; 608 var parent_patch = updated_patch; 609 parent_patch.status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(patchrev_b_id, .lower) } }; 610 { 611 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 612 .base_oid = &base_oid, 613 .source_oid = &source_b_oid, 614 .message = updated_patch.title, 615 }, &parent_entries, 10, patch_id, parent_patch, 11); 616 } 617 618 // 619 // merge the competing patch revisions 620 // 621 622 { 623 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 624 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 625 } 626 627 // 628 // check the selected revision and conflict 629 // 630 631 _ = arena.reset(.retain_capacity); 632 const merged_moment = try evt.currentMoment(repo_opts, &target); 633 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 634 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 635 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 636 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 637 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 638 639 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 640 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 641 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 642 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 643 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 644 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 645 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 646 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 647 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 648 649 // 650 // create a patch stacked on the selected revision 651 // 652 653 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 654 const child_id = evt.EventWithId.randomId(prng.random()); 655 const child_entries = [_]evt.EventTreeEntry{ 656 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 657 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 658 }; 659 { 660 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 661 .base_oid = merged_patchrev.patch_oid, 662 .source_oid = &source_b_oid, 663 .message = "stack another answer change", 664 }, &child_entries, 12, child_id, .{ 665 .title = "stack another answer change", 666 .description = "depends on the first patch", 667 .tags = "enhancement", 668 .target_branch = "master", 669 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 670 }, 13); 671 } 672 673 // 674 // check the squash commit and reverse index 675 // 676 677 _ = arena.reset(.retain_capacity); 678 const stacked_moment = try evt.currentMoment(repo_opts, &target); 679 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 680 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 681 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 682 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 683 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 684 @memcpy(&child_oid, child_patchrev.patch_oid); 685 { 686 var child_repo_moment = try target.core.latestMoment(); 687 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 688 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 689 defer child_object.deinit(); 690 const child_commit = switch (child_object.content) { 691 .commit => |commit| commit, 692 else => return error.InvalidObject, 693 }; 694 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 695 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 696 } 697 698 const targets_cursor = try stacked_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.target_patch_id_to_patch_id_set_key)) orelse return error.NotFound; 699 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 700 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 701 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 702 try std.testing.expectEqual(1, try children.count()); 703 704 // 705 // close and merge the same revision concurrently 706 // 707 708 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 709 { 710 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 711 defer result.deinit(); 712 try target.addBranch(io, .{ .name = status_side_ref.name }); 713 } 714 var closed_child = child.event; 715 closed_child.status = .closed; 716 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 717 .id = std.fmt.bytesToHex(child_id, .lower), 718 .timestamp = 14, 719 .author = author, 720 .event = .{ .patch = closed_child }, 721 }}); 722 var merged_child = child.event; 723 merged_child.status = .{ .merged = .{ .revision = .source, .patchrev_id = (child.event.revision orelse return error.NotFound).id } }; 724 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 725 .id = std.fmt.bytesToHex(child_id, .lower), 726 .timestamp = 15, 727 .author = author, 728 .event = .{ .patch = merged_child }, 729 }}); 730 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 731 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 732 733 _ = arena.reset(.retain_capacity); 734 const status_moment = try evt.currentMoment(repo_opts, &target); 735 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 736 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 737 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 738 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 739 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 740 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 741 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 742 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 743 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 744 try std.testing.expectEqual(null, child_conflict); 745 746 var reopened_child = merged_child_record.event; 747 reopened_child.status = .open; 748 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 749 .id = std.fmt.bytesToHex(child_id, .lower), 750 .timestamp = 16, 751 .author = author, 752 .event = .{ .patch = reopened_child }, 753 }})); 754 755 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 756 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 757 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 758 .timestamp = 16, 759 .author = author, 760 .event = .{ .patch = .{ 761 .title = "invalid merged patch", 762 .description = "has no revision", 763 .tags = "enhancement", 764 .target_branch = "master", 765 .status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(invalid_merged_id, .lower) } }, 766 } }, 767 }})); 768 769 // 770 // retain the ref-free commits during garbage collection 771 // 772 773 { 774 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 775 defer allocator.free(roots); 776 try std.testing.expectEqual(4, roots.len); 777 _ = try target.garbageCollect(io, allocator, .{ .extra_roots = roots }); 778 779 var after_gc_moment = try target.core.latestMoment(); 780 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 781 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 782 retained.deinit(); 783 } 784 } 785 786 test "patch lifecycle" { 787 try patchLifecycle(.squash); 788 try patchLifecycle(.source); 789 } 790 791 fn patchLifecycle(merge_revision: evt.Patch.MergeRevision) !void { 792 const io = std.testing.io; 793 const allocator = std.testing.allocator; 794 795 var temp = std.testing.tmpDir(.{}); 796 defer temp.cleanup(); 797 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 798 defer allocator.free(temp_path); 799 800 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 801 defer allocator.free(repos_dir); 802 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 803 defer allocator.free(admin_path); 804 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 805 defer allocator.free(upstream_path); 806 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 807 defer allocator.free(source_path); 808 809 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 810 const user_id = evt.EventWithId.randomId(prng.random()); 811 const repo_id = evt.EventWithId.randomId(prng.random()); 812 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 813 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 814 defer allocator.free(target_path); 815 816 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 817 defer admin.deinit(io, allocator); 818 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 819 .{ 820 .id = std.fmt.bytesToHex(user_id, .lower), 821 .timestamp = 1, 822 .author = author, 823 .event = .{ .user = .{ 824 .name = author.name, 825 .email = author.email, 826 .password_hash = "", 827 } }, 828 }, 829 .{ 830 .id = repo_id_hex, 831 .timestamp = 1, 832 .author = author, 833 .event = .{ .repo = .{ 834 .user_id = &user_id, 835 .name = "repo", 836 .description = "", 837 } }, 838 }, 839 }); 840 841 // 842 // create the target repo 843 // 844 845 { 846 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 847 defer upstream.deinit(io, allocator); 848 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 849 defer file.close(io); 850 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 851 try upstream.add(io, allocator, &.{"main.zig"}); 852 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 853 } 854 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 855 defer target.deinit(io, allocator); 856 857 // 858 // create the patch draft in the fork 859 // 860 861 const patch_id = evt.EventWithId.randomId(prng.random()); 862 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 863 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 864 .target_branch = "master", 865 .id = patch_id_hex, 866 .user_id = user_id, 867 .repo_id = repo_id, 868 .title = "add the answer", 869 .tags = "enhancement", 870 .description = "adds a reusable answer constant", 871 .author = author, 872 .timestamp = 2, 873 }); 874 defer allocator.free(draft_path); 875 876 // 877 // create and record the proposed revision in the fork 878 // 879 880 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 881 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 882 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 883 defer source.deinit(io, allocator); 884 const source_oid = blk: { 885 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 886 defer file.close(io); 887 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 888 try source.add(io, allocator, &.{"feature.zig"}); 889 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 890 }; 891 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 892 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 893 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 894 const first_entries = [_]evt.EventTreeEntry{ 895 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 896 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 897 }; 898 { 899 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 900 defer draft.deinit(io, allocator); 901 var arena = std.heap.ArenaAllocator.init(allocator); 902 defer arena.deinit(); 903 904 const initial_moment = try evt.currentMoment(repo_opts, &draft); 905 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 906 try std.testing.expect(!initial_patch.removed); 907 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 908 try std.testing.expectEqual(null, initial_patch.event.revision); 909 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 910 911 var source_moment = try source.core.latestMoment(); 912 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 913 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 914 defer objects.deinit(); 915 try objects.include(&source_oid); 916 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 917 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 918 .base_oid = &base_oid, 919 .source_oid = &source_oid, 920 .message = "add the answer", 921 }, &first_entries, 3, patch_id, .{ 922 .title = "add the answer", 923 .tags = "enhancement", 924 .description = "adds a reusable answer constant", 925 .target_branch = "master", 926 }, 3); 927 928 _ = arena.reset(.retain_capacity); 929 const moment = try evt.currentMoment(repo_opts, &draft); 930 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 931 try std.testing.expect(!patch.removed); 932 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 933 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 934 } 935 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 936 937 // draft mergeability checks leave the target repo untouched 938 const before_draft_check = (try target.core.latestMoment()).cursor.slot(); 939 pch.refreshMergeability(repo_opts, io, allocator, &target, patch_id); 940 try std.testing.expectEqualDeep(before_draft_check, (try target.core.latestMoment()).cursor.slot()); 941 942 // 943 // publish another draft before its first push 944 // 945 946 const unpushed_id = evt.EventWithId.randomId(prng.random()); 947 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 948 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 949 .id = unpushed_id_hex, 950 .user_id = user_id, 951 .repo_id = repo_id, 952 .title = "explain the answer", 953 .tags = "documentation", 954 .description = "describes the existing answer", 955 .target_branch = "master", 956 .author = author, 957 .timestamp = 3, 958 }); 959 defer allocator.free(unpushed_path); 960 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 961 .id = unpushed_id_hex, 962 .user_id = user_id, 963 .repo_id = repo_id, 964 .author = author, 965 .timestamp = 4, 966 }); 967 { 968 var verify_arena = std.heap.ArenaAllocator.init(allocator); 969 defer verify_arena.deinit(); 970 const target_moment = try evt.currentMoment(repo_opts, &target); 971 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 972 try std.testing.expectEqual(null, unpushed.event.revision); 973 974 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 975 defer unpushed_repo.deinit(io, allocator); 976 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 977 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 978 try std.testing.expect(removed.removed); 979 } 980 981 // 982 // edit the patch metadata in the fork 983 // 984 985 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 986 .id = patch_id_hex, 987 .user_id = user_id, 988 .repo_id = repo_id, 989 .title = "explain the answer", 990 .tags = "enhancement documentation", 991 .description = "adds and explains a reusable answer constant", 992 .target_branch = "master", 993 .author = author, 994 .timestamp = 4, 995 })); 996 997 // 998 // publish the metadata and revision pointer 999 // 1000 1001 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1002 .id = patch_id_hex, 1003 .user_id = user_id, 1004 .repo_id = repo_id, 1005 .author = author, 1006 .timestamp = 5, 1007 }); 1008 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1009 .id = patch_id_hex, 1010 .user_id = user_id, 1011 .repo_id = repo_id, 1012 .author = author, 1013 .timestamp = 6, 1014 }); 1015 1016 var arena = std.heap.ArenaAllocator.init(allocator); 1017 defer arena.deinit(); 1018 const moment = try evt.currentMoment(repo_opts, &target); 1019 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1020 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1021 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1022 try std.testing.expectEqual(.open, patch.event.status); 1023 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1024 1025 _ = arena.reset(.retain_capacity); 1026 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 1027 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 1028 try std.testing.expect(!published_fork.removed); 1029 try std.testing.expectEqual(.publish, published_fork.event.stage); 1030 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 1031 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 1032 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 1033 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1034 { 1035 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1036 defer draft.deinit(io, allocator); 1037 _ = arena.reset(.retain_capacity); 1038 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1039 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1040 try std.testing.expect(removed.removed); 1041 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1042 @memcpy(&squash_oid, revision.patch_oid); 1043 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1044 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1045 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1046 @memcpy(&event_oid, revision.event_oid); 1047 var repo_moment = try draft.core.latestMoment(); 1048 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1049 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1050 defer event_object.deinit(); 1051 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1052 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1053 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1054 } 1055 1056 // 1057 // merge the selected revision into the target 1058 // 1059 1060 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1061 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 1062 .id = patch_id_hex, 1063 .revision = merge_revision, 1064 .author = author, 1065 .timestamp = 5, 1066 })); 1067 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1068 1069 // run the cache scenario once; both lifecycle runs still perform a merge 1070 if (merge_revision == .source) { 1071 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 1072 } 1073 1074 const selected_oid = switch (merge_revision) { 1075 .squash => squash_oid, 1076 .source => source_oid, 1077 }; 1078 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 1079 .id = patch_id_hex, 1080 .revision = merge_revision, 1081 .author = author, 1082 .timestamp = 5, 1083 }); 1084 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1085 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1086 1087 _ = arena.reset(.retain_capacity); 1088 const merged_moment = try evt.currentMoment(repo_opts, &target); 1089 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1090 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1091 const merged_status = switch (merged.event.status) { 1092 .merged => |value| value, 1093 else => return error.InvalidPatch, 1094 }; 1095 try std.testing.expectEqual(merge_revision, merged_status.revision); 1096 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1097 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1098 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1099 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1100 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1101 1102 _ = arena.reset(.retain_capacity); 1103 const removed_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, try evt.currentMoment(evt.admin_repo_opts, &admin), &arena, &patch_id)) orelse return error.NotFound; 1104 try std.testing.expect(removed_fork.removed); 1105 } 1106 1107 fn indexedForkCount( 1108 moment: evt.AdminDB.HashMap(.read_only), 1109 index_key: []const u8, 1110 parent_id: []const u8, 1111 ) !u64 { 1112 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 1113 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 1114 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 1115 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 1116 return try ids.count(); 1117 } 1118 1119 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1120 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1121 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1122 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1123 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1124 return try ids.count(); 1125 } 1126 1127 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 1128 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 1129 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 1130 var key_buffer: evt.Patch.TagStatusKey = undefined; 1131 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 1132 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1133 return try ids.count(); 1134 } 1135 1136 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1137 var arena = std.heap.ArenaAllocator.init(allocator); 1138 defer arena.deinit(); 1139 var moment = try repo.core.latestMoment(); 1140 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1141 } 1142 1143 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1144 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1145 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1146 const checks = try Repo.DB.HashMap(.read_only).init((try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))) orelse return error.NotFound); 1147 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1148 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1149 for ([_][]const u8{ "source", "squash" }) |field| { 1150 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1151 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1152 } 1153 } 1154 1155 fn testMergeability( 1156 target: *Repo, 1157 io: std.Io, 1158 allocator: std.mem.Allocator, 1159 repos_dir: []const u8, 1160 id: *const [evt.event_id_size]u8, 1161 base: *const [hash.hexLen(repo_opts.hash)]u8, 1162 source: *const [hash.hexLen(repo_opts.hash)]u8, 1163 ) !void { 1164 var arena = std.heap.ArenaAllocator.init(allocator); 1165 defer arena.deinit(); 1166 const events = try evt.currentMoment(repo_opts, target); 1167 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1168 const patch = record.event; 1169 1170 // publication checks both methods, and reads and cache hits do no work 1171 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1172 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1173 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1174 const before = (try target.core.latestMoment()).cursor.slot(); 1175 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1176 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1177 { 1178 var moment = try target.core.latestMoment(); 1179 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1180 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1181 } 1182 1183 // a target update invalidates the old result before refresh 1184 { 1185 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1186 defer file.close(io); 1187 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1188 } 1189 try target.add(io, allocator, &.{"feature.zig"}); 1190 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1191 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1192 var squash_input = input; 1193 squash_input.revision = .squash; 1194 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1195 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1196 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1197 1198 // local edits and server comments leave checks alone 1199 var edit = evt.Patch.Update{ .fields = .{ 1200 .title = "edit the answer", 1201 .tags = patch.tags, 1202 .description = patch.description, 1203 .target_branch = patch.target_branch, 1204 } }; 1205 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1206 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1207 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1208 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1209 1210 // a server patch edit refreshes its mergeability 1211 edit.fields.title = patch.title; 1212 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1213 const events_before = try target.readRef(io, evt.events_ref); 1214 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1215 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1216 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1217 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1218 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1219 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1220 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1221 1222 // deleting and restoring the target cannot reuse the conflict result 1223 { 1224 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1225 defer detached.deinit(); 1226 } 1227 try target.removeBranch(io, .{ .name = "master" }); 1228 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1229 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1230 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1231 try target.addBranch(io, .{ .name = "master" }); 1232 { 1233 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1234 defer restored.deinit(); 1235 } 1236 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1237 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1238 1239 // a missing fork disables merging without changing the accepted events 1240 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1241 defer allocator.free(draft_path); 1242 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1243 defer allocator.free(away_path); 1244 { 1245 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1246 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1247 { 1248 const log_level = std.testing.log_level; 1249 std.testing.log_level = .err; 1250 defer std.testing.log_level = log_level; 1251 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1252 } 1253 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1254 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1255 } 1256 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1257 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1258 1259 // a changed revision is unknown even if its target has not moved 1260 var changed = patch; 1261 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1262 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1263 } 1264