╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
607 // a repo with no consumed events has no moment yet. 608 const gpa = arena.child_allocator; 609 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 610 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 611 const haxy_moment = (if (event_db_maybe) |*event_db| 612 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 613 else if (repo_kind == .git) 614 return empty 615 else 616 evt.currentMoment(repo_opts, repo)) catch { 617 if (strict) return error.NotFound; 618 return empty; 619 }; 620 621 // the sorted sets to window, per status: the label's sets when filtered, 622 // else the top-level per-status sets. a missing set is an empty window. 623 var open_set: ?DB.SortedSet(.read_only) = null; 624 var closed_set: ?DB.SortedSet(.read_only) = null; 625 var merged_set: ?DB.SortedSet(.read_only) = null; 626 if (labeled) { 627 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.label_status_to_id_set_key))) |label_to_patches_cursor| { 628 const label_to_patches = try DB.SortedMap(.read_only).init(label_to_patches_cursor); 629 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 630 open_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .open); 631 closed_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .closed); 632 merged_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .merged); 633 } 634 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 635 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 636 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 637 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 638 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 639 } else if (strict) return error.NotFound; 640 641 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 642 if (strict) return error.NotFound; 643 return empty; 644 }; 645 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 646 647 // the conflicted patches' container: a set view for membership and 648 // windowing, a map view for the resolve entry, over the same cursor. 649 var conflict_set: ?DB.SortedSet(.read_only) = null; 650 var conflicts_map: ?DB.SortedMap(.read_only) = null; 651 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 652 conflict_set = try DB.SortedSet(.read_only).init(cursor); 653 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 654 } 655 656 // a named patch roots its own status's window at itself (the conflicts 657 // view's window instead); the other windows start at the beginning. 658 var resolved_view = empty.view; 659 var open_root: ?[]const u8 = null; 660 var closed_root: ?[]const u8 = null; 661 var merged_root: ?[]const u8 = null; 662 var conflicts_root: ?[]const u8 = null; 663 var conflict_data: ?Conflict = null; 664 if (rooted and !draft_selected) { 665 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 666 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 667 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 668 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 669 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 670 671 if (view == .conflicts) { 672 // the id roots the conflicts window, so it must be conflicted 673 const set = conflict_set orelse return error.NotFound; 674 if (!try set.contains(order_key)) return error.NotFound; 675 conflicts_root = order_key; 676 } else { 677 // the named patch must be in its windowed set (a label url can name 678 // an patch that doesn't carry the label). 679 const set = (switch (patch_event.event.status) { 680 .open => open_set, 681 .closed => closed_set, 682 .merged => merged_set, 683 }) orelse return error.NotFound; 684 if (!try set.contains(order_key)) return error.NotFound; 685 686 switch (patch_event.event.status) { 687 .open => open_root = order_key, 688 .closed => closed_root = order_key, 689 .merged => merged_root = order_key, 690 } 691 // form urls keep their view; otherwise the patch's status picks it. 692 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 693 .open => .open, 694 .closed => .closed, 695 .merged => .merged, 696 }; 697 698 // a resolve url shows the resolve view only while the patch still 699 // has a conflict entry; otherwise the status list stands. the 700 // wasm client renders from the snapshot, so the diff machinery 701 // this pulls in is gated out of its build. 702 if (comptime !wasm) { 703 if (view == .resolve) { 704 if (conflicts_map) |map| { 705 if (try map.getCursor(order_key)) |conflict_cursor| { 706 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 707 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 708 resolved_view = .resolve; 709 } 710 } 711 } 712 } 713 } 714 } 715 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 716 var open_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 717 var closed_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 718 var merged_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 719 var conflicts_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, null, viewer); 720 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &open_window); 721 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &closed_window); 722 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &merged_window); 723 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &conflicts_window); 724 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 725 726 const comment_page = if (empty.comment_id.len == 0) 727 null 728 else 729 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 730 731 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 732 733 return .{ 734 .identity = empty.identity, 735 .default_target_branch = empty.default_target_branch, 736 .label = empty.label, 737 .search = empty.search, 738 .selected_id = empty.selected_id, 739 .comment_id = empty.comment_id, 740 .comments_start = comments_start, 741 .comment_page = comment_page, 742 .viewer = viewer, 743 .open = open_window, 744 .closed = closed_window, 745 .merged = merged_window, 746 .drafts = drafts_window, 747 .conflicts = conflicts_window, 748 .conflict = conflict_data, 749 .theirs_picks = empty.theirs_picks, 750 .view = resolved_view, 751 .description_page = empty.description_page, 752 .labels = labels, 753 }; 754 } 755 756 fn setPatchDetails( 757 comptime repo_kind: rp.RepoKind, 758 comptime repo_opts: rp.RepoOpts(repo_kind), 759 io: std.Io, 760 arena: *std.heap.ArenaAllocator, 761 admin_moment: ?evt.AdminDB.HashMap(.read_only), 762 users_dir: ?[]const u8, 763 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 764 repo: *rp.Repo(repo_kind, repo_opts), 765 target: *Window, 766 ) !void { 767 if (target.items.len == 0) return; 768 var config = try repo.listConfig(io, arena.child_allocator); 769 defer config.deinit(); 770 const items = try arena.allocator().dupe(PatchWithId, target.items); 771 for (items) |*item| { 772 const id = try evt.parseEventId(item.id); 773 const status = item.record.event.status.kind(); 774 // a fork-backed patch's fork lives in its forker's user repo 775 if (admin_moment) |moment| if (users_dir) |dir| if (item.record.event.source_branch == null) { 776 if (try fork.readForkerId(&config.sections, &id)) |forker_id| { 777 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &forker_id)) |forker| item.forker = forker.event.name; 778 const record = try evt.readForkById(io, arena.child_allocator, arena, dir, &forker_id, &id); 779 item.fork_exists = if (record) |value| !value.removed else false; 780 } 781 }; 782 const target_branch = item.record.event.target_branch; 783 const revision = item.record.event.revision orelse continue; 784 item.commit_count = revision.commit_count; 785 const merge_revision: evt.Patch.MergeRevision = switch (item.record.event.status) { 786 .merged => |merged| merged.revision, 787 else => .source, 788 }; 789 item.revision_oid = switch (merge_revision) { 790 .source => revision.source_oid, 791 .squash => revision.squash_oid, 792 }; 793 if (!item.fork_exists or status == .merged) { 794 const revision_id = try evt.parseEventId(&revision.id); 795 if (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, haxy_moment, arena, &revision_id)) |record| { 796 item.base_oid = record.event.base_oid; 797 } 798 } 799 if (status == .merged) { 800 if (merge_revision == .squash) item.commit_count = 1; 801 continue; 802 } 803 if (comptime repo_kind == .xit) { 804 var moment = try repo.core.latestMoment(); 805 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 806 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 807 } 808 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 809 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 810 } 811 target.items = items; 812 } 813 814 fn loadDraftWindow( 815 comptime repo_kind: rp.RepoKind, 816 comptime repo_opts: rp.RepoOpts(repo_kind), 817 arena: *std.heap.ArenaAllocator, 818 session: *ui.Session, 819 admin_moment: evt.AdminDB.HashMap(.read_only), 820 repo_id: *const [evt.event_id_size]u8, 821 selected_id: []const u8, 822 target_repo: *rp.Repo(repo_kind, repo_opts), 823 ) !Window { 824 const user_id = session.userId() orelse return .empty; 825 const user_name = session.data.user_name orelse return .empty; 826 const users_dir = session.users_dir orelse return .empty; 827 const io = session.io orelse return .empty; 828 829 // the viewer's drafts live in their user repo 830 var user_repo = (try evt.openUserRepo(io, arena.child_allocator, users_dir, &user_id)) orelse return .empty; 831 defer user_repo.deinit(io, arena.child_allocator); 832 const user_moment = (try evt.userMoment(&user_repo)) orelse return .empty; 833 const drafts_cursor = try user_moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return .empty; 834 const drafts = try evt.UserDB.HashMap(.read_only).init(drafts_cursor); 835 const set_cursor = try drafts.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return .empty; 836 const set = try evt.UserDB.SortedSet(.read_only).init(set_cursor); 837 const aa = arena.allocator(); 838 839 var root_key: ?[]const u8 = null; 840 if (selected_id.len != 0) { 841 const id = evt.parseEventId(selected_id) catch return error.NotFound; 842 if (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) |record| { 843 if (!record.removed and record.event.stage == .draft) { 844 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 845 if (!try set.contains(order_key)) return error.NotFound; 846 root_key = order_key; 847 } 848 } 849 } 850 851 var prev_id: ?[]const u8 = null; 852 var iter = if (root_key) |root| blk: { 853 const rank = try set.rank(root); 854 if (rank > 0 and rank <= page_size) { 855 prev_id = ""; 856 } else if (rank > page_size) { 857 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 858 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 859 _ = try pair.key_cursor.readBytes(&previous); 860 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 861 prev_id = try aa.dupe(u8, &hex); 862 } 863 break :blk try set.iteratorFrom(root); 864 } else try set.iteratorFromIndex(0); 865 866 var items: std.ArrayList(PatchWithId) = .empty; 867 var next_id: ?[]const u8 = null; 868 while (try iter.next()) |cursor| { 869 const id = try evt.readOrderKeyId(evt.UserDB, cursor); 870 const id_hex = std.fmt.bytesToHex(id, .lower); 871 if (items.items.len == page_size) { 872 next_id = try aa.dupe(u8, &id_hex); 873 break; 874 } 875 const path = try fork.forkPath(aa, users_dir, &user_id, &id); 876 // a fork shares its target's hash kind 877 var fork_repo = rp.Repo(.xit, .{ .hash = repo_opts.hash }).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 878 defer fork_repo.deinit(io, arena.child_allocator); 879 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, user_name)) orelse continue; 880 try items.append(aa, entry); 881 } 882 return .{ 883 .items = items.items, 884 .prev_id = prev_id, 885 .next_id = next_id, 886 .count = @intCast(try set.count()), 887 }; 888 } 889 890 pub fn loadDraftEntry( 891 comptime repo_kind: rp.RepoKind, 892 comptime repo_opts: rp.RepoOpts(repo_kind), 893 arena: *std.heap.ArenaAllocator, 894 io: std.Io, 895 admin_moment: ?evt.AdminDB.HashMap(.read_only), 896 fork_repo: *rp.Repo(.xit, .{ .hash = repo_opts.hash }), 897 target_repo: *rp.Repo(repo_kind, repo_opts), 898 id: [evt.event_id_size]u8, 899 forker: []const u8, 900 ) !?PatchWithId { 901 const aa = arena.allocator(); 902 const id_hex = std.fmt.bytesToHex(id, .lower); 903 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 904 const moment = evt.currentMoment(.{ .hash = repo_opts.hash }, fork_repo) catch return null; 905 const patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, &id)) orelse return null; 906 const target_branch = patch.event.target_branch; 907 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 908 const commit_count: ?u64 = blk: { 909 const newest = evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena) catch break :blk null; 910 break :blk if (newest) |revision| revision.record.commit_count else 0; 911 }; 912 return .{ 913 .id = try aa.dupe(u8, &id_hex), 914 .record = patch, 915 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 916 .draft = true, 917 .revision_oid = try aa.dupe(u8, &fork_oid), 918 .commit_count = commit_count, 919 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 920 .fork_exists = true, 921 .forker = try aa.dupe(u8, forker), 922 }; 923 } 924 925 pub const View = thread.View(.patch, Self); 926 pub const Detail = thread.Detail(.patch, Self); 927 pub const detail_widget_name = "repo_patch_detail"; 928 929 pub const Header = thread.Header; 930 931 const open_tab_label = "open"; 932 const closed_tab_label = "closed"; 933 const merged_tab_label = "merged"; 934 const labels_tab_label = "labels"; 935 const edit_tab_label = "edit"; 936 const publish_tab_label = "publish"; 937 const merge_tab_label = "merge"; 938 const reply_tab_label = "reply"; 939 const remove_tab_label = "remove"; 940 const resolve_tab_label = "resolve"; 941 const new_tab_label = "new"; 942 const drafts_tab_label = "drafts"; 943 const conflicts_tab_label = "conflicts"; 944 945 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 946 const aa = session.page_arena.allocator(); 947 const status_kind = entry.record.event.status.kind(); 948 const target_branch = entry.record.event.target_branch; 949 950 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 951 var buffer: [ui.RoutablePage.repo_identity_max_len]u8 = undefined; 952 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, try forkIdentity(&buffer, self.identity, entry), entry.id }); 953 const push_command = try aa.print("git push {s} HEAD:patch", .{url}); 954 const clone_command = try aa.print("git clone {s}", .{url}); 955 const merge_command = try aa.print("git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 956 const choices: [3]ui.widget.CopyableText.Choice = .{ 957 .{ 958 .selector = "push", 959 .text = push_command, 960 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 961 .label = " push to this patch from existing repo ", 962 }, 963 .{ 964 .selector = "clone", 965 .text = clone_command, 966 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 967 .label = " clone this patch ", 968 }, 969 .{ 970 .selector = "merge", 971 .text = merge_command, 972 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 973 .label = " merge this patch locally ", 974 }, 975 }; 976 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices, 0); 977 errdefer copyable_text.deinit(allocator); 978 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 979 }; 980 if (entry.record.event.source_branch) |branch| { 981 var source = try wgt.TextBox.init(allocator, branch, .{ .top_label = .{ .text = " source branch " }, .border = .single, .round_corners = true, .wrap_kind = .none }); 982 errdefer source.deinit(allocator); 983 source.getFocus().mode = .all; 984 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 985 } 986 const fields = [_]ui.widget.CopyableText.Choice{ 987 .{ 988 .selector = "from", 989 .text = entry.revision_oid, 990 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 991 }, 992 .{ 993 .selector = "to", 994 .text = target_branch, 995 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 996 }, 997 }; 998 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..], 0); 999 errdefer copyable_text.deinit(allocator); 1000 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 1001 } 1002 1003 // tabs switching between the patches page's views 1004 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1005 var header = try Header.init(allocator, session, data.search); 1006 errdefer header.deinit(allocator); 1007 const selected_index = View.viewIndex(data.view); 1008 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1009 1010 // a list tab per status, labeled with its listing's patch count 1011 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1012 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1013 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, data.label, ""), data.search); 1014 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1015 var label_buf: [64]u8 = undefined; 1016 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 1017 try header.addTab(allocator, label, link, index); 1018 } 1019 1020 // labels tab 1021 { 1022 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity, data.label), data.search); 1023 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1024 try header.addTab(allocator, labels_tab_label, labels_link, View.viewIndex(.labels)); 1025 } 1026 1027 // new-patch tab; an edit or resolve url shows its tab in this place 1028 { 1029 const route = switch (data.view) { 1030 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1031 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1032 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1033 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1034 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1035 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1036 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1037 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1038 }; 1039 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1040 const label: []const u8 = switch (data.view) { 1041 .edit => edit_tab_label, 1042 .publish => publish_tab_label, 1043 .merge => merge_tab_label, 1044 .new_comment => reply_tab_label, 1045 .edit_comment => edit_tab_label, 1046 .remove => remove_tab_label, 1047 .resolve => resolve_tab_label, 1048 else => new_tab_label, 1049 }; 1050 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1051 } 1052 1053 // drafts belong to the logged-in user and are not available locally 1054 if (session.data.host_kind != .local) { 1055 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1056 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1057 var label_buf: [64]u8 = undefined; 1058 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1059 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1060 } 1061 1062 // conflicts tab, labeled with the conflict count 1063 if (data.conflicts.count > 0) { 1064 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1065 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1066 var label_buf: [64]u8 = undefined; 1067 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1068 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1069 } 1070 1071 header.select(View.viewIndex(data.view)); 1072 return header; 1073 } 1074