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