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