╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const diff3 = @import("../../diff3.zig"); 11 const Comment = @import("Comment.zig"); 12 const Attachment = @import("Attachment.zig"); 13 const thread = ui.widget.thread; 14 const srch_thrd = @import("../../search_thread.zig"); 15 const fork = @import("../../fork.zig"); 16 const pch = @import("../../patch.zig"); 17 18 const wasm = builtin.target.cpu.arch == .wasm32; 19 20 pub const Source = struct { 21 box: wgt.Box(ui.Widget), 22 session: *ui.Session, 23 selectors: std.enums.EnumMap(Kind, usize), 24 25 pub const Kind = enum { fork, branch }; 26 27 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, initial: []const u8, initial_kind: Kind) !Source { 28 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 29 errdefer box.deinit(allocator); 30 const selected: Kind = if (session.data.host_kind == .local) .branch else initial_kind; 31 var selectors: std.enums.EnumMap(Kind, usize) = .{}; 32 if (session.data.host_kind != .local) { 33 for (std.enums.values(Kind)) |kind| { 34 const label = switch (kind) { 35 .fork => "from a new fork", 36 .branch => "from an existing branch", 37 }; 38 var selector = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 39 errdefer selector.deinit(allocator); 40 selector.getFocus().mode = .all; 41 try box.children.put(allocator, selector.getFocus().id, .{ .widget = .{ .text_box = selector }, .rect = null, .min_size = .{ .width = label.len + 2, .height = 3 } }); 42 selectors.put(kind, selector.getFocus().id); 43 } 44 } 45 var input_box = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " source branch " }, .name = "source_branch", .round_corners = true, .visible_width = null, .render_content = session.is_terminal }); 46 errdefer input_box.deinit(allocator); 47 input_box.getFocus().mode = .all; 48 try input_box.setContent(allocator, initial); 49 try box.children.put(allocator, input_box.getFocus().id, .{ .widget = .{ .text_input = input_box }, .rect = null, .min_size = .{ .width = 15, .height = 3 } }); 50 box.getFocus().child_id = selectors.get(selected) orelse input_box.getFocus().id; 51 return .{ .box = box, .session = session, .selectors = selectors }; 52 } 53 54 pub fn field(self: *Source) *wgt.TextInput { 55 for (self.box.children.values()) |*child| switch (child.widget) { 56 .text_input => |*text_input| return text_input, 57 else => {}, 58 }; 59 unreachable; 60 } 61 62 // the selection is whichever selector focus last landed on, like every 63 // other selection in the ui, so a click (which only moves focus on the 64 // web) selects. the branch text input is only reachable while branch is 65 // selected, so focus on it means branch too. 66 pub fn selectedKind(self: *Source) Kind { 67 const current = self.box.getFocus().child_id orelse return .branch; 68 var iter = self.selectors.iterator(); 69 while (iter.next()) |selector| { 70 if (selector.value.* == current) return selector.key; 71 } 72 return .branch; 73 } 74 75 pub fn deinit(self: *Source, allocator: std.mem.Allocator) void { 76 self.box.deinit(allocator); 77 } 78 79 pub fn clearGrid(self: *Source) void { 80 self.box.clearGrid(); 81 } 82 83 pub fn getGrid(self: Source) ?xitui.grid.Grid { 84 return self.box.getGrid(); 85 } 86 87 pub fn getFocus(self: *Source) *xitui.focus.Focus { 88 return self.box.getFocus(); 89 } 90 91 pub fn build(self: *Source, allocator: std.mem.Allocator, constraint: xitui.layout.Constraint, root_focus: *xitui.focus.Focus) !void { 92 self.clearGrid(); 93 const selected = self.selectedKind(); 94 for (self.box.children.keys(), self.box.children.values()) |id, *child| switch (child.widget) { 95 .text_input => child.hidden = selected == .fork, 96 .text_box => |*selector| selector.options.border = if (id == self.selectors.get(selected)) .single else .hidden, 97 else => {}, 98 }; 99 try self.box.build(allocator, constraint, root_focus); 100 if (!self.session.is_terminal) { 101 try self.session.text_inputs.put(self.session.arena.allocator(), self.field().getFocus().id, self.field()); 102 } 103 } 104 105 pub fn input(self: *Source, allocator: std.mem.Allocator, key: xitui.input.Key, root_focus: *xitui.focus.Focus) !void { 106 const current = self.box.getFocus().child_id orelse return; 107 const text_input = self.field(); 108 if (current == text_input.getFocus().id) { 109 if (key == .arrow_left) { 110 if (self.selectors.get(.branch)) |id| { 111 root_focus.setFocus(id); 112 return; 113 } 114 } 115 return text_input.input(allocator, key, root_focus); 116 } 117 118 var iter = self.selectors.iterator(); 119 var previous: ?Kind = null; 120 while (iter.next()) |selector| { 121 if (selector.value.* == current) break; 122 previous = selector.key; 123 } else return; 124 switch (key) { 125 .arrow_left => if (previous) |kind| root_focus.setFocus(self.selectors.get(kind) orelse return), 126 // past the last selector (branch) lies its text input 127 .arrow_right => if (iter.next()) |next| root_focus.setFocus(next.value.*) else root_focus.setFocus(text_input.getFocus().id), 128 .enter => if (self.selectedKind() == .branch) root_focus.setFocus(text_input.getFocus().id), 129 else => {}, 130 } 131 } 132 }; 133 134 // how many patches one window shows before a "next" link appears. 135 pub const page_size = 20; 136 137 // how many labels the labels view shows at most. 138 pub const max_labels = 1000; 139 140 // one patch from the repo's consumed event database, with its hex event id 141 // (the id lives in the event envelope, not the payload). 142 pub const PatchWithId = struct { 143 id: []const u8, 144 record: evt.Patch.Record, 145 // the creation commit's author 146 author: ui.Author = .unknown, 147 // whether the patch has an unresolved merge conflict 148 conflicted: bool = false, 149 comments: Comment.Window = .empty, 150 attachments: []const Attachment.WithId = &.{}, 151 assignees: []const ui.Assignee = &.{}, 152 // whether the viewer may change the thread, computed here so the web ui has it 153 can_modify: bool = false, 154 // the viewer's notify button, null when hidden 155 notify_action: ?ui.NotifyAction = null, 156 draft: bool = false, 157 revision_oid: []const u8 = "", 158 base_oid: []const u8 = "", 159 commit_count: ?u64 = null, 160 no_changes: bool = false, 161 fork_exists: bool = false, 162 // the url identity of the fork backing the patch: its forker and the repo name it was forked under 163 fork_identity: []const u8 = "", 164 // whether the fork's target is gone, which leaves the fork fetchable but not pushable 165 target_deleted: bool = false, 166 mergeability: pch.Mergeability = .{}, 167 }; 168 169 pub const Entry = PatchWithId; 170 171 // one side of a conflicted field: its value and who set it 172 pub const Side = struct { 173 text: []const u8, 174 author: ui.Author = .unknown, 175 }; 176 177 pub const FieldConflict = struct { 178 ours: Side, 179 theirs: Side, 180 }; 181 182 // the selected patch's conflict, read for the resolve view. ours is the live 183 // record; the chunks split the description against the merge base. 184 pub const Conflict = struct { 185 title: ?FieldConflict = null, 186 labels: ?FieldConflict = null, 187 status: ?FieldConflict = null, 188 revision: ?FieldConflict = null, 189 target_branch: ?FieldConflict = null, 190 description: ?struct { 191 chunks: []const diff3.Chunk, 192 ours_author: ui.Author = .unknown, 193 theirs_author: ui.Author = .unknown, 194 } = null, 195 }; 196 197 // one status's windowed listing. 198 pub const Window = struct { 199 items: []const PatchWithId, 200 // the id of the previous window's first patch, or null when this window 201 // is already the first. 202 prev_id: ?[]const u8, 203 // the id of the next window's first patch, or null when this is the last 204 // window. 205 next_id: ?[]const u8, 206 // how many patches the listing holds across all windows. 207 count: usize, 208 // whether a search stopped counting at its cap, so `count` is a floor. 209 more: bool = false, 210 211 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 212 }; 213 214 // "owner:name", so the view can build /owner:name/patches/... links. 215 identity: []const u8, 216 default_target_branch: []const u8 = "", 217 // the decoded query the lists are filtered to, or null when unfiltered. 218 search: ?[]const u8 = null, 219 // the hex event id of the patch its status's window is rooted at ("" = the 220 // first window), mirrored into the url. 221 selected_id: []const u8, 222 // the comment shown in the selected patch's detail pane. 223 comment_id: []const u8, 224 // the selected patch's comment window (0 = the first window). 225 comments_start: usize, 226 // the selected comment and its immediate replies. 227 comment_page: ?Comment.Permalink = null, 228 // who the page is built for, null when logged out 229 viewer: ?ui.Viewer = null, 230 open: Window, 231 closed: Window, 232 merged: Window, 233 drafts: Window, 234 // the conflicted patches' listing; its count also gates the conflicts tab. 235 conflicts: Window, 236 // the selected patch's conflict, set when the page shows the resolve view. 237 conflict: ?Conflict = null, 238 // the resolve view's comma-separated fields prefilled from their side, 239 // mirrored from the url. 240 theirs_picks: []const u8 = "", 241 // the view the page shows initially (a selected patch's status overrides the route's) 242 view: ui.RoutablePage.PatchesView, 243 // the /description page: the detail pane shows the selected patch's whole 244 // description behind a back link. 245 description_page: bool = false, 246 // every label in the repo, in sorted order, for the labels view. 247 labels: []const []const u8, 248 // the on-disk repo this page was read from, for the terminal submit path 249 // (the web posts the new-patch form to the patch route instead). 250 repo_source: ?ui.RepoSource = null, 251 repo_id: ?[evt.event_id_size]u8 = null, 252 253 const Self = @This(); 254 255 pub const Event = evt.Patch; 256 pub const Status = evt.Patch.StatusKind; 257 pub const ViewKind = ui.RoutablePage.PatchesView; 258 pub const thread_name = "patch"; 259 pub const header_widget_name = "repo_patches_header"; 260 261 pub fn statusKind(event: Event) Status { 262 return event.status.kind(); 263 } 264 265 pub fn listRoute(identity: []const u8, status: Status, selected: []const u8) ?ui.RoutablePage { 266 return ui.RoutablePage.repoPatchesRoute(identity, status, selected); 267 } 268 269 pub fn commitsRoute(identity: []const u8, entry: PatchWithId) !?ui.RoutablePage { 270 if (entry.fork_exists and entry.record.event.status.kind() != .merged) 271 return ui.RoutablePage.forkCommitsRoute(entry.fork_identity, entry.id, "") orelse error.RouteTooLong; 272 if (entry.revision_oid.len == 0 or entry.base_oid.len == 0) return null; 273 return ui.RoutablePage.repoCommitsRoute(identity, .object, entry.revision_oid, entry.base_oid) orelse error.RouteTooLong; 274 } 275 276 pub fn diffRoute(identity: []const u8, entry: PatchWithId) !?ui.RoutablePage { 277 const patch = entry.record.event; 278 const revision_id = switch (patch.status) { 279 // merged patches show what actually landed, including conflict resolutions 280 .merged => |merged| merged.patchrev_id, 281 .open, .closed => blk: { 282 if (entry.fork_exists) return ui.RoutablePage.forkDiffRoute(entry.fork_identity, entry.id, "", "", 0, "") orelse error.RouteTooLong; 283 if (patch.source_branch == null) return null; 284 break :blk (patch.revision orelse return null).id; 285 }, 286 }; 287 return ui.RoutablePage.repoPatchRevDiffRoute(identity, &revision_id, 0, "") orelse error.RouteTooLong; 288 } 289 290 pub fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 291 return ui.RoutablePage.repoPatchesDraftsRoute(identity); 292 } 293 294 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 295 return ui.RoutablePage.repoPatchesConflictsRoute(identity, selected); 296 } 297 298 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 299 return ui.RoutablePage.repoPatchesResolveRoute(identity, selected, picks); 300 } 301 302 pub fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 303 return ui.RoutablePage.repoPatchPublishRoute(identity, selected); 304 } 305 306 pub fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 307 return ui.RoutablePage.repoPatchMergeRoute(identity, selected); 308 } 309 310 pub fn canMerge(entry: Entry, session: *const ui.Session) bool { 311 return !entry.draft and 312 (entry.fork_exists or entry.record.event.source_branch != null) and 313 !entry.conflicted and 314 entry.record.event.revision != null and 315 entry.record.event.status.kind() == .open and 316 session.data.host_kind == .server; 317 } 318 319 pub fn create( 320 data: *const Self, 321 session: *ui.Session, 322 allocator: std.mem.Allocator, 323 actor: ui.Actor, 324 title: []const u8, 325 labels: []const u8, 326 description: []const u8, 327 target_branch: []const u8, 328 source_branch: ?[]const u8, 329 ) ![evt.event_id_size * 2]u8 { 330 if (!evt.Patch.fieldsValid(title, labels)) return error.InvalidFields; 331 const io = session.io orelse return error.NotFound; 332 const repo_source = data.repo_source orelse return error.NotFound; 333 var id_bytes: [evt.event_id_size]u8 = undefined; 334 io.random(&id_bytes); 335 const id = std.fmt.bytesToHex(id_bytes, .lower); 336 if (source_branch) |branch| { 337 switch (repo_source.repo_kind) { 338 inline else => |repo_kind| { 339 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, repo_source.localInitOpts()); 340 defer any_repo.deinit(io, allocator); 341 switch (any_repo) { 342 inline else => |*repo| try pch.writeBranchPatch(session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, id, .{ 343 .title = title, 344 .labels = labels, 345 .description = description, 346 .target_branch = target_branch, 347 .source_branch = branch, 348 }, null, actor.author), 349 } 350 }, 351 } 352 return id; 353 } 354 if (session.data.host_kind == .local) return error.InvalidSourceBranch; 355 const users_dir = session.users_dir orelse return error.NotFound; 356 const user_id = actor.user_id orelse return error.NotFound; 357 const repo_id = data.repo_id orelse return error.NotFound; 358 const repo_user_id = actor.repo_user_id orelse return error.NotFound; 359 const identity = ui.RoutablePage.RepoIdentity.parse(data.identity) orelse return error.NotFound; 360 if (!evt.Patch.branchValid(target_branch) or !try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidTargetBranch; 361 const fork_path = try fork.create(.{}, io, allocator, users_dir, .{ 362 .id = id, 363 .user_id = user_id, 364 .repo_id = repo_id, 365 .repo_user_id = repo_user_id, 366 .repo_name = identity.name, 367 .title = title, 368 .description = description, 369 .labels = labels, 370 .target_branch = target_branch, 371 .author = actor.author, 372 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 373 }); 374 allocator.free(fork_path); 375 return id; 376 } 377 378 pub fn publishDraft(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: []const u8) !void { 379 const io = session.io orelse return error.NotFound; 380 const server = switch (session.eventHost()) { 381 .server => |server| server, 382 .local => return error.NotFound, 383 }; 384 const admin_repo = session.admin_repo orelse return error.NotFound; 385 const repo_source = data.repo_source orelse return error.NotFound; 386 const repo_id = data.repo_id orelse return error.NotFound; 387 const user_id = session.userId() orelse return error.NotFound; 388 const patch_id = evt.parseEventId(id) catch return error.NotFound; 389 390 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 391 var target_repo = try rp.AnyRepo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 392 defer target_repo.deinit(io, allocator); 393 switch (target_repo) { 394 inline else => |*repo| try pch.publish(repo.self_repo_opts, io, allocator, server, admin_repo, repo, .{ 395 .id = id_hex, 396 .user_id = user_id, 397 .repo_id = repo_id, 398 .author = author, 399 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 400 }), 401 } 402 } 403 404 pub fn mergePatch(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: []const u8, revision: evt.Patch.MergeRevision) !void { 405 const io = session.io orelse return error.NotFound; 406 const server = switch (session.eventHost()) { 407 .server => |server| server, 408 .local => return error.NotFound, 409 }; 410 const repo_source = data.repo_source orelse return error.NotFound; 411 if (repo_source.repo_kind != .xit) return error.NotFound; 412 const patch_id = evt.parseEventId(id) catch return error.NotFound; 413 414 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 415 var target_repo = try rp.AnyRepo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 416 defer target_repo.deinit(io, allocator); 417 switch (target_repo) { 418 inline else => |*repo| try pch.mergeAndRemoveFork(repo.self_repo_opts, io, allocator, server, repo, .{ 419 .id = id_hex, 420 .revision = revision, 421 .author = author, 422 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 423 }), 424 } 425 } 426 427 pub fn editDraft( 428 data: *const Self, 429 session: *ui.Session, 430 allocator: std.mem.Allocator, 431 author: evt.CommitAuthor, 432 id: []const u8, 433 title: []const u8, 434 labels: []const u8, 435 description: []const u8, 436 target_branch: []const u8, 437 ) !void { 438 const io = session.io orelse return error.NotFound; 439 const users_dir = session.users_dir orelse return error.NotFound; 440 const admin_repo = session.admin_repo orelse return error.NotFound; 441 const repo_id = data.repo_id orelse return error.NotFound; 442 const repo_source = data.repo_source orelse return error.NotFound; 443 const user_id = session.userId() orelse return error.NotFound; 444 const patch_id = evt.parseEventId(id) catch return error.NotFound; 445 if (!evt.Patch.branchValid(target_branch) or !try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidTargetBranch; 446 447 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 448 if (!try pch.editDraft(.{}, io, allocator, users_dir, admin_repo, .{ 449 .id = id_hex, 450 .user_id = user_id, 451 .repo_id = repo_id, 452 .title = title, 453 .labels = labels, 454 .description = description, 455 .target_branch = target_branch, 456 .author = author, 457 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 458 })) return error.NotFound; 459 } 460 461 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: *const [evt.event_id_size]u8) !void { 462 const io = session.io orelse return error.NotFound; 463 const users_dir = session.users_dir orelse return error.NotFound; 464 const user_id = session.userId() orelse return error.NotFound; 465 466 const id_hex = std.fmt.bytesToHex(id.*, .lower); 467 try fork.remove(io, allocator, users_dir, &user_id, &id_hex, author); 468 } 469 470 // `status`'s windowed listing. 471 pub fn window(self: *const Self, status: Status) *const Window { 472 return switch (status) { 473 .open => &self.open, 474 .closed => &self.closed, 475 .merged => &self.merged, 476 }; 477 } 478 479 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 480 // theirs: picks. 481 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 482 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 483 while (it.next()) |pick| { 484 if (std.mem.eql(u8, pick, field)) return true; 485 } 486 return false; 487 } 488 489 // the patch `selected_id` names, in whichever window holds it. 490 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 491 if (self.selected_id.len == 0) return null; 492 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 493 for (win.items) |*entry| { 494 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 495 } 496 } 497 return null; 498 } 499 500 // an empty listing, for the wasm / no-repo paths. 501 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, search: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.PatchesView) !Self { 502 return .{ 503 .identity = try aa.dupe(u8, identity), 504 .default_target_branch = "", 505 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 506 .selected_id = try aa.dupe(u8, selected_id), 507 .comment_id = try aa.dupe(u8, comment_id), 508 .comments_start = comments_start, 509 .open = .empty, 510 .closed = .empty, 511 .merged = .empty, 512 .drafts = .empty, 513 .conflicts = .empty, 514 .theirs_picks = try aa.dupe(u8, theirs_picks), 515 // a description url shows a status list, the patch's own status 516 // picking it once the patch is read; conflicts and resolve urls also 517 // start there, upgraded by init when the conflict data exists. 518 .view = switch (view) { 519 .description, .conflicts, .resolve => .open, 520 else => view, 521 }, 522 .description_page = view == .description, 523 .labels = &.{}, 524 }; 525 } 526 527 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 528 var result = try emptyResult(aa, identity, "", entry.id, "", 0, "", .open); 529 const items = try aa.dupe(PatchWithId, &.{entry}); 530 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 531 if (entry.draft) { 532 result.drafts = detail_window; 533 result.view = .drafts; 534 } else switch (entry.record.event.status) { 535 .open => result.open = detail_window, 536 .closed => { 537 result.closed = detail_window; 538 result.view = .closed; 539 }, 540 .merged => { 541 result.merged = detail_window; 542 result.view = .merged; 543 }, 544 } 545 return result; 546 } 547 548 // read one window per status of an opened repo's patches (filtered to the 549 // search when set), ordered by creation (newest first). the window of the patch 550 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 551 // event db next to it (synced from the events branch on each page build); a 552 // xit repo reads its own db. 553 pub fn init( 554 comptime repo_kind: rp.RepoKind, 555 comptime repo_opts: rp.RepoOpts(repo_kind), 556 arena: *std.heap.ArenaAllocator, 557 repo: *rp.Repo(repo_kind, repo_opts), 558 io: std.Io, 559 // the admin db's moment, for resolving author emails to user names (null 560 // in local mode, which has no users) 561 admin_moment: ?evt.AdminDB.HashMap(.read_only), 562 session: *ui.Session, 563 repo_id_maybe: ?[evt.event_id_size]u8, 564 identity: []const u8, 565 target_branch: []const u8, 566 search: []const u8, 567 selected_id: []const u8, 568 comment_id: []const u8, 569 comments_start: usize, 570 theirs_picks: []const u8, 571 view: ui.RoutablePage.PatchesView, 572 viewer: ?ui.Viewer, 573 ) !Self { 574 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and view == .drafts) .open else view; 575 var empty = try emptyResult(arena.allocator(), identity, search, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 576 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 577 // the early returns below still let the viewer write 578 empty.viewer = viewer; 579 580 const aa = arena.allocator(); 581 const DB = evt.EventDB(repo_opts.hash); 582 const rooted = empty.selected_id.len != 0; 583 const drafts_window = if (admin_moment) |admin| 584 if (repo_id_maybe) |repo_id| 585 if (session.data.user_id != null and session.users_dir != null) 586 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 587 else 588 Window.empty 589 else 590 Window.empty 591 else 592 Window.empty; 593 var draft_selected = false; 594 if (rooted) for (drafts_window.items) |item| { 595 if (std.mem.eql(u8, item.id, empty.selected_id)) { 596 draft_selected = true; 597 break; 598 } 599 }; 600 empty.drafts = drafts_window; 601 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 602 603 // an explicitly named published patch that doesn't exist is a bad url 604 // (NotFound -> 404); drafts, labels, and bare routes can use the empty fallback. 605 const strict = rooted and !draft_selected; 606 607 // a repo with no consumed events has no moment yet. 608 const gpa = arena.child_allocator; 609 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 610 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 611 const haxy_moment = (if (event_db_maybe) |*event_db| 612 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 613 else if (repo_kind == .git) 614 return empty 615 else 616 evt.currentMoment(repo_opts, repo)) catch { 617 if (strict) return error.NotFound; 618 return empty; 619 }; 620 621 // each status's listing intersects every label's set. 622 // patches have no assignees, so an assignee term lists nothing. 623 const filters = try srch_thrd.parseFilters(aa, empty.search orelse ""); 624 const extra_sets: ?[]const DB.SortedSet(.read_only) = if (filters.assignees.len == 0) &.{} else null; 625 const open_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .open); 626 const closed_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .closed); 627 const merged_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .merged); 628 629 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 630 if (strict) return error.NotFound; 631 return empty; 632 }; 633 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 634 635 // the conflicted patches' container: a set view for membership and 636 // windowing, a map view for the resolve entry, over the same cursor. 637 var conflict_set: ?DB.SortedSet(.read_only) = null; 638 var conflicts_map: ?DB.SortedMap(.read_only) = null; 639 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 640 conflict_set = try DB.SortedSet(.read_only).init(cursor); 641 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 642 } 643 644 // a named patch roots its own status's window at itself (the conflicts 645 // view's window instead); the other windows start at the beginning. 646 var resolved_view = empty.view; 647 var open_root: ?[]const u8 = null; 648 var closed_root: ?[]const u8 = null; 649 var merged_root: ?[]const u8 = null; 650 var conflicts_root: ?[]const u8 = null; 651 var conflict_data: ?Conflict = null; 652 if (rooted and !draft_selected) { 653 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 654 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 655 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 656 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 657 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 658 659 if (view == .conflicts) { 660 // the id roots the conflicts window, so it must be conflicted 661 const set = conflict_set orelse return error.NotFound; 662 if (!try set.contains(order_key)) return error.NotFound; 663 conflicts_root = order_key; 664 } else { 665 // the named patch must be in every set its window intersects (a 666 // filtered url can name a patch the filters leave out). 667 const sets = switch (patch_event.event.status) { 668 .open => open_sets, 669 .closed => closed_sets, 670 .merged => merged_sets, 671 }; 672 if (!try thread.inEverySet(DB, sets, order_key)) return error.NotFound; 673 674 switch (patch_event.event.status) { 675 .open => open_root = order_key, 676 .closed => closed_root = order_key, 677 .merged => merged_root = order_key, 678 } 679 // form urls keep their view; otherwise the patch's status picks it. 680 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 681 .open => .open, 682 .closed => .closed, 683 .merged => .merged, 684 }; 685 686 // a resolve url shows the resolve view only while the patch still 687 // has a conflict entry; otherwise the status list stands. the 688 // wasm client renders from the snapshot, so the diff machinery 689 // this pulls in is gated out of its build. 690 if (comptime !wasm) { 691 if (view == .resolve) { 692 if (conflicts_map) |map| { 693 if (try map.getCursor(order_key)) |conflict_cursor| { 694 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 695 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 696 resolved_view = .resolve; 697 } 698 } 699 } 700 } 701 } 702 } 703 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 704 var open_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_sets, open_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 705 var closed_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_sets, closed_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 706 var merged_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_sets, merged_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 707 var conflicts_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, if (conflict_set) |*set| set[0..1] else &.{}, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, "", viewer); 708 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &open_window); 709 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &closed_window); 710 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &merged_window); 711 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &conflicts_window); 712 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 713 714 const comment_page = if (empty.comment_id.len == 0) 715 null 716 else 717 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 718 719 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 720 721 return .{ 722 .identity = empty.identity, 723 .default_target_branch = empty.default_target_branch, 724 .search = empty.search, 725 .selected_id = empty.selected_id, 726 .comment_id = empty.comment_id, 727 .comments_start = comments_start, 728 .comment_page = comment_page, 729 .viewer = viewer, 730 .open = open_window, 731 .closed = closed_window, 732 .merged = merged_window, 733 .drafts = drafts_window, 734 .conflicts = conflicts_window, 735 .conflict = conflict_data, 736 .theirs_picks = empty.theirs_picks, 737 .view = resolved_view, 738 .description_page = empty.description_page, 739 .labels = labels, 740 }; 741 } 742 743 fn setPatchDetails( 744 comptime repo_kind: rp.RepoKind, 745 comptime repo_opts: rp.RepoOpts(repo_kind), 746 io: std.Io, 747 arena: *std.heap.ArenaAllocator, 748 admin_moment: ?evt.AdminDB.HashMap(.read_only), 749 users_dir: ?[]const u8, 750 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 751 repo: *rp.Repo(repo_kind, repo_opts), 752 target: *Window, 753 ) !void { 754 if (target.items.len == 0) return; 755 var config = try repo.listConfig(io, arena.child_allocator); 756 defer config.deinit(); 757 const items = try arena.allocator().dupe(PatchWithId, target.items); 758 for (items) |*item| { 759 const id = try evt.parseEventId(item.id); 760 const status = item.record.event.status.kind(); 761 // a fork-backed patch's fork lives in its forker's user repo 762 if (admin_moment) |moment| if (users_dir) |dir| if (item.record.event.source_branch == null) { 763 if (try fork.readForkerId(&config.sections, &id)) |forker_id| { 764 if (try evt.readForkById(io, arena.child_allocator, arena, dir, &forker_id, &id)) |record| if (!record.removed) { 765 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &forker_id)) |forker| { 766 item.fork_exists = true; 767 item.fork_identity = try arena.allocator().print("{s}:{s}", .{ forker.event.name, record.event.repo_name }); 768 } 769 }; 770 } 771 }; 772 const target_branch = item.record.event.target_branch; 773 const revision = item.record.event.revision orelse continue; 774 item.commit_count = revision.commit_count; 775 const merge_revision: evt.Patch.MergeRevision = switch (item.record.event.status) { 776 .merged => |merged| merged.revision, 777 else => .source, 778 }; 779 item.revision_oid = switch (merge_revision) { 780 .source => revision.source_oid, 781 .squash => revision.squash_oid, 782 }; 783 if (!item.fork_exists or status == .merged) { 784 const revision_id = try evt.parseEventId(&revision.id); 785 if (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, haxy_moment, arena, &revision_id)) |record| { 786 item.base_oid = record.event.base_oid; 787 } 788 } 789 if (status == .merged) { 790 if (merge_revision == .squash) item.commit_count = 1; 791 continue; 792 } 793 if (comptime repo_kind == .xit) { 794 var moment = try repo.core.latestMoment(); 795 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 796 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 797 } 798 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 799 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 800 } 801 target.items = items; 802 } 803 804 fn loadDraftWindow( 805 comptime repo_kind: rp.RepoKind, 806 comptime repo_opts: rp.RepoOpts(repo_kind), 807 arena: *std.heap.ArenaAllocator, 808 session: *ui.Session, 809 admin_moment: evt.AdminDB.HashMap(.read_only), 810 repo_id: *const [evt.event_id_size]u8, 811 selected_id: []const u8, 812 target_repo: *rp.Repo(repo_kind, repo_opts), 813 ) !Window { 814 const user_id = session.userId() orelse return .empty; 815 const user_name = session.data.user_name orelse return .empty; 816 const users_dir = session.users_dir orelse return .empty; 817 const io = session.io orelse return .empty; 818 819 // the viewer's drafts live in their user repo 820 var user_repo = (try evt.openUserRepo(io, arena.child_allocator, users_dir, &user_id)) orelse return .empty; 821 defer user_repo.deinit(io, arena.child_allocator); 822 const user_moment = (try evt.userMoment(&user_repo)) orelse return .empty; 823 const drafts_cursor = try user_moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return .empty; 824 const drafts = try evt.UserDB.HashMap(.read_only).init(drafts_cursor); 825 const set_cursor = try drafts.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return .empty; 826 const set = try evt.UserDB.SortedSet(.read_only).init(set_cursor); 827 const aa = arena.allocator(); 828 829 var root_key: ?[]const u8 = null; 830 if (selected_id.len != 0) { 831 const id = evt.parseEventId(selected_id) catch return error.NotFound; 832 if (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) |record| { 833 if (!record.removed and record.event.stage == .draft) { 834 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 835 if (!try set.contains(order_key)) return error.NotFound; 836 root_key = order_key; 837 } 838 } 839 } 840 841 var prev_id: ?[]const u8 = null; 842 var iter = if (root_key) |root| blk: { 843 const rank = try set.rank(root); 844 if (rank > 0 and rank <= page_size) { 845 prev_id = ""; 846 } else if (rank > page_size) { 847 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 848 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 849 _ = try pair.key_cursor.readBytes(&previous); 850 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 851 prev_id = try aa.dupe(u8, &hex); 852 } 853 break :blk try set.iteratorFrom(root); 854 } else try set.iteratorFromIndex(0); 855 856 var items: std.ArrayList(PatchWithId) = .empty; 857 var next_id: ?[]const u8 = null; 858 while (try iter.next()) |cursor| { 859 const id = try evt.readOrderKeyId(evt.UserDB, cursor); 860 const id_hex = std.fmt.bytesToHex(id, .lower); 861 if (items.items.len == page_size) { 862 next_id = try aa.dupe(u8, &id_hex); 863 break; 864 } 865 const path = try fork.forkPath(aa, users_dir, &user_id, &id); 866 // a fork shares its target's hash kind 867 var fork_repo = rp.Repo(.xit, .{ .hash = repo_opts.hash }).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 868 defer fork_repo.deinit(io, arena.child_allocator); 869 const record = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) orelse continue; 870 const fork_identity = try aa.print("{s}:{s}", .{ user_name, record.event.repo_name }); 871 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, fork_identity)) orelse continue; 872 try items.append(aa, entry); 873 } 874 return .{ 875 .items = items.items, 876 .prev_id = prev_id, 877 .next_id = next_id, 878 .count = @intCast(try set.count()), 879 }; 880 } 881 882 pub fn loadDraftEntry( 883 comptime repo_kind: rp.RepoKind, 884 comptime repo_opts: rp.RepoOpts(repo_kind), 885 arena: *std.heap.ArenaAllocator, 886 io: std.Io, 887 admin_moment: ?evt.AdminDB.HashMap(.read_only), 888 fork_repo: *rp.Repo(.xit, .{ .hash = repo_opts.hash }), 889 target_repo: *rp.Repo(repo_kind, repo_opts), 890 id: [evt.event_id_size]u8, 891 fork_identity: []const u8, 892 ) !?PatchWithId { 893 const aa = arena.allocator(); 894 const id_hex = std.fmt.bytesToHex(id, .lower); 895 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 896 const moment = evt.currentMoment(.{ .hash = repo_opts.hash }, fork_repo) catch return null; 897 const patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, &id)) orelse return null; 898 const target_branch = patch.event.target_branch; 899 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 900 const commit_count: ?u64 = blk: { 901 const newest = evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena) catch break :blk null; 902 break :blk if (newest) |revision| revision.record.commit_count else 0; 903 }; 904 return .{ 905 .id = try aa.dupe(u8, &id_hex), 906 .record = patch, 907 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 908 .draft = true, 909 .revision_oid = try aa.dupe(u8, &fork_oid), 910 .commit_count = commit_count, 911 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 912 .fork_exists = true, 913 .fork_identity = try aa.dupe(u8, fork_identity), 914 }; 915 } 916 917 pub const View = thread.View(.patch, Self); 918 pub const Detail = thread.Detail(.patch, Self); 919 pub const detail_widget_name = "repo_patch_detail"; 920 921 pub const Header = thread.Header; 922 923 const open_tab_label = "open"; 924 const closed_tab_label = "closed"; 925 const merged_tab_label = "merged"; 926 const labels_tab_label = "labels"; 927 const edit_tab_label = "edit"; 928 const publish_tab_label = "publish"; 929 const merge_tab_label = "merge"; 930 const reply_tab_label = "reply"; 931 const remove_tab_label = "remove"; 932 const resolve_tab_label = "resolve"; 933 const new_tab_label = "new"; 934 const drafts_tab_label = "drafts"; 935 const conflicts_tab_label = "conflicts"; 936 937 pub fn appendDetails(_: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 938 const aa = session.page_arena.allocator(); 939 const status_kind = entry.record.event.status.kind(); 940 const target_branch = entry.record.event.target_branch; 941 942 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_url) |ssh_url| { 943 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, entry.fork_identity, entry.id }); 944 const push_command = try aa.print("git push {s} HEAD:patch", .{url}); 945 const clone_command = try aa.print("git clone {s}", .{url}); 946 const merge_command = try aa.print("git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 947 const choices = [_]ui.widget.CopyableText.Choice{ 948 .{ 949 .selector = "push", 950 .text = push_command, 951 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 952 .label = " push to this patch from existing repo ", 953 }, 954 .{ 955 .selector = "clone", 956 .text = clone_command, 957 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 958 .label = " clone this patch ", 959 }, 960 .{ 961 .selector = "merge", 962 .text = merge_command, 963 .copyable_text = try aa.print("{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 964 .label = " merge this patch locally ", 965 }, 966 }; 967 var copyable_text = try ui.widget.CopyableText.init(allocator, session, if (entry.target_deleted) choices[1..] else &choices, 0); 968 errdefer copyable_text.deinit(allocator); 969 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 970 }; 971 if (entry.record.event.source_branch) |branch| { 972 var source = try wgt.TextBox.init(allocator, branch, .{ .top_label = .{ .text = " source branch " }, .border = .single, .round_corners = true, .wrap_kind = .none }); 973 errdefer source.deinit(allocator); 974 source.getFocus().mode = .all; 975 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 976 } 977 const fields = [_]ui.widget.CopyableText.Choice{ 978 .{ 979 .selector = "from", 980 .text = entry.revision_oid, 981 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 982 }, 983 .{ 984 .selector = "to", 985 .text = target_branch, 986 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 987 }, 988 }; 989 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..], 0); 990 errdefer copyable_text.deinit(allocator); 991 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 992 } 993 994 // tabs switching between the patches page's views 995 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 996 var header = try Header.init(allocator, session, data.search, " search "); 997 errdefer header.deinit(allocator); 998 const selected_index = View.viewIndex(data.view); 999 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1000 1001 // a list tab per status, labeled with its listing's patch count 1002 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1003 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1004 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, ""), data.search); 1005 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1006 try header.addTab(allocator, status_label, try thread.countLabel(session, data.window(status).*), link, index); 1007 } 1008 1009 // labels tab 1010 { 1011 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity), data.search); 1012 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1013 try header.addTab(allocator, labels_tab_label, "", labels_link, View.viewIndex(.labels)); 1014 } 1015 1016 // new-patch tab; an edit or resolve url shows its tab in this place 1017 { 1018 const route = switch (data.view) { 1019 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1020 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1021 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1022 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1023 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1024 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1025 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1026 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1027 }; 1028 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1029 const label: []const u8 = switch (data.view) { 1030 .edit => edit_tab_label, 1031 .publish => publish_tab_label, 1032 .merge => merge_tab_label, 1033 .new_comment => reply_tab_label, 1034 .edit_comment => edit_tab_label, 1035 .remove => remove_tab_label, 1036 .resolve => resolve_tab_label, 1037 else => new_tab_label, 1038 }; 1039 try header.addTab(allocator, label, "", link, View.viewIndex(.new)); 1040 } 1041 1042 // drafts belong to the logged-in user and are not available locally 1043 if (session.data.host_kind != .local) { 1044 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1045 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1046 try header.addTab(allocator, drafts_tab_label, try session.page_arena.allocator().print("({d})", .{data.drafts.count}), link, View.viewIndex(.drafts)); 1047 } 1048 1049 // conflicts tab, labeled with the conflict count 1050 if (data.conflicts.count > 0) { 1051 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1052 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1053 try header.addTab(allocator, conflicts_tab_label, try session.page_arena.allocator().print("({d})", .{data.conflicts.count}), link, View.viewIndex(.conflicts)); 1054 } 1055 1056 header.select(View.viewIndex(data.view)); 1057 return header; 1058 } 1059