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