╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
877 const target_branch = patch.event.target_branch; 878 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 879 const commit_count: ?u64 = blk: { 880 const newest = evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, arena) catch break :blk null; 881 break :blk if (newest) |revision| revision.record.commit_count else 0; 882 }; 883 return .{ 884 .id = try aa.dupe(u8, &id_hex), 885 .record = patch, 886 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 887 .draft = true, 888 .revision_oid = try aa.dupe(u8, &fork_oid), 889 .commit_count = commit_count, 890 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 891 .fork_exists = true, 892 }; 893 } 894 895 pub const View = thread.View(.patch, Self); 896 pub const Detail = thread.Detail(.patch, Self); 897 pub const detail_widget_name = "repo_patch_detail"; 898 899 pub const Header = thread.Header; 900 901 const open_tab_label = "open"; 902 const closed_tab_label = "closed"; 903 const merged_tab_label = "merged"; 904 const tags_tab_label = "tags"; 905 const edit_tab_label = "edit"; 906 const publish_tab_label = "publish"; 907 const merge_tab_label = "merge"; 908 const reply_tab_label = "reply"; 909 const remove_tab_label = "remove"; 910 const resolve_tab_label = "resolve"; 911 const new_tab_label = "new"; 912 const drafts_tab_label = "drafts"; 913 const conflicts_tab_label = "conflicts"; 914 915 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 916 const aa = session.page_arena.allocator(); 917 const status_kind = entry.record.event.status.kind(); 918 const target_branch = entry.record.event.target_branch; 919 920 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 921 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 922 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 923 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 924 const clone_command = if (clone_name.len == 0) 925 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 926 else 927 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 928 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 929 const choices: [3]ui.widget.CopyableText.Choice = .{ 930 .{ 931 .selector = "push", 932 .text = push_command, 933 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 934 .label = " push to this patch from existing repo ", 935 }, 936 .{ 937 .selector = "clone", 938 .text = clone_command, 939 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 940 .label = " clone this patch ", 941 }, 942 .{ 943 .selector = "merge", 944 .text = merge_command, 945 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 946 .label = " merge this patch locally ", 947 }, 948 }; 949 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 950 errdefer copyable_text.deinit(allocator); 951 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 952 }; 953 if (entry.record.event.source_branch) |branch| { 954 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 955 errdefer source.deinit(allocator); 956 source.getFocus().mode = .all; 957 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 958 } 959 const fields = [_]ui.widget.CopyableText.Choice{ 960 .{ 961 .selector = "from", 962 .text = entry.revision_oid, 963 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 964 }, 965 .{ 966 .selector = "to", 967 .text = target_branch, 968 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 969 }, 970 }; 971 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 972 errdefer copyable_text.deinit(allocator); 973 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 974 } 975 976 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 977 var name: [64]u8 = undefined; 978 var len: usize = 0; 979 var pending: ?u8 = null; 980 981 for (title) |char| { 982 if (std.ascii.isAlphanumeric(char)) { 983 const needed: usize = if (pending == null) 1 else 2; 984 if (len + needed > name.len) break; 985 if (pending) |value| { 986 name[len] = value; 987 len += 1; 988 } 989 name[len] = std.ascii.toLower(char); 990 len += 1; 991 pending = null; 992 } else if (char == ' ') { 993 if (len != 0) pending = '-'; 994 } else if (char == '.') { 995 if (len != 0 and pending != '-') pending = '.'; 996 } 997 } 998 if (len == 0) return ""; 999 return allocator.dupe(u8, name[0..len]); 1000 } 1001 1002 // tabs switching between the patches page's views 1003 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1004 var header = try Header.init(allocator); 1005 errdefer header.deinit(allocator); 1006 const aa = session.page_arena.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 = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 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 std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ status_label, data.window(status).count }); 1017 try header.addTab(allocator, label, link, index); 1018 } 1019 1020 // tags tab, labeled with the active tag filter 1021 { 1022 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 1023 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 1024 const label = if (data.tag.len == 0) tags_tab_label else blk: { 1025 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1026 break :blk try std.fmt.allocPrint(aa, tags_tab_label ++ " ({s})", .{decoded}); 1027 }; 1028 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 1029 } 1030 1031 // new-patch tab; an edit or resolve url shows its tab in this place 1032 { 1033 const route = switch (data.view) { 1034 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1035 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1036 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1037 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1038 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1039 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1040 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1041 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1042 }; 1043 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1044 const label: []const u8 = switch (data.view) { 1045 .edit => edit_tab_label, 1046 .publish => publish_tab_label, 1047 .merge => merge_tab_label, 1048 .new_comment => reply_tab_label, 1049 .edit_comment => edit_tab_label, 1050 .remove => remove_tab_label, 1051 .resolve => resolve_tab_label, 1052 else => new_tab_label, 1053 }; 1054 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1055 } 1056 1057 // drafts belong to the logged-in user and are not available locally 1058 if (session.data.host_kind != .local) { 1059 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1060 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1061 var label_buf: [64]u8 = undefined; 1062 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1063 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1064 } 1065 1066 // conflicts tab, labeled with the conflict count 1067 if (data.conflicts.count > 0) { 1068 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1069 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1070 var label_buf: [64]u8 = undefined; 1071 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1072 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1073 } 1074 1075 header.select(View.viewIndex(data.view)); 1076 return header; 1077 } 1078