╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1086 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, users_dir, &target, .{ 1087 .id = patch_id_hex, 1088 .revision = merge_revision, 1089 .author = author, 1090 .timestamp = 5, 1091 })); 1092 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .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, users_dir, &user_id, &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, users_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 users_dir: []const u8, 1187 forker_id: *const [evt.event_id_size]u8, 1188 id: *const [evt.event_id_size]u8, 1189 base: *const [hash.hexLen(repo_opts.hash)]u8, 1190 source: *const [hash.hexLen(repo_opts.hash)]u8, 1191 ) !void { 1192 var arena = std.heap.ArenaAllocator.init(allocator); 1193 defer arena.deinit(); 1194 const events = try evt.currentMoment(repo_opts, target); 1195 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1196 const patch = record.event; 1197 1198 // publication checks both methods, and reads and cache hits do no work 1199 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1200 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1201 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1202 const before = (try target.core.latestMoment()).cursor.slot(); 1203 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1204 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1205 { 1206 var moment = try target.core.latestMoment(); 1207 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1208 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1209 } 1210 1211 // a target update invalidates the old result before refresh 1212 { 1213 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1214 defer file.close(io); 1215 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1216 } 1217 try target.add(io, allocator, &.{"feature.zig"}); 1218 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1219 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1220 var squash_input = input; 1221 squash_input.revision = .squash; 1222 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1223 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, users_dir, target, input)); 1224 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, users_dir, target, squash_input)); 1225 1226 // local edits and server comments leave checks alone 1227 var edit = evt.Patch.Update{ .fields = .{ 1228 .title = "edit the answer", 1229 .labels = patch.labels, 1230 .description = patch.description, 1231 .target_branch = patch.target_branch, 1232 } }; 1233 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1234 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1235 _ = try evt.Comment.create(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1236 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1237 1238 // a server patch edit refreshes its mergeability 1239 edit.fields.title = patch.title; 1240 const before_edit = try target.core.db.rootCursor().count(); 1241 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, id, edit, author); 1242 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1243 const events_before = try target.readRef(io, evt.events_ref); 1244 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1245 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1246 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, users_dir, target, input)); 1247 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, users_dir, target, squash_input)); 1248 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1249 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1250 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1251 1252 // deleting and restoring the target cannot reuse the conflict result 1253 { 1254 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1255 defer detached.deinit(); 1256 } 1257 try target.removeBranch(io, .{ .name = "master" }); 1258 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1259 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1260 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1261 try target.addBranch(io, .{ .name = "master" }); 1262 { 1263 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1264 defer restored.deinit(); 1265 } 1266 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1267 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1268 1269 // a missing fork disables merging without changing the accepted events 1270 const draft_path = try fork.forkPath(allocator, users_dir, forker_id, id); 1271 defer allocator.free(draft_path); 1272 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1273 defer allocator.free(away_path); 1274 { 1275 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1276 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1277 { 1278 const log_level = std.testing.log_level; 1279 std.testing.log_level = .err; 1280 defer std.testing.log_level = log_level; 1281 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, users_dir, target, input)); 1282 } 1283 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1284 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1285 } 1286 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1287 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1288 1289 // a changed revision is unknown even if its target has not moved 1290 var changed = patch; 1291 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1292 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1293 } 1294