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