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