╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
125 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 126 defer objects.deinit(); 127 try objects.include(&source_oid); 128 try target.copyObjects(.xit, repo_opts, &objects, io, null); 129 } 130 131 // 132 // define the patch revision and patch 133 // 134 135 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 136 const patchrev_id = evt.EventWithId.randomId(prng.random()); 137 const patch_id = evt.EventWithId.randomId(prng.random()); 138 const patch = evt.Patch{ 139 .title = "add the answer", 140 .description = "adds a reusable answer constant", 141 .tags = "enhancement", 142 .target_branch = "master", 143 }; 144 const tree_entries = [_]evt.EventTreeEntry{ 145 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 146 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 147 }; 148 149 // 150 // consume the patch revision and patch 151 // 152 153 { 154 try consumePatchWithRevision( 155 .repo, 156 io, 157 allocator, 158 &target, 159 evt.events_ref, 160 patchrev_id, 161 .{ .base_oid = &base_oid, .source_oid = &source_oid, .message = patch.title }, 162 &tree_entries, 163 3, 164 patch_id, 165 patch, 166 4, 167 ); 168 } 169 170 // 171 // check the records and squash commit 172 // 173 174 var arena = std.heap.ArenaAllocator.init(allocator); 175 defer arena.deinit(); 176 const moment = try evt.currentMoment(repo_opts, &target); 177 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 178 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 179 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 180 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 181 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 182 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 183 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 184 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 185 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 186 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 187 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 188 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 189 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 190 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 191 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 192 193 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 194 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 195 { 196 var repo_moment = try target.core.latestMoment(); 197 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 198 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 199 defer patch_object.deinit(); 200 switch (patch_object.content) { 201 .commit => |commit| { 202 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 203 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 204 try std.testing.expectEqual(1, parents.len); 205 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 206 }, 207 else => return error.InvalidObject, 208 } 209 } 210 211 // 212 // update metadata without creating another patch revision 213 // 214 215 var updated_patch = patch; 216 updated_patch.revision = .{ 217 .id = std.fmt.bytesToHex(patchrev_id, .lower), 218 .squash_oid = &initial_patch_oid, 219 .source_oid = &source_oid, 220 }; 221 updated_patch.title = "add a reusable answer"; 222 { 223 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 224 .id = std.fmt.bytesToHex(patch_id, .lower), 225 .timestamp = 5, 226 .author = author, 227 .event = .{ .patch = updated_patch }, 228 }}); 229 } 230 231 _ = arena.reset(.retain_capacity); 232 const updated_moment = try evt.currentMoment(repo_opts, &target); 233 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 234 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 235 236 // 237 // branch the events history before creating competing revisions 238 // 239 240 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 241 { 242 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 243 defer result.deinit(); 244 try target.addBranch(io, .{ .name = side_events_ref.name }); 245 } 246 247 // 248 // create two competing code revisions 249 // 250 251 const source_a_oid = blk: { 252 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 253 defer file.close(io); 254 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 255 try source.add(io, allocator, &.{"feature.zig"}); 256 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 257 }; 258 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 259 const source_b_oid = blk: { 260 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 261 defer file.close(io); 262 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 263 try source.add(io, allocator, &.{"feature.zig"}); 264 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 265 }; 266 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 267 { 268 var source_moment = try source.core.latestMoment(); 269 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 270 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 271 defer objects.deinit(); 272 try objects.include(&source_b_oid); 273 try target.copyObjects(.xit, repo_opts, &objects, io, null); 274 } 275 276 // 277 // select the first revision on the events branch 278 // 279 280 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 281 const target_entries = [_]evt.EventTreeEntry{ 282 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 283 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 284 }; 285 var target_patch = updated_patch; 286 target_patch.status = .closed; 287 { 288 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 289 .base_oid = &base_oid, 290 .source_oid = &source_a_oid, 291 .message = updated_patch.title, 292 }, &target_entries, 8, patch_id, target_patch, 9); 293 } 294 295 // 296 // select the second revision on the side branch 297 // 298 299 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 300 const parent_entries = [_]evt.EventTreeEntry{ 301 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 302 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 303 }; 304 var parent_patch = updated_patch; 305 parent_patch.status = .{ .merged = &source_b_oid }; 306 { 307 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 308 .base_oid = &base_oid, 309 .source_oid = &source_b_oid, 310 .message = updated_patch.title, 311 }, &parent_entries, 10, patch_id, parent_patch, 11); 312 } 313 314 // 315 // merge the competing patch revisions 316 // 317 318 { 319 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 320 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 321 } 322 323 // 324 // check the selected revision and conflict 325 // 326 327 _ = arena.reset(.retain_capacity); 328 const merged_moment = try evt.currentMoment(repo_opts, &target); 329 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 330 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 331 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 332 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 333 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 334 335 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 336 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 337 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 338 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 339 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 340 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 341 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 342 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 343 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 344 345 // 346 // create a patch stacked on the selected revision 347 // 348 349 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 350 const child_id = evt.EventWithId.randomId(prng.random()); 351 const child_entries = [_]evt.EventTreeEntry{ 352 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 353 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 354 }; 355 { 356 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 357 .base_oid = merged_patchrev.patch_oid, 358 .source_oid = &source_b_oid, 359 .message = "stack another answer change", 360 }, &child_entries, 12, child_id, .{ 361 .title = "stack another answer change", 362 .description = "depends on the first patch", 363 .tags = "enhancement", 364 .target_branch = "master", 365 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 366 }, 13); 367 } 368 369 // 370 // check the squash commit and reverse index 371 // 372 373 _ = arena.reset(.retain_capacity); 374 const stacked_moment = try evt.currentMoment(repo_opts, &target); 375 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 376 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 377 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 378 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 379 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 380 @memcpy(&child_oid, child_patchrev.patch_oid); 381 { 382 var child_repo_moment = try target.core.latestMoment(); 383 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 384 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 385 defer child_object.deinit(); 386 const child_commit = switch (child_object.content) { 387 .commit => |commit| commit, 388 else => return error.InvalidObject, 389 }; 390 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 391 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 392 } 393 394 const targets_cursor = try stacked_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.target_patch_id_to_patch_id_set_key)) orelse return error.NotFound; 395 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 396 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 397 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 398 try std.testing.expectEqual(1, try children.count()); 399 400 // 401 // close and merge the same revision concurrently 402 // 403 404 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 405 { 406 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 407 defer result.deinit(); 408 try target.addBranch(io, .{ .name = status_side_ref.name }); 409 } 410 var closed_child = child.event; 411 closed_child.status = .closed; 412 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 413 .id = std.fmt.bytesToHex(child_id, .lower), 414 .timestamp = 14, 415 .author = author, 416 .event = .{ .patch = closed_child }, 417 }}); 418 var merged_child = child.event; 419 merged_child.status = .{ .merged = (child.event.revision orelse return error.NotFound).source_oid }; 420 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 421 .id = std.fmt.bytesToHex(child_id, .lower), 422 .timestamp = 15, 423 .author = author, 424 .event = .{ .patch = merged_child }, 425 }}); 426 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 427 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 428 429 _ = arena.reset(.retain_capacity); 430 const status_moment = try evt.currentMoment(repo_opts, &target); 431 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 432 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 433 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 434 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 435 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 436 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 437 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 438 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 439 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 440 try std.testing.expectEqual(null, child_conflict); 441 442 var reopened_child = merged_child_record.event; 443 reopened_child.status = .open; 444 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 445 .id = std.fmt.bytesToHex(child_id, .lower), 446 .timestamp = 16, 447 .author = author, 448 .event = .{ .patch = reopened_child }, 449 }})); 450 451 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 452 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 453 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 454 .timestamp = 16, 455 .author = author, 456 .event = .{ .patch = .{ 457 .title = "invalid merged patch", 458 .description = "has no revision", 459 .tags = "enhancement", 460 .target_branch = "master", 461 .status = .{ .merged = &base_oid }, 462 } }, 463 }})); 464 465 // 466 // retain the ref-free commits during garbage collection 467 // 468 469 { 470 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 471 defer allocator.free(roots); 472 try std.testing.expectEqual(4, roots.len); 473 _ = try target.garbageCollect(io, allocator, roots); 474 475 var after_gc_moment = try target.core.latestMoment(); 476 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 477 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 478 retained.deinit(); 479 } 480 } 481 482 test "patch lifecycle" { 483 try patchLifecycle(.squash); 484 try patchLifecycle(.source); 485 } 486 487 fn patchLifecycle(merge_revision: pch.MergeRevision) !void { 488 const io = std.testing.io; 489 const allocator = std.testing.allocator; 490 491 var temp = std.testing.tmpDir(.{}); 492 defer temp.cleanup(); 493 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 494 defer allocator.free(temp_path); 495 496 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 497 defer allocator.free(repos_dir); 498 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 499 defer allocator.free(admin_path); 500 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 501 defer allocator.free(upstream_path); 502 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 503 defer allocator.free(source_path); 504 505 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 506 const user_id = evt.EventWithId.randomId(prng.random()); 507 const repo_id = evt.EventWithId.randomId(prng.random()); 508 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 509 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 510 defer allocator.free(target_path); 511 512 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 513 defer admin.deinit(io, allocator); 514 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 515 .{ 516 .id = std.fmt.bytesToHex(user_id, .lower), 517 .timestamp = 1, 518 .author = author, 519 .event = .{ .user = .{ 520 .name = author.name, 521 .email = author.email, 522 .password_hash = "", 523 } }, 524 }, 525 .{ 526 .id = repo_id_hex, 527 .timestamp = 1, 528 .author = author, 529 .event = .{ .repo = .{ 530 .user_id = &user_id, 531 .name = "repo", 532 .description = "", 533 } }, 534 }, 535 }); 536 537 // 538 // create the target repo 539 // 540 541 { 542 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 543 defer upstream.deinit(io, allocator); 544 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 545 defer file.close(io); 546 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 547 try upstream.add(io, allocator, &.{"main.zig"}); 548 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 549 } 550 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 551 defer target.deinit(io, allocator); 552 553 // 554 // create the patch draft in the fork 555 // 556 557 const patch_id = evt.EventWithId.randomId(prng.random()); 558 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 559 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 560 .target_branch = "master", 561 .id = patch_id_hex, 562 .user_id = user_id, 563 .repo_id = repo_id, 564 .title = "add the answer", 565 .tags = "enhancement", 566 .description = "adds a reusable answer constant", 567 .author = author, 568 .timestamp = 2, 569 }); 570 defer allocator.free(draft_path); 571 572 // 573 // create and record the proposed revision in the fork 574 // 575 576 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 577 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 578 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 579 defer source.deinit(io, allocator); 580 const source_oid = blk: { 581 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 582 defer file.close(io); 583 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 584 try source.add(io, allocator, &.{"feature.zig"}); 585 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 586 }; 587 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 588 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 589 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 590 const first_entries = [_]evt.EventTreeEntry{ 591 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 592 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 593 }; 594 { 595 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 596 defer draft.deinit(io, allocator); 597 var arena = std.heap.ArenaAllocator.init(allocator); 598 defer arena.deinit(); 599 600 const initial_moment = try evt.currentMoment(repo_opts, &draft); 601 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 602 try std.testing.expect(!initial_patch.removed); 603 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 604 try std.testing.expectEqual(null, initial_patch.event.revision); 605 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 606 607 var source_moment = try source.core.latestMoment(); 608 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 609 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 610 defer objects.deinit(); 611 try objects.include(&source_oid); 612 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 613 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 614 .base_oid = &base_oid, 615 .source_oid = &source_oid, 616 .message = "add the answer", 617 }, &first_entries, 3, patch_id, .{ 618 .title = "add the answer", 619 .tags = "enhancement", 620 .description = "adds a reusable answer constant", 621 .target_branch = "master", 622 }, 3); 623 624 _ = arena.reset(.retain_capacity); 625 const moment = try evt.currentMoment(repo_opts, &draft); 626 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 627 try std.testing.expect(!patch.removed); 628 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 629 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 630 } 631 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 632 633 // 634 // publish another draft before its first push 635 // 636 637 const unpushed_id = evt.EventWithId.randomId(prng.random()); 638 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 639 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 640 .id = unpushed_id_hex, 641 .user_id = user_id, 642 .repo_id = repo_id, 643 .title = "explain the answer", 644 .tags = "documentation", 645 .description = "describes the existing answer", 646 .target_branch = "master", 647 .author = author, 648 .timestamp = 3, 649 }); 650 defer allocator.free(unpushed_path); 651 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 652 .id = unpushed_id_hex, 653 .user_id = user_id, 654 .repo_id = repo_id, 655 .author = author, 656 .timestamp = 4, 657 }); 658 { 659 var verify_arena = std.heap.ArenaAllocator.init(allocator); 660 defer verify_arena.deinit(); 661 const target_moment = try evt.currentMoment(repo_opts, &target); 662 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 663 try std.testing.expectEqual(null, unpushed.event.revision); 664 665 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 666 defer unpushed_repo.deinit(io, allocator); 667 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 668 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 669 try std.testing.expect(removed.removed); 670 } 671 672 // 673 // edit the patch metadata in the fork 674 // 675 676 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 677 .id = patch_id_hex, 678 .user_id = user_id, 679 .repo_id = repo_id, 680 .title = "explain the answer", 681 .tags = "enhancement documentation", 682 .description = "adds and explains a reusable answer constant", 683 .target_branch = "master", 684 .author = author, 685 .timestamp = 4, 686 })); 687 688 // 689 // publish the metadata and revision pointer 690 // 691 692 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 693 .id = patch_id_hex, 694 .user_id = user_id, 695 .repo_id = repo_id, 696 .author = author, 697 .timestamp = 5, 698 }); 699 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 700 .id = patch_id_hex, 701 .user_id = user_id, 702 .repo_id = repo_id, 703 .author = author, 704 .timestamp = 6, 705 }); 706 707 var arena = std.heap.ArenaAllocator.init(allocator); 708 defer arena.deinit(); 709 const moment = try evt.currentMoment(repo_opts, &target); 710 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 711 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 712 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 713 try std.testing.expectEqual(.open, patch.event.status); 714 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 715 716 _ = arena.reset(.retain_capacity); 717 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 718 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 719 try std.testing.expect(!published_fork.removed); 720 try std.testing.expectEqual(.publish, published_fork.event.stage); 721 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 722 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 723 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 724 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 725 { 726 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 727 defer draft.deinit(io, allocator); 728 _ = arena.reset(.retain_capacity); 729 const draft_moment = try evt.currentMoment(repo_opts, &draft); 730 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 731 try std.testing.expect(removed.removed); 732 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 733 @memcpy(&squash_oid, revision.patch_oid); 734 try std.testing.expectEqualStrings("master", removed.event.target_branch); 735 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 736 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 737 @memcpy(&event_oid, revision.event_oid); 738 var repo_moment = try draft.core.latestMoment(); 739 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 740 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 741 defer event_object.deinit(); 742 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 743 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 744 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 745 } 746 747 // 748 // merge the selected revision into the target 749 // 750 751 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 752 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 753 .id = patch_id_hex, 754 .revision = merge_revision, 755 .author = author, 756 .timestamp = 5, 757 })); 758 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 759 760 // run the cache scenario once; both lifecycle runs still perform a merge 761 if (merge_revision == .source) { 762 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 763 } 764 765 const selected_oid = switch (merge_revision) { 766 .squash => squash_oid, 767 .source => source_oid, 768 }; 769 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 770 .id = patch_id_hex, 771 .revision = merge_revision, 772 .author = author, 773 .timestamp = 5, 774 }); 775 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 776 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 777 778 _ = arena.reset(.retain_capacity); 779 const merged_moment = try evt.currentMoment(repo_opts, &target); 780 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 781 try std.testing.expectEqual(.merged, merged.event.status.kind()); 782 const merged_oid = switch (merged.event.status) { 783 .merged => |oid| oid, 784 else => return error.InvalidPatch, 785 }; 786 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 787 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 788 789 _ = arena.reset(.retain_capacity); 790 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; 791 try std.testing.expect(removed_fork.removed); 792 } 793 794 fn indexedForkCount( 795 moment: evt.AdminDB.HashMap(.read_only), 796 index_key: []const u8, 797 parent_id: []const u8, 798 ) !u64 { 799 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 800 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 801 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 802 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 803 return try ids.count(); 804 } 805 806 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 807 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 808 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 809 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 810 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 811 return try ids.count(); 812 } 813 814 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 815 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 816 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 817 var key_buffer: evt.Patch.TagStatusKey = undefined; 818 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 819 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 820 return try ids.count(); 821 } 822 823 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 824 var arena = std.heap.ArenaAllocator.init(allocator); 825 defer arena.deinit(); 826 var moment = try repo.core.latestMoment(); 827 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 828 } 829 830 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 831 const checks = try Repo.DB.HashMap(.read_only).init((try moment.getCursor(hash.hashInt(repo_opts.hash, "patch-id->mergeability"))) orelse return error.NotFound); 832 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 833 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 834 for ([_][]const u8{ "source", "squash" }) |field| { 835 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 836 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 837 } 838 } 839 840 fn testMergeability( 841 target: *Repo, 842 io: std.Io, 843 allocator: std.mem.Allocator, 844 repos_dir: []const u8, 845 id: *const [evt.event_id_size]u8, 846 base: *const [hash.hexLen(repo_opts.hash)]u8, 847 source: *const [hash.hexLen(repo_opts.hash)]u8, 848 ) !void { 849 var arena = std.heap.ArenaAllocator.init(allocator); 850 defer arena.deinit(); 851 const events = try evt.currentMoment(repo_opts, target); 852 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 853 const patch = record.event; 854 855 // publication checks both methods, and reads and cache hits do no work 856 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 857 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 858 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 859 const before = (try target.core.latestMoment()).cursor.slot(); 860 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 861 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 862 { 863 var moment = try target.core.latestMoment(); 864 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 865 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 866 } 867 868 // a target update invalidates the old result before refresh 869 { 870 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 871 defer file.close(io); 872 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 873 } 874 try target.add(io, allocator, &.{"feature.zig"}); 875 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 876 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 877 var squash_input = input; 878 squash_input.revision = .squash; 879 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 880 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 881 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 882 883 // local edits and server comments leave checks alone 884 var edit = evt.Patch.Update{ .fields = .{ 885 .title = "edit the answer", 886 .tags = patch.tags, 887 .description = patch.description, 888 .target_branch = patch.target_branch, 889 } }; 890 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 891 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 892 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 893 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 894 895 // a server patch edit refreshes its mergeability 896 edit.fields.title = patch.title; 897 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 898 const events_before = try target.readRef(io, evt.events_ref); 899 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 900 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 901 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 902 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 903 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 904 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 905 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 906 907 // deleting and restoring the target cannot reuse the conflict result 908 { 909 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 910 defer detached.deinit(); 911 } 912 try target.removeBranch(io, .{ .name = "master" }); 913 pch.refreshMergeability(repo_opts, io, allocator, target, null); 914 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 915 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 916 try target.addBranch(io, .{ .name = "master" }); 917 { 918 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 919 defer restored.deinit(); 920 } 921 pch.refreshMergeability(repo_opts, io, allocator, target, null); 922 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 923 924 // a missing fork disables merging without changing the accepted events 925 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 926 defer allocator.free(draft_path); 927 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 928 defer allocator.free(away_path); 929 { 930 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 931 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 932 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 933 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 934 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 935 } 936 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 937 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 938 939 // a changed revision is unknown even if its target has not moved 940 var changed = patch; 941 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 942 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 943 } 944