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