╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
524 if (entry.draft) { 525 result.drafts = detail_window; 526 result.view = .drafts; 527 } else switch (entry.record.event.status) { 528 .open => result.open = detail_window, 529 .closed => { 530 result.closed = detail_window; 531 result.view = .closed; 532 }, 533 .merged => { 534 result.merged = detail_window; 535 result.view = .merged; 536 }, 537 } 538 return result; 539 } 540 541 // read one window per status of an opened repo's patches (filtered to `tag` 542 // when set), ordered by creation (newest first). the window of the patch 543 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 544 // event db next to it (synced from the events branch on each page build); a 545 // xit repo reads its own db. 546 pub fn init( 547 comptime repo_kind: rp.RepoKind, 548 comptime repo_opts: rp.RepoOpts(repo_kind), 549 arena: *std.heap.ArenaAllocator, 550 repo: *rp.Repo(repo_kind, repo_opts), 551 io: std.Io, 552 // the admin db's moment, for resolving author emails to user names (null 553 // in local mode, which has no users) 554 admin_moment: ?evt.AdminDB.HashMap(.read_only), 555 session: *ui.Session, 556 repo_id_maybe: ?[evt.event_id_size]u8, 557 identity: []const u8, 558 target_branch: []const u8, 559 tag: []const u8, 560 search: []const u8, 561 selected_id: []const u8, 562 comment_id: []const u8, 563 comments_start: usize, 564 theirs_picks: []const u8, 565 view: ui.RoutablePage.PatchesView, 566 ) !Self { 567 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and view == .drafts) .open else view; 568 var empty = try emptyResult(arena.allocator(), identity, tag, search, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 569 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 570 571 const aa = arena.allocator(); 572 const DB = evt.EventDB(repo_opts.hash); 573 const rooted = empty.selected_id.len != 0; 574 const tagged = empty.tag.len != 0; 575 const drafts_window = if (admin_moment) |admin| 576 if (repo_id_maybe) |repo_id| 577 if (session.data.user_id != null and session.repos_dir != null) 578 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 579 else 580 Window.empty 581 else 582 Window.empty 583 else 584 Window.empty; 585 var draft_selected = false; 586 if (rooted) for (drafts_window.items) |item| { 587 if (std.mem.eql(u8, item.id, empty.selected_id)) { 588 draft_selected = true; 589 break; 590 } 591 }; 592 empty.drafts = drafts_window; 593 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 594 595 // an explicitly named published patch that doesn't exist is a bad url 596 // (NotFound -> 404); drafts, tags, and bare routes can use the empty fallback. 597 const strict = rooted and !draft_selected; 598 599 // a repo with no consumed events has no moment yet. 600 const gpa = arena.child_allocator; 601 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; 602 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 603 const haxy_moment = (if (event_db_maybe) |*event_db| 604 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 605 else if (repo_kind == .git) 606 return empty 607 else 608 evt.currentMoment(repo_opts, repo)) catch { 609 if (strict) return error.NotFound; 610 return empty; 611 }; 612 613 // the sorted sets to window, per status: the tag's sets when filtered, 614 // else the top-level per-status sets. a missing set is an empty window. 615 var open_set: ?DB.SortedSet(.read_only) = null; 616 var closed_set: ?DB.SortedSet(.read_only) = null; 617 var merged_set: ?DB.SortedSet(.read_only) = null; 618 if (tagged) { 619 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key))) |tag_to_patches_cursor| { 620 const tag_to_patches = try DB.SortedMap(.read_only).init(tag_to_patches_cursor); 621 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 622 open_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .open); 623 closed_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .closed); 624 merged_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .merged); 625 } 626 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 627 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 628 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 629 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 630 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 631 } else if (strict) return error.NotFound; 632 633 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 634 if (strict) return error.NotFound; 635 return empty; 636 }; 637 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 638 639 // the conflicted patches' container: a set view for membership and 640 // windowing, a map view for the resolve entry, over the same cursor. 641 var conflict_set: ?DB.SortedSet(.read_only) = null; 642 var conflicts_map: ?DB.SortedMap(.read_only) = null; 643 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 644 conflict_set = try DB.SortedSet(.read_only).init(cursor); 645 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 646 } 647 648 // a named patch roots its own status's window at itself (the conflicts 649 // view's window instead); the other windows start at the beginning. 650 var resolved_view = empty.view; 651 var open_root: ?[]const u8 = null; 652 var closed_root: ?[]const u8 = null; 653 var merged_root: ?[]const u8 = null; 654 var conflicts_root: ?[]const u8 = null; 655 var conflict_data: ?Conflict = null; 656 if (rooted and !draft_selected) { 657 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 658 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 659 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 660 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 661 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 662 663 if (view == .conflicts) { 664 // the id roots the conflicts window, so it must be conflicted 665 const set = conflict_set orelse return error.NotFound; 666 if (!try set.contains(order_key)) return error.NotFound; 667 conflicts_root = order_key; 668 } else { 669 // the named patch must be in its windowed set (a tag url can name 670 // an patch that doesn't carry the tag). 671 const set = (switch (patch_event.event.status) { 672 .open => open_set, 673 .closed => closed_set, 674 .merged => merged_set, 675 }) orelse return error.NotFound; 676 if (!try set.contains(order_key)) return error.NotFound; 677 678 switch (patch_event.event.status) { 679 .open => open_root = order_key, 680 .closed => closed_root = order_key, 681 .merged => merged_root = order_key, 682 } 683 // form urls keep their view; otherwise the patch's status picks it. 684 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 685 .open => .open, 686 .closed => .closed, 687 .merged => .merged, 688 }; 689 690 // a resolve url shows the resolve view only while the patch still 691 // has a conflict entry; otherwise the status list stands. the 692 // wasm client renders from the snapshot, so the diff machinery 693 // this pulls in is gated out of its build. 694 if (comptime !wasm) { 695 if (view == .resolve) { 696 if (conflicts_map) |map| { 697 if (try map.getCursor(order_key)) |conflict_cursor| { 698 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 699 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 700 resolved_view = .resolve; 701 } 702 } 703 } 704 } 705 } 706 } 707 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 708 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); 709 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); 710 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); 711 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); 712 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, haxy_moment, repo, &open_window); 713 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, haxy_moment, repo, &closed_window); 714 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, haxy_moment, repo, &merged_window); 715 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, haxy_moment, repo, &conflicts_window); 716 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 717 718 const comment_page = if (empty.comment_id.len == 0) 719 null 720 else 721 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 722 723 const tags = try thread.loadTags(Self, repo_opts.hash, arena, haxy_moment); 724 725 return .{ 726 .identity = empty.identity, 727 .default_target_branch = empty.default_target_branch, 728 .tag = empty.tag, 729 .search = empty.search, 730 .selected_id = empty.selected_id, 731 .comment_id = empty.comment_id, 732 .comments_start = comments_start, 733 .comment_page = comment_page, 734 .open = open_window, 735 .closed = closed_window, 736 .merged = merged_window, 737 .drafts = drafts_window, 738 .conflicts = conflicts_window, 739 .conflict = conflict_data, 740 .theirs_picks = empty.theirs_picks, 741 .view = resolved_view, 742 .description_page = empty.description_page, 743 .tags = tags, 744 }; 745 } 746 747 fn setPatchDetails( 748 comptime repo_kind: rp.RepoKind, 749 comptime repo_opts: rp.RepoOpts(repo_kind), 750 io: std.Io, 751 arena: *std.heap.ArenaAllocator, 752 admin_moment: ?evt.AdminDB.HashMap(.read_only), 753 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 754 repo: *rp.Repo(repo_kind, repo_opts), 755 target: *Window, 756 ) !void { 757 if (target.items.len == 0) return; 758 const items = try arena.allocator().dupe(PatchWithId, target.items); 759 for (items) |*item| { 760 const id = try evt.parseEventId(item.id); 761 const status = item.record.event.status.kind(); 762 if (admin_moment) |moment| { 763 const record = try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &id); 764 item.fork_exists = if (record) |value| !value.removed else false; 765 } 766 const target_branch = item.record.event.target_branch; 767 const revision = item.record.event.revision orelse continue; 768 item.commit_count = revision.commit_count; 769 const merge_revision: evt.Patch.MergeRevision = switch (item.record.event.status) { 770 .merged => |merged| merged.revision, 771 else => .source, 772 }; 773 item.revision_oid = switch (merge_revision) { 774 .source => revision.source_oid, 775 .squash => revision.squash_oid, 776 }; 777 if (!item.fork_exists or status == .merged) { 778 const revision_id = try evt.parseEventId(&revision.id); 779 if (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, haxy_moment, arena, &revision_id)) |record| { 780 item.base_oid = record.event.base_oid; 781 } 782 } 783 if (status == .merged) { 784 if (merge_revision == .squash) item.commit_count = 1; 785 continue; 786 } 787 if (comptime repo_kind == .xit) { 788 var moment = try repo.core.latestMoment(); 789 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 790 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 791 } 792 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 793 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 794 } 795 target.items = items; 796 } 797 798 fn loadDraftWindow( 799 comptime repo_kind: rp.RepoKind, 800 comptime repo_opts: rp.RepoOpts(repo_kind), 801 arena: *std.heap.ArenaAllocator, 802 session: *ui.Session, 803 admin_moment: evt.AdminDB.HashMap(.read_only), 804 repo_id: *const [evt.event_id_size]u8, 805 selected_id: []const u8, 806 target_repo: *rp.Repo(repo_kind, repo_opts), 807 ) !Window { 808 const user_id = session.data.user_id orelse return .empty; 809 if (user_id.len != evt.event_id_size) return .empty; 810 const repos_dir = session.repos_dir orelse return .empty; 811 const io = session.io orelse return .empty; 812 const key = evt.Fork.draftKey(repo_id, user_id); 813 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; 814 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 815 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 816 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 817 const aa = arena.allocator(); 818 819 var root_key: ?[]const u8 = null; 820 if (selected_id.len != 0) { 821 const id = evt.parseEventId(selected_id) catch return error.NotFound; 822 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 823 if (!record.removed and record.event.stage == .draft) { 824 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 825 if (!try set.contains(order_key)) return error.NotFound; 826 root_key = order_key; 827 } 828 } 829 } 830 831 var prev_id: ?[]const u8 = null; 832 var iter = if (root_key) |root| blk: { 833 const rank = try set.rank(root); 834 if (rank > 0 and rank <= page_size) { 835 prev_id = ""; 836 } else if (rank > page_size) { 837 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 838 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 839 _ = try pair.key_cursor.readBytes(&previous); 840 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 841 prev_id = try aa.dupe(u8, &hex); 842 } 843 break :blk try set.iteratorFrom(root); 844 } else try set.iteratorFromIndex(0); 845 846 var items: std.ArrayList(PatchWithId) = .empty; 847 var next_id: ?[]const u8 = null; 848 while (try iter.next()) |cursor| { 849 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 850 const id_hex = std.fmt.bytesToHex(id, .lower); 851 if (items.items.len == page_size) { 852 next_id = try aa.dupe(u8, &id_hex); 853 break; 854 } 855 const path = try fork.forkPath(aa, repos_dir, &id_hex); 856 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 857 defer fork_repo.deinit(io, arena.child_allocator); 858 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id)) orelse continue; 859 try items.append(aa, entry); 860 } 861 return .{ 862 .items = items.items, 863 .prev_id = prev_id, 864 .next_id = next_id, 865 .count = @intCast(try set.count()), 866 }; 867 } 868 869 pub fn loadDraftEntry( 870 comptime repo_kind: rp.RepoKind, 871 comptime repo_opts: rp.RepoOpts(repo_kind), 872 arena: *std.heap.ArenaAllocator, 873 io: std.Io, 874 admin_moment: ?evt.AdminDB.HashMap(.read_only), 875 fork_repo: *rp.Repo(.xit, .{}), 876 target_repo: *rp.Repo(repo_kind, repo_opts), 877 id: [evt.event_id_size]u8, 878 ) !?PatchWithId { 879 const aa = arena.allocator(); 880 const id_hex = std.fmt.bytesToHex(id, .lower); 881 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 882 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 883 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 884 const target_branch = patch.event.target_branch; 885 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 886 const commit_count: ?u64 = blk: { 887 const newest = evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, arena) catch break :blk null; 888 break :blk if (newest) |revision| revision.record.commit_count else 0; 889 }; 890 return .{ 891 .id = try aa.dupe(u8, &id_hex), 892 .record = patch, 893 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 894 .draft = true, 895 .revision_oid = try aa.dupe(u8, &fork_oid), 896 .commit_count = commit_count, 897 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 898 .fork_exists = true, 899 }; 900 } 901 902 pub const View = thread.View(.patch, Self); 903 pub const Detail = thread.Detail(.patch, Self); 904 pub const detail_widget_name = "repo_patch_detail"; 905 906 pub const Header = thread.Header; 907 908 const open_tab_label = "open"; 909 const closed_tab_label = "closed"; 910 const merged_tab_label = "merged"; 911 const tags_tab_label = "tags"; 912 const edit_tab_label = "edit"; 913 const publish_tab_label = "publish"; 914 const merge_tab_label = "merge"; 915 const reply_tab_label = "reply"; 916 const remove_tab_label = "remove"; 917 const resolve_tab_label = "resolve"; 918 const new_tab_label = "new"; 919 const drafts_tab_label = "drafts"; 920 const conflicts_tab_label = "conflicts"; 921 922 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 923 const aa = session.page_arena.allocator(); 924 const status_kind = entry.record.event.status.kind(); 925 const target_branch = entry.record.event.target_branch; 926 927 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 928 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 929 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 930 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 931 const clone_command = if (clone_name.len == 0) 932 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 933 else 934 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 935 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 936 const choices: [3]ui.widget.CopyableText.Choice = .{ 937 .{ 938 .selector = "push", 939 .text = push_command, 940 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 941 .label = " push to this patch from existing repo ", 942 }, 943 .{ 944 .selector = "clone", 945 .text = clone_command, 946 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 947 .label = " clone this patch ", 948 }, 949 .{ 950 .selector = "merge", 951 .text = merge_command, 952 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 953 .label = " merge this patch locally ", 954 }, 955 }; 956 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 957 errdefer copyable_text.deinit(allocator); 958 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 959 }; 960 if (entry.record.event.source_branch) |branch| { 961 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 962 errdefer source.deinit(allocator); 963 source.getFocus().mode = .all; 964 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 965 } 966 const fields = [_]ui.widget.CopyableText.Choice{ 967 .{ 968 .selector = "from", 969 .text = entry.revision_oid, 970 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 971 }, 972 .{ 973 .selector = "to", 974 .text = target_branch, 975 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 976 }, 977 }; 978 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 979 errdefer copyable_text.deinit(allocator); 980 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 981 } 982 983 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 984 var name: [64]u8 = undefined; 985 var len: usize = 0; 986 var pending: ?u8 = null; 987 988 for (title) |char| { 989 if (std.ascii.isAlphanumeric(char)) { 990 const needed: usize = if (pending == null) 1 else 2; 991 if (len + needed > name.len) break; 992 if (pending) |value| { 993 name[len] = value; 994 len += 1; 995 } 996 name[len] = std.ascii.toLower(char); 997 len += 1; 998 pending = null; 999 } else if (char == ' ') { 1000 if (len != 0) pending = '-'; 1001 } else if (char == '.') { 1002 if (len != 0 and pending != '-') pending = '.'; 1003 } 1004 } 1005 if (len == 0) return ""; 1006 return allocator.dupe(u8, name[0..len]); 1007 } 1008 1009 // tabs switching between the patches page's views 1010 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1011 var header = try Header.init(allocator, session, data.search); 1012 errdefer header.deinit(allocator); 1013 const aa = session.page_arena.allocator(); 1014 const selected_index = View.viewIndex(data.view); 1015 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1016 1017 // a list tab per status, labeled with its listing's patch count 1018 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1019 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1020 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, ""), data.search); 1021 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1022 var label_buf: [64]u8 = undefined; 1023 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 1024 try header.addTab(allocator, label, link, index); 1025 } 1026 1027 // tags tab, labeled with the active tag filter 1028 { 1029 const tags_route = try thread.searchRoute(ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag), data.search); 1030 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 1031 const label = if (data.tag.len == 0) tags_tab_label else blk: { 1032 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1033 break :blk try std.fmt.allocPrint(aa, tags_tab_label ++ " ({s})", .{decoded}); 1034 }; 1035 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 1036 } 1037 1038 // new-patch tab; an edit or resolve url shows its tab in this place 1039 { 1040 const route = switch (data.view) { 1041 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1042 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1043 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1044 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1045 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1046 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1047 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1048 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1049 }; 1050 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1051 const label: []const u8 = switch (data.view) { 1052 .edit => edit_tab_label, 1053 .publish => publish_tab_label, 1054 .merge => merge_tab_label, 1055 .new_comment => reply_tab_label, 1056 .edit_comment => edit_tab_label, 1057 .remove => remove_tab_label, 1058 .resolve => resolve_tab_label, 1059 else => new_tab_label, 1060 }; 1061 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1062 } 1063 1064 // drafts belong to the logged-in user and are not available locally 1065 if (session.data.host_kind != .local) { 1066 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1067 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1068 var label_buf: [64]u8 = undefined; 1069 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1070 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1071 } 1072 1073 // conflicts tab, labeled with the conflict count 1074 if (data.conflicts.count > 0) { 1075 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1076 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1077 var label_buf: [64]u8 = undefined; 1078 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1079 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1080 } 1081 1082 header.select(View.viewIndex(data.view)); 1083 return header; 1084 } 1085