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