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