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