╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
679 .count = @intCast(try set.count()), 680 }; 681 } 682 683 pub fn loadDraftEntry( 684 comptime repo_kind: rp.RepoKind, 685 comptime repo_opts: rp.RepoOpts(repo_kind), 686 arena: *std.heap.ArenaAllocator, 687 io: std.Io, 688 admin_moment: ?evt.AdminDB.HashMap(.read_only), 689 fork_repo: *rp.Repo(.xit, .{}), 690 target_repo: *rp.Repo(repo_kind, repo_opts), 691 id: [evt.event_id_size]u8, 692 ) !?PatchWithId { 693 const aa = arena.allocator(); 694 const id_hex = std.fmt.bytesToHex(id, .lower); 695 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 696 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 697 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 698 const target_branch = patch.event.target_branch; 699 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 700 return .{ 701 .id = try aa.dupe(u8, &id_hex), 702 .record = patch, 703 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 704 .draft = true, 705 .fork_oid = try aa.dupe(u8, &fork_oid), 706 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 707 .fork_exists = true, 708 }; 709 } 710 711 pub const View = thread.View(.patch, Self); 712 pub const Detail = thread.Detail(.patch, Self); 713 pub const detail_widget_name = "repo_patch_detail"; 714 715 pub const Header = thread.Header; 716 717 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 718 const aa = session.page_arena.allocator(); 719 const status_kind = entry.record.event.status.kind(); 720 const target_branch = entry.record.event.target_branch; 721 var fields: [2]ui.widget.CopyableText.Choice = undefined; 722 var field_count: usize = 0; 723 724 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 725 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 726 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 727 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 728 const clone_command = if (clone_name.len == 0) 729 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 730 else 731 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 732 const choices: [2]ui.widget.CopyableText.Choice = .{ 733 .{ 734 .selector = "push", 735 .text = push_command, 736 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 737 .label = " push to this patch from existing repo ", 738 }, 739 .{ 740 .selector = "clone", 741 .text = clone_command, 742 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 743 .label = " clone this patch ", 744 }, 745 }; 746 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 747 errdefer copyable_text.deinit(allocator); 748 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 749 }; 750 if (entry.fork_oid.len != 0) { 751 fields[field_count] = .{ 752 .selector = "from", 753 .text = entry.fork_oid, 754 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 755 }; 756 field_count += 1; 757 } 758 fields[field_count] = .{ 759 .selector = "to", 760 .text = target_branch, 761 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 762 }; 763 field_count += 1; 764 765 if (field_count > 0) { 766 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[0..field_count]); 767 errdefer copyable_text.deinit(allocator); 768 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 769 } 770 } 771 772 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 773 var name: [64]u8 = undefined; 774 var len: usize = 0; 775 var pending: ?u8 = null; 776 777 for (title) |char| { 778 if (std.ascii.isAlphanumeric(char)) { 779 const needed: usize = if (pending == null) 1 else 2; 780 if (len + needed > name.len) break; 781 if (pending) |value| { 782 name[len] = value; 783 len += 1; 784 } 785 name[len] = std.ascii.toLower(char); 786 len += 1; 787 pending = null; 788 } else if (char == ' ') { 789 if (len != 0) pending = '-'; 790 } else if (char == '.') { 791 if (len != 0 and pending != '-') pending = '.'; 792 } 793 } 794 if (len == 0) return ""; 795 return allocator.dupe(u8, name[0..len]); 796 } 797 798 // tabs switching between the patches page's views 799 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 800 var header = try Header.init(allocator); 801 errdefer header.deinit(allocator); 802 const aa = session.page_arena.allocator(); 803 const selected_index = View.viewIndex(data.view); 804 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 805 806 // a list tab per status, labeled with its listing's patch count 807 for ([_]Status{ .open, .closed, .merged }, 0..) |status, index| { 808 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 809 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 810 var label_buf: [64]u8 = undefined; 811 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 812 try header.addTab(allocator, label, link, index); 813 } 814 815 // tags tab, labeled with the active tag filter 816 { 817 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 818 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 819 const label = if (data.tag.len == 0) "tags" else blk: { 820 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 821 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 822 }; 823 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 824 } 825 826 // new-patch tab; an edit or resolve url shows its tab in this place 827 if (session.local == null) { 828 const route = switch (data.view) { 829 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 830 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 831 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 832 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 833 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 834 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 835 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 836 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 837 }; 838 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 839 const label: []const u8 = switch (data.view) { 840 .edit => "edit", 841 .publish => "publish", 842 .merge => "merge", 843 .new_comment => "reply", 844 .edit_comment => "edit", 845 .remove => "remove", 846 .resolve => "resolve", 847 else => "new", 848 }; 849 try header.addTab(allocator, label, link, View.viewIndex(.new)); 850 } 851 852 // drafts belong to the logged-in user and are not available locally 853 if (session.local == null) { 854 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 855 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 856 var label_buf: [64]u8 = undefined; 857 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 858 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 859 } 860 861 // conflicts tab, labeled with the conflict count 862 if (data.conflicts.count > 0) { 863 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 864 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 865 var label_buf: [64]u8 = undefined; 866 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 867 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 868 } 869 870 header.select(View.viewIndex(data.view)); 871 return header; 872 } 873