╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
844 var next_id: ?[]const u8 = null; 845 while (try iter.next()) |cursor| { 846 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 847 const id_hex = std.fmt.bytesToHex(id, .lower); 848 if (items.items.len == page_size) { 849 next_id = try aa.dupe(u8, &id_hex); 850 break; 851 } 852 const path = try fork.forkPath(aa, repos_dir, &id_hex); 853 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 854 defer fork_repo.deinit(io, arena.child_allocator); 855 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id)) orelse continue; 856 try items.append(aa, entry); 857 } 858 return .{ 859 .items = items.items, 860 .prev_id = prev_id, 861 .next_id = next_id, 862 .count = @intCast(try set.count()), 863 }; 864 } 865 866 pub fn loadDraftEntry( 867 comptime repo_kind: rp.RepoKind, 868 comptime repo_opts: rp.RepoOpts(repo_kind), 869 arena: *std.heap.ArenaAllocator, 870 io: std.Io, 871 admin_moment: ?evt.AdminDB.HashMap(.read_only), 872 fork_repo: *rp.Repo(.xit, .{}), 873 target_repo: *rp.Repo(repo_kind, repo_opts), 874 id: [evt.event_id_size]u8, 875 ) !?PatchWithId { 876 const aa = arena.allocator(); 877 const id_hex = std.fmt.bytesToHex(id, .lower); 878 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 879 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 880 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 881 const target_branch = patch.event.target_branch; 882 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 883 const commit_count: ?u64 = blk: { 884 const newest = evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, arena) catch break :blk null; 885 break :blk if (newest) |revision| revision.record.commit_count else 0; 886 }; 887 return .{ 888 .id = try aa.dupe(u8, &id_hex), 889 .record = patch, 890 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 891 .draft = true, 892 .revision_oid = try aa.dupe(u8, &fork_oid), 893 .commit_count = commit_count, 894 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 895 .fork_exists = true, 896 }; 897 } 898 899 pub const View = thread.View(.patch, Self); 900 pub const Detail = thread.Detail(.patch, Self); 901 pub const detail_widget_name = "repo_patch_detail"; 902 903 pub const Header = thread.Header; 904 905 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 906 const aa = session.page_arena.allocator(); 907 const status_kind = entry.record.event.status.kind(); 908 const target_branch = entry.record.event.target_branch; 909 910 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 911 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 912 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 913 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 914 const clone_command = if (clone_name.len == 0) 915 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 916 else 917 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 918 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 919 const choices: [3]ui.widget.CopyableText.Choice = .{ 920 .{ 921 .selector = "push", 922 .text = push_command, 923 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 924 .label = " push to this patch from existing repo ", 925 }, 926 .{ 927 .selector = "clone", 928 .text = clone_command, 929 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 930 .label = " clone this patch ", 931 }, 932 .{ 933 .selector = "merge", 934 .text = merge_command, 935 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 936 .label = " merge this patch locally ", 937 }, 938 }; 939 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 940 errdefer copyable_text.deinit(allocator); 941 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 942 }; 943 if (entry.record.event.source_branch) |branch| { 944 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 945 errdefer source.deinit(allocator); 946 source.getFocus().mode = .all; 947 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 948 } 949 const fields = [_]ui.widget.CopyableText.Choice{ 950 .{ 951 .selector = "from", 952 .text = entry.revision_oid, 953 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 954 }, 955 .{ 956 .selector = "to", 957 .text = target_branch, 958 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 959 }, 960 }; 961 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 962 errdefer copyable_text.deinit(allocator); 963 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 964 } 965 966 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 967 var name: [64]u8 = undefined; 968 var len: usize = 0; 969 var pending: ?u8 = null; 970 971 for (title) |char| { 972 if (std.ascii.isAlphanumeric(char)) { 973 const needed: usize = if (pending == null) 1 else 2; 974 if (len + needed > name.len) break; 975 if (pending) |value| { 976 name[len] = value; 977 len += 1; 978 } 979 name[len] = std.ascii.toLower(char); 980 len += 1; 981 pending = null; 982 } else if (char == ' ') { 983 if (len != 0) pending = '-'; 984 } else if (char == '.') { 985 if (len != 0 and pending != '-') pending = '.'; 986 } 987 } 988 if (len == 0) return ""; 989 return allocator.dupe(u8, name[0..len]); 990 } 991 992 // tabs switching between the patches page's views 993 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 994 var header = try Header.init(allocator); 995 errdefer header.deinit(allocator); 996 const aa = session.page_arena.allocator(); 997 const selected_index = View.viewIndex(data.view); 998 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 999 1000 // a list tab per status, labeled with its listing's patch count 1001 for ([_]Status{ .open, .closed, .merged }, 0..) |status, index| { 1002 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1003 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1004 var label_buf: [64]u8 = undefined; 1005 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1006 try header.addTab(allocator, label, link, index); 1007 } 1008 1009 // tags tab, labeled with the active tag filter 1010 { 1011 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 1012 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 1013 const label = if (data.tag.len == 0) "tags" else blk: { 1014 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1015 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1016 }; 1017 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 1018 } 1019 1020 // new-patch tab; an edit or resolve url shows its tab in this place 1021 { 1022 const route = switch (data.view) { 1023 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1024 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1025 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1026 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1027 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1028 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1029 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1030 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1031 }; 1032 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1033 const label: []const u8 = switch (data.view) { 1034 .edit => "edit", 1035 .publish => "publish", 1036 .merge => "merge", 1037 .new_comment => "reply", 1038 .edit_comment => "edit", 1039 .remove => "remove", 1040 .resolve => "resolve", 1041 else => "new", 1042 }; 1043 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1044 } 1045 1046 // drafts belong to the logged-in user and are not available locally 1047 if (session.data.host_kind != .local) { 1048 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1049 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1050 var label_buf: [64]u8 = undefined; 1051 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 1052 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1053 } 1054 1055 // conflicts tab, labeled with the conflict count 1056 if (data.conflicts.count > 0) { 1057 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1058 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1059 var label_buf: [64]u8 = undefined; 1060 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 1061 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1062 } 1063 1064 header.select(View.viewIndex(data.view)); 1065 return header; 1066 } 1067