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