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