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