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