╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
926 pub const Header = thread.Header; 927 928 const open_tab_label = "open"; 929 const closed_tab_label = "closed"; 930 const merged_tab_label = "merged"; 931 const labels_tab_label = "labels"; 932 const edit_tab_label = "edit"; 933 const publish_tab_label = "publish"; 934 const merge_tab_label = "merge"; 935 const reply_tab_label = "reply"; 936 const remove_tab_label = "remove"; 937 const resolve_tab_label = "resolve"; 938 const new_tab_label = "new"; 939 const drafts_tab_label = "drafts"; 940 const conflicts_tab_label = "conflicts"; 941 942 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 943 const aa = session.page_arena.allocator(); 944 const status_kind = entry.record.event.status.kind(); 945 const target_branch = entry.record.event.target_branch; 946 947 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 948 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 949 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 950 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 951 const clone_command = if (clone_name.len == 0) 952 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 953 else 954 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 955 const merge_command = try std.fmt.allocPrint(aa, "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 std.fmt.allocPrint(aa, "{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 std.fmt.allocPrint(aa, "{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 std.fmt.allocPrint(aa, "{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, .{ .label = " source branch ", .border_style = .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 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 1004 var name: [64]u8 = undefined; 1005 var len: usize = 0; 1006 var pending: ?u8 = null; 1007 1008 for (title) |char| { 1009 if (std.ascii.isAlphanumeric(char)) { 1010 const needed: usize = if (pending == null) 1 else 2; 1011 if (len + needed > name.len) break; 1012 if (pending) |value| { 1013 name[len] = value; 1014 len += 1; 1015 } 1016 name[len] = std.ascii.toLower(char); 1017 len += 1; 1018 pending = null; 1019 } else if (char == ' ') { 1020 if (len != 0) pending = '-'; 1021 } else if (char == '.') { 1022 if (len != 0 and pending != '-') pending = '.'; 1023 } 1024 } 1025 if (len == 0) return ""; 1026 return allocator.dupe(u8, name[0..len]); 1027 } 1028 1029 // tabs switching between the patches page's views 1030 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1031 var header = try Header.init(allocator, session, data.search); 1032 errdefer header.deinit(allocator); 1033 const aa = session.page_arena.allocator(); 1034 const selected_index = View.viewIndex(data.view); 1035 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1036 1037 // a list tab per status, labeled with its listing's patch count 1038 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1039 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1040 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, data.label, ""), data.search); 1041 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1042 var label_buf: [64]u8 = undefined; 1043 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 1044 try header.addTab(allocator, label, link, index); 1045 } 1046 1047 // labels tab, showing the active label filter 1048 { 1049 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity, data.label), data.search); 1050 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1051 const label = if (data.label.len == 0) labels_tab_label else blk: { 1052 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 1053 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 1054 }; 1055 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 1056 } 1057 1058 // new-patch tab; an edit or resolve url shows its tab in this place 1059 { 1060 const route = switch (data.view) { 1061 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1062 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1063 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1064 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1065 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1066 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1067 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1068 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1069 }; 1070 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1071 const label: []const u8 = switch (data.view) { 1072 .edit => edit_tab_label, 1073 .publish => publish_tab_label, 1074 .merge => merge_tab_label, 1075 .new_comment => reply_tab_label, 1076 .edit_comment => edit_tab_label, 1077 .remove => remove_tab_label, 1078 .resolve => resolve_tab_label, 1079 else => new_tab_label, 1080 }; 1081 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1082 } 1083 1084 // drafts belong to the logged-in user and are not available locally 1085 if (session.data.host_kind != .local) { 1086 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1087 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1088 var label_buf: [64]u8 = undefined; 1089 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1090 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1091 } 1092 1093 // conflicts tab, labeled with the conflict count 1094 if (data.conflicts.count > 0) { 1095 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1096 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1097 var label_buf: [64]u8 = undefined; 1098 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1099 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1100 } 1101 1102 header.select(View.viewIndex(data.view)); 1103 return header; 1104 } 1105