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