╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1044 _ = arena.reset(.retain_capacity); 1045 const merged_moment = try evt.currentMoment(repo_opts, &target); 1046 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1047 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1048 const merged_oid = switch (merged.event.status) { 1049 .merged => |oid| oid, 1050 else => return error.InvalidPatch, 1051 }; 1052 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 1053 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1054 1055 _ = arena.reset(.retain_capacity); 1056 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; 1057 try std.testing.expect(removed_fork.removed); 1058 } 1059 1060 fn indexedForkCount( 1061 moment: evt.AdminDB.HashMap(.read_only), 1062 index_key: []const u8, 1063 parent_id: []const u8, 1064 ) !u64 { 1065 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 1066 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 1067 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 1068 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 1069 return try ids.count(); 1070 } 1071 1072 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1073 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1074 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1075 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1076 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1077 return try ids.count(); 1078 } 1079 1080 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 1081 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 1082 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 1083 var key_buffer: evt.Patch.TagStatusKey = undefined; 1084 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 1085 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1086 return try ids.count(); 1087 } 1088 1089 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1090 var arena = std.heap.ArenaAllocator.init(allocator); 1091 defer arena.deinit(); 1092 var moment = try repo.core.latestMoment(); 1093 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1094 } 1095 1096 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1097 const checks = try Repo.DB.HashMap(.read_only).init((try moment.getCursor(hash.hashInt(repo_opts.hash, "patch-id->mergeability"))) orelse return error.NotFound); 1098 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1099 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1100 for ([_][]const u8{ "source", "squash" }) |field| { 1101 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1102 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1103 } 1104 } 1105 1106 fn testMergeability( 1107 target: *Repo, 1108 io: std.Io, 1109 allocator: std.mem.Allocator, 1110 repos_dir: []const u8, 1111 id: *const [evt.event_id_size]u8, 1112 base: *const [hash.hexLen(repo_opts.hash)]u8, 1113 source: *const [hash.hexLen(repo_opts.hash)]u8, 1114 ) !void { 1115 var arena = std.heap.ArenaAllocator.init(allocator); 1116 defer arena.deinit(); 1117 const events = try evt.currentMoment(repo_opts, target); 1118 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1119 const patch = record.event; 1120 1121 // publication checks both methods, and reads and cache hits do no work 1122 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1123 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1124 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1125 const before = (try target.core.latestMoment()).cursor.slot(); 1126 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1127 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1128 { 1129 var moment = try target.core.latestMoment(); 1130 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1131 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1132 } 1133 1134 // a target update invalidates the old result before refresh 1135 { 1136 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1137 defer file.close(io); 1138 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1139 } 1140 try target.add(io, allocator, &.{"feature.zig"}); 1141 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1142 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1143 var squash_input = input; 1144 squash_input.revision = .squash; 1145 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1146 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1147 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1148 1149 // local edits and server comments leave checks alone 1150 var edit = evt.Patch.Update{ .fields = .{ 1151 .title = "edit the answer", 1152 .tags = patch.tags, 1153 .description = patch.description, 1154 .target_branch = patch.target_branch, 1155 } }; 1156 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1157 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1158 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1159 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1160 1161 // a server patch edit refreshes its mergeability 1162 edit.fields.title = patch.title; 1163 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1164 const events_before = try target.readRef(io, evt.events_ref); 1165 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1166 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1167 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1168 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1169 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1170 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1171 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1172 1173 // deleting and restoring the target cannot reuse the conflict result 1174 { 1175 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1176 defer detached.deinit(); 1177 } 1178 try target.removeBranch(io, .{ .name = "master" }); 1179 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1180 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1181 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1182 try target.addBranch(io, .{ .name = "master" }); 1183 { 1184 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1185 defer restored.deinit(); 1186 } 1187 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1188 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1189 1190 // a missing fork disables merging without changing the accepted events 1191 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1192 defer allocator.free(draft_path); 1193 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1194 defer allocator.free(away_path); 1195 { 1196 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1197 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1198 { 1199 const log_level = std.testing.log_level; 1200 std.testing.log_level = .err; 1201 defer std.testing.log_level = log_level; 1202 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1203 } 1204 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1205 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1206 } 1207 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1208 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1209 1210 // a changed revision is unknown even if its target has not moved 1211 var changed = patch; 1212 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1213 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1214 } 1215