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