╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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(.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(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 759 760 const selected_oid = switch (merge_revision) { 761 .squash => squash_oid, 762 .source => source_oid, 763 }; 764 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 765 .id = patch_id_hex, 766 .revision = merge_revision, 767 .author = author, 768 .timestamp = 5, 769 }); 770 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 771 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 772 773 _ = arena.reset(.retain_capacity); 774 const merged_moment = try evt.currentMoment(repo_opts, &target); 775 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 776 try std.testing.expectEqual(.merged, merged.event.status.kind()); 777 const merged_oid = switch (merged.event.status) { 778 .merged => |oid| oid, 779 else => return error.InvalidPatch, 780 }; 781 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 782 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 783 784 _ = arena.reset(.retain_capacity); 785 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; 786 try std.testing.expect(removed_fork.removed); 787 } 788 789 fn indexedForkCount( 790 moment: evt.AdminDB.HashMap(.read_only), 791 index_key: []const u8, 792 parent_id: []const u8, 793 ) !u64 { 794 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 795 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 796 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 797 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 798 return try ids.count(); 799 } 800 801 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 802 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 803 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 804 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 805 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 806 return try ids.count(); 807 } 808 809 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 810 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 811 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 812 var key_buffer: evt.Patch.TagStatusKey = undefined; 813 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 814 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 815 return try ids.count(); 816 } 817