╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1084 // 1085 // merge the selected revision into the target 1086 // 1087 1088 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1089 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, &target, .{ 1090 .id = patch_id_hex, 1091 .revision = merge_revision, 1092 .author = author, 1093 .timestamp = 5, 1094 })); 1095 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1096 1097 // run the cache scenario once; both lifecycle runs still perform a merge 1098 if (merge_revision == .source) { 1099 try testMergeability(&target, io, allocator, users_dir, &user_id, &patch_id, &base_oid, &source_oid); 1100 } 1101 1102 const selected_oid = switch (merge_revision) { 1103 .squash => squash_oid, 1104 .source => source_oid, 1105 }; 1106 try pch.mergeAndRemoveFork(repo_opts, io, allocator, .{ .users_dir = users_dir }, &target, .{ 1107 .id = patch_id_hex, 1108 .revision = merge_revision, 1109 .author = author, 1110 .timestamp = 5, 1111 }); 1112 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1113 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1114 1115 _ = arena.reset(.retain_capacity); 1116 const merged_moment = try evt.currentMoment(repo_opts, &target); 1117 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1118 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1119 const merged_status = switch (merged.event.status) { 1120 .merged => |value| value, 1121 else => return error.InvalidPatch, 1122 }; 1123 try std.testing.expectEqual(merge_revision, merged_status.revision); 1124 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1125 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1126 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1127 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1128 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1129 1130 _ = arena.reset(.retain_capacity); 1131 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; 1132 try std.testing.expect(removed_fork.removed); 1133 } 1134 1135 fn draftForkCount(moment: evt.UserDB.HashMap(.read_only), repo_id: []const u8) !u64 { 1136 const index_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return 0; 1137 const index = try evt.UserDB.HashMap(.read_only).init(index_cursor); 1138 const ids_cursor = try index.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return 0; 1139 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1140 return try ids.count(); 1141 } 1142 1143 fn activeForkCount(moment: evt.UserDB.HashMap(.read_only)) !u64 { 1144 const ids_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.active_id_set_key)) orelse return 0; 1145 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1146 return try ids.count(); 1147 } 1148 1149 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1150 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1151 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1152 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1153 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1154 return try ids.count(); 1155 } 1156 1157 fn patchLabelStatusCount(moment: Repo.DB.HashMap(.read_only), label: []const u8, status: evt.Patch.StatusKind) !u64 { 1158 const labels_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.label_status_to_id_set_key)) orelse return 0; 1159 const labels = try Repo.DB.SortedMap(.read_only).init(labels_cursor); 1160 var key_buffer: evt.Patch.LabelStatusKey = undefined; 1161 const ids_cursor = try labels.getCursor(try evt.Patch.labelStatusKey(&key_buffer, label, status)) orelse return 0; 1162 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1163 return try ids.count(); 1164 } 1165 1166 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1167 var arena = std.heap.ArenaAllocator.init(allocator); 1168 defer arena.deinit(); 1169 var moment = try repo.core.latestMoment(); 1170 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1171 } 1172 1173 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1174 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1175 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1176 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); 1177 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1178 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1179 for ([_][]const u8{ "source", "squash" }) |field| { 1180 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1181 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1182 } 1183 } 1184 1185 fn testMergeability( 1186 target: *Repo, 1187 io: std.Io, 1188 allocator: std.mem.Allocator, 1189 users_dir: []const u8, 1190 forker_id: *const [evt.event_id_size]u8, 1191 id: *const [evt.event_id_size]u8, 1192 base: *const [hash.hexLen(repo_opts.hash)]u8, 1193 source: *const [hash.hexLen(repo_opts.hash)]u8, 1194 ) !void { 1195 var arena = std.heap.ArenaAllocator.init(allocator); 1196 defer arena.deinit(); 1197 const events = try evt.currentMoment(repo_opts, target); 1198 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1199 const patch = record.event; 1200 1201 // publication checks both methods, and reads and cache hits do no work 1202 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1203 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1204 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1205 const before = (try target.core.latestMoment()).cursor.slot(); 1206 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1207 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1208 { 1209 var moment = try target.core.latestMoment(); 1210 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1211 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1212 } 1213 1214 // a target update invalidates the old result before refresh 1215 { 1216 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1217 defer file.close(io); 1218 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1219 } 1220 try target.add(io, allocator, &.{"feature.zig"}); 1221 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1222 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1223 var squash_input = input; 1224 squash_input.revision = .squash; 1225 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1226 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1227 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, squash_input)); 1228 1229 // local edits and server comments leave checks alone 1230 var edit = evt.Patch.Update{ .fields = .{ 1231 .title = "edit the answer", 1232 .labels = patch.labels, 1233 .description = patch.description, 1234 .target_branch = patch.target_branch, 1235 } }; 1236 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1237 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1238 _ = try evt.Comment.create(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, .patch, &input.id, &input.id, "reviewing the answer", author); 1239 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1240 1241 // a server patch edit refreshes its mergeability 1242 edit.fields.title = patch.title; 1243 const before_edit = try target.core.db.rootCursor().count(); 1244 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, id, edit, author); 1245 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1246 const events_before = try target.readRef(io, evt.events_ref); 1247 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1248 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1249 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1250 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, squash_input)); 1251 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1252 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1253 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1254 1255 // deleting and restoring the target cannot reuse the conflict result 1256 { 1257 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1258 defer detached.deinit(); 1259 } 1260 try target.removeBranch(io, .{ .name = "master" }); 1261 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1262 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1263 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1264 try target.addBranch(io, .{ .name = "master" }); 1265 { 1266 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1267 defer restored.deinit(); 1268 } 1269 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1270 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1271 1272 // a missing fork disables merging without changing the accepted events 1273 const draft_path = try fork.forkPath(allocator, users_dir, forker_id, id); 1274 defer allocator.free(draft_path); 1275 const away_path = try allocator.print("{s}.away", .{draft_path}); 1276 defer allocator.free(away_path); 1277 { 1278 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1279 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1280 { 1281 const log_level = std.testing.log_level; 1282 std.testing.log_level = .err; 1283 defer std.testing.log_level = log_level; 1284 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1285 } 1286 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1287 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1288 } 1289 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1290 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1291 1292 // a changed revision is unknown even if its target has not moved 1293 var changed = patch; 1294 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1295 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1296 } 1297