╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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