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