╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
428 { 429 var source_moment = try source.core.latestMoment(); 430 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 431 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 432 defer objects.deinit(); 433 try objects.include(&source_b_oid); 434 try target.copyObjects(.xit, repo_opts, &objects, io, null); 435 } 436 437 // 438 // select the first revision on the events branch 439 // 440 441 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 442 const target_entries = [_]evt.EventTreeEntry{ 443 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 444 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 445 }; 446 var target_patch = updated_patch; 447 target_patch.status = .closed; 448 { 449 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 450 .base_oid = &base_oid, 451 .source_oid = &source_a_oid, 452 .message = updated_patch.title, 453 }, &target_entries, 8, patch_id, target_patch, 9); 454 } 455 456 // 457 // select the second revision on the side branch 458 // 459 460 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 461 const parent_entries = [_]evt.EventTreeEntry{ 462 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 463 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 464 }; 465 var parent_patch = updated_patch; 466 parent_patch.status = .{ .merged = &source_b_oid }; 467 { 468 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 469 .base_oid = &base_oid, 470 .source_oid = &source_b_oid, 471 .message = updated_patch.title, 472 }, &parent_entries, 10, patch_id, parent_patch, 11); 473 } 474 475 // 476 // merge the competing patch revisions 477 // 478 479 { 480 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 481 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 482 } 483 484 // 485 // check the selected revision and conflict 486 // 487 488 _ = arena.reset(.retain_capacity); 489 const merged_moment = try evt.currentMoment(repo_opts, &target); 490 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 491 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 492 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 493 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 494 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 495 496 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 497 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 498 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 499 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 500 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 501 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 502 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 503 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 504 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 505 506 // 507 // create a patch stacked on the selected revision 508 // 509 510 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 511 const child_id = evt.EventWithId.randomId(prng.random()); 512 const child_entries = [_]evt.EventTreeEntry{ 513 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 514 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 515 }; 516 { 517 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 518 .base_oid = merged_patchrev.patch_oid, 519 .source_oid = &source_b_oid, 520 .message = "stack another answer change", 521 }, &child_entries, 12, child_id, .{ 522 .title = "stack another answer change", 523 .description = "depends on the first patch", 524 .tags = "enhancement", 525 .target_branch = "master", 526 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 527 }, 13); 528 } 529 530 // 531 // check the squash commit and reverse index 532 // 533 534 _ = arena.reset(.retain_capacity); 535 const stacked_moment = try evt.currentMoment(repo_opts, &target); 536 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 537 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 538 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 539 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 540 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 541 @memcpy(&child_oid, child_patchrev.patch_oid); 542 { 543 var child_repo_moment = try target.core.latestMoment(); 544 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 545 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 546 defer child_object.deinit(); 547 const child_commit = switch (child_object.content) { 548 .commit => |commit| commit, 549 else => return error.InvalidObject, 550 }; 551 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 552 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 553 } 554 555 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; 556 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 557 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 558 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 559 try std.testing.expectEqual(1, try children.count()); 560 561 // 562 // close and merge the same revision concurrently 563 // 564 565 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 566 { 567 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 568 defer result.deinit(); 569 try target.addBranch(io, .{ .name = status_side_ref.name }); 570 } 571 var closed_child = child.event; 572 closed_child.status = .closed; 573 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 574 .id = std.fmt.bytesToHex(child_id, .lower), 575 .timestamp = 14, 576 .author = author, 577 .event = .{ .patch = closed_child }, 578 }}); 579 var merged_child = child.event; 580 merged_child.status = .{ .merged = (child.event.revision orelse return error.NotFound).source_oid }; 581 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 582 .id = std.fmt.bytesToHex(child_id, .lower), 583 .timestamp = 15, 584 .author = author, 585 .event = .{ .patch = merged_child }, 586 }}); 587 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 588 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 589 590 _ = arena.reset(.retain_capacity); 591 const status_moment = try evt.currentMoment(repo_opts, &target); 592 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 593 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 594 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 595 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 596 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 597 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 598 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 599 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 600 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 601 try std.testing.expectEqual(null, child_conflict); 602 603 var reopened_child = merged_child_record.event; 604 reopened_child.status = .open; 605 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 606 .id = std.fmt.bytesToHex(child_id, .lower), 607 .timestamp = 16, 608 .author = author, 609 .event = .{ .patch = reopened_child }, 610 }})); 611 612 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 613 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 614 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 615 .timestamp = 16, 616 .author = author, 617 .event = .{ .patch = .{ 618 .title = "invalid merged patch", 619 .description = "has no revision", 620 .tags = "enhancement", 621 .target_branch = "master", 622 .status = .{ .merged = &base_oid }, 623 } }, 624 }})); 625 626 // 627 // retain the ref-free commits during garbage collection 628 // 629 630 { 631 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 632 defer allocator.free(roots); 633 try std.testing.expectEqual(4, roots.len); 634 _ = try target.garbageCollect(io, allocator, roots); 635 636 var after_gc_moment = try target.core.latestMoment(); 637 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 638 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 639 retained.deinit(); 640 } 641 } 642 643 test "patch lifecycle" { 644 try patchLifecycle(.squash); 645 try patchLifecycle(.source); 646 } 647 648 fn patchLifecycle(merge_revision: pch.MergeRevision) !void { 649 const io = std.testing.io; 650 const allocator = std.testing.allocator; 651 652 var temp = std.testing.tmpDir(.{}); 653 defer temp.cleanup(); 654 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 655 defer allocator.free(temp_path); 656 657 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 658 defer allocator.free(repos_dir); 659 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 660 defer allocator.free(admin_path); 661 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 662 defer allocator.free(upstream_path); 663 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 664 defer allocator.free(source_path); 665 666 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 667 const user_id = evt.EventWithId.randomId(prng.random()); 668 const repo_id = evt.EventWithId.randomId(prng.random()); 669 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 670 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 671 defer allocator.free(target_path); 672 673 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 674 defer admin.deinit(io, allocator); 675 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 676 .{ 677 .id = std.fmt.bytesToHex(user_id, .lower), 678 .timestamp = 1, 679 .author = author, 680 .event = .{ .user = .{ 681 .name = author.name, 682 .email = author.email, 683 .password_hash = "", 684 } }, 685 }, 686 .{ 687 .id = repo_id_hex, 688 .timestamp = 1, 689 .author = author, 690 .event = .{ .repo = .{ 691 .user_id = &user_id, 692 .name = "repo", 693 .description = "", 694 } }, 695 }, 696 }); 697 698 // 699 // create the target repo 700 // 701 702 { 703 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 704 defer upstream.deinit(io, allocator); 705 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 706 defer file.close(io); 707 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 708 try upstream.add(io, allocator, &.{"main.zig"}); 709 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 710 } 711 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 712 defer target.deinit(io, allocator); 713 714 // 715 // create the patch draft in the fork 716 // 717 718 const patch_id = evt.EventWithId.randomId(prng.random()); 719 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 720 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 721 .target_branch = "master", 722 .id = patch_id_hex, 723 .user_id = user_id, 724 .repo_id = repo_id, 725 .title = "add the answer", 726 .tags = "enhancement", 727 .description = "adds a reusable answer constant", 728 .author = author, 729 .timestamp = 2, 730 }); 731 defer allocator.free(draft_path); 732 733 // 734 // create and record the proposed revision in the fork 735 // 736 737 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 738 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 739 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 740 defer source.deinit(io, allocator); 741 const source_oid = blk: { 742 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 743 defer file.close(io); 744 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 745 try source.add(io, allocator, &.{"feature.zig"}); 746 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 747 }; 748 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 749 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 750 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 751 const first_entries = [_]evt.EventTreeEntry{ 752 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 753 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 754 }; 755 { 756 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 757 defer draft.deinit(io, allocator); 758 var arena = std.heap.ArenaAllocator.init(allocator); 759 defer arena.deinit(); 760 761 const initial_moment = try evt.currentMoment(repo_opts, &draft); 762 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 763 try std.testing.expect(!initial_patch.removed); 764 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 765 try std.testing.expectEqual(null, initial_patch.event.revision); 766 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 767 768 var source_moment = try source.core.latestMoment(); 769 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 770 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 771 defer objects.deinit(); 772 try objects.include(&source_oid); 773 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 774 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 775 .base_oid = &base_oid, 776 .source_oid = &source_oid, 777 .message = "add the answer", 778 }, &first_entries, 3, patch_id, .{ 779 .title = "add the answer", 780 .tags = "enhancement", 781 .description = "adds a reusable answer constant", 782 .target_branch = "master", 783 }, 3); 784 785 _ = arena.reset(.retain_capacity); 786 const moment = try evt.currentMoment(repo_opts, &draft); 787 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 788 try std.testing.expect(!patch.removed); 789 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 790 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 791 } 792 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 793 794 // 795 // publish another draft before its first push 796 // 797 798 const unpushed_id = evt.EventWithId.randomId(prng.random()); 799 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 800 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 801 .id = unpushed_id_hex, 802 .user_id = user_id, 803 .repo_id = repo_id, 804 .title = "explain the answer", 805 .tags = "documentation", 806 .description = "describes the existing answer", 807 .target_branch = "master", 808 .author = author, 809 .timestamp = 3, 810 }); 811 defer allocator.free(unpushed_path); 812 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 813 .id = unpushed_id_hex, 814 .user_id = user_id, 815 .repo_id = repo_id, 816 .author = author, 817 .timestamp = 4, 818 }); 819 { 820 var verify_arena = std.heap.ArenaAllocator.init(allocator); 821 defer verify_arena.deinit(); 822 const target_moment = try evt.currentMoment(repo_opts, &target); 823 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 824 try std.testing.expectEqual(null, unpushed.event.revision); 825 826 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 827 defer unpushed_repo.deinit(io, allocator); 828 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 829 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 830 try std.testing.expect(removed.removed); 831 } 832 833 // 834 // edit the patch metadata in the fork 835 // 836 837 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 838 .id = patch_id_hex, 839 .user_id = user_id, 840 .repo_id = repo_id, 841 .title = "explain the answer", 842 .tags = "enhancement documentation", 843 .description = "adds and explains a reusable answer constant", 844 .target_branch = "master", 845 .author = author, 846 .timestamp = 4, 847 })); 848 849 // 850 // publish the metadata and revision pointer 851 // 852 853 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 854 .id = patch_id_hex, 855 .user_id = user_id, 856 .repo_id = repo_id, 857 .author = author, 858 .timestamp = 5, 859 }); 860 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 861 .id = patch_id_hex, 862 .user_id = user_id, 863 .repo_id = repo_id, 864 .author = author, 865 .timestamp = 6, 866 }); 867 868 var arena = std.heap.ArenaAllocator.init(allocator); 869 defer arena.deinit(); 870 const moment = try evt.currentMoment(repo_opts, &target); 871 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 872 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 873 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 874 try std.testing.expectEqual(.open, patch.event.status); 875 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 876 877 _ = arena.reset(.retain_capacity); 878 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 879 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 880 try std.testing.expect(!published_fork.removed); 881 try std.testing.expectEqual(.publish, published_fork.event.stage); 882 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 883 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 884 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 885 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 886 { 887 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 888 defer draft.deinit(io, allocator); 889 _ = arena.reset(.retain_capacity); 890 const draft_moment = try evt.currentMoment(repo_opts, &draft); 891 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 892 try std.testing.expect(removed.removed); 893 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 894 @memcpy(&squash_oid, revision.patch_oid); 895 try std.testing.expectEqualStrings("master", removed.event.target_branch); 896 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 897 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 898 @memcpy(&event_oid, revision.event_oid); 899 var repo_moment = try draft.core.latestMoment(); 900 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 901 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 902 defer event_object.deinit(); 903 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 904 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 905 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 906 } 907 908 // 909 // merge the selected revision into the target 910 // 911 912 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 913 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 914 .id = patch_id_hex, 915 .revision = merge_revision, 916 .author = author, 917 .timestamp = 5, 918 })); 919 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 920 921 // run the cache scenario once; both lifecycle runs still perform a merge 922 if (merge_revision == .source) { 923 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 924 } 925 926 const selected_oid = switch (merge_revision) { 927 .squash => squash_oid, 928 .source => source_oid, 929 }; 930 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 931 .id = patch_id_hex, 932 .revision = merge_revision, 933 .author = author, 934 .timestamp = 5, 935 }); 936 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 937 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 938 939 _ = arena.reset(.retain_capacity); 940 const merged_moment = try evt.currentMoment(repo_opts, &target); 941 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 942 try std.testing.expectEqual(.merged, merged.event.status.kind()); 943 const merged_oid = switch (merged.event.status) { 944 .merged => |oid| oid, 945 else => return error.InvalidPatch, 946 }; 947 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 948 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 949 950 _ = arena.reset(.retain_capacity); 951 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; 952 try std.testing.expect(removed_fork.removed); 953 } 954 955 fn indexedForkCount( 956 moment: evt.AdminDB.HashMap(.read_only), 957 index_key: []const u8, 958 parent_id: []const u8, 959 ) !u64 { 960 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 961 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 962 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 963 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 964 return try ids.count(); 965 } 966 967 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 968 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 969 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 970 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 971 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 972 return try ids.count(); 973 } 974 975 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 976 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 977 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 978 var key_buffer: evt.Patch.TagStatusKey = undefined; 979 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 980 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 981 return try ids.count(); 982 } 983 984 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 985 var arena = std.heap.ArenaAllocator.init(allocator); 986 defer arena.deinit(); 987 var moment = try repo.core.latestMoment(); 988 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 989 } 990 991 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 992 const checks = try Repo.DB.HashMap(.read_only).init((try moment.getCursor(hash.hashInt(repo_opts.hash, "patch-id->mergeability"))) orelse return error.NotFound); 993 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 994 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 995 for ([_][]const u8{ "source", "squash" }) |field| { 996 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 997 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 998 } 999 } 1000 1001 fn testMergeability( 1002 target: *Repo, 1003 io: std.Io, 1004 allocator: std.mem.Allocator, 1005 repos_dir: []const u8, 1006 id: *const [evt.event_id_size]u8, 1007 base: *const [hash.hexLen(repo_opts.hash)]u8, 1008 source: *const [hash.hexLen(repo_opts.hash)]u8, 1009 ) !void { 1010 var arena = std.heap.ArenaAllocator.init(allocator); 1011 defer arena.deinit(); 1012 const events = try evt.currentMoment(repo_opts, target); 1013 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1014 const patch = record.event; 1015 1016 // publication checks both methods, and reads and cache hits do no work 1017 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1018 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1019 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1020 const before = (try target.core.latestMoment()).cursor.slot(); 1021 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1022 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1023 { 1024 var moment = try target.core.latestMoment(); 1025 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1026 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1027 } 1028 1029 // a target update invalidates the old result before refresh 1030 { 1031 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1032 defer file.close(io); 1033 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1034 } 1035 try target.add(io, allocator, &.{"feature.zig"}); 1036 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1037 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1038 var squash_input = input; 1039 squash_input.revision = .squash; 1040 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1041 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1042 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1043 1044 // local edits and server comments leave checks alone 1045 var edit = evt.Patch.Update{ .fields = .{ 1046 .title = "edit the answer", 1047 .tags = patch.tags, 1048 .description = patch.description, 1049 .target_branch = patch.target_branch, 1050 } }; 1051 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1052 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1053 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1054 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1055 1056 // a server patch edit refreshes its mergeability 1057 edit.fields.title = patch.title; 1058 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1059 const events_before = try target.readRef(io, evt.events_ref); 1060 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1061 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1062 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1063 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1064 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1065 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1066 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1067 1068 // deleting and restoring the target cannot reuse the conflict result 1069 { 1070 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1071 defer detached.deinit(); 1072 } 1073 try target.removeBranch(io, .{ .name = "master" }); 1074 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1075 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1076 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1077 try target.addBranch(io, .{ .name = "master" }); 1078 { 1079 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1080 defer restored.deinit(); 1081 } 1082 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1083 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1084 1085 // a missing fork disables merging without changing the accepted events 1086 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1087 defer allocator.free(draft_path); 1088 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1089 defer allocator.free(away_path); 1090 { 1091 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1092 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1093 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1094 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1095 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1096 } 1097 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1098 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1099 1100 // a changed revision is unknown even if its target has not moved 1101 var changed = patch; 1102 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1103 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1104 } 1105