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