╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
647 } 648 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 649 650 // 651 // post the metadata and revision pointer 652 // 653 654 try pch.post(repo_opts, io, allocator, &admin, &target, draft_path, .{ 655 .id = patch_id_hex, 656 .user_id = user_id, 657 .repo_id = repo_id, 658 .author = author, 659 .timestamp = 3, 660 }); 661 try pch.post(repo_opts, io, allocator, &admin, &target, draft_path, .{ 662 .id = patch_id_hex, 663 .user_id = user_id, 664 .repo_id = repo_id, 665 .author = author, 666 .timestamp = 4, 667 }); 668 669 var arena = std.heap.ArenaAllocator.init(allocator); 670 defer arena.deinit(); 671 const moment = try evt.currentMoment(repo_opts, &target); 672 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 673 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 674 try std.testing.expectEqual(.open, patch.event.status); 675 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 676 677 _ = arena.reset(.retain_capacity); 678 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 679 const posted_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 680 try std.testing.expect(!posted_fork.removed); 681 try std.testing.expectEqual(.posted, posted_fork.event.stage); 682 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 683 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 684 try std.testing.expectEqual(1, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 685 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 686 { 687 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 688 defer draft.deinit(io, allocator); 689 _ = arena.reset(.retain_capacity); 690 const draft_moment = try evt.currentMoment(repo_opts, &draft); 691 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 692 try std.testing.expect(removed.removed); 693 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 694 @memcpy(&squash_oid, revision.patch_oid); 695 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 696 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 697 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 698 @memcpy(&event_oid, revision.event_oid); 699 var repo_moment = try draft.core.latestMoment(); 700 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 701 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 702 defer event_object.deinit(); 703 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 704 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 705 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 706 } 707 708 // 709 // merge the selected revision into the target 710 // 711 712 const selected_oid = switch (merge_revision) { 713 .squash => squash_oid, 714 .source => source_oid, 715 }; 716 try pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 717 .id = patch_id_hex, 718 .revision = merge_revision, 719 .author = author, 720 .timestamp = 5, 721 }); 722 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 723 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 724 725 _ = arena.reset(.retain_capacity); 726 const merged_moment = try evt.currentMoment(repo_opts, &target); 727 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 728 try std.testing.expectEqual(.merged, merged.event.status); 729 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 730 } 731 732 fn indexedForkCount( 733 moment: evt.AdminDB.HashMap(.read_only), 734 index_key: []const u8, 735 parent_id: []const u8, 736 ) !u64 { 737 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 738 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 739 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 740 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 741 return try ids.count(); 742 } 743 744 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.Status) !u64 { 745 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 746 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 747 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 748 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 749 return try ids.count(); 750 } 751 752 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.Status) !u64 { 753 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 754 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 755 var key_buffer: evt.Patch.TagStatusKey = undefined; 756 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 757 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 758 return try ids.count(); 759 } 760