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