╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1007 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 1008 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 1009 try std.testing.expect(removed.removed); 1010 } 1011 1012 // 1013 // edit the patch metadata in the fork 1014 // 1015 1016 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 1017 .id = patch_id_hex, 1018 .user_id = user_id, 1019 .repo_id = repo_id, 1020 .title = "explain the answer", 1021 .tags = "enhancement documentation", 1022 .description = "adds and explains a reusable answer constant", 1023 .target_branch = "master", 1024 .author = author, 1025 .timestamp = 4, 1026 })); 1027 1028 // 1029 // publish the metadata and revision pointer 1030 // 1031 1032 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1033 .id = patch_id_hex, 1034 .user_id = user_id, 1035 .repo_id = repo_id, 1036 .author = author, 1037 .timestamp = 5, 1038 }); 1039 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1040 .id = patch_id_hex, 1041 .user_id = user_id, 1042 .repo_id = repo_id, 1043 .author = author, 1044 .timestamp = 6, 1045 }); 1046 1047 var arena = std.heap.ArenaAllocator.init(allocator); 1048 defer arena.deinit(); 1049 const moment = try evt.currentMoment(repo_opts, &target); 1050 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1051 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1052 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1053 try std.testing.expectEqual(.open, patch.event.status); 1054 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1055 1056 _ = arena.reset(.retain_capacity); 1057 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 1058 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 1059 try std.testing.expect(!published_fork.removed); 1060 try std.testing.expectEqual(.publish, published_fork.event.stage); 1061 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 1062 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 1063 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 1064 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1065 { 1066 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1067 defer draft.deinit(io, allocator); 1068 _ = arena.reset(.retain_capacity); 1069 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1070 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1071 try std.testing.expect(removed.removed); 1072 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1073 @memcpy(&squash_oid, revision.patch_oid); 1074 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1075 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1076 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1077 @memcpy(&event_oid, revision.event_oid); 1078 var repo_moment = try draft.core.latestMoment(); 1079 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1080 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1081 defer event_object.deinit(); 1082 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1083 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1084 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1085 } 1086 1087 // 1088 // merge the selected revision into the target 1089 // 1090 1091 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1092 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 1093 .id = patch_id_hex, 1094 .revision = merge_revision, 1095 .author = author, 1096 .timestamp = 5, 1097 })); 1098 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1099 1100 // run the cache scenario once; both lifecycle runs still perform a merge 1101 if (merge_revision == .source) { 1102 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 1103 } 1104 1105 const selected_oid = switch (merge_revision) { 1106 .squash => squash_oid, 1107 .source => source_oid, 1108 }; 1109 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 1110 .id = patch_id_hex, 1111 .revision = merge_revision, 1112 .author = author, 1113 .timestamp = 5, 1114 }); 1115 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1116 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1117 1118 _ = arena.reset(.retain_capacity); 1119 const merged_moment = try evt.currentMoment(repo_opts, &target); 1120 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1121 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1122 const merged_status = switch (merged.event.status) { 1123 .merged => |value| value, 1124 else => return error.InvalidPatch, 1125 }; 1126 try std.testing.expectEqual(merge_revision, merged_status.revision); 1127 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1128 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1129 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1130 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1131 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1132 1133 _ = arena.reset(.retain_capacity); 1134 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; 1135 try std.testing.expect(removed_fork.removed); 1136 } 1137 1138 fn indexedForkCount( 1139 moment: evt.AdminDB.HashMap(.read_only), 1140 index_key: []const u8, 1141 parent_id: []const u8, 1142 ) !u64 { 1143 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 1144 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 1145 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 1146 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 1147 return try ids.count(); 1148 } 1149 1150 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1151 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1152 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1153 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1154 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1155 return try ids.count(); 1156 } 1157 1158 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 1159 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 1160 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 1161 var key_buffer: evt.Patch.TagStatusKey = undefined; 1162 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 1163 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1164 return try ids.count(); 1165 } 1166 1167 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1168 var arena = std.heap.ArenaAllocator.init(allocator); 1169 defer arena.deinit(); 1170 var moment = try repo.core.latestMoment(); 1171 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1172 } 1173 1174 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1175 const checks = try Repo.DB.HashMap(.read_only).init((try moment.getCursor(hash.hashInt(repo_opts.hash, "patch-id->mergeability"))) orelse return error.NotFound); 1176 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1177 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1178 for ([_][]const u8{ "source", "squash" }) |field| { 1179 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1180 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1181 } 1182 } 1183 1184 fn testMergeability( 1185 target: *Repo, 1186 io: std.Io, 1187 allocator: std.mem.Allocator, 1188 repos_dir: []const 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, 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, repos_dir, target, input)); 1225 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_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 .tags = patch.tags, 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, .xit, repo_opts, io, allocator, target, &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 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 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.refreshMergeability(repo_opts, io, allocator, target, 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.refreshMergeability(repo_opts, io, allocator, target, 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