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