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