╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
451 .repo_id = repo_id, 452 .title = title, 453 .labels = labels, 454 .description = description, 455 .target_branch = target_branch, 456 .author = author, 457 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 458 })) return error.NotFound; 459 } 460 461 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: *const [evt.event_id_size]u8) !void { 462 const io = session.io orelse return error.NotFound; 463 const users_dir = session.users_dir orelse return error.NotFound; 464 const user_id = session.userId() orelse return error.NotFound; 465 466 const id_hex = std.fmt.bytesToHex(id.*, .lower); 467 try fork.remove(io, allocator, users_dir, &user_id, &id_hex, author); 468 } 469 470 // `status`'s windowed listing. 471 pub fn window(self: *const Self, status: Status) *const Window { 472 return switch (status) { 473 .open => &self.open, 474 .closed => &self.closed, 475 .merged => &self.merged, 476 }; 477 } 478 479 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 480 // theirs: picks. 481 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 482 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 483 while (it.next()) |pick| { 484 if (std.mem.eql(u8, pick, field)) return true; 485 } 486 return false; 487 } 488 489 // the patch `selected_id` names, in whichever window holds it. 490 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 491 if (self.selected_id.len == 0) return null; 492 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 493 for (win.items) |*entry| { 494 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 495 } 496 } 497 return null; 498 } 499 500 // an empty listing, for the wasm / no-repo paths. 501 pub fn emptyResult(aa: std.mem.Allocator, identity: []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 { 502 return .{ 503 .identity = try aa.dupe(u8, identity), 504 .default_target_branch = "", 505 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 506 .selected_id = try aa.dupe(u8, selected_id), 507 .comment_id = try aa.dupe(u8, comment_id), 508 .comments_start = comments_start, 509 .open = .empty, 510 .closed = .empty, 511 .merged = .empty, 512 .drafts = .empty, 513 .conflicts = .empty, 514 .theirs_picks = try aa.dupe(u8, theirs_picks), 515 // a description url shows a status list, the patch's own status 516 // picking it once the patch is read; conflicts and resolve urls also 517 // start there, upgraded by init when the conflict data exists. 518 .view = switch (view) { 519 .description, .conflicts, .resolve => .open, 520 else => view, 521 }, 522 .description_page = view == .description, 523 .labels = &.{}, 524 }; 525 } 526 527 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 528 var result = try emptyResult(aa, identity, "", entry.id, "", 0, "", .open); 529 const items = try aa.dupe(PatchWithId, &.{entry}); 530 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 531 if (entry.draft) { 532 result.drafts = detail_window; 533 result.view = .drafts; 534 } else switch (entry.record.event.status) { 535 .open => result.open = detail_window, 536 .closed => { 537 result.closed = detail_window; 538 result.view = .closed; 539 }, 540 .merged => { 541 result.merged = detail_window; 542 result.view = .merged; 543 }, 544 } 545 return result; 546 } 547 548 // read one window per status of an opened repo's patches (filtered to the 549 // search when set), ordered by creation (newest first). the window of the patch 550 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 551 // event db next to it (synced from the events branch on each page build); a 552 // xit repo reads its own db. 553 pub fn init( 554 comptime repo_kind: rp.RepoKind, 555 comptime repo_opts: rp.RepoOpts(repo_kind), 556 arena: *std.heap.ArenaAllocator, 557 repo: *rp.Repo(repo_kind, repo_opts), 558 io: std.Io, 559 // the admin db's moment, for resolving author emails to user names (null 560 // in local mode, which has no users) 561 admin_moment: ?evt.AdminDB.HashMap(.read_only), 562 session: *ui.Session, 563 repo_id_maybe: ?[evt.event_id_size]u8, 564 identity: []const u8, 565 target_branch: []const u8, 566 search: []const u8, 567 selected_id: []const u8, 568 comment_id: []const u8, 569 comments_start: usize, 570 theirs_picks: []const u8, 571 view: ui.RoutablePage.PatchesView, 572 viewer: ?ui.Viewer, 573 ) !Self { 574 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and view == .drafts) .open else view; 575 var empty = try emptyResult(arena.allocator(), identity, search, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 576 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 577 // the early returns below still let the viewer write 578 empty.viewer = viewer; 579 580 const aa = arena.allocator(); 581 const DB = evt.EventDB(repo_opts.hash); 582 const rooted = empty.selected_id.len != 0; 583 const drafts_window = if (admin_moment) |admin| 584 if (repo_id_maybe) |repo_id| 585 if (session.data.user_id != null and session.users_dir != null) 586 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 587 else 588 Window.empty 589 else 590 Window.empty 591 else 592 Window.empty; 593 var draft_selected = false; 594 if (rooted) for (drafts_window.items) |item| { 595 if (std.mem.eql(u8, item.id, empty.selected_id)) { 596 draft_selected = true; 597 break; 598 } 599 }; 600 empty.drafts = drafts_window; 601 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 602 603 // an explicitly named published patch that doesn't exist is a bad url 604 // (NotFound -> 404); drafts, labels, and bare routes can use the empty fallback. 605 const strict = rooted and !draft_selected; 606 607 // a repo with no consumed events has no moment yet. 608 const gpa = arena.child_allocator; 609 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; 610 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 611 const haxy_moment = (if (event_db_maybe) |*event_db| 612 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 613 else if (repo_kind == .git) 614 return empty 615 else 616 evt.currentMoment(repo_opts, repo)) catch { 617 if (strict) return error.NotFound; 618 return empty; 619 }; 620 621 // each status's listing intersects every label's set. 622 // patches have no assignees, so an assignee term lists nothing. 623 const filters = try srch_thrd.parseFilters(aa, empty.search orelse ""); 624 const extra_sets: ?[]const DB.SortedSet(.read_only) = if (filters.assignees.len == 0) &.{} else null; 625 const open_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .open); 626 const closed_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .closed); 627 const merged_sets = try thread.statusSets(Self, repo_opts.hash, aa, haxy_moment, filters.labels, extra_sets, .merged); 628 629 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 630 if (strict) return error.NotFound; 631 return empty; 632 }; 633 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 634 635 // the conflicted patches' container: a set view for membership and 636 // windowing, a map view for the resolve entry, over the same cursor. 637 var conflict_set: ?DB.SortedSet(.read_only) = null; 638 var conflicts_map: ?DB.SortedMap(.read_only) = null; 639 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 640 conflict_set = try DB.SortedSet(.read_only).init(cursor); 641 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 642 } 643 644 // a named patch roots its own status's window at itself (the conflicts 645 // view's window instead); the other windows start at the beginning. 646 var resolved_view = empty.view; 647 var open_root: ?[]const u8 = null; 648 var closed_root: ?[]const u8 = null; 649 var merged_root: ?[]const u8 = null; 650 var conflicts_root: ?[]const u8 = null; 651 var conflict_data: ?Conflict = null; 652 if (rooted and !draft_selected) { 653 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 654 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 655 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 656 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 657 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 658 659 if (view == .conflicts) { 660 // the id roots the conflicts window, so it must be conflicted 661 const set = conflict_set orelse return error.NotFound; 662 if (!try set.contains(order_key)) return error.NotFound; 663 conflicts_root = order_key; 664 } else { 665 // the named patch must be in every set its window intersects (a 666 // filtered url can name a patch the filters leave out). 667 const sets = switch (patch_event.event.status) { 668 .open => open_sets, 669 .closed => closed_sets, 670 .merged => merged_sets, 671 }; 672 if (!try thread.inEverySet(DB, sets, order_key)) return error.NotFound; 673 674 switch (patch_event.event.status) { 675 .open => open_root = order_key, 676 .closed => closed_root = order_key, 677 .merged => merged_root = order_key, 678 } 679 // form urls keep their view; otherwise the patch's status picks it. 680 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 681 .open => .open, 682 .closed => .closed, 683 .merged => .merged, 684 }; 685 686 // a resolve url shows the resolve view only while the patch still 687 // has a conflict entry; otherwise the status list stands. the 688 // wasm client renders from the snapshot, so the diff machinery 689 // this pulls in is gated out of its build. 690 if (comptime !wasm) { 691 if (view == .resolve) { 692 if (conflicts_map) |map| { 693 if (try map.getCursor(order_key)) |conflict_cursor| { 694 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 695 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 696 resolved_view = .resolve; 697 } 698 } 699 } 700 } 701 } 702 } 703 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 704 var open_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_sets, open_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 705 var closed_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_sets, closed_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 706 var merged_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_sets, merged_root, conflict_set, empty.selected_id, thread_comments_start, filters.words, viewer); 707 var conflicts_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, if (conflict_set) |*set| set[0..1] else &.{}, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, "", viewer); 708 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &open_window); 709 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &closed_window); 710 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &merged_window); 711 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.users_dir, haxy_moment, repo, &conflicts_window); 712 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 713 714 const comment_page = if (empty.comment_id.len == 0) 715 null 716 else 717 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 718 719 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 720 721 return .{ 722 .identity = empty.identity, 723 .default_target_branch = empty.default_target_branch, 724 .search = empty.search, 725 .selected_id = empty.selected_id, 726 .comment_id = empty.comment_id, 727 .comments_start = comments_start, 728 .comment_page = comment_page, 729 .viewer = viewer, 730 .open = open_window, 731 .closed = closed_window, 732 .merged = merged_window, 733 .drafts = drafts_window, 734 .conflicts = conflicts_window, 735 .conflict = conflict_data, 736 .theirs_picks = empty.theirs_picks, 737 .view = resolved_view, 738 .description_page = empty.description_page, 739 .labels = labels, 740 }; 741 } 742 743 fn setPatchDetails( 744 comptime repo_kind: rp.RepoKind, 745 comptime repo_opts: rp.RepoOpts(repo_kind), 746 io: std.Io, 747 arena: *std.heap.ArenaAllocator, 748 admin_moment: ?evt.AdminDB.HashMap(.read_only), 749 users_dir: ?[]const u8, 750 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 751 repo: *rp.Repo(repo_kind, repo_opts), 752 target: *Window, 753 ) !void { 754 if (target.items.len == 0) return; 755 var config = try repo.listConfig(io, arena.child_allocator); 756 defer config.deinit(); 757 const items = try arena.allocator().dupe(PatchWithId, target.items); 758 for (items) |*item| { 759 const id = try evt.parseEventId(item.id); 760 const status = item.record.event.status.kind(); 761 // a fork-backed patch's fork lives in its forker's user repo 762 if (admin_moment) |moment| if (users_dir) |dir| if (item.record.event.source_branch == null) { 763 if (try fork.readForkerId(&config.sections, &id)) |forker_id| { 764 if (try evt.readForkById(io, arena.child_allocator, arena, dir, &forker_id, &id)) |record| if (!record.removed) { 765 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &forker_id)) |forker| { 766 item.fork_exists = true; 767 item.fork_identity = try arena.allocator().print("{s}:{s}", .{ forker.event.name, record.event.repo_name }); 768 } 769 }; 770 } 771 }; 772 const target_branch = item.record.event.target_branch; 773 const revision = item.record.event.revision orelse continue; 774 item.commit_count = revision.commit_count; 775 const merge_revision: evt.Patch.MergeRevision = switch (item.record.event.status) { 776 .merged => |merged| merged.revision, 777 else => .source, 778 }; 779 item.revision_oid = switch (merge_revision) { 780 .source => revision.source_oid, 781 .squash => revision.squash_oid, 782 }; 783 if (!item.fork_exists or status == .merged) { 784 const revision_id = try evt.parseEventId(&revision.id); 785 if (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, haxy_moment, arena, &revision_id)) |record| { 786 item.base_oid = record.event.base_oid; 787 } 788 } 789 if (status == .merged) { 790 if (merge_revision == .squash) item.commit_count = 1; 791 continue; 792 } 793 if (comptime repo_kind == .xit) { 794 var moment = try repo.core.latestMoment(); 795 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 796 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 797 } 798 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 799 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 800 } 801 target.items = items; 802 } 803 804 fn loadDraftWindow( 805 comptime repo_kind: rp.RepoKind, 806 comptime repo_opts: rp.RepoOpts(repo_kind), 807 arena: *std.heap.ArenaAllocator, 808 session: *ui.Session, 809 admin_moment: evt.AdminDB.HashMap(.read_only), 810 repo_id: *const [evt.event_id_size]u8, 811 selected_id: []const u8, 812 target_repo: *rp.Repo(repo_kind, repo_opts), 813 ) !Window { 814 const user_id = session.userId() orelse return .empty; 815 const user_name = session.data.user_name orelse return .empty; 816 const users_dir = session.users_dir orelse return .empty; 817 const io = session.io orelse return .empty; 818 819 // the viewer's drafts live in their user repo 820 var user_repo = (try evt.openUserRepo(io, arena.child_allocator, users_dir, &user_id)) orelse return .empty; 821 defer user_repo.deinit(io, arena.child_allocator); 822 const user_moment = (try evt.userMoment(&user_repo)) orelse return .empty; 823 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; 824 const drafts = try evt.UserDB.HashMap(.read_only).init(drafts_cursor); 825 const set_cursor = try drafts.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return .empty; 826 const set = try evt.UserDB.SortedSet(.read_only).init(set_cursor); 827 const aa = arena.allocator(); 828 829 var root_key: ?[]const u8 = null; 830 if (selected_id.len != 0) { 831 const id = evt.parseEventId(selected_id) catch return error.NotFound; 832 if (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) |record| { 833 if (!record.removed and record.event.stage == .draft) { 834 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 835 if (!try set.contains(order_key)) return error.NotFound; 836 root_key = order_key; 837 } 838 } 839 } 840 841 var prev_id: ?[]const u8 = null; 842 var iter = if (root_key) |root| blk: { 843 const rank = try set.rank(root); 844 if (rank > 0 and rank <= page_size) { 845 prev_id = ""; 846 } else if (rank > page_size) { 847 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 848 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 849 _ = try pair.key_cursor.readBytes(&previous); 850 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 851 prev_id = try aa.dupe(u8, &hex); 852 } 853 break :blk try set.iteratorFrom(root); 854 } else try set.iteratorFromIndex(0); 855 856 var items: std.ArrayList(PatchWithId) = .empty; 857 var next_id: ?[]const u8 = null; 858 while (try iter.next()) |cursor| { 859 const id = try evt.readOrderKeyId(evt.UserDB, cursor); 860 const id_hex = std.fmt.bytesToHex(id, .lower); 861 if (items.items.len == page_size) { 862 next_id = try aa.dupe(u8, &id_hex); 863 break; 864 } 865 const path = try fork.forkPath(aa, users_dir, &user_id, &id); 866 // a fork shares its target's hash kind 867 var fork_repo = rp.Repo(.xit, .{ .hash = repo_opts.hash }).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 868 defer fork_repo.deinit(io, arena.child_allocator); 869 const record = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) orelse continue; 870 const fork_identity = try aa.print("{s}:{s}", .{ user_name, record.event.repo_name }); 871 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, fork_identity)) orelse continue; 872 try items.append(aa, entry); 873 } 874 return .{ 875 .items = items.items, 876 .prev_id = prev_id, 877 .next_id = next_id, 878 .count = @intCast(try set.count()), 879 }; 880 } 881 882 pub fn loadDraftEntry( 883 comptime repo_kind: rp.RepoKind, 884 comptime repo_opts: rp.RepoOpts(repo_kind), 885 arena: *std.heap.ArenaAllocator, 886 io: std.Io, 887 admin_moment: ?evt.AdminDB.HashMap(.read_only), 888 fork_repo: *rp.Repo(.xit, .{ .hash = repo_opts.hash }), 889 target_repo: *rp.Repo(repo_kind, repo_opts), 890 id: [evt.event_id_size]u8, 891 fork_identity: []const u8, 892 ) !?PatchWithId { 893 const aa = arena.allocator(); 894 const id_hex = std.fmt.bytesToHex(id, .lower); 895 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 896 const moment = evt.currentMoment(.{ .hash = repo_opts.hash }, fork_repo) catch return null; 897 const patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, &id)) orelse return null; 898 const target_branch = patch.event.target_branch; 899 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 900 const commit_count: ?u64 = blk: { 901 const newest = evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena) catch break :blk null; 902 break :blk if (newest) |revision| revision.record.commit_count else 0; 903 }; 904 return .{ 905 .id = try aa.dupe(u8, &id_hex), 906 .record = patch, 907 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 908 .draft = true, 909 .revision_oid = try aa.dupe(u8, &fork_oid), 910 .commit_count = commit_count, 911 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 912 .fork_exists = true, 913 .fork_identity = try aa.dupe(u8, fork_identity), 914 }; 915 } 916 917 pub const View = thread.View(.patch, Self); 918 pub const Detail = thread.Detail(.patch, Self); 919 pub const detail_widget_name = "repo_patch_detail"; 920 921 pub const Header = thread.Header; 922 923 const open_tab_label = "open"; 924 const closed_tab_label = "closed"; 925 const merged_tab_label = "merged"; 926 const labels_tab_label = "labels"; 927 const edit_tab_label = "edit"; 928 const publish_tab_label = "publish"; 929 const merge_tab_label = "merge"; 930 const reply_tab_label = "reply"; 931 const remove_tab_label = "remove"; 932 const resolve_tab_label = "resolve"; 933 const new_tab_label = "new"; 934 const drafts_tab_label = "drafts"; 935 const conflicts_tab_label = "conflicts"; 936 937 pub fn appendDetails(_: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 938 const aa = session.page_arena.allocator(); 939 const status_kind = entry.record.event.status.kind(); 940 const target_branch = entry.record.event.target_branch; 941 942 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_url) |ssh_url| { 943 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, entry.fork_identity, entry.id }); 944 const push_command = try aa.print("git push {s} HEAD:patch", .{url}); 945 const clone_command = try aa.print("git clone {s}", .{url}); 946 const merge_command = try aa.print("git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 947 const choices = [_]ui.widget.CopyableText.Choice{ 948 .{ 949 .selector = "push", 950 .text = push_command, 951 .copyable_text = try aa.print("{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 aa.print("{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 aa.print("{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, if (entry.target_deleted) choices[1..] else &choices, 0); 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, .{ .top_label = .{ .text = " source branch " }, .border = .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)..], 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 // tabs switching between the patches page's views 995 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 996 var header = try Header.init(allocator, session, data.search, " search "); 997 errdefer header.deinit(allocator); 998 const selected_index = View.viewIndex(data.view); 999 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1000 1001 // a list tab per status, labeled with its listing's patch count 1002 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1003 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1004 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, ""), data.search); 1005 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1006 try header.addTab(allocator, status_label, try thread.countLabel(session, data.window(status).*), link, index); 1007 } 1008 1009 // labels tab 1010 { 1011 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity), data.search); 1012 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1013 try header.addTab(allocator, labels_tab_label, "", labels_link, View.viewIndex(.labels)); 1014 } 1015 1016 // new-patch tab; an edit or resolve url shows its tab in this place 1017 { 1018 const route = switch (data.view) { 1019 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1020 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1021 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1022 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1023 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1024 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1025 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1026 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1027 }; 1028 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1029 const label: []const u8 = switch (data.view) { 1030 .edit => edit_tab_label, 1031 .publish => publish_tab_label, 1032 .merge => merge_tab_label, 1033 .new_comment => reply_tab_label, 1034 .edit_comment => edit_tab_label, 1035 .remove => remove_tab_label, 1036 .resolve => resolve_tab_label, 1037 else => new_tab_label, 1038 }; 1039 try header.addTab(allocator, label, "", link, View.viewIndex(.new)); 1040 } 1041 1042 // drafts belong to the logged-in user and are not available locally 1043 if (session.data.host_kind != .local) { 1044 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1045 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1046 try header.addTab(allocator, drafts_tab_label, try session.page_arena.allocator().print("({d})", .{data.drafts.count}), link, View.viewIndex(.drafts)); 1047 } 1048 1049 // conflicts tab, labeled with the conflict count 1050 if (data.conflicts.count > 0) { 1051 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1052 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1053 try header.addTab(allocator, conflicts_tab_label, try session.page_arena.allocator().print("({d})", .{data.conflicts.count}), link, View.viewIndex(.conflicts)); 1054 } 1055 1056 header.select(View.viewIndex(data.view)); 1057 return header; 1058 } 1059