╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
555 .repo_id = repo_id, 556 .title = "add the answer", 557 .tags = "enhancement", 558 .description = "adds a reusable answer constant", 559 .author = author, 560 .timestamp = 2, 561 }); 562 defer allocator.free(draft_path); 563 564 // 565 // create and record the proposed revision in the fork 566 // 567 568 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 569 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 570 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 571 defer source.deinit(io, allocator); 572 const source_oid = blk: { 573 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 574 defer file.close(io); 575 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 576 try source.add(io, allocator, &.{"feature.zig"}); 577 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 578 }; 579 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 580 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 581 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 582 const first_entries = [_]evt.EventTreeEntry{ 583 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 584 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 585 }; 586 { 587 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 588 defer draft.deinit(io, allocator); 589 var arena = std.heap.ArenaAllocator.init(allocator); 590 defer arena.deinit(); 591 592 const initial_moment = try evt.currentMoment(repo_opts, &draft); 593 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 594 try std.testing.expect(!initial_patch.removed); 595 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 596 try std.testing.expectEqual(null, initial_patch.event.patchrev_id); 597 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 598 599 var source_moment = try source.core.latestMoment(); 600 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 601 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 602 defer objects.deinit(); 603 try objects.include(&source_oid); 604 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 605 try evt.consume(.fork, .xit, repo_opts, io, allocator, &draft, evt.events_ref, &.{ 606 .{ 607 .id = first_patchrev_hex, 608 .timestamp = 3, 609 .author = author, 610 .tree_entries = &first_entries, 611 .event = .{ .patchrev = .{ 612 .base_oid = &base_oid, 613 .source_oid = &source_oid, 614 .target_ref = "refs/heads/master", 615 .message = "add the answer", 616 } }, 617 }, 618 .{ 619 .id = patch_id_hex, 620 .timestamp = 3, 621 .author = author, 622 .event = .{ .patch = .{ 623 .title = "add the answer", 624 .tags = "enhancement", 625 .description = "adds a reusable answer constant", 626 .patchrev_id = first_patchrev_hex, 627 } }, 628 }, 629 }); 630 631 _ = arena.reset(.retain_capacity); 632 const moment = try evt.currentMoment(repo_opts, &draft); 633 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 634 try std.testing.expect(!patch.removed); 635 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.patchrev_id orelse return error.NotFound)); 636 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 637 } 638 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 639 640 // 641 // publish the metadata and revision pointer 642 // 643 644 try fork.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 645 .id = patch_id_hex, 646 .user_id = user_id, 647 .repo_id = repo_id, 648 .author = author, 649 .timestamp = 3, 650 }); 651 try fork.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 652 .id = patch_id_hex, 653 .user_id = user_id, 654 .repo_id = repo_id, 655 .author = author, 656 .timestamp = 4, 657 }); 658 659 var arena = std.heap.ArenaAllocator.init(allocator); 660 defer arena.deinit(); 661 const moment = try evt.currentMoment(repo_opts, &target); 662 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 663 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.patchrev_id orelse return error.NotFound)); 664 try std.testing.expectEqual(.open, patch.event.status); 665 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 666 667 _ = arena.reset(.retain_capacity); 668 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 669 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 670 try std.testing.expect(!published_fork.removed); 671 try std.testing.expectEqual(.posted, published_fork.event.stage); 672 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 673 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 674 try std.testing.expectEqual(1, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 675 { 676 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 677 defer draft.deinit(io, allocator); 678 _ = arena.reset(.retain_capacity); 679 const draft_moment = try evt.currentMoment(repo_opts, &draft); 680 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 681 try std.testing.expect(removed.removed); 682 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 683 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 684 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 685 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 686 @memcpy(&event_oid, revision.event_oid); 687 var repo_moment = try draft.core.latestMoment(); 688 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 689 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 690 defer event_object.deinit(); 691 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 692 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 693 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 694 } 695 } 696 697 fn indexedForkCount( 698 moment: evt.AdminDB.HashMap(.read_only), 699 index_key: []const u8, 700 parent_id: []const u8, 701 ) !u64 { 702 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 703 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 704 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 705 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 706 return try ids.count(); 707 } 708 709 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.Status) !u64 { 710 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 711 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 712 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 713 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 714 return try ids.count(); 715 } 716 717 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.Status) !u64 { 718 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 719 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 720 var key_buffer: evt.Patch.TagStatusKey = undefined; 721 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 722 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 723 return try ids.count(); 724 } 725