╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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