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