╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
813 ) !Window { 814 const user_id = session.userId() orelse return .empty; 815 const user_name = session.data.user_name orelse return .empty; 816 const users_dir = session.users_dir orelse return .empty; 817 const io = session.io orelse return .empty; 818 819 // the viewer's drafts live in their user repo 820 var user_repo = (try evt.openUserRepo(io, arena.child_allocator, users_dir, &user_id)) orelse return .empty; 821 defer user_repo.deinit(io, arena.child_allocator); 822 const user_moment = (try evt.userMoment(&user_repo)) orelse return .empty; 823 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; 824 const drafts = try evt.UserDB.HashMap(.read_only).init(drafts_cursor); 825 const set_cursor = try drafts.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return .empty; 826 const set = try evt.UserDB.SortedSet(.read_only).init(set_cursor); 827 const aa = arena.allocator(); 828 829 var root_key: ?[]const u8 = null; 830 if (selected_id.len != 0) { 831 const id = evt.parseEventId(selected_id) catch return error.NotFound; 832 if (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) |record| { 833 if (!record.removed and record.event.stage == .draft) { 834 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 835 if (!try set.contains(order_key)) return error.NotFound; 836 root_key = order_key; 837 } 838 } 839 } 840 841 var prev_id: ?[]const u8 = null; 842 var iter = if (root_key) |root| blk: { 843 const rank = try set.rank(root); 844 if (rank > 0 and rank <= page_size) { 845 prev_id = ""; 846 } else if (rank > page_size) { 847 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 848 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 849 _ = try pair.key_cursor.readBytes(&previous); 850 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 851 prev_id = try aa.dupe(u8, &hex); 852 } 853 break :blk try set.iteratorFrom(root); 854 } else try set.iteratorFromIndex(0); 855 856 var items: std.ArrayList(PatchWithId) = .empty; 857 var next_id: ?[]const u8 = null; 858 while (try iter.next()) |cursor| { 859 const id = try evt.readOrderKeyId(evt.UserDB, cursor); 860 const id_hex = std.fmt.bytesToHex(id, .lower); 861 if (items.items.len == page_size) { 862 next_id = try aa.dupe(u8, &id_hex); 863 break; 864 } 865 const path = try fork.forkPath(aa, users_dir, &user_id, &id); 866 // a fork shares its target's hash kind 867 var fork_repo = rp.Repo(.xit, .{ .hash = repo_opts.hash }).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 868 defer fork_repo.deinit(io, arena.child_allocator); 869 const record = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) orelse continue; 870 const fork_identity = try aa.print("{s}:{s}", .{ user_name, record.event.repo_name }); 871 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, fork_identity)) orelse continue; 872 try items.append(aa, entry); 873 } 874 return .{ 875 .items = items.items, 876 .prev_id = prev_id, 877 .next_id = next_id, 878 .count = @intCast(try set.count()), 879 }; 880 } 881 882 pub fn loadDraftEntry( 883 comptime repo_kind: rp.RepoKind, 884 comptime repo_opts: rp.RepoOpts(repo_kind), 885 arena: *std.heap.ArenaAllocator, 886 io: std.Io, 887 admin_moment: ?evt.AdminDB.HashMap(.read_only), 888 fork_repo: *rp.Repo(.xit, .{ .hash = repo_opts.hash }), 889 target_repo: *rp.Repo(repo_kind, repo_opts), 890 id: [evt.event_id_size]u8, 891 fork_identity: []const u8, 892 ) !?PatchWithId { 893 const aa = arena.allocator(); 894 const id_hex = std.fmt.bytesToHex(id, .lower); 895 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 896 const moment = evt.currentMoment(.{ .hash = repo_opts.hash }, fork_repo) catch return null; 897 const patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, &id)) orelse return null; 898 const target_branch = patch.event.target_branch; 899 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 900 const commit_count: ?u64 = blk: { 901 const newest = evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena) catch break :blk null; 902 break :blk if (newest) |revision| revision.record.commit_count else 0; 903 }; 904 return .{ 905 .id = try aa.dupe(u8, &id_hex), 906 .record = patch, 907 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 908 .draft = true, 909 .revision_oid = try aa.dupe(u8, &fork_oid), 910 .commit_count = commit_count, 911 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 912 .fork_exists = true, 913 .fork_identity = try aa.dupe(u8, fork_identity), 914 }; 915 } 916 917 pub const View = thread.View(.patch, Self); 918 pub const Detail = thread.Detail(.patch, Self); 919 pub const detail_widget_name = "repo_patch_detail"; 920 921 pub const Header = thread.Header; 922 923 const open_tab_label = "open"; 924 const closed_tab_label = "closed"; 925 const merged_tab_label = "merged"; 926 const labels_tab_label = "labels"; 927 const edit_tab_label = "edit"; 928 const publish_tab_label = "publish"; 929 const merge_tab_label = "merge"; 930 const reply_tab_label = "reply"; 931 const remove_tab_label = "remove"; 932 const resolve_tab_label = "resolve"; 933 const new_tab_label = "new"; 934 const drafts_tab_label = "drafts"; 935 const conflicts_tab_label = "conflicts"; 936 937 pub fn appendDetails(_: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 938 const aa = session.page_arena.allocator(); 939 const status_kind = entry.record.event.status.kind(); 940 const target_branch = entry.record.event.target_branch; 941 942 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_url) |ssh_url| { 943 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, entry.fork_identity, entry.id }); 944 const push_command = try aa.print("git push {s} HEAD:patch", .{url}); 945 const clone_command = try aa.print("git clone {s}", .{url}); 946 const merge_command = try aa.print("git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 947 const choices = [_]ui.widget.CopyableText.Choice{ 948 .{ 949 .selector = "push", 950 .text = push_command, 951 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 952 .label = " push to this patch from existing repo ", 953 }, 954 .{ 955 .selector = "clone", 956 .text = clone_command, 957 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 958 .label = " clone this patch ", 959 }, 960 .{ 961 .selector = "merge", 962 .text = merge_command, 963 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 964 .label = " merge this patch locally ", 965 }, 966 }; 967 var copyable_text = try ui.widget.CopyableText.init(allocator, session, if (entry.target_deleted) choices[1..] else &choices, 0); 968 errdefer copyable_text.deinit(allocator); 969 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 970 }; 971 if (entry.record.event.source_branch) |branch| { 972 var source = try wgt.TextBox.init(allocator, branch, .{ .top_label = .{ .text = " source branch " }, .border = .single, .round_corners = true, .wrap_kind = .none }); 973 errdefer source.deinit(allocator); 974 source.getFocus().mode = .all; 975 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 976 } 977 const fields = [_]ui.widget.CopyableText.Choice{ 978 .{ 979 .selector = "from", 980 .text = entry.revision_oid, 981 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 982 }, 983 .{ 984 .selector = "to", 985 .text = target_branch, 986 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 987 }, 988 }; 989 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..], 0); 990 errdefer copyable_text.deinit(allocator); 991 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 992 } 993 994 // tabs switching between the patches page's views 995 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 996 var header = try Header.init(allocator, session, data.search, " search "); 997 errdefer header.deinit(allocator); 998 const selected_index = View.viewIndex(data.view); 999 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1000 1001 // a list tab per status, labeled with its listing's patch count 1002 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1003 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1004 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, ""), data.search); 1005 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1006 try header.addTab(allocator, status_label, try thread.countLabel(session, data.window(status).*), link, index); 1007 } 1008 1009 // labels tab 1010 { 1011 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity), data.search); 1012 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1013 try header.addTab(allocator, labels_tab_label, "", labels_link, View.viewIndex(.labels)); 1014 } 1015 1016 // new-patch tab; an edit or resolve url shows its tab in this place 1017 { 1018 const route = switch (data.view) { 1019 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1020 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1021 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1022 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1023 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1024 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1025 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1026 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1027 }; 1028 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1029 const label: []const u8 = switch (data.view) { 1030 .edit => edit_tab_label, 1031 .publish => publish_tab_label, 1032 .merge => merge_tab_label, 1033 .new_comment => reply_tab_label, 1034 .edit_comment => edit_tab_label, 1035 .remove => remove_tab_label, 1036 .resolve => resolve_tab_label, 1037 else => new_tab_label, 1038 }; 1039 try header.addTab(allocator, label, "", link, View.viewIndex(.new)); 1040 } 1041 1042 // drafts belong to the logged-in user and are not available locally 1043 if (session.data.host_kind != .local) { 1044 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1045 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1046 try header.addTab(allocator, drafts_tab_label, try session.page_arena.allocator().print("({d})", .{data.drafts.count}), link, View.viewIndex(.drafts)); 1047 } 1048 1049 // conflicts tab, labeled with the conflict count 1050 if (data.conflicts.count > 0) { 1051 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1052 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1053 try header.addTab(allocator, conflicts_tab_label, try session.page_arena.allocator().print("({d})", .{data.conflicts.count}), link, View.viewIndex(.conflicts)); 1054 } 1055 1056 header.select(View.viewIndex(data.view)); 1057 return header; 1058 } 1059