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