╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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