╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1760 return null; 1761 } 1762 if (!params.only(&.{.start})) return null; 1763 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1764 } 1765 } 1766 const word = params.scanTail(&segments) catch return null; 1767 if (word) |tail| { 1768 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1769 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1770 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{.attachment})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "", params.values.get(.attachment) orelse "") else null; 1771 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1772 return null; 1773 } 1774 if (!params.only(&.{ .start, .search })) return null; 1775 return withEncodedSearch(repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null), params.values.get(.search) orelse ""); 1776 } 1777 if (std.mem.eql(u8, tab, "issues")) { 1778 // the word is a list view; a filter with neither is never emitted. 1779 const word = params.scanTail(&segments) catch return null; 1780 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "") else null; 1781 // the conflicts list windows by an issue id, not a number, so it 1782 // reads the start param raw 1783 if (std.mem.eql(u8, w, "conflicts")) { 1784 if (!params.only(&.{.start})) return null; 1785 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1786 } 1787 if (!params.only(&.{.search})) return null; 1788 const search_value = params.values.get(.search) orelse ""; 1789 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return withEncodedSearch(repoIssuesRoute(pair, status, ""), search_value); 1790 if (std.mem.eql(u8, w, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.issue, pair), search_value); 1791 if (std.mem.eql(u8, w, "new")) return if (search_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1792 return null; 1793 } 1794 if (std.mem.eql(u8, tab, "patches")) { 1795 const word = params.scanTail(&segments) catch return null; 1796 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "") else null; 1797 if (std.mem.eql(u8, view, "conflicts")) { 1798 if (!params.only(&.{.start})) return null; 1799 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1800 } 1801 if (!params.only(&.{.search})) return null; 1802 const search_value = params.values.get(.search) orelse ""; 1803 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return withEncodedSearch(repoPatchesRoute(pair, status, ""), search_value); 1804 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.patch, pair), search_value); 1805 if (std.mem.eql(u8, view, "new")) return if (search_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1806 if (std.mem.eql(u8, view, "drafts")) return if (search_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1807 return null; 1808 } 1809 if (std.mem.eql(u8, tab, "discussions")) { 1810 const word = params.scanTail(&segments) catch return null; 1811 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "") else null; 1812 if (!params.only(&.{.search})) return null; 1813 const search_value = params.values.get(.search) orelse ""; 1814 if (std.mem.eql(u8, view, "recent")) return withEncodedSearch(repoDiscussionsRoute(pair, ""), search_value); 1815 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.discuss, pair), search_value); 1816 if (std.mem.eql(u8, view, "new")) return if (search_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1817 return null; 1818 } 1819 if (std.mem.eql(u8, tab, "undo")) { 1820 const word = segments.next(); 1821 if (segments.next() != null) return null; 1822 const value = word orelse return repoUndoRoute(pair, null); 1823 return repoUndoRoute(pair, std.fmt.parseInt(u64, value, 10) catch return null); 1824 } 1825 if (std.mem.eql(u8, tab, "events")) { 1826 const word = params.scanTail(&segments) catch return null; 1827 if (!params.only(&.{.moment})) return null; 1828 const view = if (word) |value| std.meta.stringToEnum(EventsView, value) orelse return null else .active; 1829 return repoEventsRoute(pair, view, null, "", params.moment() catch return null); 1830 } 1831 if (std.mem.startsWith(u8, tab, "event:")) { 1832 const event_id = tab["event:".len..]; 1833 const kind_segment = segments.next() orelse return null; 1834 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1835 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1836 if (event_id.len == 0) return null; 1837 params.scanPairs(&segments) catch return null; 1838 if (!params.only(&.{.moment}) or segments.next() != null) return null; 1839 return repoEventsRoute(pair, .active, kind, event_id, params.moment() catch return null); 1840 } 1841 if (std.mem.eql(u8, tab, files_seg)) { 1842 params.scanPairs(&segments) catch return null; 1843 if (params.values.get(.patchrev)) |id| { 1844 if (!params.only(&.{ .line, .patchrev })) return null; 1845 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1846 } 1847 if (!params.only(&.{ .line, .branch, .tag, .object, .find })) return null; 1848 const line = params.line() orelse return null; 1849 const dir = pathValue(segments.rest()) orelse return null; 1850 const find = params.values.get(.find) orelse ""; 1851 const ref = params.ref() catch return null; 1852 // a find route's `path` names the selected result, so unlike a 1853 // directory path it doesn't need a ref beside it 1854 var route = if (ref) |r| 1855 repoFilesRoute(pair, r.kind, r.value, dir, line) orelse return null 1856 else if (dir.len == 0 or find.len != 0) 1857 repoFilesRoute(pair, null, "", "", line) orelse return null 1858 else 1859 return null; 1860 if (find.len != 0) { 1861 route.repo_files.find = Array(find_route_max_len).from(find) orelse return null; 1862 route.repo_files.path = Array(repo_route_max_len).from(dir) orelse return null; 1863 } 1864 return route; 1865 } 1866 if (std.mem.eql(u8, tab, commits_seg)) { 1867 params.scanPairs(&segments) catch return null; 1868 if (params.values.get(.merge)) |oid| { 1869 if (!params.only(&.{.merge}) or segments.next() != null) return null; 1870 return repoCommitsMergeRoute(pair, oid); 1871 } 1872 if (!params.only(&.{ .branch, .tag, .object, .base, .search, .from })) return null; 1873 const base_oid = params.values.get(.base) orelse ""; 1874 const search = params.values.get(.search) orelse ""; 1875 const from = params.values.get(.from) orelse ""; 1876 const ref = (params.ref() catch return null) orelse { 1877 if (search.len != 0 or from.len != 0 or segments.rest().len != 0) return null; 1878 return repoCommitsRoute(pair, null, "", base_oid); 1879 }; 1880 // the index covers branch and tag tips, so only they can be 1881 // searched or paged by commit; an object roots the list itself 1882 if ((search.len != 0 or from.len != 0) and ref.kind == .object) return null; 1883 var route = if (std.mem.eql(u8, segments.rest(), message_seg)) 1884 repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1885 else if (segments.rest().len == 0) 1886 repoCommitsRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1887 else 1888 return null; 1889 if (search.len != 0) route.repo_commits.search = Array(search_route_max_len).from(search) orelse return null; 1890 if (from.len != 0) route.repo_commits.from = Array(ref_route_max_len).from(from) orelse return null; 1891 return route; 1892 } 1893 if (std.mem.eql(u8, tab, "diff")) { 1894 params.scanPairs(&segments) catch return null; 1895 if (params.values.get(.patchrev)) |id| { 1896 if (!params.only(&.{ .start, .patchrev })) return null; 1897 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1898 } 1899 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1900 const base_oid = params.values.get(.base) orelse return null; 1901 const start = params.start() orelse return null; 1902 const ref = params.ref() catch return null; 1903 const file = pathValue(segments.rest()) orelse return null; 1904 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1905 } 1906 return null; // unknown sub-path 1907 } 1908 1909 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1910 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1911 return switch (a) { 1912 .home_users => |a_u| a_u.start == b.home_users.start and std.mem.eql(u8, a_u.search.slice(), b.home_users.search.slice()), 1913 .home_user => |a_t| a_t.eql(b.home_user), 1914 .user_repos => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_repos.name.slice()) and a_u.start == b.user_repos.start and std.mem.eql(u8, a_u.search.slice(), b.user_repos.search.slice()), 1915 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1916 .user_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_repo_new.slice()), 1917 .user_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_user_new.slice()), 1918 .user_user => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_user.name.slice()) and a_u.tab.eql(b.user_user.tab), 1919 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1920 a_f.ref_kind == b.repo_files.ref_kind and 1921 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1922 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1923 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1924 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1925 a_f.line == b.repo_files.line, 1926 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1927 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1928 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1929 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1930 std.mem.eql(u8, a_c.search.slice(), b.repo_commits.search.slice()) and 1931 std.mem.eql(u8, a_c.from.slice(), b.repo_commits.from.slice()) and 1932 std.mem.eql(u8, a_c.merge.slice(), b.repo_commits.merge.slice()) and 1933 a_c.message == b.repo_commits.message, 1934 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1935 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1936 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1937 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1938 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1939 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1940 .repo_refs => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_refs.name.slice()) and a_r.kind == b.repo_refs.kind and std.mem.eql(u8, a_r.from.slice(), b.repo_refs.from.slice()) and std.mem.eql(u8, a_r.search.slice(), b.repo_refs.search.slice()), 1941 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1942 std.mem.eql(u8, a_i.search.slice(), b.repo_issues.search.slice()) and 1943 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1944 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1945 a_i.view == b.repo_issues.view and 1946 a_i.comments_start == b.repo_issues.comments_start and 1947 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1948 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1949 std.mem.eql(u8, a_t.search.slice(), b.repo_discussions.search.slice()) and 1950 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1951 a_t.view == b.repo_discussions.view and 1952 a_t.comments_start == b.repo_discussions.comments_start and 1953 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1954 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1955 std.mem.eql(u8, a_p.search.slice(), b.repo_patches.search.slice()) and 1956 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1957 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1958 a_p.view == b.repo_patches.view and 1959 a_p.comments_start == b.repo_patches.comments_start and 1960 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1961 .repo_undo => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_undo.name.slice()) and 1962 a_u.index == b.repo_undo.index, 1963 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1964 a_e.view == b.repo_events.view and 1965 a_e.moment == b.repo_events.moment and 1966 a_e.kind == b.repo_events.kind and 1967 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1968 .repo_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_repo_new.slice()), 1969 .repo_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_user_new.slice()), 1970 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1971 .repo_members => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_members.name.slice()) and 1972 a_r.start == b.repo_members.start and 1973 std.mem.eql(u8, a_r.user.slice(), b.repo_members.user.slice()) and 1974 a_r.confirm == b.repo_members.confirm, 1975 .repo_branches => |a_b| std.mem.eql(u8, a_b.name.slice(), b.repo_branches.name.slice()) and 1976 std.mem.eql(u8, a_b.branch.slice(), b.repo_branches.branch.slice()), 1977 .repo_delete => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_delete.slice()), 1978 .repo_user => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_user.name.slice()) and a_u.tab.eql(b.repo_user.tab), 1979 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1980 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1981 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1982 std.mem.eql(u8, a_d.oid.slice(), b.fork_diff.oid.slice()) and 1983 std.mem.eql(u8, a_d.base_oid.slice(), b.fork_diff.base_oid.slice()) and 1984 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1985 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1986 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1987 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1988 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1989 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1990 a_f.line == b.fork_files.line, 1991 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1992 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1993 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1994 a_c.message == b.fork_commits.message, 1995 .fork_delete => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_delete.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_delete.id.slice()), 1996 .fork_repo_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_repo_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_repo_new.id.slice()), 1997 .fork_user_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_user_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_user_new.id.slice()), 1998 .fork_user => |a_u| std.mem.eql(u8, a_u.fork.name.slice(), b.fork_user.fork.name.slice()) and 1999 std.mem.eql(u8, a_u.fork.id.slice(), b.fork_user.fork.id.slice()) and 2000 a_u.tab.eql(b.fork_user.tab), 2001 else => true, 2002 }; 2003 } 2004 2005 // true when following a link from `b` to `a` (both repo routes) should reload 2006 // rather than stay in page 2007 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 2008 if (a.parent() != .repo or b.parent() != .repo) return false; 2009 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 2010 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 2011 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 2012 return !a.eql(b); 2013 } 2014 2015 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 2016 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 2017 } 2018 2019 // true when `a` and `b` are different users' pages, different tabs of one 2020 // user's page, or the same list paginated to a different window or search. 2021 // the header's tab switches are in-page `ai:` links, which never ask this. 2022 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 2023 if (a.userPageName()) |a_name| if (b.userPageName()) |b_name| { 2024 if (!std.mem.eql(u8, a_name, b_name) or std.meta.activeTag(a) != std.meta.activeTag(b)) return true; 2025 }; 2026 return switch (a) { 2027 .user_repos => |aa| switch (b) { 2028 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 2029 else => false, 2030 }, 2031 .user_forks => |aa| switch (b) { 2032 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 2033 else => false, 2034 }, 2035 .user_user => |aa| switch (b) { 2036 .user_user => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 2037 else => false, 2038 }, 2039 else => false, 2040 }; 2041 } 2042 2043 // the user a user page belongs to, or null for any other page 2044 fn userPageName(self: *const RoutablePage) ?[]const u8 { 2045 return switch (self.*) { 2046 .user_repos => |*page| page.name.slice(), 2047 .user_forks => |*page| page.name.slice(), 2048 .user_user => |*page| page.name.slice(), 2049 .user_repo_new, .user_user_new => |*name| name.slice(), 2050 else => null, 2051 }; 2052 } 2053 2054 // true when `a` and `b` are the same home list tab paginated to a different 2055 // window or search. switching tabs is in-page, so only a changed `start` or 2056 // search on the same tab navigates. 2057 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 2058 return switch (a) { 2059 .home_users => switch (b) { 2060 .home_users => !a.eql(b), 2061 else => false, 2062 }, 2063 .home_user => switch (b) { 2064 .home_user => !a.eql(b), 2065 else => false, 2066 }, 2067 else => false, 2068 }; 2069 } 2070 }; 2071 2072 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 2073 fn isUnreserved(c: u8) bool { 2074 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 2075 } 2076 2077 // percent-encode a ref name for use as a single url path segment. 2078 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 2079 var out: std.Io.Writer.Allocating = .init(aa); 2080 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 2081 return out.written(); 2082 } 2083 2084 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 2085 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2086 return struct { 2087 pub const hex_len = hash.hexLen(repo_opts.hash); 2088 2089 // the concrete ref/oid (a null request resolves to the default branch) 2090 ref_or_oid: RoutablePage.RefOrOid, 2091 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 2092 // the route layer — which splits on '/' — stores them encoded). duped 2093 // into `aa`. 2094 value: []const u8, 2095 // the commit oid it points at 2096 oid: [hex_len]u8, 2097 2098 const Self = @This(); 2099 2100 // resolve a requested ref/oid (null = the repo's default branch) to a 2101 // concrete ref/oid plus the commit oid it points at. `requested_value` is 2102 // the url-encoded form (as it appears in the route). null when it doesn't 2103 // resolve (an unknown branch/tag, or a malformed/unknown oid). 2104 pub fn init( 2105 repo: *rp.Repo(repo_kind, repo_opts), 2106 io: std.Io, 2107 aa: std.mem.Allocator, 2108 requested_ref_or_oid: ?RoutablePage.RefOrOid, 2109 requested_value: []const u8, 2110 ) !?Self { 2111 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 2112 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 2113 var value: []const u8 = if (requested_ref_or_oid == null) 2114 requested_value 2115 else 2116 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 2117 // no ref named: fall back to HEAD's branch (or its oid when detached). 2118 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 2119 if (requested_ref_or_oid == null) { 2120 if (repo.head(io, &head_buf)) |head| switch (head) { 2121 .ref => |r| { 2122 ref_or_oid = .branch; 2123 value = r.name; 2124 }, 2125 .oid => |o| { 2126 ref_or_oid = .object; 2127 value = o; 2128 }, 2129 } else |_| {} 2130 } 2131 2132 var oid: [hex_len]u8 = undefined; 2133 switch (ref_or_oid) { 2134 .object => { 2135 if (value.len != hex_len) return null; 2136 @memcpy(&oid, value); 2137 }, 2138 .branch, .tag => { 2139 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 2140 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 2141 }, 2142 } 2143 // store url-encoded so the route layer can hold the value verbatim. 2144 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 2145 } 2146 }; 2147 } 2148 2149 // who a repo page is built for, which picks the actions it shows 2150 pub const Viewer = struct { 2151 role: evt.Repo.Role, 2152 email: []const u8, 2153 2154 // the session's viewer of a repo it holds `role` in, or null when logged 2155 // out. local mode owns everything. 2156 pub fn init(session: *Session, arena: *std.heap.ArenaAllocator, role: evt.Repo.Role) !?Viewer { 2157 if (session.data.host_kind == .local) return .{ .role = .owner, .email = "" }; 2158 const user_id = session.userId() orelse return null; 2159 const moment = session.haxy_moment orelse unreachable; 2160 const user = (try activeUser(moment, arena, user_id)) orelse return null; 2161 return .{ .role = role, .email = user.event.email }; 2162 } 2163 }; 2164 2165 // a repo a page shows, and what its viewer may do there 2166 pub const RepoHandle = struct { 2167 location: RoutablePage.RepoLocation, 2168 viewer: ?Viewer = null, 2169 // only a public repo can be cloned over http 2170 read_access: evt.Repo.Access = .private, 2171 2172 pub fn dupe(self: RepoHandle, allocator: std.mem.Allocator) !RepoHandle { 2173 var copy = self; 2174 copy.location = try self.location.dupe(allocator); 2175 return copy; 2176 } 2177 2178 // pushing, and seeing the events and undo tabs. local mode is always owner. 2179 pub fn canWrite(self: RepoHandle) bool { 2180 const viewer = self.viewer orelse return false; 2181 return viewer.role.atLeast(.write); 2182 } 2183 2184 pub fn canUndo(self: RepoHandle) bool { 2185 const viewer = self.viewer orelse return false; 2186 return viewer.role == .owner; 2187 } 2188 }; 2189 2190 // where a repo page reads its on-disk repo from 2191 pub const RepoSource = struct { 2192 path: []const u8, 2193 repo_kind: rp.RepoKind, 2194 // the user's global git config, so local mode picks up their identity and 2195 // their signing and ssh settings. null on the server paths, which must not 2196 // read whoever runs the server. 2197 global_config_path: ?[]const u8 = null, 2198 2199 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 2200 return .{ .path = self.path, .global_config_path = self.global_config_path }; 2201 } 2202 2203 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 2204 if (!xit.ref.validateName(branch)) return false; 2205 return switch (self.repo_kind) { 2206 inline else => |repo_kind| blk: { 2207 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2208 defer any_repo.deinit(io, allocator); 2209 break :blk switch (any_repo) { 2210 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 2211 }; 2212 }, 2213 }; 2214 } 2215 2216 // whether the actor may change the event `id` names 2217 pub fn canModify( 2218 self: RepoSource, 2219 io: std.Io, 2220 allocator: std.mem.Allocator, 2221 actor: Actor, 2222 kind: evt.EventKind, 2223 id: *const [evt.event_id_size]u8, 2224 ) !bool { 2225 // a role that may change an event with no author needs no read 2226 if (actor.role.canModify(actor.author.email, null)) return true; 2227 2228 var arena = std.heap.ArenaAllocator.init(allocator); 2229 defer arena.deinit(); 2230 return switch (self.repo_kind) { 2231 inline else => |repo_kind| blk: { 2232 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2233 defer any_repo.deinit(io, allocator); 2234 break :blk switch (any_repo) { 2235 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 2236 }; 2237 }, 2238 }; 2239 } 2240 }; 2241 2242 // who a write acts as and the role they hold in its repo 2243 // the thread button that changes the viewer's notifications, named by its label 2244 pub const NotifyAction = enum { subscribe, mute }; 2245 2246 pub const Actor = struct { 2247 author: evt.CommitAuthor, 2248 // local mode has no accounts, so it has no user id 2249 user_id: ?[evt.event_id_size]u8 = null, 2250 role: evt.Repo.Role, 2251 // the repo owner's id, which local mode lacks 2252 repo_user_id: ?[evt.event_id_size]u8 = null, 2253 }; 2254 2255 pub const Authorization = union(enum) { 2256 actor: Actor, 2257 login_required, 2258 repo_not_found, 2259 forbidden, 2260 }; 2261 2262 // the account `user_id` names, or null once it has been removed. a user id is 2263 // checked here wherever one enters, so the role lookups can trust theirs. 2264 pub fn activeUser( 2265 moment: evt.AdminDB.HashMap(.read_only), 2266 arena: *std.heap.ArenaAllocator, 2267 user_id: [evt.event_id_size]u8, 2268 ) !?evt.User.Record { 2269 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 2270 return if (user.removed) null else user; 2271 } 2272 2273 // what the typed assignee resolves to 2274 pub const AssigneeLookup = union(enum) { 2275 email: []const u8, 2276 failure: Session.FormFeedback.AssignmentFailure, 2277 }; 2278 2279 // the email `text` assigns: itself when it holds an @, else the email of the 2280 // user it names. a null moment is local mode, which has no users to look up. 2281 pub fn assigneeEmail(moment_maybe: ?evt.AdminDB.HashMap(.read_only), arena: *std.heap.ArenaAllocator, text: []const u8) !AssigneeLookup { 2282 const trimmed = std.mem.trim(u8, text, " \t\r\n"); 2283 if (std.mem.indexOfScalar(u8, trimmed, '@') != null) return .{ .email = trimmed }; 2284 const moment = moment_maybe orelse return .{ .failure = .not_an_email }; 2285 const user_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, trimmed)) orelse return .{ .failure = .not_found }; 2286 const user = (try activeUser(moment, arena, user_id)) orelse return .{ .failure = .not_found }; 2287 return .{ .email = user.event.email }; 2288 } 2289 2290 // whether the user may write to the repo `identity` names with at least 2291 // `min_role`, and to the `thread_kind` tab when there is one 2292 pub fn authorizeUser( 2293 io: std.Io, 2294 moment: evt.AdminDB.HashMap(.read_only), 2295 arena: *std.heap.ArenaAllocator, 2296 users_dir: []const u8, 2297 user_id: [evt.event_id_size]u8, 2298 identity: []const u8, 2299 min_role: evt.Repo.Role, 2300 thread_kind: ?evt.EventKind, 2301 ) !Authorization { 2302 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 2303 2304 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 2305 const repo = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, moment, users_dir, owner_repo.owner, owner_repo.name, user_id)) orelse return .repo_not_found; 2306 // a repo the user can't read doesn't exist to them 2307 const role = repo.role orelse return .repo_not_found; 2308 if (!role.atLeast(min_role)) return .forbidden; 2309 if (thread_kind) |kind| { 2310 // a tab that is off doesn't exist 2311 const tab_role = repo.repo.event.threadRole(kind) orelse return .repo_not_found; 2312 if (!role.atLeast(tab_role)) return .forbidden; 2313 } 2314 return .{ .actor = .{ 2315 .author = .{ .name = user.event.name, .email = user.event.email }, 2316 .user_id = user_id, 2317 .role = role, 2318 .repo_user_id = try evt.parseEventId(&repo.repo.event.user_id), 2319 } }; 2320 } 2321 2322 // events created in local mode have no account behind them, so the commit 2323 // author comes from the repo's config, which the global config feeds into. 2324 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 2325 2326 pub fn localAuthor( 2327 src: RepoSource, 2328 io: std.Io, 2329 allocator: std.mem.Allocator, 2330 arena: *std.heap.ArenaAllocator, 2331 ) !evt.CommitAuthor { 2332 switch (src.repo_kind) { 2333 inline else => |repo_kind| { 2334 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 2335 defer any_repo.deinit(io, allocator); 2336 2337 switch (any_repo) { 2338 inline else => |*repo| { 2339 var config = try repo.listConfig(io, allocator); 2340 defer config.deinit(); 2341 2342 const user = config.sections.get("user") orelse return local_author_fallback; 2343 const name = user.get("name") orelse return local_author_fallback; 2344 const email = user.get("email") orelse return local_author_fallback; 2345 return .{ 2346 .name = try arena.allocator().dupe(u8, name), 2347 .email = try arena.allocator().dupe(u8, email), 2348 }; 2349 }, 2350 } 2351 }, 2352 } 2353 } 2354 2355 // per-connection mutable state. each SSH session / web session / local TUI 2356 // run gets its own. 2357 pub const Session = struct { 2358 data: Data = .{}, 2359 // session-lifetime allocations: login/user_id, persisted prefs, queued 2360 // actions. lives as long as the connection. 2361 arena: *std.heap.ArenaAllocator, 2362 // current page's allocations: page data and the page-scoped strings widgets 2363 // build (form actions, links). owned by Nav, swapped on each navigation, so 2364 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 2365 // where a render is one-shot, this points at the same arena as `arena`. 2366 page_arena: *std.heap.ArenaAllocator, 2367 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 2368 // filesystem io and the path to <server>/repos, for opening on-disk repos 2369 // during page construction. both null on wasm, which has no filesystem and 2370 // rebuilds pages from the serialized snapshot rather than from disk. 2371 io: ?std.Io = null, 2372 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 2373 users_dir: ?[]const u8 = null, 2374 // the single on-disk repo this session views, when running in local mode 2375 // (haxy invoked with no arguments inside a repo). null on the server paths, 2376 // which resolve repos from the admin db instead. 2377 local: ?RepoSource = null, 2378 // focus id -> the live TextInput, refreshed each frame by the views that own 2379 // inputs. web/wasm form handling looks widgets up here by focus id. 2380 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2381 back: enum { unavailable, available, requested } = .unavailable, 2382 refresh_requested: bool = false, // set by input (ctrl+r) 2383 // a requested forward navigation. set this (via navigate) to move to a new 2384 // page; Nav.sync builds it and then copies it into current_page, clearing 2385 // this back to null. setting current_page directly only updates the url and 2386 // does not navigate. 2387 next_page: ?RoutablePage = null, 2388 is_terminal: bool = false, // true on remote SSH and local TUI 2389 color: bool = true, // false when NO_COLOR is set, which also hides the art 2390 // the terminal's reported default background, which the art blends toward 2391 terminal_background: ?Grid.Color.Rgb = null, 2392 // the web UI's url, for the TUI/SSH footer. null on the web itself (no 2393 // footer there). 2394 web_url: ?[]const u8 = null, 2395 // the authorized_keys line of the key an ssh session connected with 2396 ssh_key: ?[]const u8 = null, 2397 // a host operation requested by a widget 2398 host_request: ?HostRequest = null, 2399 quit_requested: bool = false, 2400 2401 pub const HostRequest = union(enum) { 2402 show_copyable_text: []const u8, 2403 sync_events, 2404 undo: RoutablePage.RepoUndoRoute, 2405 }; 2406 2407 pub const FormFeedback = union(enum) { 2408 pub const LoginFailure = enum { unknown_user, wrong_password }; 2409 pub const ThreadFailure = enum { required_title }; 2410 pub const PatchFailure = enum { 2411 required_title, 2412 invalid_target_branch, 2413 invalid_source_branch, 2414 same_branch, 2415 unrelated_branches, 2416 2417 pub fn fromError(err: anyerror) ?PatchFailure { 2418 return switch (err) { 2419 error.InvalidTargetBranch => .invalid_target_branch, 2420 error.InvalidSourceBranch => .invalid_source_branch, 2421 error.SameBranch => .same_branch, 2422 error.UnrelatedBranches => .unrelated_branches, 2423 else => null, 2424 }; 2425 } 2426 }; 2427 2428 pub const RepoFailure = enum { 2429 required_name, 2430 invalid_name, 2431 name_taken, 2432 2433 pub fn fromError(err: anyerror) ?RepoFailure { 2434 return switch (err) { 2435 error.NameEmpty => .required_name, 2436 error.NameTooLong, error.InvalidName => .invalid_name, 2437 error.NameTaken => .name_taken, 2438 else => null, 2439 }; 2440 } 2441 }; 2442 2443 pub const MembersFailure = enum { not_found, already_added }; 2444 2445 pub const BranchesFailure = enum { 2446 not_found, 2447 already_added, 2448 too_many, 2449 too_long, 2450 2451 pub fn fromError(err: anyerror) ?BranchesFailure { 2452 return switch (err) { 2453 error.BranchProtected => .already_added, 2454 error.TooManyProtectedBranches => .too_many, 2455 else => null, 2456 }; 2457 } 2458 }; 2459 2460 pub const AssignmentFailure = enum { not_found, not_an_email, already_assigned }; 2461 2462 pub const DeleteFailure = enum { wrong_confirm, wrong_password }; 2463 2464 pub const KeyFailure = enum { 2465 invalid, 2466 unsupported, 2467 already_added, 2468 too_many, 2469 2470 pub fn fromError(err: anyerror) ?KeyFailure { 2471 return switch (err) { 2472 error.InvalidKey => .invalid, 2473 error.UnsupportedKey => .unsupported, 2474 error.KeyAdded => .already_added, 2475 error.TooManyKeys => .too_many, 2476 else => null, 2477 }; 2478 } 2479 }; 2480 2481 pub const UserFailure = enum { 2482 required_name, 2483 invalid_name, 2484 name_taken, 2485 required_email, 2486 email_taken, 2487 required_password, 2488 password_mismatch, 2489 wrong_password, 2490 2491 pub fn fromError(err: anyerror) ?UserFailure { 2492 return switch (err) { 2493 error.NameEmpty => .required_name, 2494 error.NameTooShort, error.NameTooLong, error.InvalidName => .invalid_name, 2495 error.NameTaken => .name_taken, 2496 error.EmailEmpty => .required_email, 2497 error.EmailTaken => .email_taken, 2498 error.PasswordEmpty => .required_password, 2499 error.PasswordMismatch => .password_mismatch, 2500 error.WrongPassword => .wrong_password, 2501 else => null, 2502 }; 2503 } 2504 }; 2505 2506 login: struct { 2507 failure: LoginFailure, 2508 username: []const u8, 2509 }, 2510 user: struct { 2511 failure: UserFailure, 2512 // passwords are never retained 2513 fields: struct { 2514 name: []const u8, 2515 email: []const u8, 2516 }, 2517 }, 2518 user_settings: struct { 2519 failure: UserFailure, 2520 // passwords are never retained 2521 fields: struct { email: []const u8 }, 2522 }, 2523 user_keys: struct { 2524 failure: KeyFailure, 2525 key: []const u8, 2526 }, 2527 repo: struct { 2528 failure: RepoFailure, 2529 fields: struct { 2530 name: []const u8, 2531 description: []const u8, 2532 hash: hash.HashKind, 2533 access: evt.Repo.Access, 2534 }, 2535 }, 2536 repo_settings: struct { 2537 failure: RepoFailure, 2538 fields: struct { 2539 name: []const u8, 2540 description: []const u8, 2541 access: evt.Repo.Access, 2542 discuss_role: ?evt.Repo.Role, 2543 issue_role: ?evt.Repo.Role, 2544 patch_role: ?evt.Repo.Role, 2545 }, 2546 }, 2547 repo_members: struct { 2548 failure: MembersFailure, 2549 name: []const u8, 2550 }, 2551 repo_branches: struct { 2552 failure: BranchesFailure, 2553 name: []const u8, 2554 // whether it came from the default branch form, not the protect form 2555 default: bool = false, 2556 }, 2557 assignment: struct { 2558 failure: AssignmentFailure, 2559 text: []const u8, 2560 }, 2561 delete: struct { 2562 failure: DeleteFailure, 2563 // a page can hold two delete forms, so this names the failed one 2564 target: std.meta.Tag(Delete.Target), 2565 // the password is never retained 2566 confirm: []const u8, 2567 }, 2568 issue: struct { 2569 failure: ThreadFailure, 2570 fields: ?struct { 2571 title: []const u8, 2572 labels: []const u8, 2573 description: []const u8, 2574 } = null, 2575 }, 2576 patch: struct { 2577 failure: PatchFailure, 2578 fields: ?struct { 2579 title: []const u8, 2580 labels: []const u8, 2581 description: []const u8, 2582 target_branch: []const u8, 2583 source_branch: []const u8 = "", 2584 source_kind: Repo.Patches.Source.Kind = .fork, 2585 } = null, 2586 }, 2587 discussion: struct { 2588 failure: ThreadFailure, 2589 fields: ?struct { 2590 title: []const u8, 2591 labels: []const u8, 2592 description: []const u8, 2593 } = null, 2594 }, 2595 }; 2596 2597 const Self = @This(); 2598 2599 // serializable data sent down to web client 2600 pub const Data = struct { 2601 // the server's reference time for relative timestamps on this page. 2602 now: i64 = 0, 2603 user_id: ?[]const u8 = null, 2604 // the logged-in user's name and email, for the views that show them 2605 user_name: ?[]const u8 = null, 2606 user_email: ?[]const u8 = null, 2607 // the logged-in user's unread notifications, refreshed on each page build 2608 unread_notifs: u64 = 0, 2609 // a failed form retained long enough to render it for correction 2610 form_feedback: ?FormFeedback = null, 2611 // a transient local event-sync error 2612 sync_failure: ?[]const u8 = null, 2613 // a failed terminal undo retained on the current tab 2614 undo_failure: ?[]const u8 = null, 2615 current_page: RoutablePage = .default, 2616 // the selected ANSI art. the server serializes one image for WASM so 2617 // the browser does not need the whole build-generated collection. 2618 ansi_art: []const u8 = "", 2619 // where git clones and pushes go. absent in local mode, where remote 2620 // urls are not shown. 2621 git_http_url: ?[]const u8 = null, 2622 git_ssh_url: ?[]const u8 = null, 2623 git_ssh_prefix: []const u8 = "", 2624 // whether this session views a local repo or the server. unlike `local` 2625 // (the host-side filesystem source) this travels in the snapshot, so 2626 // the wasm side also parses elided link urls and hides the multi-user 2627 // chrome. 2628 host_kind: evt.HostKind = .server, 2629 }; 2630 2631 pub fn init( 2632 arena: *std.heap.ArenaAllocator, 2633 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2634 data: Data, 2635 ) !Self { 2636 var session = Self{ 2637 .data = data, 2638 .arena = arena, 2639 // until a host swaps in a page-scoped arena (Nav does this), page 2640 // allocations land in the session arena. on the web path that's the 2641 // intended behavior, since the whole arena is per-request. 2642 .page_arena = arena, 2643 .admin_repo = repo, 2644 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2645 }; 2646 try session.loadUser(); 2647 return session; 2648 } 2649 2650 // web views share the page's reference time; terminals use the live clock. 2651 pub fn nowSeconds(self: *const Self) !i64 { 2652 if (builtin.target.cpu.arch == .wasm32 or !self.is_terminal) return self.data.now; 2653 const io = self.io orelse return error.NoClock; 2654 return std.math.cast(i64, std.Io.Timestamp.now(io, .real).toSeconds()) orelse error.TimestampOutOfRange; 2655 } 2656 2657 // load the logged-in user's name and persisted preferences from the db 2658 pub fn loadUser(self: *Self) !void { 2659 const user_id = self.userId() orelse return; 2660 const moment = self.haxy_moment orelse return; 2661 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2662 self.data.user_name = user.event.name; 2663 self.data.user_email = user.event.email; 2664 } 2665 2666 // forget the logged-in user, on a logout or once their account is removed 2667 pub fn logOut(self: *Self) void { 2668 self.data.user_id = null; 2669 self.data.user_name = null; 2670 self.data.user_email = null; 2671 } 2672 2673 // log in as the account the session's ssh key last logged into with a 2674 // password, while that account still holds the key 2675 pub fn logInWithSshKey(self: *Self) !void { 2676 const key = self.ssh_key orelse return; 2677 const admin_repo = self.admin_repo orelse return; 2678 const moment = self.haxy_moment orelse return; 2679 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2680 const user_id = (try evt.readLoginKeyUser(try admin_repo.core.latestMoment(), &fingerprint)) orelse return; 2681 if (!try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, &user_id, &fingerprint)) return; 2682 self.data.user_id = try self.arena.allocator().dupe(u8, &user_id); 2683 try self.loadUser(); 2684 } 2685 2686 // add the session's ssh key to the user who just logged in with a 2687 // password, which proves they hold it, and let it log them in from now on 2688 pub fn rememberSshKey(self: *Self) !void { 2689 if (builtin.target.cpu.arch == .wasm32) return; 2690 const key = self.ssh_key orelse return; 2691 const io = self.io orelse return; 2692 const users_dir = self.users_dir orelse return; 2693 const admin_repo = self.admin_repo orelse return; 2694 const user_id = self.userId() orelse return; 2695 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2696 const allocator = self.arena.child_allocator; 2697 2698 const day = (std.time.epoch.EpochSeconds{ .secs = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()) }).getEpochDay().calculateYearDay(); 2699 const month = day.calculateMonthDay(); 2700 const line = try std.fmt.allocPrint(allocator, "{s} added {d:0>4}-{d:0>2}-{d:0>2} via login", .{ key, day.year, month.month.numeric(), month.day_index + 1 }); 2701 defer allocator.free(line); 2702 evt.addUserKey(io, allocator, users_dir, admin_repo, &user_id, line) catch |err| switch (err) { 2703 // it's already there, or there's no room for it 2704 error.KeyAdded, error.TooManyKeys => {}, 2705 else => return err, 2706 }; 2707 try evt.setLoginKey(io, admin_repo, &fingerprint, &user_id, .trust); 2708 self.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2709 } 2710 2711 // stop the session's ssh key logging the user in, on a logout 2712 pub fn forgetSshKey(self: *Self) !void { 2713 if (builtin.target.cpu.arch == .wasm32) return; 2714 const key = self.ssh_key orelse return; 2715 const io = self.io orelse return; 2716 const admin_repo = self.admin_repo orelse return; 2717 const user_id = self.userId() orelse return; 2718 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2719 try evt.setLoginKey(io, admin_repo, &fingerprint, &user_id, .forget); 2720 } 2721 2722 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2723 const bytes = self.data.user_id orelse return null; 2724 if (bytes.len != evt.event_id_size) return null; 2725 var id: [evt.event_id_size]u8 = undefined; 2726 @memcpy(&id, bytes); 2727 return id; 2728 } 2729 2730 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2731 const feedback = self.data.form_feedback orelse return null; 2732 if (std.meta.activeTag(feedback) != tag) return null; 2733 return @field(feedback, @tagName(tag)); 2734 } 2735 2736 pub fn clearFormFeedback(self: *Self) void { 2737 self.data.form_feedback = null; 2738 } 2739 2740 // the actor for a write to the repo `identity` names, or null when the 2741 // session may not write there with at least `min_role` or in the 2742 // `thread_kind` tab. local mode always writes. 2743 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role, thread_kind: ?evt.EventKind) !?Actor { 2744 if (self.data.host_kind == .local) { 2745 const author = blk: { 2746 const src = self.local orelse break :blk local_author_fallback; 2747 const io = self.io orelse break :blk local_author_fallback; 2748 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2749 }; 2750 return .{ .author = author, .role = .owner }; 2751 } 2752 const user_id = self.userId() orelse return null; 2753 // the session's moment predates this input, so read the roles afresh 2754 const admin_repo = self.admin_repo orelse return null; 2755 const io = self.io orelse return null; 2756 const users_dir = self.users_dir orelse return null; 2757 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2758 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role, thread_kind)) { 2759 .actor => |actor| actor, 2760 .login_required, .repo_not_found, .forbidden => null, 2761 }; 2762 } 2763 2764 // where this session's event writes go. a server session always has a users dir and an admin repo 2765 pub fn eventHost(self: *const Self) evt.Host { 2766 return switch (self.data.host_kind) { 2767 .local => .local, 2768 .server => .{ .server = .{ 2769 .users_dir = self.users_dir orelse unreachable, 2770 .admin_repo_path = (self.admin_repo orelse unreachable).core.work_path, 2771 } }, 2772 }; 2773 } 2774 2775 // reload the moment from the admin repo and log out a removed user 2776 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2777 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2778 self.haxy_moment = moment; 2779 2780 const user_id = self.userId() orelse return; 2781 var arena = std.heap.ArenaAllocator.init(allocator); 2782 defer arena.deinit(); 2783 if (try activeUser(moment, &arena, user_id) == null) self.logOut(); 2784 } 2785 2786 // request a forward navigation to `route`; the host consumes next_page 2787 // (Nav.sync on the terminal, the wasm tick on the web). 2788 pub fn navigate(self: *Session, route: RoutablePage) !void { 2789 self.clearFormFeedback(); 2790 self.next_page = route; 2791 } 2792 }; 2793 2794 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2795 var nav = try Nav.init(allocator, session); 2796 defer nav.deinit(allocator); 2797 2798 var terminal = try term.Terminal.init(io, allocator); 2799 terminal.render_state.no_color = !session.color; 2800 var terminal_live = true; 2801 defer if (terminal_live) terminal.deinit(io); 2802 2803 // set term as active so it will be properly cooked 2804 // when a panic/segfault happens 2805 term.setActive(&terminal); 2806 defer term.setActive(null); 2807 2808 try terminal.queryBackground(); 2809 2810 while (!terminal.shouldQuit()) { 2811 const grid_changed = try terminal.render(&nav.root); 2812 2813 // process any inputs. 2814 // 2815 // if the grid didn't change, then first do a blocking 2816 // read, so the thread will sleep until further input. 2817 // after that, all remaining reads are non-blocking so 2818 // we can process the rest of the queued inputs. 2819 // 2820 // if the grid *did* change, then only do non-blocking 2821 // reads. we do not want to sleep the thread because 2822 // there may be an animation that requires more looping. 2823 var blocking = !grid_changed; 2824 while (try terminal.readKey(io, blocking)) |key| { 2825 blocking = false; 2826 try inputKey(allocator, &nav.root, key, session); 2827 } 2828 2829 if (session.host_request) |request| { 2830 session.host_request = null; 2831 switch (request) { 2832 .show_copyable_text => |copyable_text| { 2833 term.setActive(null); 2834 terminal.deinit(io); 2835 terminal_live = false; 2836 showCopyableText(io, copyable_text) catch |show_err| { 2837 terminal = try term.Terminal.init(io, allocator); 2838 terminal_live = true; 2839 term.setActive(&terminal); 2840 return show_err; 2841 }; 2842 terminal = try term.Terminal.init(io, allocator); 2843 terminal.render_state.no_color = !session.color; 2844 terminal_live = true; 2845 term.setActive(&terminal); 2846 }, 2847 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2848 .sync_events => { 2849 try nav.root.build(allocator, .{ 2850 .min_size = .{ .width = null, .height = null }, 2851 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2852 }, nav.root.getFocus()); 2853 _ = try terminal.render(&nav.root); 2854 try Repo.Events.performSync(allocator, session); 2855 }, 2856 } 2857 } 2858 2859 // reload so the next navigation builds its page from the current moment 2860 if (repo_maybe) |repo| try session.reloadMoment(allocator, repo); 2861 2862 // reconcile navigation: forward to a new page, or back on escape. 2863 // the terminal background can change at any time, so recheck it on each page 2864 if (try nav.sync(allocator, session)) try terminal.queryBackground(); 2865 2866 // the quit button (on the quit tab) asks the host to tear down. 2867 if (session.quit_requested) terminal.requestQuit(); 2868 2869 try nav.root.build(allocator, .{ 2870 .min_size = .{ .width = null, .height = null }, 2871 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2872 }, nav.root.getFocus()); 2873 } 2874 } 2875 2876 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2877 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2878 null 2879 else 2880 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2881 defer if (tty) |file| file.close(io); 2882 2883 const input = if (tty) |file| file else std.Io.File.stdin(); 2884 const output = if (tty) |file| file else std.Io.File.stdout(); 2885 2886 var write_buf: [1024]u8 = undefined; 2887 var writer = output.writer(io, &write_buf); 2888 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2889 try writer.interface.flush(); 2890 2891 var read_buf: [1]u8 = undefined; 2892 var reader = input.reader(io, &read_buf); 2893 while (true) { 2894 const byte = try reader.interface.takeByte(); 2895 if (byte == '\r' or byte == '\n') return; 2896 } 2897 } 2898 2899 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2900 const root_focus = root.getFocus(); 2901 const focused_before = root_focus.grandchild_id; 2902 var activate_in_page_link = false; 2903 switch (key) { 2904 // request a navigation pop; the host's Nav.sync goes back a page, or 2905 // quits when there's no history left. 2906 .escape => { 2907 session.back = .requested; 2908 return; 2909 }, 2910 .ctrl => |letter| switch (letter) { 2911 'r' => { 2912 session.refresh_requested = true; 2913 return; 2914 }, 2915 else => try root.input(allocator, key, root_focus), 2916 }, 2917 .enter => { 2918 if (root_focus.grandchild_id) |gid| { 2919 // follow a cross-page link 2920 if (crossPageLink(root_focus, gid, session.data)) |route| { 2921 return session.navigate(route); 2922 } 2923 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2924 } 2925 activate_in_page_link = true; 2926 try root.input(allocator, key, root_focus); 2927 }, 2928 .mouse => |mouse| { 2929 if (mouse.button == .left) { 2930 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2931 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2932 const focus_id = hit.id; 2933 if (hit.kind == .background) { 2934 root_focus.setFocus(focus_id); 2935 return; 2936 } 2937 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2938 if (session.back == .available) session.back = .requested; 2939 return; 2940 } 2941 // follow a cross-page link 2942 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2943 return session.navigate(route); 2944 } 2945 if (currentPageLink(root_focus, focus_id, session.data)) return; 2946 if (rawLink(root_focus, focus_id)) |url| { 2947 if (!mouse.ctrl) try requestRawLink(session, url); 2948 return; 2949 } 2950 root_focus.setFocus(focus_id); 2951 activate_in_page_link = true; 2952 } 2953 // forward the press into the widget tree so buttons (and any 2954 // future click-aware widgets) can react. widgets that don't 2955 // care about presses ignore it. 2956 try root.input(allocator, key, root_focus); 2957 } else { 2958 try root.input(allocator, key, root_focus); 2959 } 2960 }, 2961 .event => |event| switch (event) { 2962 .background => |rgb| session.terminal_background = rgb, 2963 .resize => try root.input(allocator, key, root_focus), 2964 }, 2965 // a scroll tick goes to the pane under the pointer, focusing it first. 2966 // from outside any pane (the tabs, a sub-header) or from a text input 2967 // it moves like the arrows instead, so scrolling up from a search box 2968 // reaches the header and stops there. 2969 .scroll_up, .scroll_down => |position| { 2970 if (focusedPane(root_focus)) |pane| if (scrollUnder(root_focus, position)) |under| { 2971 if (under != pane) root_focus.setFocus(under); 2972 }; 2973 try root.input(allocator, key, root_focus); 2974 }, 2975 else => try root.input(allocator, key, root_focus), 2976 } 2977 2978 const focused_after = root_focus.grandchild_id; 2979 if (!activate_in_page_link and focused_before == focused_after) return; 2980 const focus_id = focused_after orelse return; 2981 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2982 } 2983 2984 // the innermost vertically scrolling widget laid out under `position` 2985 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2986 var found: ?usize = null; 2987 var found_area: usize = std.math.maxInt(usize); 2988 var iter = root_focus.children.iterator(); 2989 while (iter.next()) |entry| { 2990 const info = entry.value_ptr.focus.scroll orelse continue; 2991 if (info.direction == .horiz) continue; 2992 const r = entry.value_ptr.rect; 2993 if (position.x < r.x or position.y < r.y or position.x >= r.x + r.size.width or position.y >= r.y + r.size.height) continue; 2994 // a nested scroll lies within its parent, so the smallest is innermost 2995 const area = r.size.width * r.size.height; 2996 if (area < found_area) { 2997 found = entry.key_ptr.*; 2998 found_area = area; 2999 } 3000 } 3001 return found; 3002 } 3003 3004 // the innermost vertically scrolling widget the focus lies in, unless the 3005 // focused widget is a text input 3006 fn focusedPane(root_focus: *const Focus) ?usize { 3007 var current = root_focus.grandchild_id orelse return null; 3008 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 3009 .text_input, .text_input_password, .text_area => return null, 3010 else => {}, 3011 } 3012 while (root_focus.children.get(current)) |child| { 3013 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 3014 current = child.parent_id; 3015 } 3016 return null; 3017 } 3018 3019 // a link to bytes the server serves directly rather than to a page route, so 3020 // it never resolves to a RoutablePage 3021 pub const raw_link_prefix = "ax:"; 3022 3023 // a route already loaded into the current page; focusing it changes the url 3024 // without rebuilding the page 3025 pub const in_page_link_prefix = "ai:"; 3026 3027 // an in-page tab's base route, or the exact current route when it is selected 3028 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 3029 const target = if (selected) session.data.current_page else route; 3030 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 3031 } 3032 3033 // a button the web renderer covers with a file picker that posts the chosen 3034 // file to the url after the prefix 3035 pub const file_input_prefix = "file:"; 3036 3037 // a submit button that overrides its enclosing form's action 3038 pub const submit_action_prefix = "submit:"; 3039 3040 // one option of a radio field, as `radio:<name>=<value>` 3041 pub const radio_prefix = "radio:"; 3042 3043 // the url an `ax:` link points at, or null for anything else. a host that 3044 // suppresses the anchor's own navigation needs this to follow the link. 3045 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 3046 const child = root_focus.children.get(focus_id) orelse return null; 3047 const custom = switch (child.focus.kind) { 3048 .custom => |c| c, 3049 else => return null, 3050 }; 3051 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 3052 return custom[raw_link_prefix.len..]; 3053 } 3054 3055 fn requestRawLink(session: *Session, url: []const u8) !void { 3056 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 3057 } 3058 3059 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 3060 // an absolute url is already complete 3061 if (!std.mem.startsWith(u8, path, "/")) return path; 3062 return if (session.web_url) |web_url| 3063 try allocator.print("{s}{s}", .{ web_url, path }) 3064 else 3065 path; 3066 } 3067 3068 // if the focus target at focus_id is an `a:` link that should navigate (a 3069 // different parent page, or a different files directory within the repo page), 3070 // return its route; otherwise null. lets a host turn a click / enter on such a 3071 // link into a navigation rather than a focus change. 3072 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3073 const current = data.current_page; 3074 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 3075 // a link to a different parent page always navigates; within a page, a 3076 // files-directory / commits-page / list-window change navigates while tab 3077 // links stay in-page. 3078 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 3079 return null; 3080 } 3081 3082 // the page the clicked cell links to, from a link inside a text box's content 3083 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 3084 const grid = root.getGrid() orelse return null; 3085 const cell = grid.cell(x, y) catch return null; 3086 const link = cell.link orelse return null; 3087 if (link.len == 0 or link[0] != '/') return null; 3088 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 3089 } 3090 3091 // whether the `a:` focus target links to the page already shown, which a click 3092 // leaves alone rather than focusing 3093 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 3094 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 3095 return route.eql(data.current_page); 3096 } 3097 3098 // the route carried by an `ai:` focus target, or null for any other target 3099 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3100 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 3101 } 3102 3103 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 3104 const child = root_focus.children.get(focus_id) orelse return null; 3105 const custom = switch (child.focus.kind) { 3106 .custom => |c| c, 3107 else => return null, 3108 }; 3109 if (!std.mem.startsWith(u8, custom, prefix)) return null; 3110 const path = custom[prefix.len..]; 3111 // local sessions build (and parse) links with the repo identity elided 3112 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 3113 } 3114 3115 // an assignee as the detail view shows it: the stored email, and the name of 3116 // the user holding it on this instance when there is one 3117 pub const Assignee = struct { 3118 email: []const u8, 3119 name: ?[]const u8 = null, 3120 3121 pub fn display(self: Assignee) []const u8 { 3122 return self.name orelse self.email; 3123 } 3124 }; 3125 3126 // a display author, resolved against the admin db at read time 3127 pub const Author = union(enum) { 3128 unknown, 3129 email: []const u8, // an email no user matches 3130 user_name: []const u8, 3131 3132 // the commit author line's email, resolved like initFromEmail 3133 pub fn init( 3134 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3135 arena: *std.heap.ArenaAllocator, 3136 author_line: []const u8, 3137 ) !Author { 3138 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 3139 } 3140 3141 // from a record's stored author email: it resolves to the user currently 3142 // holding it, else stays the email. the result is allocated in `arena`, 3143 // so the email may borrow transient memory. 3144 pub fn initFromEmail( 3145 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3146 arena: *std.heap.ArenaAllocator, 3147 author_email: ?[]const u8, 3148 ) !Author { 3149 const email = author_email orelse return .unknown; 3150 if (haxy_moment) |moment| { 3151 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 3152 return .{ .user_name = user.name }; 3153 } 3154 } 3155 return .{ .email = try arena.allocator().dupe(u8, email) }; 3156 } 3157 }; 3158 3159 // the "a:" link to user `name`'s page. 3160 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3161 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3162 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3163 return page_arena.allocator().print("a:{s}", .{url}); 3164 } 3165 3166 // a focusable " author " box showing `author`, linking to their user page 3167 // when it is a known user 3168 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3169 const text = switch (author) { 3170 .unknown => "", 3171 .email, .user_name => |t| t, 3172 }; 3173 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3174 errdefer tb.deinit(allocator); 3175 tb.getFocus().mode = .all; 3176 switch (author) { 3177 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3178 else => {}, 3179 } 3180 return tb; 3181 } 3182 3183 // native-TUI navigation 3184 pub const Nav = struct { 3185 root: Widget, 3186 // backs the current page (its Page data and the page-scoped strings its 3187 // widgets build). owned here, swapped on each navigation. session.page_arena 3188 // tracks whichever of these is current so widgets allocate into it. 3189 arena: *std.heap.ArenaAllocator, 3190 // the login the current root was built for 3191 user_id: ?[evt.event_id_size]u8, 3192 history: std.ArrayList(Entry), 3193 3194 // each retained page keeps its own arena, freed when the page leaves the 3195 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3196 // long-lived session from accumulating every page it ever visited. 3197 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3198 3199 // cap on retained back-history so the chain can't grow memory without bound 3200 const max_history: usize = 16; 3201 3202 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3203 session.back = .unavailable; 3204 const arena = try allocator.create(std.heap.ArenaAllocator); 3205 arena.* = std.heap.ArenaAllocator.init(allocator); 3206 errdefer { 3207 arena.deinit(); 3208 allocator.destroy(arena); 3209 } 3210 3211 return .{ 3212 .root = try initCurrentRoot(allocator, arena, session), 3213 .arena = arena, 3214 .user_id = session.userId(), 3215 .history = .empty, 3216 }; 3217 } 3218 3219 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3220 self.root.deinit(allocator); 3221 freeArena(allocator, self.arena); 3222 for (self.history.items) |*entry| { 3223 entry.root.deinit(allocator); 3224 freeArena(allocator, entry.arena); 3225 } 3226 self.history.deinit(allocator); 3227 } 3228 3229 // rebuild the current page in place from a current moment 3230 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3231 if (session.haxy_moment == null and session.local == null) return; 3232 const arena = try allocator.create(std.heap.ArenaAllocator); 3233 arena.* = std.heap.ArenaAllocator.init(allocator); 3234 errdefer freeArena(allocator, arena); 3235 3236 const new_root = try initCurrentRoot(allocator, arena, session); 3237 3238 self.root.deinit(allocator); 3239 freeArena(allocator, self.arena); 3240 self.root = new_root; 3241 self.arena = arena; 3242 self.user_id = session.userId(); 3243 } 3244 3245 // build the current page's root in `arena`, or the not-found page's when 3246 // the route names nothing 3247 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3248 session.page_arena = arena; 3249 const page = try arena.allocator().create(Page); 3250 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3251 error.NotFound => blk: { 3252 session.data.current_page = .not_found; 3253 break :blk .not_found; 3254 }, 3255 else => |e| return e, 3256 }; 3257 return initRoot(allocator, page, session); 3258 } 3259 3260 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3261 arena.deinit(); 3262 allocator.destroy(arena); 3263 } 3264 3265 // reconcile the displayed root with the session's navigation state. a 3266 // forward nav (current_page moved to a different parent page) pushes the 3267 // current root and builds the new page in a fresh arena; a back request 3268 // frees the current page and restores the previous one. when escape is 3269 // pressed with no history left we switch to the quit tab instead of quitting. 3270 // returns whether the root was replaced. 3271 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3272 // refresh: rebuild the current page in place from a current moment 3273 if (session.refresh_requested) { 3274 session.refresh_requested = false; 3275 try self.rebuild(allocator, session); 3276 return true; 3277 } 3278 3279 if (session.back == .requested) { 3280 if (self.history.pop()) |entry| { 3281 session.back = if (self.history.items.len == 0) .unavailable else .available; 3282 self.root.deinit(allocator); 3283 freeArena(allocator, self.arena); 3284 self.root = entry.root; 3285 self.arena = entry.arena; 3286 self.user_id = entry.user_id; 3287 session.page_arena = entry.arena; 3288 session.data.current_page = entry.route; 3289 // a page built under another login is rebuilt rather than restored 3290 if (!std.meta.eql(entry.user_id, session.userId())) { 3291 try self.rebuild(allocator, session); 3292 return true; 3293 } 3294 chooseAnsiArtForNavigation(session); 3295 return true; 3296 } 3297 session.back = .unavailable; 3298 // nothing to go back to; switch to the quit confirmation 3299 const root_focus = self.root.getFocus(); 3300 var iter = root_focus.children.iterator(); 3301 while (iter.next()) |entry| { 3302 switch (entry.value_ptr.focus.kind) { 3303 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3304 root_focus.setFocus(entry.key_ptr.*); 3305 // a Stack only builds its selected child, so the quit 3306 // button isn't in the focus tree until a build runs with 3307 // the quit tab selected. build once, then send a 3308 // synthetic arrow_down to drop focus from the tab onto 3309 // the button so it's ready to confirm. 3310 try self.root.build(allocator, .{ 3311 .min_size = .{ .width = null, .height = 40 }, 3312 .max_size = .{ .width = 80, .height = null }, 3313 }, root_focus); 3314 try self.root.input(allocator, .arrow_down, root_focus); 3315 break; 3316 }, 3317 else => {}, 3318 } 3319 } 3320 return false; 3321 } 3322 3323 // forward navigation: navigate() set next_page to the page to move to (a 3324 // cross-page link or tab change crossing pages) 3325 if (session.next_page) |route| { 3326 session.next_page = null; 3327 if (session.haxy_moment == null and session.local == null) return false; 3328 const previous_route = session.data.current_page; 3329 // the page we navigated to becomes the current page 3330 session.data.current_page = route; 3331 session.back = .available; 3332 3333 const arena = try allocator.create(std.heap.ArenaAllocator); 3334 arena.* = std.heap.ArenaAllocator.init(allocator); 3335 errdefer freeArena(allocator, arena); 3336 3337 const new_root = try initCurrentRoot(allocator, arena, session); 3338 3339 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3340 // drop the oldest entry (freeing its widget tree and arena) once over cap 3341 if (self.history.items.len > max_history) { 3342 var oldest = self.history.orderedRemove(0); 3343 oldest.root.deinit(allocator); 3344 freeArena(allocator, oldest.arena); 3345 } 3346 self.root = new_root; 3347 self.arena = arena; 3348 self.user_id = session.userId(); 3349 return true; 3350 } 3351 return false; 3352 } 3353 }; 3354 3355 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3356 const page_widget: Widget = switch (page.*) { 3357 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3358 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3359 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3360 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3361 .not_found => .{ .not_found = try .init(allocator, session) }, 3362 }; 3363 3364 chooseAnsiArtForNavigation(session); 3365 3366 // on the TUI/SSH, the page sits above a one-row footer showing its web 3367 // url, when there's a web UI to point at 3368 var root = if (session.is_terminal) blk: { 3369 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3370 errdefer box.deinit(allocator); 3371 const bg_id = bg_blk: { 3372 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3373 errdefer background.deinit(allocator); 3374 const id = background.getFocus().id; 3375 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3376 break :bg_blk id; 3377 }; 3378 if (session.web_url != null) { 3379 var footer = try widget.Footer.init(allocator, session); 3380 errdefer footer.deinit(allocator); 3381 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3382 } 3383 box.getFocus().child_id = bg_id; 3384 break :blk Widget{ .box = box }; 3385 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3386 errdefer root.deinit(allocator); 3387 3388 // input-owning views build their TextInputs in init, so reset their 3389 // registrations here 3390 session.text_inputs.clearRetainingCapacity(); 3391 3392 try root.build(allocator, .{ 3393 .min_size = .{ .width = null, .height = 40 }, 3394 .max_size = .{ .width = 100, .height = null }, 3395 }, root.getFocus()); 3396 3397 return root; 3398 } 3399 3400 // native and server-side sessions choose from the embedded collection. WASM 3401 // keeps the art serialized by the server instead. 3402 fn chooseAnsiArtForNavigation(session: *Session) void { 3403 if (comptime ansi_art.art.len == 0) return; 3404 if (session.data.current_page == .not_found) { 3405 session.data.ansi_art = ansi_art.not_found; 3406 return; 3407 } 3408 const io = session.io orelse return; 3409 var random: usize = undefined; 3410 io.random(std.mem.asBytes(&random)); 3411 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3412 } 3413