╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
641 _ = arena.reset(.retain_capacity); 642 const moment = try evt.currentMoment(repo_opts, &draft); 643 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 644 try std.testing.expect(!patch.removed); 645 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 646 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 647 } 648 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 649 650 // 651 // publish another draft before its first push 652 // 653 654 const unpushed_id = evt.EventWithId.randomId(prng.random()); 655 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 656 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 657 .id = unpushed_id_hex, 658 .user_id = user_id, 659 .repo_id = repo_id, 660 .title = "explain the answer", 661 .tags = "documentation", 662 .description = "describes the existing answer", 663 .target_branch = "master", 664 .author = author, 665 .timestamp = 3, 666 }); 667 defer allocator.free(unpushed_path); 668 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 669 .id = unpushed_id_hex, 670 .user_id = user_id, 671 .repo_id = repo_id, 672 .author = author, 673 .timestamp = 4, 674 }); 675 { 676 var verify_arena = std.heap.ArenaAllocator.init(allocator); 677 defer verify_arena.deinit(); 678 const target_moment = try evt.currentMoment(repo_opts, &target); 679 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 680 try std.testing.expectEqual(null, unpushed.event.revision); 681 682 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 683 defer unpushed_repo.deinit(io, allocator); 684 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 685 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 686 try std.testing.expect(removed.removed); 687 } 688 689 // 690 // edit the patch metadata in the fork 691 // 692 693 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 694 .id = patch_id_hex, 695 .user_id = user_id, 696 .repo_id = repo_id, 697 .title = "explain the answer", 698 .tags = "enhancement documentation", 699 .description = "adds and explains a reusable answer constant", 700 .target_branch = "master", 701 .author = author, 702 .timestamp = 4, 703 })); 704 705 // 706 // publish the metadata and revision pointer 707 // 708 709 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 710 .id = patch_id_hex, 711 .user_id = user_id, 712 .repo_id = repo_id, 713 .author = author, 714 .timestamp = 5, 715 }); 716 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 717 .id = patch_id_hex, 718 .user_id = user_id, 719 .repo_id = repo_id, 720 .author = author, 721 .timestamp = 6, 722 }); 723 724 var arena = std.heap.ArenaAllocator.init(allocator); 725 defer arena.deinit(); 726 const moment = try evt.currentMoment(repo_opts, &target); 727 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 728 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 729 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 730 try std.testing.expectEqual(.open, patch.event.status); 731 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 732 733 _ = arena.reset(.retain_capacity); 734 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 735 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 736 try std.testing.expect(!published_fork.removed); 737 try std.testing.expectEqual(.publish, published_fork.event.stage); 738 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 739 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 740 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 741 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 742 { 743 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 744 defer draft.deinit(io, allocator); 745 _ = arena.reset(.retain_capacity); 746 const draft_moment = try evt.currentMoment(repo_opts, &draft); 747 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 748 try std.testing.expect(removed.removed); 749 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 750 @memcpy(&squash_oid, revision.patch_oid); 751 try std.testing.expectEqualStrings("master", removed.event.target_branch); 752 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 753 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 754 @memcpy(&event_oid, revision.event_oid); 755 var repo_moment = try draft.core.latestMoment(); 756 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 757 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 758 defer event_object.deinit(); 759 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 760 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 761 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 762 } 763 764 // 765 // merge the selected revision into the target 766 // 767 768 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 769 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 770 .id = patch_id_hex, 771 .revision = merge_revision, 772 .author = author, 773 .timestamp = 5, 774 })); 775 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 776 777 const selected_oid = switch (merge_revision) { 778 .squash => squash_oid, 779 .source => source_oid, 780 }; 781 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 782 .id = patch_id_hex, 783 .revision = merge_revision, 784 .author = author, 785 .timestamp = 5, 786 }); 787 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 788 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 789 790 _ = arena.reset(.retain_capacity); 791 const merged_moment = try evt.currentMoment(repo_opts, &target); 792 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 793 try std.testing.expectEqual(.merged, merged.event.status.kind()); 794 const merged_oid = switch (merged.event.status) { 795 .merged => |oid| oid, 796 else => return error.InvalidPatch, 797 }; 798 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 799 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 800 801 _ = arena.reset(.retain_capacity); 802 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; 803 try std.testing.expect(removed_fork.removed); 804 } 805 806 fn indexedForkCount( 807 moment: evt.AdminDB.HashMap(.read_only), 808 index_key: []const u8, 809 parent_id: []const u8, 810 ) !u64 { 811 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 812 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 813 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 814 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 815 return try ids.count(); 816 } 817 818 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 819 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 820 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 821 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 822 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 823 return try ids.count(); 824 } 825 826 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 827 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 828 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 829 var key_buffer: evt.Patch.TagStatusKey = undefined; 830 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 831 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 832 return try ids.count(); 833 } 834