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