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