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