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