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