╭ 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 10 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 11 const Repo = rp.Repo(.xit, repo_opts); 12 const author = evt.CommitAuthor{ .name = "alice", .email = "alice@example.test" }; 13 14 fn commitTree( 15 io: std.Io, 16 allocator: std.mem.Allocator, 17 repo: *Repo, 18 oid: *const [hash.hexLen(repo_opts.hash)]u8, 19 ) ![hash.hexLen(repo_opts.hash)]u8 { 20 var moment = try repo.core.latestMoment(); 21 const state = Repo.State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 22 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 23 defer object.deinit(); 24 return switch (object.content) { 25 .commit => |commit| commit.tree, 26 else => error.InvalidObject, 27 }; 28 } 29 30 test "patch event conflicts, stacking, and gc" { 31 const io = std.testing.io; 32 const allocator = std.testing.allocator; 33 const temp_dir_name = "temp-event-patch"; 34 const cwd = std.Io.Dir.cwd(); 35 36 // create the temp dir 37 if (cwd.openDir(io, temp_dir_name, .{})) |dir_value| { 38 var dir = dir_value; 39 dir.close(io); 40 try cwd.deleteTree(io, temp_dir_name); 41 } else |_| {} 42 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 43 defer cwd.deleteTree(io, temp_dir_name) catch {}; 44 defer temp_dir.close(io); 45 46 const cwd_path = try std.process.currentPathAlloc(io, allocator); 47 defer allocator.free(cwd_path); 48 const upstream_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "upstream" }); 49 defer allocator.free(upstream_path); 50 const target_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "target" }); 51 defer allocator.free(target_path); 52 const source_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "source" }); 53 defer allocator.free(source_path); 54 55 // 56 // create the upstream repo and its base commit 57 // 58 59 const base_oid = blk: { 60 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 61 defer upstream.deinit(io, allocator); 62 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 63 defer file.close(io); 64 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 65 try upstream.add(io, allocator, &.{"main.zig"}); 66 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 67 }; 68 69 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 70 defer target.deinit(io, allocator); 71 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 72 73 // 74 // fork the target and commit the proposed change 75 // 76 77 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 78 defer source.deinit(io, allocator); 79 80 const source_oid = blk: { 81 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 82 defer file.close(io); 83 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 84 try source.add(io, allocator, &.{"feature.zig"}); 85 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 86 }; 87 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 88 89 // 90 // copy the proposed objects without creating a target ref 91 // 92 93 { 94 var source_moment = try source.core.latestMoment(); 95 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 96 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 97 defer objects.deinit(); 98 try objects.include(&source_oid); 99 try target.copyObjects(.xit, repo_opts, &objects, io, null); 100 } 101 102 // 103 // define the patch revision and patch 104 // 105 106 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 107 const patchrev_id = evt.EventWithId.randomId(prng.random()); 108 const patch_id = evt.EventWithId.randomId(prng.random()); 109 const patch = evt.Patch{ 110 .title = "add the answer", 111 .description = "adds a reusable answer constant", 112 .tags = "enhancement", 113 .patchrev_id = std.fmt.bytesToHex(patchrev_id, .lower), 114 }; 115 const tree_entries = [_]evt.EventTreeEntry{ 116 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 117 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 118 }; 119 120 // 121 // consume the patch revision and patch 122 // 123 124 { 125 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{ 126 .{ 127 .id = std.fmt.bytesToHex(patchrev_id, .lower), 128 .timestamp = 3, 129 .author = author, 130 .tree_entries = &tree_entries, 131 .event = .{ .patchrev = .{ .base_oid = &base_oid, .source_oid = &source_oid, .target_ref = "refs/heads/master", .message = patch.title } }, 132 }, 133 .{ 134 .id = std.fmt.bytesToHex(patch_id, .lower), 135 .timestamp = 4, 136 .author = author, 137 .event = .{ .patch = patch }, 138 }, 139 }); 140 } 141 142 // 143 // check the records and derived commit 144 // 145 146 var arena = std.heap.ArenaAllocator.init(allocator); 147 defer arena.deinit(); 148 const moment = try evt.currentMoment(repo_opts, &target); 149 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 150 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 151 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 152 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 153 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.patchrev_id orelse return error.NotFound)); 154 155 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 156 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 157 { 158 var repo_moment = try target.core.latestMoment(); 159 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 160 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 161 defer patch_object.deinit(); 162 switch (patch_object.content) { 163 .commit => |commit| { 164 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 165 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 166 try std.testing.expectEqual(1, parents.len); 167 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 168 }, 169 else => return error.InvalidObject, 170 } 171 } 172 173 // 174 // update metadata without creating another patch revision 175 // 176 177 var updated_patch = patch; 178 updated_patch.title = "add a reusable answer"; 179 { 180 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 181 .id = std.fmt.bytesToHex(patch_id, .lower), 182 .timestamp = 5, 183 .author = author, 184 .event = .{ .patch = updated_patch }, 185 }}); 186 } 187 188 _ = arena.reset(.retain_capacity); 189 const updated_moment = try evt.currentMoment(repo_opts, &target); 190 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 191 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 192 193 // 194 // branch the events history before creating competing revisions 195 // 196 197 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 198 { 199 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 200 defer result.deinit(); 201 try target.addBranch(io, .{ .name = side_events_ref.name }); 202 } 203 204 // 205 // create two competing code revisions 206 // 207 208 const source_a_oid = blk: { 209 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 210 defer file.close(io); 211 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 212 try source.add(io, allocator, &.{"feature.zig"}); 213 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 214 }; 215 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 216 const source_b_oid = blk: { 217 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 218 defer file.close(io); 219 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 220 try source.add(io, allocator, &.{"feature.zig"}); 221 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 222 }; 223 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 224 { 225 var source_moment = try source.core.latestMoment(); 226 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 227 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 228 defer objects.deinit(); 229 try objects.include(&source_b_oid); 230 try target.copyObjects(.xit, repo_opts, &objects, io, null); 231 } 232 233 // 234 // select the first revision on the events branch 235 // 236 237 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 238 const target_entries = [_]evt.EventTreeEntry{ 239 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 240 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 241 }; 242 var target_patch = updated_patch; 243 target_patch.patchrev_id = std.fmt.bytesToHex(patchrev_a_id, .lower); 244 target_patch.status = .closed; 245 { 246 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{ 247 .{ 248 .id = std.fmt.bytesToHex(patchrev_a_id, .lower), 249 .timestamp = 8, 250 .author = author, 251 .tree_entries = &target_entries, 252 .event = .{ .patchrev = .{ .base_oid = &base_oid, .source_oid = &source_a_oid, .target_ref = "refs/heads/master", .message = updated_patch.title } }, 253 }, 254 .{ 255 .id = std.fmt.bytesToHex(patch_id, .lower), 256 .timestamp = 9, 257 .author = author, 258 .event = .{ .patch = target_patch }, 259 }, 260 }); 261 } 262 263 // 264 // select the second revision on the side branch 265 // 266 267 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 268 const parent_entries = [_]evt.EventTreeEntry{ 269 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 270 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 271 }; 272 var parent_patch = updated_patch; 273 parent_patch.patchrev_id = std.fmt.bytesToHex(patchrev_b_id, .lower); 274 parent_patch.status = .merged; 275 { 276 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, side_events_ref, &.{ 277 .{ 278 .id = std.fmt.bytesToHex(patchrev_b_id, .lower), 279 .timestamp = 10, 280 .author = author, 281 .tree_entries = &parent_entries, 282 .event = .{ .patchrev = .{ .base_oid = &base_oid, .source_oid = &source_b_oid, .target_ref = "refs/heads/master", .message = updated_patch.title } }, 283 }, 284 .{ 285 .id = std.fmt.bytesToHex(patch_id, .lower), 286 .timestamp = 11, 287 .author = author, 288 .event = .{ .patch = parent_patch }, 289 }, 290 }); 291 } 292 293 // 294 // merge the competing patch revisions 295 // 296 297 { 298 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 299 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 300 } 301 302 // 303 // check the selected revision and conflict 304 // 305 306 _ = arena.reset(.retain_capacity); 307 const merged_moment = try evt.currentMoment(repo_opts, &target); 308 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 309 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.patchrev_id orelse return error.NotFound)); 310 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 311 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 312 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 313 314 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 315 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 316 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 317 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 318 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 319 try std.testing.expectEqualStrings("patchrev_id status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 320 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 321 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 322 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.patchrev_id orelse return error.NotFound)); 323 324 // 325 // create a patch stacked on the selected revision 326 // 327 328 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 329 const child_id = evt.EventWithId.randomId(prng.random()); 330 const child_entries = [_]evt.EventTreeEntry{ 331 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 332 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 333 }; 334 { 335 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{ 336 .{ 337 .id = std.fmt.bytesToHex(child_patchrev_id, .lower), 338 .timestamp = 12, 339 .author = author, 340 .tree_entries = &child_entries, 341 .event = .{ .patchrev = .{ .base_oid = merged_patchrev.patch_oid, .source_oid = &source_b_oid, .target_ref = "refs/heads/master", .message = "stack another answer change" } }, 342 }, 343 .{ 344 .id = std.fmt.bytesToHex(child_id, .lower), 345 .timestamp = 13, 346 .author = author, 347 .event = .{ .patch = .{ 348 .title = "stack another answer change", 349 .description = "depends on the first patch", 350 .tags = "enhancement", 351 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 352 .patchrev_id = std.fmt.bytesToHex(child_patchrev_id, .lower), 353 } }, 354 }, 355 }); 356 } 357 358 // 359 // check the derived commit and reverse index 360 // 361 362 _ = arena.reset(.retain_capacity); 363 const stacked_moment = try evt.currentMoment(repo_opts, &target); 364 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 365 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 366 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.patchrev_id orelse return error.NotFound)); 367 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 368 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 369 @memcpy(&child_oid, child_patchrev.patch_oid); 370 { 371 var child_repo_moment = try target.core.latestMoment(); 372 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 373 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 374 defer child_object.deinit(); 375 const child_commit = switch (child_object.content) { 376 .commit => |commit| commit, 377 else => return error.InvalidObject, 378 }; 379 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 380 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 381 } 382 383 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; 384 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 385 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 386 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 387 try std.testing.expectEqual(1, try children.count()); 388 389 // 390 // close and merge the same revision concurrently 391 // 392 393 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 394 { 395 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 396 defer result.deinit(); 397 try target.addBranch(io, .{ .name = status_side_ref.name }); 398 } 399 var closed_child = child.event; 400 closed_child.status = .closed; 401 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 402 .id = std.fmt.bytesToHex(child_id, .lower), 403 .timestamp = 14, 404 .author = author, 405 .event = .{ .patch = closed_child }, 406 }}); 407 var merged_child = child.event; 408 merged_child.status = .merged; 409 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 410 .id = std.fmt.bytesToHex(child_id, .lower), 411 .timestamp = 15, 412 .author = author, 413 .event = .{ .patch = merged_child }, 414 }}); 415 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 416 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 417 418 _ = arena.reset(.retain_capacity); 419 const status_moment = try evt.currentMoment(repo_opts, &target); 420 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 421 try std.testing.expectEqual(.merged, merged_child_record.event.status); 422 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 423 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 424 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 425 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 426 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 427 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 428 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 429 try std.testing.expectEqual(null, child_conflict); 430 431 var reopened_child = merged_child_record.event; 432 reopened_child.status = .open; 433 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 434 .id = std.fmt.bytesToHex(child_id, .lower), 435 .timestamp = 16, 436 .author = author, 437 .event = .{ .patch = reopened_child }, 438 }})); 439 440 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 441 try std.testing.expectError(error.InvalidPatch, evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 442 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 443 .timestamp = 16, 444 .author = author, 445 .event = .{ .patch = .{ 446 .title = "invalid merged patch", 447 .description = "has no revision", 448 .tags = "enhancement", 449 .status = .merged, 450 } }, 451 }})); 452 453 // 454 // retain the ref-free commits during garbage collection 455 // 456 457 { 458 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 459 defer allocator.free(roots); 460 try std.testing.expectEqual(4, roots.len); 461 _ = try target.garbageCollect(io, allocator, roots); 462 463 var after_gc_moment = try target.core.latestMoment(); 464 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 465 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 466 retained.deinit(); 467 } 468 } 469 470 test "patch lifecycle" { 471 const io = std.testing.io; 472 const allocator = std.testing.allocator; 473 const temp_dir_name = "temp-patch-lifecycle"; 474 const cwd = std.Io.Dir.cwd(); 475 476 if (cwd.openDir(io, temp_dir_name, .{})) |dir_value| { 477 var dir = dir_value; 478 dir.close(io); 479 try cwd.deleteTree(io, temp_dir_name); 480 } else |_| {} 481 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 482 defer cwd.deleteTree(io, temp_dir_name) catch {}; 483 defer temp_dir.close(io); 484 485 const cwd_path = try std.process.currentPathAlloc(io, allocator); 486 defer allocator.free(cwd_path); 487 const root = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 488 defer allocator.free(root); 489 const repos_dir = try std.fs.path.join(allocator, &.{ root, "repos" }); 490 defer allocator.free(repos_dir); 491 const admin_path = try std.fs.path.join(allocator, &.{ root, "admin" }); 492 defer allocator.free(admin_path); 493 const upstream_path = try std.fs.path.join(allocator, &.{ root, "upstream" }); 494 defer allocator.free(upstream_path); 495 const source_path = try std.fs.path.join(allocator, &.{ root, "source" }); 496 defer allocator.free(source_path); 497 498 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 499 const user_id = evt.EventWithId.randomId(prng.random()); 500 const repo_id = evt.EventWithId.randomId(prng.random()); 501 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 502 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 503 defer allocator.free(target_path); 504 505 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 506 defer admin.deinit(io, allocator); 507 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 508 .{ 509 .id = std.fmt.bytesToHex(user_id, .lower), 510 .timestamp = 1, 511 .author = author, 512 .event = .{ .user = .{ 513 .name = author.name, 514 .email = author.email, 515 .password_hash = "", 516 } }, 517 }, 518 .{ 519 .id = repo_id_hex, 520 .timestamp = 1, 521 .author = author, 522 .event = .{ .repo = .{ 523 .user_id = &user_id, 524 .name = "repo", 525 .description = "", 526 } }, 527 }, 528 }); 529 530 // 531 // create the target repo 532 // 533 534 { 535 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 536 defer upstream.deinit(io, allocator); 537 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 538 defer file.close(io); 539 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 540 try upstream.add(io, allocator, &.{"main.zig"}); 541 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 542 } 543 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 544 defer target.deinit(io, allocator); 545 546 // 547 // create the patch draft in the fork 548 // 549 550 const patch_id = evt.EventWithId.randomId(prng.random()); 551 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 552 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 553 .id = patch_id_hex, 554 .user_id = user_id, 555 .repo_id = repo_id, 556 .title = "add the answer", 557 .tags = "enhancement", 558 .description = "adds a reusable answer constant", 559 .author = author, 560 .timestamp = 2, 561 }); 562 defer allocator.free(draft_path); 563 564 // 565 // create and record the proposed revision in the fork 566 // 567 568 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 569 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 570 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 571 defer source.deinit(io, allocator); 572 const source_oid = blk: { 573 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 574 defer file.close(io); 575 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 576 try source.add(io, allocator, &.{"feature.zig"}); 577 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 578 }; 579 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 580 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 581 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 582 const first_entries = [_]evt.EventTreeEntry{ 583 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 584 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 585 }; 586 { 587 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 588 defer draft.deinit(io, allocator); 589 var arena = std.heap.ArenaAllocator.init(allocator); 590 defer arena.deinit(); 591 592 const initial_moment = try evt.currentMoment(repo_opts, &draft); 593 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 594 try std.testing.expect(!initial_patch.removed); 595 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 596 try std.testing.expectEqual(null, initial_patch.event.patchrev_id); 597 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 598 599 var source_moment = try source.core.latestMoment(); 600 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 601 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 602 defer objects.deinit(); 603 try objects.include(&source_oid); 604 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 605 try evt.consume(.fork, .xit, repo_opts, io, allocator, &draft, evt.events_ref, &.{ 606 .{ 607 .id = first_patchrev_hex, 608 .timestamp = 3, 609 .author = author, 610 .tree_entries = &first_entries, 611 .event = .{ .patchrev = .{ 612 .base_oid = &base_oid, 613 .source_oid = &source_oid, 614 .target_ref = "refs/heads/master", 615 .message = "add the answer", 616 } }, 617 }, 618 .{ 619 .id = patch_id_hex, 620 .timestamp = 3, 621 .author = author, 622 .event = .{ .patch = .{ 623 .title = "add the answer", 624 .tags = "enhancement", 625 .description = "adds a reusable answer constant", 626 .patchrev_id = first_patchrev_hex, 627 } }, 628 }, 629 }); 630 631 _ = arena.reset(.retain_capacity); 632 const moment = try evt.currentMoment(repo_opts, &draft); 633 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 634 try std.testing.expect(!patch.removed); 635 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.patchrev_id orelse return error.NotFound)); 636 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 637 } 638 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 639 640 // 641 // publish the metadata and revision pointer 642 // 643 644 try fork.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 645 .id = patch_id_hex, 646 .user_id = user_id, 647 .repo_id = repo_id, 648 .author = author, 649 .timestamp = 3, 650 }); 651 try fork.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 652 .id = patch_id_hex, 653 .user_id = user_id, 654 .repo_id = repo_id, 655 .author = author, 656 .timestamp = 4, 657 }); 658 659 var arena = std.heap.ArenaAllocator.init(allocator); 660 defer arena.deinit(); 661 const moment = try evt.currentMoment(repo_opts, &target); 662 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 663 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.patchrev_id orelse return error.NotFound)); 664 try std.testing.expectEqual(.open, patch.event.status); 665 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 666 667 _ = arena.reset(.retain_capacity); 668 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 669 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 670 try std.testing.expect(!published_fork.removed); 671 try std.testing.expectEqual(.posted, published_fork.event.stage); 672 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 673 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 674 try std.testing.expectEqual(1, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 675 { 676 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 677 defer draft.deinit(io, allocator); 678 _ = arena.reset(.retain_capacity); 679 const draft_moment = try evt.currentMoment(repo_opts, &draft); 680 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 681 try std.testing.expect(removed.removed); 682 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 683 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 684 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 685 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 686 @memcpy(&event_oid, revision.event_oid); 687 var repo_moment = try draft.core.latestMoment(); 688 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 689 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 690 defer event_object.deinit(); 691 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 692 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 693 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 694 } 695 } 696 697 fn indexedForkCount( 698 moment: evt.AdminDB.HashMap(.read_only), 699 index_key: []const u8, 700 parent_id: []const u8, 701 ) !u64 { 702 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 703 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 704 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 705 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 706 return try ids.count(); 707 } 708 709 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.Status) !u64 { 710 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 711 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 712 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 713 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 714 return try ids.count(); 715 } 716 717 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.Status) !u64 { 718 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 719 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 720 var key_buffer: evt.Patch.TagStatusKey = undefined; 721 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 722 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 723 return try ids.count(); 724 } 725