╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
947 const target_branch = entry.record.event.target_branch; 948 949 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 950 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 951 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 952 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 953 const clone_command = if (clone_name.len == 0) 954 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 955 else 956 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 957 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 958 const choices: [3]ui.widget.CopyableText.Choice = .{ 959 .{ 960 .selector = "push", 961 .text = push_command, 962 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 963 .label = " push to this patch from existing repo ", 964 }, 965 .{ 966 .selector = "clone", 967 .text = clone_command, 968 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 969 .label = " clone this patch ", 970 }, 971 .{ 972 .selector = "merge", 973 .text = merge_command, 974 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 975 .label = " merge this patch locally ", 976 }, 977 }; 978 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 979 errdefer copyable_text.deinit(allocator); 980 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 981 }; 982 if (entry.record.event.source_branch) |branch| { 983 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .round_corners = true, .wrap_kind = .none }); 984 errdefer source.deinit(allocator); 985 source.getFocus().mode = .all; 986 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 987 } 988 const fields = [_]ui.widget.CopyableText.Choice{ 989 .{ 990 .selector = "from", 991 .text = entry.revision_oid, 992 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 993 }, 994 .{ 995 .selector = "to", 996 .text = target_branch, 997 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 998 }, 999 }; 1000 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 1001 errdefer copyable_text.deinit(allocator); 1002 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 1003 } 1004 1005 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 1006 var name: [64]u8 = undefined; 1007 var len: usize = 0; 1008 var pending: ?u8 = null; 1009 1010 for (title) |char| { 1011 if (std.ascii.isAlphanumeric(char)) { 1012 const needed: usize = if (pending == null) 1 else 2; 1013 if (len + needed > name.len) break; 1014 if (pending) |value| { 1015 name[len] = value; 1016 len += 1; 1017 } 1018 name[len] = std.ascii.toLower(char); 1019 len += 1; 1020 pending = null; 1021 } else if (char == ' ') { 1022 if (len != 0) pending = '-'; 1023 } else if (char == '.') { 1024 if (len != 0 and pending != '-') pending = '.'; 1025 } 1026 } 1027 if (len == 0) return ""; 1028 return allocator.dupe(u8, name[0..len]); 1029 } 1030 1031 // tabs switching between the patches page's views 1032 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1033 var header = try Header.init(allocator, session, data.search); 1034 errdefer header.deinit(allocator); 1035 const aa = session.page_arena.allocator(); 1036 const selected_index = View.viewIndex(data.view); 1037 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1038 1039 // a list tab per status, labeled with its listing's patch count 1040 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1041 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1042 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, data.label, ""), data.search); 1043 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1044 var label_buf: [64]u8 = undefined; 1045 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 1046 try header.addTab(allocator, label, link, index); 1047 } 1048 1049 // labels tab, showing the active label filter 1050 { 1051 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity, data.label), data.search); 1052 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1053 const label = if (data.label.len == 0) labels_tab_label else blk: { 1054 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 1055 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 1056 }; 1057 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 1058 } 1059 1060 // new-patch tab; an edit or resolve url shows its tab in this place 1061 { 1062 const route = switch (data.view) { 1063 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1064 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1065 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1066 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1067 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1068 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1069 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1070 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1071 }; 1072 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1073 const label: []const u8 = switch (data.view) { 1074 .edit => edit_tab_label, 1075 .publish => publish_tab_label, 1076 .merge => merge_tab_label, 1077 .new_comment => reply_tab_label, 1078 .edit_comment => edit_tab_label, 1079 .remove => remove_tab_label, 1080 .resolve => resolve_tab_label, 1081 else => new_tab_label, 1082 }; 1083 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1084 } 1085 1086 // drafts belong to the logged-in user and are not available locally 1087 if (session.data.host_kind != .local) { 1088 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1089 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1090 var label_buf: [64]u8 = undefined; 1091 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1092 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1093 } 1094 1095 // conflicts tab, labeled with the conflict count 1096 if (data.conflicts.count > 0) { 1097 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1098 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1099 var label_buf: [64]u8 = undefined; 1100 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1101 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1102 } 1103 1104 header.select(View.viewIndex(data.view)); 1105 return header; 1106 } 1107