╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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