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