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