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