╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
356 var patch = patch_value; 357 patch.revision = evt.Patch.Revision.fromRecord(revision_id, record); 358 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 359 .id = std.fmt.bytesToHex(patch_id, .lower), 360 .timestamp = patch_timestamp, 361 .author = author, 362 .event = .{ .patch = patch }, 363 }}); 364 } 365 366 test "patch event conflicts, stacking, and gc" { 367 const io = std.testing.io; 368 const allocator = std.testing.allocator; 369 370 // create the temp dir 371 var temp = std.testing.tmpDir(.{}); 372 defer temp.cleanup(); 373 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 374 defer allocator.free(temp_path); 375 376 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 377 defer allocator.free(upstream_path); 378 const target_path = try std.fs.path.join(allocator, &.{ temp_path, "target" }); 379 defer allocator.free(target_path); 380 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 381 defer allocator.free(source_path); 382 383 // 384 // create the upstream repo and its base commit 385 // 386 387 const base_oid = blk: { 388 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 389 defer upstream.deinit(io, allocator); 390 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 391 defer file.close(io); 392 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 393 try upstream.add(io, allocator, &.{"main.zig"}); 394 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 395 }; 396 397 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 398 defer target.deinit(io, allocator); 399 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 400 401 // 402 // fork the target and commit the proposed change 403 // 404 405 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 406 defer source.deinit(io, allocator); 407 408 const source_oid = blk: { 409 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 410 defer file.close(io); 411 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 412 try source.add(io, allocator, &.{"feature.zig"}); 413 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 414 }; 415 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 416 417 // 418 // copy the proposed objects without creating a target ref 419 // 420 421 { 422 var source_moment = try source.core.latestMoment(); 423 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 424 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 425 defer objects.deinit(); 426 try objects.include(&source_oid); 427 try target.copyObjects(.xit, repo_opts, &objects, io, null); 428 } 429 430 // 431 // define the patch revision and patch 432 // 433 434 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 435 const patchrev_id = evt.EventWithId.randomId(prng.random()); 436 const patch_id = evt.EventWithId.randomId(prng.random()); 437 const patch = evt.Patch{ 438 .title = "add the answer", 439 .description = "adds a reusable answer constant", 440 .tags = "enhancement", 441 .target_branch = "master", 442 }; 443 const tree_entries = [_]evt.EventTreeEntry{ 444 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 445 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 446 }; 447 448 // reject invalid branches even when the event bypasses patch creation 449 { 450 const cases = [_]struct { source: ?[]const u8, target: []const u8, expected_error: anyerror }{ 451 .{ .source = "master", .target = "master", .expected_error = error.SameBranch }, 452 .{ .source = null, .target = evt.events_ref.name, .expected_error = error.InvalidTargetBranch }, 453 }; 454 for (cases) |case| { 455 var invalid = patch; 456 invalid.source_branch = case.source; 457 invalid.target_branch = case.target; 458 try std.testing.expectError(case.expected_error, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 459 .id = std.fmt.bytesToHex(patch_id, .lower), 460 .timestamp = 3, 461 .author = author, 462 .event = .{ .patch = invalid }, 463 }})); 464 try std.testing.expectEqual(null, try target.readRef(io, evt.events_ref)); 465 } 466 } 467 468 // 469 // consume the patch revision and patch 470 // 471 472 { 473 try consumePatchWithRevision( 474 .repo, 475 io, 476 allocator, 477 &target, 478 evt.events_ref, 479 patchrev_id, 480 .{ .base_oid = &base_oid, .source_oid = &source_oid, .message = patch.title }, 481 &tree_entries, 482 3, 483 patch_id, 484 patch, 485 4, 486 ); 487 } 488 489 // 490 // check the records and squash commit 491 // 492 493 var arena = std.heap.ArenaAllocator.init(allocator); 494 defer arena.deinit(); 495 const moment = try evt.currentMoment(repo_opts, &target); 496 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 497 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 498 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 499 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 500 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 501 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 502 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 503 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 504 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 505 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 506 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 507 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 508 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 509 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 510 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 511 512 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 513 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 514 { 515 var repo_moment = try target.core.latestMoment(); 516 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 517 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 518 defer patch_object.deinit(); 519 switch (patch_object.content) { 520 .commit => |commit| { 521 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 522 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 523 try std.testing.expectEqual(1, parents.len); 524 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 525 }, 526 else => return error.InvalidObject, 527 } 528 } 529 530 // 531 // update metadata without creating another patch revision 532 // 533 534 var updated_patch = patch; 535 updated_patch.revision = .{ 536 .id = std.fmt.bytesToHex(patchrev_id, .lower), 537 .squash_oid = &initial_patch_oid, 538 .source_oid = &source_oid, 539 }; 540 updated_patch.title = "add a reusable answer"; 541 { 542 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 543 .id = std.fmt.bytesToHex(patch_id, .lower), 544 .timestamp = 5, 545 .author = author, 546 .event = .{ .patch = updated_patch }, 547 }}); 548 } 549 550 _ = arena.reset(.retain_capacity); 551 const updated_moment = try evt.currentMoment(repo_opts, &target); 552 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 553 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 554 555 // 556 // branch the events history before creating competing revisions 557 // 558 559 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 560 { 561 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 562 defer result.deinit(); 563 try target.addBranch(io, .{ .name = side_events_ref.name }); 564 } 565 566 // 567 // create two competing code revisions 568 // 569 570 const source_a_oid = blk: { 571 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 572 defer file.close(io); 573 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 574 try source.add(io, allocator, &.{"feature.zig"}); 575 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 576 }; 577 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 578 const source_b_oid = blk: { 579 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 580 defer file.close(io); 581 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 582 try source.add(io, allocator, &.{"feature.zig"}); 583 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 584 }; 585 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 586 { 587 var source_moment = try source.core.latestMoment(); 588 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 589 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 590 defer objects.deinit(); 591 try objects.include(&source_b_oid); 592 try target.copyObjects(.xit, repo_opts, &objects, io, null); 593 } 594 595 // 596 // select the first revision on the events branch 597 // 598 599 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 600 const target_entries = [_]evt.EventTreeEntry{ 601 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 602 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 603 }; 604 var target_patch = updated_patch; 605 target_patch.status = .closed; 606 { 607 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 608 .base_oid = &base_oid, 609 .source_oid = &source_a_oid, 610 .message = updated_patch.title, 611 }, &target_entries, 8, patch_id, target_patch, 9); 612 } 613 614 // 615 // select the second revision on the side branch 616 // 617 618 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 619 const parent_entries = [_]evt.EventTreeEntry{ 620 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 621 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 622 }; 623 var parent_patch = updated_patch; 624 parent_patch.status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(patchrev_b_id, .lower) } }; 625 { 626 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 627 .base_oid = &base_oid, 628 .source_oid = &source_b_oid, 629 .message = updated_patch.title, 630 }, &parent_entries, 10, patch_id, parent_patch, 11); 631 } 632 633 // 634 // merge the competing patch revisions 635 // 636 637 { 638 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 639 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 640 } 641 642 // 643 // check the selected revision and conflict 644 // 645 646 _ = arena.reset(.retain_capacity); 647 const merged_moment = try evt.currentMoment(repo_opts, &target); 648 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 649 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 650 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 651 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 652 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 653 654 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 655 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 656 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 657 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 658 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 659 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 660 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 661 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 662 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 663 664 // 665 // create a patch stacked on the selected revision 666 // 667 668 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 669 const child_id = evt.EventWithId.randomId(prng.random()); 670 const child_entries = [_]evt.EventTreeEntry{ 671 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 672 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 673 }; 674 { 675 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 676 .base_oid = merged_patchrev.patch_oid, 677 .source_oid = &source_b_oid, 678 .message = "stack another answer change", 679 }, &child_entries, 12, child_id, .{ 680 .title = "stack another answer change", 681 .description = "depends on the first patch", 682 .tags = "enhancement", 683 .target_branch = "master", 684 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 685 }, 13); 686 } 687 688 // 689 // check the squash commit and reverse index 690 // 691 692 _ = arena.reset(.retain_capacity); 693 const stacked_moment = try evt.currentMoment(repo_opts, &target); 694 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 695 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 696 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 697 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 698 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 699 @memcpy(&child_oid, child_patchrev.patch_oid); 700 { 701 var child_repo_moment = try target.core.latestMoment(); 702 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 703 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 704 defer child_object.deinit(); 705 const child_commit = switch (child_object.content) { 706 .commit => |commit| commit, 707 else => return error.InvalidObject, 708 }; 709 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 710 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 711 } 712 713 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; 714 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 715 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 716 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 717 try std.testing.expectEqual(1, try children.count()); 718 719 // 720 // close and merge the same revision concurrently 721 // 722 723 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 724 { 725 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 726 defer result.deinit(); 727 try target.addBranch(io, .{ .name = status_side_ref.name }); 728 } 729 var closed_child = child.event; 730 closed_child.status = .closed; 731 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 732 .id = std.fmt.bytesToHex(child_id, .lower), 733 .timestamp = 14, 734 .author = author, 735 .event = .{ .patch = closed_child }, 736 }}); 737 var merged_child = child.event; 738 merged_child.status = .{ .merged = .{ .revision = .source, .patchrev_id = (child.event.revision orelse return error.NotFound).id } }; 739 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 740 .id = std.fmt.bytesToHex(child_id, .lower), 741 .timestamp = 15, 742 .author = author, 743 .event = .{ .patch = merged_child }, 744 }}); 745 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 746 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 747 748 _ = arena.reset(.retain_capacity); 749 const status_moment = try evt.currentMoment(repo_opts, &target); 750 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 751 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 752 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 753 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 754 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 755 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 756 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 757 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 758 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 759 try std.testing.expectEqual(null, child_conflict); 760 761 var reopened_child = merged_child_record.event; 762 reopened_child.status = .open; 763 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 764 .id = std.fmt.bytesToHex(child_id, .lower), 765 .timestamp = 16, 766 .author = author, 767 .event = .{ .patch = reopened_child }, 768 }})); 769 770 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 771 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 772 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 773 .timestamp = 16, 774 .author = author, 775 .event = .{ .patch = .{ 776 .title = "invalid merged patch", 777 .description = "has no revision", 778 .tags = "enhancement", 779 .target_branch = "master", 780 .status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(invalid_merged_id, .lower) } }, 781 } }, 782 }})); 783 784 // 785 // retain the ref-free commits during garbage collection 786 // 787 788 { 789 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 790 defer allocator.free(roots); 791 try std.testing.expectEqual(4, roots.len); 792 _ = try target.garbageCollect(io, allocator, .{ .extra_roots = roots }); 793 794 var after_gc_moment = try target.core.latestMoment(); 795 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 796 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 797 retained.deinit(); 798 } 799 } 800 801 test "patch lifecycle" { 802 try patchLifecycle(.squash); 803 try patchLifecycle(.source); 804 } 805 806 fn patchLifecycle(merge_revision: evt.Patch.MergeRevision) !void { 807 const io = std.testing.io; 808 const allocator = std.testing.allocator; 809 810 var temp = std.testing.tmpDir(.{}); 811 defer temp.cleanup(); 812 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 813 defer allocator.free(temp_path); 814 815 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 816 defer allocator.free(repos_dir); 817 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 818 defer allocator.free(admin_path); 819 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 820 defer allocator.free(upstream_path); 821 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 822 defer allocator.free(source_path); 823 824 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 825 const user_id = evt.EventWithId.randomId(prng.random()); 826 const repo_id = evt.EventWithId.randomId(prng.random()); 827 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 828 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 829 defer allocator.free(target_path); 830 831 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 832 defer admin.deinit(io, allocator); 833 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 834 .{ 835 .id = std.fmt.bytesToHex(user_id, .lower), 836 .timestamp = 1, 837 .author = author, 838 .event = .{ .user = .{ 839 .name = author.name, 840 .email = author.email, 841 .password_hash = "", 842 } }, 843 }, 844 .{ 845 .id = repo_id_hex, 846 .timestamp = 1, 847 .author = author, 848 .event = .{ .repo = .{ 849 .user_id = &user_id, 850 .name = "repo", 851 .description = "", 852 } }, 853 }, 854 }); 855 856 // 857 // create the target repo 858 // 859 860 { 861 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 862 defer upstream.deinit(io, allocator); 863 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 864 defer file.close(io); 865 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 866 try upstream.add(io, allocator, &.{"main.zig"}); 867 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 868 } 869 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 870 defer target.deinit(io, allocator); 871 872 // 873 // create the patch draft in the fork 874 // 875 876 const patch_id = evt.EventWithId.randomId(prng.random()); 877 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 878 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 879 .target_branch = "master", 880 .id = patch_id_hex, 881 .user_id = user_id, 882 .repo_id = repo_id, 883 .title = "add the answer", 884 .tags = "enhancement", 885 .description = "adds a reusable answer constant", 886 .author = author, 887 .timestamp = 2, 888 }); 889 defer allocator.free(draft_path); 890 891 // 892 // create and record the proposed revision in the fork 893 // 894 895 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 896 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 897 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 898 defer source.deinit(io, allocator); 899 const source_oid = blk: { 900 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 901 defer file.close(io); 902 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 903 try source.add(io, allocator, &.{"feature.zig"}); 904 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 905 }; 906 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 907 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 908 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 909 const first_entries = [_]evt.EventTreeEntry{ 910 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 911 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 912 }; 913 { 914 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 915 defer draft.deinit(io, allocator); 916 var arena = std.heap.ArenaAllocator.init(allocator); 917 defer arena.deinit(); 918 919 const initial_moment = try evt.currentMoment(repo_opts, &draft); 920 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 921 try std.testing.expect(!initial_patch.removed); 922 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 923 try std.testing.expectEqual(null, initial_patch.event.revision); 924 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 925 926 var source_moment = try source.core.latestMoment(); 927 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 928 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 929 defer objects.deinit(); 930 try objects.include(&source_oid); 931 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 932 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 933 .base_oid = &base_oid, 934 .source_oid = &source_oid, 935 .message = "add the answer", 936 }, &first_entries, 3, patch_id, .{ 937 .title = "add the answer", 938 .tags = "enhancement", 939 .description = "adds a reusable answer constant", 940 .target_branch = "master", 941 }, 3); 942 943 _ = arena.reset(.retain_capacity); 944 const moment = try evt.currentMoment(repo_opts, &draft); 945 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 946 try std.testing.expect(!patch.removed); 947 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 948 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 949 } 950 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 951 952 // draft mergeability checks leave the target repo untouched 953 const before_draft_check = (try target.core.latestMoment()).cursor.slot(); 954 pch.refreshMergeability(repo_opts, io, allocator, &target, patch_id); 955 try std.testing.expectEqualDeep(before_draft_check, (try target.core.latestMoment()).cursor.slot()); 956 957 // 958 // publish another draft before its first push 959 // 960 961 const unpushed_id = evt.EventWithId.randomId(prng.random()); 962 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 963 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 964 .id = unpushed_id_hex, 965 .user_id = user_id, 966 .repo_id = repo_id, 967 .title = "explain the answer", 968 .tags = "documentation", 969 .description = "describes the existing answer", 970 .target_branch = "master", 971 .author = author, 972 .timestamp = 3, 973 }); 974 defer allocator.free(unpushed_path); 975 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 976 .id = unpushed_id_hex, 977 .user_id = user_id, 978 .repo_id = repo_id, 979 .author = author, 980 .timestamp = 4, 981 }); 982 { 983 var verify_arena = std.heap.ArenaAllocator.init(allocator); 984 defer verify_arena.deinit(); 985 const target_moment = try evt.currentMoment(repo_opts, &target); 986 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 987 try std.testing.expectEqual(null, unpushed.event.revision); 988 989 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 990 defer unpushed_repo.deinit(io, allocator); 991 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 992 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 993 try std.testing.expect(removed.removed); 994 } 995 996 // 997 // edit the patch metadata in the fork 998 // 999 1000 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 1001 .id = patch_id_hex, 1002 .user_id = user_id, 1003 .repo_id = repo_id, 1004 .title = "explain the answer", 1005 .tags = "enhancement documentation", 1006 .description = "adds and explains a reusable answer constant", 1007 .target_branch = "master", 1008 .author = author, 1009 .timestamp = 4, 1010 })); 1011 1012 // 1013 // publish the metadata and revision pointer 1014 // 1015 1016 const before_publish = try target.core.db.rootCursor().count(); 1017 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1018 .id = patch_id_hex, 1019 .user_id = user_id, 1020 .repo_id = repo_id, 1021 .author = author, 1022 .timestamp = 5, 1023 }); 1024 try std.testing.expectEqual(before_publish + 1, try target.core.db.rootCursor().count()); 1025 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1026 .id = patch_id_hex, 1027 .user_id = user_id, 1028 .repo_id = repo_id, 1029 .author = author, 1030 .timestamp = 6, 1031 }); 1032 1033 var arena = std.heap.ArenaAllocator.init(allocator); 1034 defer arena.deinit(); 1035 const moment = try evt.currentMoment(repo_opts, &target); 1036 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1037 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1038 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1039 try std.testing.expectEqual(.open, patch.event.status); 1040 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1041 1042 _ = arena.reset(.retain_capacity); 1043 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 1044 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 1045 try std.testing.expect(!published_fork.removed); 1046 try std.testing.expectEqual(.publish, published_fork.event.stage); 1047 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 1048 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 1049 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 1050 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1051 { 1052 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1053 defer draft.deinit(io, allocator); 1054 _ = arena.reset(.retain_capacity); 1055 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1056 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1057 try std.testing.expect(removed.removed); 1058 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1059 @memcpy(&squash_oid, revision.patch_oid); 1060 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1061 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1062 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1063 @memcpy(&event_oid, revision.event_oid); 1064 var repo_moment = try draft.core.latestMoment(); 1065 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1066 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1067 defer event_object.deinit(); 1068 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1069 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1070 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1071 } 1072 1073 // 1074 // merge the selected revision into the target 1075 // 1076 1077 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1078 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 1079 .id = patch_id_hex, 1080 .revision = merge_revision, 1081 .author = author, 1082 .timestamp = 5, 1083 })); 1084 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1085 1086 // run the cache scenario once; both lifecycle runs still perform a merge 1087 if (merge_revision == .source) { 1088 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 1089 } 1090 1091 const selected_oid = switch (merge_revision) { 1092 .squash => squash_oid, 1093 .source => source_oid, 1094 }; 1095 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 1096 .id = patch_id_hex, 1097 .revision = merge_revision, 1098 .author = author, 1099 .timestamp = 5, 1100 }); 1101 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1102 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1103 1104 _ = arena.reset(.retain_capacity); 1105 const merged_moment = try evt.currentMoment(repo_opts, &target); 1106 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1107 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1108 const merged_status = switch (merged.event.status) { 1109 .merged => |value| value, 1110 else => return error.InvalidPatch, 1111 }; 1112 try std.testing.expectEqual(merge_revision, merged_status.revision); 1113 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1114 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1115 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1116 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1117 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1118 1119 _ = arena.reset(.retain_capacity); 1120 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; 1121 try std.testing.expect(removed_fork.removed); 1122 } 1123 1124 fn indexedForkCount( 1125 moment: evt.AdminDB.HashMap(.read_only), 1126 index_key: []const u8, 1127 parent_id: []const u8, 1128 ) !u64 { 1129 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 1130 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 1131 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 1132 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 1133 return try ids.count(); 1134 } 1135 1136 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1137 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1138 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1139 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1140 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1141 return try ids.count(); 1142 } 1143 1144 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 1145 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 1146 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 1147 var key_buffer: evt.Patch.TagStatusKey = undefined; 1148 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 1149 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1150 return try ids.count(); 1151 } 1152 1153 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1154 var arena = std.heap.ArenaAllocator.init(allocator); 1155 defer arena.deinit(); 1156 var moment = try repo.core.latestMoment(); 1157 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1158 } 1159 1160 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1161 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1162 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1163 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); 1164 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1165 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1166 for ([_][]const u8{ "source", "squash" }) |field| { 1167 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1168 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1169 } 1170 } 1171 1172 fn testMergeability( 1173 target: *Repo, 1174 io: std.Io, 1175 allocator: std.mem.Allocator, 1176 repos_dir: []const u8, 1177 id: *const [evt.event_id_size]u8, 1178 base: *const [hash.hexLen(repo_opts.hash)]u8, 1179 source: *const [hash.hexLen(repo_opts.hash)]u8, 1180 ) !void { 1181 var arena = std.heap.ArenaAllocator.init(allocator); 1182 defer arena.deinit(); 1183 const events = try evt.currentMoment(repo_opts, target); 1184 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1185 const patch = record.event; 1186 1187 // publication checks both methods, and reads and cache hits do no work 1188 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1189 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1190 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1191 const before = (try target.core.latestMoment()).cursor.slot(); 1192 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1193 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1194 { 1195 var moment = try target.core.latestMoment(); 1196 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1197 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1198 } 1199 1200 // a target update invalidates the old result before refresh 1201 { 1202 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1203 defer file.close(io); 1204 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1205 } 1206 try target.add(io, allocator, &.{"feature.zig"}); 1207 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1208 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1209 var squash_input = input; 1210 squash_input.revision = .squash; 1211 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1212 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1213 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1214 1215 // local edits and server comments leave checks alone 1216 var edit = evt.Patch.Update{ .fields = .{ 1217 .title = "edit the answer", 1218 .tags = patch.tags, 1219 .description = patch.description, 1220 .target_branch = patch.target_branch, 1221 } }; 1222 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1223 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1224 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1225 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1226 1227 // a server patch edit refreshes its mergeability 1228 edit.fields.title = patch.title; 1229 const before_edit = try target.core.db.rootCursor().count(); 1230 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1231 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1232 const events_before = try target.readRef(io, evt.events_ref); 1233 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1234 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1235 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1236 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1237 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1238 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1239 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1240 1241 // deleting and restoring the target cannot reuse the conflict result 1242 { 1243 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1244 defer detached.deinit(); 1245 } 1246 try target.removeBranch(io, .{ .name = "master" }); 1247 pch.refreshOpenMergeability(repo_opts, io, allocator, target, null, null); 1248 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1249 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1250 try target.addBranch(io, .{ .name = "master" }); 1251 { 1252 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1253 defer restored.deinit(); 1254 } 1255 pch.refreshOpenMergeability(repo_opts, io, allocator, target, null, null); 1256 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1257 1258 // a missing fork disables merging without changing the accepted events 1259 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1260 defer allocator.free(draft_path); 1261 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1262 defer allocator.free(away_path); 1263 { 1264 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1265 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1266 { 1267 const log_level = std.testing.log_level; 1268 std.testing.log_level = .err; 1269 defer std.testing.log_level = log_level; 1270 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1271 } 1272 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1273 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1274 } 1275 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1276 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1277 1278 // a changed revision is unknown even if its target has not moved 1279 var changed = patch; 1280 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1281 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1282 } 1283