╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 patchLabelStatusCount(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 .labels = "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 const user_repo_path = try evt.userRepoPath(allocator, repos_dir, &user_id); 847 defer allocator.free(user_repo_path); 848 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 849 defer user_repo.deinit(io, allocator); 850 try evt.consume(.server, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 851 .{ 852 .id = repo_id_hex, 853 .timestamp = 1, 854 .author = author, 855 .event = .{ .repo = .{ 856 .user_id = &user_id, 857 .name = "repo", 858 .description = "", 859 } }, 860 }, 861 }); 862 863 // 864 // create the target repo 865 // 866 867 { 868 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 869 defer upstream.deinit(io, allocator); 870 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 871 defer file.close(io); 872 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 873 try upstream.add(io, allocator, &.{"main.zig"}); 874 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 875 } 876 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 877 defer target.deinit(io, allocator); 878 879 // 880 // create the patch draft in the fork 881 // 882 883 const patch_id = evt.EventWithId.randomId(prng.random()); 884 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 885 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &user_repo, .{ 886 .target_branch = "master", 887 .id = patch_id_hex, 888 .user_id = user_id, 889 .repo_id = repo_id, 890 .repo_user_id = user_id, 891 .title = "add the answer", 892 .labels = "enhancement", 893 .description = "adds a reusable answer constant", 894 .author = author, 895 .timestamp = 2, 896 }); 897 defer allocator.free(draft_path); 898 899 // 900 // create and record the proposed revision in the fork 901 // 902 903 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 904 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 905 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 906 defer source.deinit(io, allocator); 907 const source_oid = blk: { 908 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 909 defer file.close(io); 910 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 911 try source.add(io, allocator, &.{"feature.zig"}); 912 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 913 }; 914 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 915 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 916 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 917 const first_entries = [_]evt.EventTreeEntry{ 918 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 919 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 920 }; 921 { 922 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 923 defer draft.deinit(io, allocator); 924 var arena = std.heap.ArenaAllocator.init(allocator); 925 defer arena.deinit(); 926 927 const initial_moment = try evt.currentMoment(repo_opts, &draft); 928 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 929 try std.testing.expect(!initial_patch.removed); 930 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 931 try std.testing.expectEqual(null, initial_patch.event.revision); 932 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 933 934 var source_moment = try source.core.latestMoment(); 935 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 936 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 937 defer objects.deinit(); 938 try objects.include(&source_oid); 939 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 940 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 941 .base_oid = &base_oid, 942 .source_oid = &source_oid, 943 .message = "add the answer", 944 }, &first_entries, 3, patch_id, .{ 945 .title = "add the answer", 946 .labels = "enhancement", 947 .description = "adds a reusable answer constant", 948 .target_branch = "master", 949 }, 3); 950 951 _ = arena.reset(.retain_capacity); 952 const moment = try evt.currentMoment(repo_opts, &draft); 953 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 954 try std.testing.expect(!patch.removed); 955 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 956 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 957 } 958 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 959 960 // draft mergeability checks leave the target repo untouched 961 const before_draft_check = (try target.core.latestMoment()).cursor.slot(); 962 pch.refreshMergeability(repo_opts, io, allocator, &target, patch_id); 963 try std.testing.expectEqualDeep(before_draft_check, (try target.core.latestMoment()).cursor.slot()); 964 965 // 966 // publish another draft before its first push 967 // 968 969 const unpushed_id = evt.EventWithId.randomId(prng.random()); 970 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 971 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &user_repo, .{ 972 .id = unpushed_id_hex, 973 .user_id = user_id, 974 .repo_id = repo_id, 975 .repo_user_id = user_id, 976 .title = "explain the answer", 977 .labels = "documentation", 978 .description = "describes the existing answer", 979 .target_branch = "master", 980 .author = author, 981 .timestamp = 3, 982 }); 983 defer allocator.free(unpushed_path); 984 try pch.publish(repo_opts, io, allocator, &admin, &user_repo, &target, unpushed_path, .{ 985 .id = unpushed_id_hex, 986 .user_id = user_id, 987 .repo_id = repo_id, 988 .author = author, 989 .timestamp = 4, 990 }); 991 { 992 var verify_arena = std.heap.ArenaAllocator.init(allocator); 993 defer verify_arena.deinit(); 994 const target_moment = try evt.currentMoment(repo_opts, &target); 995 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 996 try std.testing.expectEqual(null, unpushed.event.revision); 997 998 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 999 defer unpushed_repo.deinit(io, allocator); 1000 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 1001 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 1002 try std.testing.expect(removed.removed); 1003 } 1004 1005 // 1006 // edit the patch metadata in the fork 1007 // 1008 1009 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, &user_repo, draft_path, .{ 1010 .id = patch_id_hex, 1011 .user_id = user_id, 1012 .repo_id = repo_id, 1013 .title = "explain the answer", 1014 .labels = "enhancement documentation", 1015 .description = "adds and explains a reusable answer constant", 1016 .target_branch = "master", 1017 .author = author, 1018 .timestamp = 4, 1019 })); 1020 1021 // 1022 // publish the metadata and revision pointer 1023 // 1024 1025 const before_publish = try target.core.db.rootCursor().count(); 1026 try pch.publish(repo_opts, io, allocator, &admin, &user_repo, &target, draft_path, .{ 1027 .id = patch_id_hex, 1028 .user_id = user_id, 1029 .repo_id = repo_id, 1030 .author = author, 1031 .timestamp = 5, 1032 }); 1033 try std.testing.expectEqual(before_publish + 1, try target.core.db.rootCursor().count()); 1034 try pch.publish(repo_opts, io, allocator, &admin, &user_repo, &target, draft_path, .{ 1035 .id = patch_id_hex, 1036 .user_id = user_id, 1037 .repo_id = repo_id, 1038 .author = author, 1039 .timestamp = 6, 1040 }); 1041 1042 var arena = std.heap.ArenaAllocator.init(allocator); 1043 defer arena.deinit(); 1044 const moment = try evt.currentMoment(repo_opts, &target); 1045 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1046 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1047 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1048 try std.testing.expectEqual(.open, patch.event.status); 1049 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1050 1051 _ = arena.reset(.retain_capacity); 1052 const user_moment = try evt.currentMoment(evt.user_repo_opts, &user_repo); 1053 const published_fork = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, &arena, &patch_id)) orelse return error.NotFound; 1054 try std.testing.expect(!published_fork.removed); 1055 try std.testing.expectEqual(.publish, published_fork.event.stage); 1056 try std.testing.expectEqual(0, try draftForkCount(user_moment, &repo_id)); 1057 try std.testing.expectEqual(2, try activeForkCount(user_moment)); 1058 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1059 { 1060 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1061 defer draft.deinit(io, allocator); 1062 _ = arena.reset(.retain_capacity); 1063 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1064 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1065 try std.testing.expect(removed.removed); 1066 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1067 @memcpy(&squash_oid, revision.patch_oid); 1068 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1069 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1070 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1071 @memcpy(&event_oid, revision.event_oid); 1072 var repo_moment = try draft.core.latestMoment(); 1073 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1074 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1075 defer event_object.deinit(); 1076 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1077 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1078 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1079 } 1080 1081 // 1082 // merge the selected revision into the target 1083 // 1084 1085 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1086 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 1087 .id = patch_id_hex, 1088 .revision = merge_revision, 1089 .author = author, 1090 .timestamp = 5, 1091 })); 1092 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1093 1094 // run the cache scenario once; both lifecycle runs still perform a merge 1095 if (merge_revision == .source) { 1096 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 1097 } 1098 1099 const selected_oid = switch (merge_revision) { 1100 .squash => squash_oid, 1101 .source => source_oid, 1102 }; 1103 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &target, .{ 1104 .id = patch_id_hex, 1105 .revision = merge_revision, 1106 .author = author, 1107 .timestamp = 5, 1108 }); 1109 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1110 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1111 1112 _ = arena.reset(.retain_capacity); 1113 const merged_moment = try evt.currentMoment(repo_opts, &target); 1114 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1115 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1116 const merged_status = switch (merged.event.status) { 1117 .merged => |value| value, 1118 else => return error.InvalidPatch, 1119 }; 1120 try std.testing.expectEqual(merge_revision, merged_status.revision); 1121 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1122 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1123 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1124 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1125 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1126 1127 _ = arena.reset(.retain_capacity); 1128 const removed_fork = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, try evt.currentMoment(evt.user_repo_opts, &user_repo), &arena, &patch_id)) orelse return error.NotFound; 1129 try std.testing.expect(removed_fork.removed); 1130 } 1131 1132 fn draftForkCount(moment: evt.UserDB.HashMap(.read_only), repo_id: []const u8) !u64 { 1133 const index_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return 0; 1134 const index = try evt.UserDB.HashMap(.read_only).init(index_cursor); 1135 const ids_cursor = try index.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return 0; 1136 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1137 return try ids.count(); 1138 } 1139 1140 fn activeForkCount(moment: evt.UserDB.HashMap(.read_only)) !u64 { 1141 const ids_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.active_id_set_key)) orelse return 0; 1142 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1143 return try ids.count(); 1144 } 1145 1146 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1147 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1148 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1149 const ids_cursor = try statuses.getCursor(@tagName(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 patchLabelStatusCount(moment: Repo.DB.HashMap(.read_only), label: []const u8, status: evt.Patch.StatusKind) !u64 { 1155 const labels_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.label_status_to_id_set_key)) orelse return 0; 1156 const labels = try Repo.DB.SortedMap(.read_only).init(labels_cursor); 1157 var key_buffer: evt.Patch.LabelStatusKey = undefined; 1158 const ids_cursor = try labels.getCursor(try evt.Patch.labelStatusKey(&key_buffer, label, status)) orelse return 0; 1159 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1160 return try ids.count(); 1161 } 1162 1163 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1164 var arena = std.heap.ArenaAllocator.init(allocator); 1165 defer arena.deinit(); 1166 var moment = try repo.core.latestMoment(); 1167 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1168 } 1169 1170 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1171 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1172 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1173 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); 1174 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1175 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1176 for ([_][]const u8{ "source", "squash" }) |field| { 1177 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1178 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1179 } 1180 } 1181 1182 fn testMergeability( 1183 target: *Repo, 1184 io: std.Io, 1185 allocator: std.mem.Allocator, 1186 repos_dir: []const u8, 1187 id: *const [evt.event_id_size]u8, 1188 base: *const [hash.hexLen(repo_opts.hash)]u8, 1189 source: *const [hash.hexLen(repo_opts.hash)]u8, 1190 ) !void { 1191 var arena = std.heap.ArenaAllocator.init(allocator); 1192 defer arena.deinit(); 1193 const events = try evt.currentMoment(repo_opts, target); 1194 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1195 const patch = record.event; 1196 1197 // publication checks both methods, and reads and cache hits do no work 1198 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1199 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1200 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1201 const before = (try target.core.latestMoment()).cursor.slot(); 1202 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1203 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1204 { 1205 var moment = try target.core.latestMoment(); 1206 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1207 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1208 } 1209 1210 // a target update invalidates the old result before refresh 1211 { 1212 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1213 defer file.close(io); 1214 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1215 } 1216 try target.add(io, allocator, &.{"feature.zig"}); 1217 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1218 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1219 var squash_input = input; 1220 squash_input.revision = .squash; 1221 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1222 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1223 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1224 1225 // local edits and server comments leave checks alone 1226 var edit = evt.Patch.Update{ .fields = .{ 1227 .title = "edit the answer", 1228 .labels = patch.labels, 1229 .description = patch.description, 1230 .target_branch = patch.target_branch, 1231 } }; 1232 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1233 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1234 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1235 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1236 1237 // a server patch edit refreshes its mergeability 1238 edit.fields.title = patch.title; 1239 const before_edit = try target.core.db.rootCursor().count(); 1240 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1241 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1242 const events_before = try target.readRef(io, evt.events_ref); 1243 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1244 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1245 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1246 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1247 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1248 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1249 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1250 1251 // deleting and restoring the target cannot reuse the conflict result 1252 { 1253 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1254 defer detached.deinit(); 1255 } 1256 try target.removeBranch(io, .{ .name = "master" }); 1257 pch.refreshOpenMergeability(repo_opts, io, allocator, target, null, null); 1258 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1259 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1260 try target.addBranch(io, .{ .name = "master" }); 1261 { 1262 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1263 defer restored.deinit(); 1264 } 1265 pch.refreshOpenMergeability(repo_opts, io, allocator, target, null, null); 1266 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1267 1268 // a missing fork disables merging without changing the accepted events 1269 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1270 defer allocator.free(draft_path); 1271 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1272 defer allocator.free(away_path); 1273 { 1274 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1275 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1276 { 1277 const log_level = std.testing.log_level; 1278 std.testing.log_level = .err; 1279 defer std.testing.log_level = log_level; 1280 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1281 } 1282 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1283 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1284 } 1285 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1286 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1287 1288 // a changed revision is unknown even if its target has not moved 1289 var changed = patch; 1290 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1291 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1292 } 1293