╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
581 .title = "add the answer", 582 .tags = "enhancement", 583 .description = "adds a reusable answer constant", 584 .author = author, 585 .timestamp = 2, 586 }); 587 defer allocator.free(draft_path); 588 589 // 590 // create and record the proposed revision in the fork 591 // 592 593 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 594 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 595 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 596 defer source.deinit(io, allocator); 597 const source_oid = blk: { 598 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 599 defer file.close(io); 600 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 601 try source.add(io, allocator, &.{"feature.zig"}); 602 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 603 }; 604 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 605 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 606 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 607 const first_entries = [_]evt.EventTreeEntry{ 608 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 609 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 610 }; 611 { 612 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 613 defer draft.deinit(io, allocator); 614 var arena = std.heap.ArenaAllocator.init(allocator); 615 defer arena.deinit(); 616 617 const initial_moment = try evt.currentMoment(repo_opts, &draft); 618 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 619 try std.testing.expect(!initial_patch.removed); 620 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 621 try std.testing.expectEqual(null, initial_patch.event.revision); 622 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 623 624 var source_moment = try source.core.latestMoment(); 625 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 626 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 627 defer objects.deinit(); 628 try objects.include(&source_oid); 629 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 630 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 631 .base_oid = &base_oid, 632 .source_oid = &source_oid, 633 .target_ref = "refs/heads/master", 634 .message = "add the answer", 635 }, &first_entries, 3, patch_id, .{ 636 .title = "add the answer", 637 .tags = "enhancement", 638 .description = "adds a reusable answer constant", 639 }, 3); 640 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 .author = author, 664 .timestamp = 3, 665 }); 666 defer allocator.free(unpushed_path); 667 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 668 .id = unpushed_id_hex, 669 .user_id = user_id, 670 .repo_id = repo_id, 671 .author = author, 672 .timestamp = 4, 673 }); 674 { 675 var verify_arena = std.heap.ArenaAllocator.init(allocator); 676 defer verify_arena.deinit(); 677 const target_moment = try evt.currentMoment(repo_opts, &target); 678 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 679 try std.testing.expectEqual(null, unpushed.event.revision); 680 681 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 682 defer unpushed_repo.deinit(io, allocator); 683 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 684 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 685 try std.testing.expect(removed.removed); 686 } 687 688 // 689 // edit the patch metadata in the fork 690 // 691 692 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 693 .id = patch_id_hex, 694 .user_id = user_id, 695 .repo_id = repo_id, 696 .title = "explain the answer", 697 .tags = "enhancement documentation", 698 .description = "adds and explains a reusable answer constant", 699 .author = author, 700 .timestamp = 4, 701 })); 702 703 // 704 // publish the metadata and revision pointer 705 // 706 707 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 708 .id = patch_id_hex, 709 .user_id = user_id, 710 .repo_id = repo_id, 711 .author = author, 712 .timestamp = 5, 713 }); 714 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 715 .id = patch_id_hex, 716 .user_id = user_id, 717 .repo_id = repo_id, 718 .author = author, 719 .timestamp = 6, 720 }); 721 722 var arena = std.heap.ArenaAllocator.init(allocator); 723 defer arena.deinit(); 724 const moment = try evt.currentMoment(repo_opts, &target); 725 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 726 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 727 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 728 try std.testing.expectEqual(.open, patch.event.status); 729 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 730 731 _ = arena.reset(.retain_capacity); 732 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 733 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 734 try std.testing.expect(!published_fork.removed); 735 try std.testing.expectEqual(.publish, published_fork.event.stage); 736 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 737 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 738 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 739 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 740 { 741 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 742 defer draft.deinit(io, allocator); 743 _ = arena.reset(.retain_capacity); 744 const draft_moment = try evt.currentMoment(repo_opts, &draft); 745 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 746 try std.testing.expect(removed.removed); 747 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 748 @memcpy(&squash_oid, revision.patch_oid); 749 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 750 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 751 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 752 @memcpy(&event_oid, revision.event_oid); 753 var repo_moment = try draft.core.latestMoment(); 754 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 755 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 756 defer event_object.deinit(); 757 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 758 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 759 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 760 } 761 762 // 763 // merge the selected revision into the target 764 // 765 766 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 767 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 768 .id = patch_id_hex, 769 .revision = merge_revision, 770 .author = author, 771 .timestamp = 5, 772 })); 773 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 774 775 const selected_oid = switch (merge_revision) { 776 .squash => squash_oid, 777 .source => source_oid, 778 }; 779 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 780 .id = patch_id_hex, 781 .revision = merge_revision, 782 .author = author, 783 .timestamp = 5, 784 }); 785 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 786 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 787 788 _ = arena.reset(.retain_capacity); 789 const merged_moment = try evt.currentMoment(repo_opts, &target); 790 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 791 try std.testing.expectEqual(.merged, merged.event.status.kind()); 792 const merged_oid = switch (merged.event.status) { 793 .merged => |oid| oid, 794 else => return error.InvalidPatch, 795 }; 796 try std.testing.expectEqualStrings(&selected_oid, merged_oid); 797 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 798 799 _ = arena.reset(.retain_capacity); 800 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; 801 try std.testing.expect(removed_fork.removed); 802 } 803 804 fn indexedForkCount( 805 moment: evt.AdminDB.HashMap(.read_only), 806 index_key: []const u8, 807 parent_id: []const u8, 808 ) !u64 { 809 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 810 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 811 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 812 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 813 return try ids.count(); 814 } 815 816 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 817 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 818 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 819 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 820 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 821 return try ids.count(); 822 } 823 824 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 825 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 826 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 827 var key_buffer: evt.Patch.TagStatusKey = undefined; 828 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 829 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 830 return try ids.count(); 831 } 832