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