╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
244 // the "line:<n>" first-line param (0 when absent), null when malformed 245 fn line(self: Params) ?usize { 246 const value = self.values.get(.line) orelse return 0; 247 return std.fmt.parseInt(usize, value, 10) catch null; 248 } 249 250 // the single branch:/tag:/object: param, null when none is set 251 fn ref(self: Params) error{BadUrl}!?RefParam { 252 var result: ?RefParam = null; 253 inline for (@typeInfo(RefOrOid).@"enum".fields) |field| { 254 if (self.values.get(@field(ParamKey, field.name))) |value| { 255 if (result != null) return error.BadUrl; 256 result = .{ .kind = @field(RefOrOid, field.name), .value = value }; 257 } 258 } 259 return result; 260 } 261 }; 262 263 const user_segment = "/user/"; 264 const repo_segment = "/repo/"; 265 const fork_segment = "/fork/"; 266 const files_seg = "files"; 267 const commits_seg = "commits"; 268 // ends a commits route naming its commit's full message 269 const message_seg = "message"; 270 // the `Params` key spellings the url emitters use 271 const tag_filter_seg = @tagName(Params.ParamKey.tag) ++ ":"; 272 const start_seg = @tagName(Params.ParamKey.start) ++ ":"; 273 const line_seg = @tagName(Params.ParamKey.line) ++ ":"; 274 const base_seg = @tagName(Params.ParamKey.base) ++ ":"; 275 const theirs_seg = @tagName(Params.ParamKey.theirs) ++ ":"; 276 const issue_seg = "issue:"; 277 const patch_seg = "patch:"; 278 const discuss_seg = "discuss:"; 279 const comment_seg = "comment:"; 280 // marks a files or commits route's trailing path: the value is the raw 281 // remainder of the url, so it must come last 282 const path_seg = "path:"; 283 284 // a repo route is "username/reponame", optionally followed by the files 285 // tab's "/files/<refkind>:<refvalue>[/line:<n>][/path:<dir>]"; the bare 286 // pair means the default branch's files root. a local session's routes 287 // elide the identity: "" at the bare root, else starting at the tail's "/". 288 pub const repo_route_max_len = 1024; 289 pub const repo_identity_max_len = evt.User.name_max_len + 1 + evt.Repo.name_max_len; 290 291 // true when a repo route's stored string elides its identity (local mode). 292 // unambiguous because a full string always starts with its owner segment. 293 fn identityElided(s: []const u8) bool { 294 return s.len == 0 or s[0] == '/'; 295 } 296 297 // a url-encoded tag can grow to three bytes per source byte. 298 pub const issue_tag_route_max_len = evt.Issue.tag_max_len * 3; 299 pub const patch_tag_route_max_len = evt.Patch.tag_max_len * 3; 300 pub const discussion_tag_route_max_len = evt.Discussion.tag_max_len * 3; 301 302 // caps the resolve view's theirs: field list (a longer one doesn't route). 303 pub const theirs_route_max_len = 512; 304 305 // caps a url-encoded ref name in a route (a longer name doesn't route). 306 pub const ref_route_max_len = 512; 307 308 // the identity stored by every repo route. Local routes elide it. 309 pub const RepoIdentity = struct { 310 identity: []const u8, // "owner/name" 311 owner: []const u8, 312 name: []const u8, 313 314 pub fn parse(s: []const u8) ?RepoIdentity { 315 var result: RepoIdentity = .{ 316 .identity = "", 317 .owner = "", 318 .name = "", 319 }; 320 if (identityElided(s)) { 321 return result; 322 } else { 323 var id_segments = std.mem.splitScalar(u8, s, '/'); 324 const owner = id_segments.next() orelse return null; 325 const name = id_segments.next() orelse return null; 326 if (owner.len == 0 or name.len == 0) return null; 327 result.identity = s[0 .. owner.len + 1 + name.len]; 328 result.owner = owner; 329 result.name = name; 330 } 331 return result; 332 } 333 }; 334 335 pub const RepoFilesRoute = struct { 336 name: Array(repo_identity_max_len), 337 ref_kind: ?RefOrOid = null, 338 ref_value: Array(ref_route_max_len) = .{}, 339 patchrev_id: Array(evt.event_id_size * 2) = .{}, 340 path: Array(repo_route_max_len) = .{}, 341 // the selected file's first visible line (1-based; 0 = unset). 342 line: usize = 0, 343 }; 344 345 pub const RepoDiffRoute = struct { 346 name: Array(repo_identity_max_len), 347 ref_or_oid: ?RefOrOid = null, 348 value: Array(ref_route_max_len) = .{}, 349 base_oid: Array(ref_route_max_len) = .{}, 350 patchrev_id: Array(evt.event_id_size * 2) = .{}, 351 start: usize = 0, 352 path: Array(repo_route_max_len) = .{}, 353 }; 354 355 pub const RepoCommitsRoute = struct { 356 name: Array(repo_identity_max_len), 357 ref_or_oid: ?RefOrOid = null, 358 value: Array(ref_route_max_len) = .{}, 359 base_oid: Array(ref_route_max_len) = .{}, 360 // what the pane shows for the commit the log walks from. 361 content: Content = .{ .diff = .{} }, 362 363 // a diff window and the message are alternatives, so the window's 364 // params can't be named alongside the message. 365 pub const Content = union(enum) { 366 diff: struct { 367 // the hunk index the window starts at. 368 start: usize = 0, 369 // the file the diff is filtered to ("" = every file). 370 path: Array(repo_route_max_len) = .{}, 371 }, 372 message, 373 }; 374 }; 375 376 pub const RepoEventsRoute = struct { 377 name: Array(repo_identity_max_len), 378 view: EventsView = .active, 379 kind: ?evt.EventKind = null, 380 selected: Array(evt.event_id_size * 2) = .{}, 381 }; 382 383 pub const ForkRoute = struct { 384 name: Array(repo_identity_max_len), 385 id: Array(evt.event_id_size * 2), 386 }; 387 388 pub const ForkFilesRoute = struct { 389 fork: ForkRoute, 390 oid: Array(ref_route_max_len) = .{}, 391 path: Array(repo_route_max_len) = .{}, 392 line: usize = 0, 393 }; 394 395 pub const ForkCommitsRoute = struct { 396 fork: ForkRoute, 397 oid: Array(ref_route_max_len) = .{}, 398 content: RepoCommitsRoute.Content = .{ .diff = .{} }, 399 }; 400 401 // where the shared files and commits views are mounted. fork routes always 402 // read their implicit `patch` branch unless they name an object directly. 403 pub const RepoLocation = union(enum) { 404 repo: []const u8, 405 fork: struct { 406 identity: []const u8, 407 id: []const u8, 408 }, 409 410 pub fn dupe(self: RepoLocation, allocator: std.mem.Allocator) !RepoLocation { 411 return switch (self) { 412 .repo => |identity_value| .{ .repo = try allocator.dupe(u8, identity_value) }, 413 .fork => |f| .{ .fork = .{ 414 .identity = try allocator.dupe(u8, f.identity), 415 .id = try allocator.dupe(u8, f.id), 416 } }, 417 }; 418 } 419 420 pub fn filesRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, path: []const u8, line: usize) ?RoutablePage { 421 return switch (self) { 422 .repo => |repo_identity| repoFilesRoute(repo_identity, ref_or_oid, value, path, line), 423 .fork => |f| if (ref_or_oid == .branch and std.mem.eql(u8, value, "patch")) 424 forkFilesRoute(f.identity, f.id, "", path, line) 425 else if (ref_or_oid == .object) 426 forkFilesRoute(f.identity, f.id, value, path, line) 427 else 428 null, 429 }; 430 } 431 432 pub fn commitsRoute(self: RepoLocation, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 433 return switch (self) { 434 .repo => |repo_identity| repoCommitsRoute(repo_identity, ref_or_oid, value, start, path, base_oid), 435 .fork => |f| if (ref_or_oid == null or (ref_or_oid == .branch and std.mem.eql(u8, value, "patch"))) 436 forkCommitsRoute(f.identity, f.id, "", start, path) 437 else if (ref_or_oid == .object) 438 forkCommitsRoute(f.identity, f.id, value, start, path) 439 else 440 null, 441 }; 442 } 443 444 pub fn commitMessageRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 445 return switch (self) { 446 .repo => |repo_identity| repoCommitMessageRoute(repo_identity, ref_or_oid, value, base_oid), 447 .fork => |f| if (ref_or_oid == .object) forkCommitMessageRoute(f.identity, f.id, value) else null, 448 }; 449 } 450 }; 451 452 fn initForkRoute(identity: []const u8, id: []const u8) ?ForkRoute { 453 if (id.len != evt.event_id_size * 2) return null; 454 return .{ 455 .name = Array(repo_identity_max_len).from(identity) orelse return null, 456 .id = Array(evt.event_id_size * 2).from(id) orelse return null, 457 }; 458 } 459 460 pub fn forkPatchRoute(identity: []const u8, id: []const u8) ?RoutablePage { 461 return .{ .fork_patch = initForkRoute(identity, id) orelse return null }; 462 } 463 464 pub fn forkDiffRoute(identity: []const u8, id: []const u8, start: usize, path: []const u8) ?RoutablePage { 465 return .{ .fork_diff = .{ 466 .fork = initForkRoute(identity, id) orelse return null, 467 .start = start, 468 .path = Array(repo_route_max_len).from(path) orelse return null, 469 } }; 470 } 471 472 pub fn forkFilesRoute(identity: []const u8, id: []const u8, oid: []const u8, path: []const u8, line: usize) ?RoutablePage { 473 return .{ .fork_files = .{ 474 .fork = initForkRoute(identity, id) orelse return null, 475 .oid = Array(ref_route_max_len).from(oid) orelse return null, 476 .path = Array(repo_route_max_len).from(path) orelse return null, 477 .line = line, 478 } }; 479 } 480 481 pub fn forkCommitsRoute(identity: []const u8, id: []const u8, oid: []const u8, start: usize, path: []const u8) ?RoutablePage { 482 return .{ .fork_commits = .{ 483 .fork = initForkRoute(identity, id) orelse return null, 484 .oid = Array(ref_route_max_len).from(oid) orelse return null, 485 .content = .{ .diff = .{ 486 .start = start, 487 .path = Array(repo_route_max_len).from(path) orelse return null, 488 } }, 489 } }; 490 } 491 492 pub fn forkCommitMessageRoute(identity: []const u8, id: []const u8, oid: []const u8) ?RoutablePage { 493 if (oid.len == 0) return null; 494 return .{ .fork_commits = .{ 495 .fork = initForkRoute(identity, id) orelse return null, 496 .oid = Array(ref_route_max_len).from(oid) orelse return null, 497 .content = .message, 498 } }; 499 } 500 501 pub fn forkSettingsRoute(identity: []const u8, id: []const u8) ?RoutablePage { 502 return .{ .fork_settings = initForkRoute(identity, id) orelse return null }; 503 } 504 505 pub fn forkAuthRoute(identity: []const u8, id: []const u8) ?RoutablePage { 506 return .{ .fork_auth = initForkRoute(identity, id) orelse return null }; 507 } 508 509 // build a `.repo_files` route (a null ref_kind = the bare default-branch 510 // root). null if the result doesn't fit the inline name. 511 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8, line: usize) ?RoutablePage { 512 if (ref_kind == null and (ref_value.len != 0 or dir.len != 0)) return null; 513 if (ref_kind != null and ref_value.len == 0 and dir.len != 0) return null; 514 return .{ .repo_files = .{ 515 .name = Array(repo_identity_max_len).from(identity) orelse return null, 516 .ref_kind = ref_kind, 517 .ref_value = Array(ref_route_max_len).from(ref_value) orelse return null, 518 .path = Array(repo_route_max_len).from(dir) orelse return null, 519 .line = line, 520 } }; 521 } 522 523 pub fn repoPatchRevFilesRoute(identity: []const u8, id: []const u8, path: []const u8, line: usize) ?RoutablePage { 524 _ = evt.parseEventId(id) catch return null; 525 return .{ .repo_files = .{ 526 .name = Array(repo_identity_max_len).from(identity) orelse return null, 527 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 528 .path = Array(repo_route_max_len).from(path) orelse return null, 529 .line = line, 530 } }; 531 } 532 533 // build a `.repo_commits` route (a null ref_or_oid = the default branch). 534 // always carries the `commits` marker so the bare route doesn't collide 535 // with the files root. a non-empty `path` filters the diff pane to that 536 // file and requires a ref. 537 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 538 if (ref_or_oid == null and (value.len != 0 or path.len != 0)) return null; 539 return .{ .repo_commits = .{ 540 .name = Array(repo_identity_max_len).from(identity) orelse return null, 541 .ref_or_oid = ref_or_oid, 542 .value = Array(ref_route_max_len).from(value) orelse return null, 543 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 544 .content = .{ .diff = .{ 545 .start = start, 546 .path = Array(repo_route_max_len).from(path) orelse return null, 547 } }, 548 } }; 549 } 550 551 pub fn repoDiffRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 552 if (base_oid.len == 0 or (ref_or_oid == null and value.len != 0)) return null; 553 return .{ .repo_diff = .{ 554 .name = Array(repo_identity_max_len).from(identity) orelse return null, 555 .ref_or_oid = ref_or_oid, 556 .value = Array(ref_route_max_len).from(value) orelse return null, 557 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 558 .start = start, 559 .path = Array(repo_route_max_len).from(path) orelse return null, 560 } }; 561 } 562 563 pub fn repoPatchRevDiffRoute(identity: []const u8, id: []const u8, start: usize, path: []const u8) ?RoutablePage { 564 _ = evt.parseEventId(id) catch return null; 565 return .{ .repo_diff = .{ 566 .name = Array(repo_identity_max_len).from(identity) orelse return null, 567 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 568 .start = start, 569 .path = Array(repo_route_max_len).from(path) orelse return null, 570 } }; 571 } 572 573 // build a `.repo_commits` route showing the ref/oid's commit message in 574 // full, which is the only content that page renders. 575 pub fn repoCommitMessageRoute(identity: []const u8, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 576 if (value.len == 0) return null; 577 return .{ .repo_commits = .{ 578 .name = Array(repo_identity_max_len).from(identity) orelse return null, 579 .ref_or_oid = ref_or_oid, 580 .value = Array(ref_route_max_len).from(value) orelse return null, 581 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 582 .content = .message, 583 } }; 584 } 585 586 // build a `.repo_refs` route windowing `kind`'s column from the 587 // url-encoded ref name `from` ("" = the first window). 588 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, from: []const u8) ?RoutablePage { 589 return .{ .repo_refs = .{ 590 .name = Array(repo_route_max_len).from(identity) orelse return null, 591 .kind = kind, 592 .from = Array(ref_route_max_len).from(from) orelse return null, 593 } }; 594 } 595 596 // build the events list or a window rooted at one event. 597 pub fn repoEventsRoute(identity: []const u8, view: EventsView, kind: ?evt.EventKind, selected: []const u8) ?RoutablePage { 598 if ((kind == null) != (selected.len == 0)) return null; 599 return .{ .repo_events = .{ 600 .name = Array(repo_identity_max_len).from(identity) orelse return null, 601 .view = view, 602 .kind = kind, 603 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 604 } }; 605 } 606 607 // build a `.repo_issues` route for "owner/name" (identity) showing 608 // `status`'s list, filtered to the url-encoded `tag` ("" = unfiltered) and 609 // rooted at the issue with hex event id `selected` ("" = the first window). 610 pub fn repoIssuesRoute(identity: []const u8, status: evt.Issue.Status, tag: []const u8, selected: []const u8) ?RoutablePage { 611 return .{ .repo_issues = .{ 612 .name = Array(repo_route_max_len).from(identity) orelse return null, 613 .tag = Array(issue_tag_route_max_len).from(tag) orelse return null, 614 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 615 .view = switch (status) { 616 .open => .open, 617 .closed => .closed, 618 }, 619 } }; 620 } 621 622 pub fn repoPatchesRoute(identity: []const u8, status: evt.Patch.StatusKind, tag: []const u8, selected: []const u8) ?RoutablePage { 623 return .{ .repo_patches = .{ 624 .name = Array(repo_route_max_len).from(identity) orelse return null, 625 .tag = Array(patch_tag_route_max_len).from(tag) orelse return null, 626 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 627 .view = switch (status) { 628 .open => .open, 629 .closed => .closed, 630 .merged => .merged, 631 }, 632 } }; 633 } 634 635 pub fn repoPatchesDraftsRoute(identity: []const u8) ?RoutablePage { 636 return .{ .repo_patches = .{ 637 .name = Array(repo_route_max_len).from(identity) orelse return null, 638 .view = .drafts, 639 } }; 640 } 641 642 pub fn repoPatchPublishRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 643 if (selected.len == 0) return null; 644 var route = repoPatchesRoute(identity, .open, "", selected) orelse return null; 645 switch (route) { 646 .repo_patches => |*patch| patch.view = .publish, 647 else => unreachable, 648 } 649 return route; 650 } 651 652 pub fn repoPatchMergeRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 653 if (selected.len == 0) return null; 654 var route = repoPatchesRoute(identity, .open, "", selected) orelse return null; 655 switch (route) { 656 .repo_patches => |*patch| patch.view = .merge, 657 else => unreachable, 658 } 659 return route; 660 } 661 662 pub fn repoDiscussionsRoute(identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 663 return .{ .repo_discussions = .{ 664 .name = Array(repo_route_max_len).from(identity) orelse return null, 665 .tag = Array(discussion_tag_route_max_len).from(tag) orelse return null, 666 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 667 } }; 668 } 669 670 // build the default list route for a thread event kind. 671 pub fn repoThreadRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 672 return switch (kind) { 673 .issue => repoIssuesRoute(identity, .open, tag, selected), 674 .patch => repoPatchesRoute(identity, .open, tag, selected), 675 .discuss => repoDiscussionsRoute(identity, tag, selected), 676 else => null, 677 }; 678 } 679 680 // build a selected thread route at one of its comment windows. 681 pub fn repoThreadCommentsRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8, start: usize) ?RoutablePage { 682 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 683 switch (route) { 684 .repo_issues => |*thread| thread.comments_start = start, 685 .repo_patches => |*thread| thread.comments_start = start, 686 .repo_discussions => |*thread| thread.comments_start = start, 687 else => unreachable, 688 } 689 return route; 690 } 691 692 // build a comment permalink route at one of its immediate-reply windows. 693 pub fn repoThreadCommentRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?RoutablePage { 694 if (thread_id.len == 0 or comment_id.len == 0) return null; 695 var route = repoThreadCommentsRoute(kind, identity, thread_id, start) orelse return null; 696 const comment = Array(evt.event_id_size * 2).from(comment_id) orelse return null; 697 switch (route) { 698 .repo_issues => |*thread| thread.comment = comment, 699 .repo_patches => |*thread| thread.comment = comment, 700 .repo_discussions => |*thread| thread.comment = comment, 701 else => unreachable, 702 } 703 return route; 704 } 705 706 // build the form route for replying to a thread or one of its comments. 707 pub fn repoThreadCommentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?RoutablePage { 708 if (thread_id.len == 0) return null; 709 var route = repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null; 710 const parent_event_id = Array(evt.event_id_size * 2).from(parent_id) orelse return null; 711 switch (route) { 712 .repo_issues => |*thread| { 713 thread.comment = parent_event_id; 714 thread.view = .new_comment; 715 }, 716 .repo_patches => |*thread| { 717 thread.comment = parent_event_id; 718 thread.view = .new_comment; 719 }, 720 .repo_discussions => |*thread| { 721 thread.comment = parent_event_id; 722 thread.view = .new_comment; 723 }, 724 else => unreachable, 725 } 726 return route; 727 } 728 729 // build the form route for editing a comment. 730 pub fn repoThreadCommentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 731 var route = repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 732 switch (route) { 733 .repo_issues => |*thread| thread.view = .edit_comment, 734 .repo_patches => |*thread| thread.view = .edit_comment, 735 .repo_discussions => |*thread| thread.view = .edit_comment, 736 else => unreachable, 737 } 738 return route; 739 } 740 741 // build the confirmation route for removing a thread or comment. 742 pub fn repoThreadRemoveRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 743 var route = if (comment_id.len == 0) 744 repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null 745 else 746 repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 747 switch (route) { 748 .repo_issues => |*thread| thread.view = .remove, 749 .repo_patches => |*thread| thread.view = .remove, 750 .repo_discussions => |*thread| thread.view = .remove, 751 else => unreachable, 752 } 753 return route; 754 } 755 756 // build a thread tags route, keeping its url-encoded tag filter. 757 pub fn repoThreadTagsRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8) ?RoutablePage { 758 var route = repoThreadRoute(kind, identity, tag, "") orelse return null; 759 switch (route) { 760 .repo_issues => |*thread| thread.view = .tags, 761 .repo_patches => |*thread| thread.view = .tags, 762 .repo_discussions => |*thread| thread.view = .tags, 763 else => unreachable, 764 } 765 return route; 766 } 767 768 // build a new-thread form route. 769 pub fn repoThreadNewRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 770 var route = repoThreadRoute(kind, identity, "", "") orelse return null; 771 switch (route) { 772 .repo_issues => |*thread| thread.view = .new, 773 .repo_patches => |*thread| thread.view = .new, 774 .repo_discussions => |*thread| thread.view = .new, 775 else => unreachable, 776 } 777 return route; 778 } 779 780 // build the edit form route for a thread. 781 pub fn repoThreadEditRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 782 if (selected.len == 0) return null; 783 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 784 switch (route) { 785 .repo_issues => |*thread| thread.view = .edit, 786 .repo_patches => |*thread| thread.view = .edit, 787 .repo_discussions => |*thread| thread.view = .edit, 788 else => unreachable, 789 } 790 return route; 791 } 792 793 // build a route showing a thread's whole description. 794 pub fn repoThreadDescriptionRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 795 if (selected.len == 0) return null; 796 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 797 switch (route) { 798 .repo_issues => |*thread| thread.view = .description, 799 .repo_patches => |*thread| thread.view = .description, 800 .repo_discussions => |*thread| thread.view = .description, 801 else => unreachable, 802 } 803 return route; 804 } 805 806 // build a `.repo_issues` route showing the conflicts list, rooted at the 807 // issue with hex event id `start` ("" = the first window). 808 pub fn repoIssuesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 809 return .{ .repo_issues = .{ 810 .name = Array(repo_route_max_len).from(identity) orelse return null, 811 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 812 .view = .conflicts, 813 } }; 814 } 815 816 // build a `.repo_issues` route showing the conflict resolve form for the 817 // issue with hex event id `selected`; `theirs` is the comma-separated list 818 // of fields prefilled from their side. 819 pub fn repoIssuesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 820 if (selected.len == 0) return null; 821 return .{ .repo_issues = .{ 822 .name = Array(repo_route_max_len).from(identity) orelse return null, 823 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 824 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 825 .view = .resolve, 826 } }; 827 } 828 829 pub fn repoPatchesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 830 return .{ .repo_patches = .{ 831 .name = Array(repo_route_max_len).from(identity) orelse return null, 832 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 833 .view = .conflicts, 834 } }; 835 } 836 837 pub fn repoPatchesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 838 if (selected.len == 0) return null; 839 return .{ .repo_patches = .{ 840 .name = Array(repo_route_max_len).from(identity) orelse return null, 841 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 842 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 843 .view = .resolve, 844 } }; 845 } 846 847 // an inline, owned array of data. keeping it in the route (rather than a 848 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 849 // in history, and serialized without any arena tracking. 850 pub fn Array(comptime max_len: usize) type { 851 return struct { 852 bytes: [max_len]u8 = undefined, 853 len: u16 = 0, 854 855 pub fn from(s: []const u8) ?Array(max_len) { 856 if (s.len > max_len) return null; 857 var name = Array(max_len){ .len = @intCast(s.len) }; 858 @memcpy(name.bytes[0..s.len], s); 859 return name; 860 } 861 862 pub fn slice(self: *const Array(max_len)) []const u8 { 863 return self.bytes[0..self.len]; 864 } 865 866 // round-trip as a json string instead of an array of numbers. 867 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 868 try jw.write(self.slice()); 869 } 870 871 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 872 const s = try std.json.innerParse([]const u8, allocator, source, options); 873 // ValueTooLong is std.json's error for an over-long field. 874 return from(s) orelse error.ValueTooLong; 875 } 876 }; 877 } 878 879 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 880 return switch (self) { 881 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 882 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 883 .home_settings => "/settings", 884 .home_auth => "/auth", 885 .user_repos => |u| if (u.start == 0) 886 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 887 else 888 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 889 .user_forks => |u| if (u.start == 0) 890 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks", .{u.name.slice()}) 891 else 892 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 893 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 894 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 895 .repo_files => |f| blk: { 896 const prefix = try repoUrlPrefix(arena, f.name.slice()); 897 if (f.ref_kind == null and f.patchrev_id.len == 0 and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 898 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 899 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 900 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 901 if (f.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{f.patchrev_id.slice()}); 902 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 903 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 904 break :blk out.written(); 905 }, 906 .repo_commits => |c| blk: { 907 const prefix = try repoUrlPrefix(arena, c.name.slice()); 908 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 909 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 910 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 911 if (c.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{c.base_oid.slice()}); 912 switch (c.content) { 913 .diff => |d| { 914 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 915 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 916 }, 917 .message => try out.writer.print("/" ++ message_seg, .{}), 918 } 919 break :blk out.written(); 920 }, 921 .repo_diff => |d| blk: { 922 const prefix = try repoUrlPrefix(arena, d.name.slice()); 923 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 924 try out.writer.print("{s}/diff", .{prefix}); 925 if (d.ref_or_oid) |kind| if (d.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), d.value.slice() }); 926 if (d.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{d.patchrev_id.slice()}); 927 if (d.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{d.base_oid.slice()}); 928 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 929 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 930 break :blk out.written(); 931 }, 932 .repo_refs => |r| blk: { 933 const prefix = try repoUrlPrefix(arena, r.name.slice()); 934 break :blk if (r.from.len == 0) 935 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 936 else 937 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 938 }, 939 .repo_issues => |i| blk: { 940 const prefix = try repoUrlPrefix(arena, i.name.slice()); 941 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 942 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 943 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 944 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 945 else 946 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 947 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 948 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 949 else 950 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 951 if (i.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, i.selected.slice(), i.comment.slice() }); 952 if (i.view == .remove) break :blk if (i.comment.len == 0) 953 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 954 else 955 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 956 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 957 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/conflicts", .{prefix}) 958 else 959 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 960 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 961 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 962 else 963 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comment.slice(), i.comments_start }); 964 if (i.selected.len != 0) break :blk if (i.comments_start == 0) 965 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }) 966 else 967 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comments_start }); 968 break :blk if (i.tag.len == 0) 969 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, @tagName(i.view) }) 970 else 971 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(i.view), i.tag.slice() }); 972 }, 973 .repo_patches => |p| blk: { 974 const prefix = try repoUrlPrefix(arena, p.name.slice()); 975 if (p.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 976 if (p.view == .publish) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 977 if (p.view == .merge) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/merge", .{ prefix, p.selected.slice() }); 978 if (p.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 979 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 980 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 981 else 982 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 983 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 984 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 985 else 986 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 987 if (p.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, p.selected.slice(), p.comment.slice() }); 988 if (p.view == .remove) break :blk if (p.comment.len == 0) 989 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 990 else 991 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 992 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 993 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/conflicts", .{prefix}) 994 else 995 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 996 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 997 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 998 else 999 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comment.slice(), p.comments_start }); 1000 if (p.selected.len != 0) break :blk if (p.comments_start == 0) 1001 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }) 1002 else 1003 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comments_start }); 1004 break :blk if (p.tag.len == 0) 1005 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}", .{ prefix, @tagName(p.view) }) 1006 else 1007 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(p.view), p.tag.slice() }); 1008 }, 1009 .repo_discussions => |t| blk: { 1010 const prefix = try repoUrlPrefix(arena, t.name.slice()); 1011 if (t.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 1012 if (t.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 1013 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 1014 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 1015 else 1016 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 1017 if (t.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, t.selected.slice(), t.comment.slice() }); 1018 if (t.view == .remove) break :blk if (t.comment.len == 0) 1019 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 1020 else 1021 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 1022 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 1023 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 1024 else 1025 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comment.slice(), t.comments_start }); 1026 if (t.selected.len != 0) break :blk if (t.comments_start == 0) 1027 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }) 1028 else 1029 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comments_start }); 1030 break :blk if (t.tag.len == 0) 1031 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}", .{ prefix, @tagName(t.view) }) 1032 else 1033 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(t.view), t.tag.slice() }); 1034 }, 1035 .repo_events => |e| blk: { 1036 const prefix = try repoUrlPrefix(arena, e.name.slice()); 1037 break :blk if (e.kind) |kind| 1038 try std.fmt.allocPrint(arena.allocator(), "{s}/event:{s}/kind:{s}", .{ prefix, e.selected.slice(), @tagName(kind) }) 1039 else 1040 try std.fmt.allocPrint(arena.allocator(), "{s}/events/{s}", .{ prefix, @tagName(e.view) }); 1041 }, 1042 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 1043 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 1044 .fork_patch => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}", .{ f.name.slice(), f.id.slice() }), 1045 .fork_diff => |d| blk: { 1046 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1047 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/diff", .{ d.fork.name.slice(), d.fork.id.slice() }); 1048 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1049 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1050 break :blk out.written(); 1051 }, 1052 .fork_files => |f| blk: { 1053 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1054 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 1055 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 1056 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1057 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1058 break :blk out.written(); 1059 }, 1060 .fork_commits => |c| blk: { 1061 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1062 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 1063 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 1064 switch (c.content) { 1065 .diff => |d| { 1066 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1067 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1068 }, 1069 .message => try out.writer.print("/" ++ message_seg, .{}), 1070 } 1071 break :blk out.written(); 1072 }, 1073 .fork_settings => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/settings", .{ f.name.slice(), f.id.slice() }), 1074 .fork_auth => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/auth", .{ f.name.slice(), f.id.slice() }), 1075 }; 1076 } 1077 1078 pub fn fromUrl(path: []const u8) ?RoutablePage { 1079 if (std.mem.eql(u8, path, "/")) return default; 1080 if (path.len < 2 or path[0] != '/') return null; 1081 var segments = std.mem.splitScalar(u8, path[1..], '/'); 1082 const first = segments.next() orelse return null; 1083 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 1084 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 1085 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 1086 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 1087 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 1088 if (std.mem.eql(u8, first, "user")) { 1089 const name = segments.next() orelse return null; 1090 if (name.len == 0) return null; 1091 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 1092 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 1093 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1094 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1095 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 1096 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 1097 return null; // unknown sub-path 1098 } 1099 // "fork/<username>/<reponame>/patch:<id>[/files|/commits]"; the 1100 // patch branch is implicit, while object ids and view-specific params 1101 // use the same tails as the repo files and commits routes. 1102 if (std.mem.eql(u8, first, "fork")) { 1103 const rest = segments.rest(); 1104 const owner = segments.next() orelse return null; 1105 const repo_name = segments.next() orelse return null; 1106 if (owner.len == 0 or repo_name.len == 0) return null; 1107 const identity = rest[0 .. owner.len + 1 + repo_name.len]; 1108 const patch = segments.next() orelse return null; 1109 if (!std.mem.startsWith(u8, patch, patch_seg)) return null; 1110 const id = patch[patch_seg.len..]; 1111 if (id.len != evt.event_id_size * 2) return null; 1112 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1113 var params = Params{}; 1114 if (std.mem.eql(u8, tab, "diff")) { 1115 params.scanPairs(&segments) catch return null; 1116 if (!params.only(&.{.start})) return null; 1117 return forkDiffRoute(identity, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1118 } 1119 if (std.mem.eql(u8, tab, files_seg)) { 1120 params.scanPairs(&segments) catch return null; 1121 if (!params.only(&.{ .line, .object })) return null; 1122 const line = params.line() orelse return null; 1123 const ref = params.ref() catch return null; 1124 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1125 const file_path = pathValue(segments.rest()) orelse return null; 1126 return forkFilesRoute(identity, id, oid, file_path, line); 1127 } 1128 if (std.mem.eql(u8, tab, commits_seg)) { 1129 params.scanPairs(&segments) catch return null; 1130 if (!params.only(&.{ .start, .object })) return null; 1131 const start = params.start() orelse return null; 1132 const ref = params.ref() catch return null; 1133 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1134 if (std.mem.eql(u8, segments.rest(), message_seg)) 1135 return if (start == 0 and oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1136 const file_path = pathValue(segments.rest()) orelse return null; 1137 return forkCommitsRoute(identity, id, oid, start, file_path); 1138 } 1139 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) forkSettingsRoute(identity, id) else null; 1140 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) forkAuthRoute(identity, id) else null; 1141 return null; 1142 } 1143 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 1144 // files root 1145 if (std.mem.eql(u8, first, "repo")) { 1146 const rest = segments.rest(); // "username/reponame[/...]" 1147 const owner = segments.next() orelse return null; 1148 const repo_name = segments.next() orelse return null; 1149 // reject an empty username or reponame 1150 if (owner.len == 0 or repo_name.len == 0) return null; 1151 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 1152 if (segments.peek() != null) { 1153 return repoSubRoute(pair, segments.rest()); 1154 } 1155 return repoFilesRoute(pair, null, "", "", 0); 1156 } 1157 return null; 1158 } 1159 1160 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 1161 // "/" is the files root and the sub-paths follow directly. 1162 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1163 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1164 if (path.len < 2 or path[0] != '/') return null; 1165 return repoSubRoute("", path[1..]); 1166 } 1167 1168 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 1169 // when the identity is elided. 1170 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1171 if (identity.len == 0) return ""; 1172 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 1173 } 1174 1175 // true when the page builds at its content height on the web, letting the 1176 // browser scroll the whole page, rather than being pinned to the viewport 1177 pub fn fullHeight(self: RoutablePage) bool { 1178 return switch (self) { 1179 .repo_issues => |i| i.view == .resolve, 1180 .repo_patches => |p| p.view == .resolve, 1181 else => false, 1182 }; 1183 } 1184 1185 // the "owner/name" a repo route carries, null for any other route. the 1186 // result borrows the route, so it must outlive the slice. 1187 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1188 return switch (self.*) { 1189 .repo_files => |*f| f.name.slice(), 1190 .repo_commits => |*c| c.name.slice(), 1191 .repo_diff => |*d| d.name.slice(), 1192 .repo_refs => |*r| r.name.slice(), 1193 .repo_issues => |*i| i.name.slice(), 1194 .repo_patches => |*p| p.name.slice(), 1195 .repo_discussions => |*t| t.name.slice(), 1196 .repo_events => |*e| e.name.slice(), 1197 .repo_settings, .repo_auth => |*name| name.slice(), 1198 else => null, 1199 }; 1200 } 1201 1202 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1203 return switch (self.*) { 1204 .fork_patch => |*f| f, 1205 .fork_diff => |*d| &d.fork, 1206 .fork_files => |*f| &f.fork, 1207 .fork_commits => |*f| &f.fork, 1208 .fork_settings => |*f| f, 1209 .fork_auth => |*f| f, 1210 else => null, 1211 }; 1212 } 1213 1214 // the route this route's page lands on, with no tab or params: the users 1215 // list, a user's repos, or a repo's files root. 1216 pub fn pageRoot(self: RoutablePage) RoutablePage { 1217 return switch (self.parent()) { 1218 .home => .default, 1219 .user => switch (self) { 1220 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1221 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1222 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 1223 else => self, 1224 }, 1225 // every repo route stores the same identity, so it always fits 1226 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1227 .fork => blk: { 1228 const f = self.forkRoute() orelse break :blk self; 1229 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1230 }, 1231 }; 1232 } 1233 1234 pub fn parent(self: RoutablePage) PageKind { 1235 return switch (self) { 1236 .home_users, .home_repos, .home_settings, .home_auth => .home, 1237 .user_repos, .user_forks, .user_settings, .user_auth => .user, 1238 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .repo, 1239 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_settings, .fork_auth => .fork, 1240 }; 1241 } 1242 1243 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1244 // absent, null when malformed) 1245 fn pathValue(rest: []const u8) ?[]const u8 { 1246 if (rest.len == 0) return ""; 1247 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1248 const dir = rest[path_seg.len..]; 1249 return if (dir.len == 0) null else dir; 1250 } 1251 1252 // a list route's tail: at most a "start:<n>" param, null on anything else 1253 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1254 var params = Params{}; 1255 const word = params.scanTail(segments) catch return null; 1256 if (word != null or !params.only(&.{.start})) return null; 1257 return params.start(); 1258 } 1259 1260 // parse the sub-path after a repo route's identity ("" for a local 1261 // route): a tab keyword followed by "key:value" params in any order, plus 1262 // the files tab's trailing "path:<dir>". 1263 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1264 var segments = std.mem.splitScalar(u8, sub, '/'); 1265 const tab = segments.next() orelse return null; 1266 var params = Params{}; 1267 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null } else null; 1268 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) .{ .repo_auth = Array(repo_route_max_len).from(pair) orelse return null } else null; 1269 if (std.mem.eql(u8, tab, "refs")) { 1270 const word = params.scanTail(&segments) catch return null; 1271 if (word != null or !params.only(&.{ .branch, .tag })) return null; 1272 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 1273 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 1274 return repoRefsRoute(pair, kind, ref.value); 1275 } 1276 if (std.mem.startsWith(u8, tab, issue_seg)) { 1277 const issue_id = tab[issue_seg.len..]; 1278 if (issue_id.len == 0) return null; 1279 if (segments.peek()) |tail| { 1280 if (std.mem.startsWith(u8, tail, comment_seg)) { 1281 _ = segments.next(); 1282 const comment_id = tail[comment_seg.len..]; 1283 if (comment_id.len == 0) return null; 1284 const word = params.scanTail(&segments) catch return null; 1285 if (word) |last| { 1286 if (!params.only(&.{})) return null; 1287 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 1288 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1289 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1290 return null; 1291 } 1292 if (!params.only(&.{.start})) return null; 1293 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 1294 } 1295 } 1296 const word = params.scanTail(&segments) catch return null; 1297 if (word) |tail| { 1298 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 1299 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1300 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1301 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1302 if (std.mem.eql(u8, tail, "resolve")) { 1303 if (!params.only(&.{.theirs})) return null; 1304 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1305 } 1306 return null; 1307 } 1308 if (!params.only(&.{.start})) return null; 1309 return repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null); 1310 } 1311 if (std.mem.startsWith(u8, tab, patch_seg)) { 1312 const patch_id = tab[patch_seg.len..]; 1313 if (patch_id.len == 0) return null; 1314 if (segments.peek()) |tail| { 1315 if (std.mem.startsWith(u8, tail, comment_seg)) { 1316 _ = segments.next(); 1317 const comment_id = tail[comment_seg.len..]; 1318 if (comment_id.len == 0) return null; 1319 const word = params.scanTail(&segments) catch return null; 1320 if (word) |last| { 1321 if (!params.only(&.{})) return null; 1322 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1323 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1324 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1325 return null; 1326 } 1327 if (!params.only(&.{.start})) return null; 1328 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1329 } 1330 } 1331 const word = params.scanTail(&segments) catch return null; 1332 if (word) |tail| { 1333 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1334 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1335 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1336 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1337 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1338 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1339 if (std.mem.eql(u8, tail, "resolve")) { 1340 if (!params.only(&.{.theirs})) return null; 1341 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1342 } 1343 return null; 1344 } 1345 if (!params.only(&.{.start})) return null; 1346 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1347 } 1348 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1349 const discussion_id = tab[discuss_seg.len..]; 1350 if (discussion_id.len == 0) return null; 1351 if (segments.peek()) |tail| { 1352 if (std.mem.startsWith(u8, tail, comment_seg)) { 1353 _ = segments.next(); 1354 const comment_id = tail[comment_seg.len..]; 1355 if (comment_id.len == 0) return null; 1356 const word = params.scanTail(&segments) catch return null; 1357 if (word) |last| { 1358 if (!params.only(&.{})) return null; 1359 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1360 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1361 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1362 return null; 1363 } 1364 if (!params.only(&.{.start})) return null; 1365 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1366 } 1367 } 1368 const word = params.scanTail(&segments) catch return null; 1369 if (word) |tail| { 1370 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1371 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1372 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1373 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1374 return null; 1375 } 1376 if (!params.only(&.{.start})) return null; 1377 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1378 } 1379 if (std.mem.eql(u8, tab, "issues")) { 1380 // the word is a list view; a filter with neither is never emitted. 1381 const word = params.scanTail(&segments) catch return null; 1382 const tag_value = params.values.get(.tag) orelse ""; 1383 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1384 // the conflicts list windows by an issue id, not a number, so it 1385 // reads the start param raw 1386 if (std.mem.eql(u8, w, "conflicts")) { 1387 if (!params.only(&.{.start})) return null; 1388 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1389 } 1390 if (!params.only(&.{.tag})) return null; 1391 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1392 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1393 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1394 return null; 1395 } 1396 if (std.mem.eql(u8, tab, "patches")) { 1397 const word = params.scanTail(&segments) catch return null; 1398 const tag_value = params.values.get(.tag) orelse ""; 1399 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1400 if (std.mem.eql(u8, view, "conflicts")) { 1401 if (!params.only(&.{.start})) return null; 1402 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1403 } 1404 if (!params.only(&.{.tag})) return null; 1405 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1406 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1407 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1408 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1409 return null; 1410 } 1411 if (std.mem.eql(u8, tab, "discussions")) { 1412 const word = params.scanTail(&segments) catch return null; 1413 const tag_value = params.values.get(.tag) orelse ""; 1414 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1415 if (!params.only(&.{.tag})) return null; 1416 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1417 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1418 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1419 return null; 1420 } 1421 if (std.mem.eql(u8, tab, "events")) { 1422 const view = if (segments.next()) |word| 1423 std.meta.stringToEnum(EventsView, word) orelse return null 1424 else 1425 .active; 1426 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1427 } 1428 if (std.mem.startsWith(u8, tab, "event:")) { 1429 const event_id = tab["event:".len..]; 1430 const kind_segment = segments.next() orelse return null; 1431 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1432 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1433 if (event_id.len == 0 or segments.next() != null) return null; 1434 return repoEventsRoute(pair, .active, kind, event_id); 1435 } 1436 if (std.mem.eql(u8, tab, files_seg)) { 1437 params.scanPairs(&segments) catch return null; 1438 if (params.values.get(.patchrev)) |id| { 1439 if (!params.only(&.{ .line, .patchrev })) return null; 1440 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1441 } 1442 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 1443 const line = params.line() orelse return null; 1444 const dir = pathValue(segments.rest()) orelse return null; 1445 const ref = (params.ref() catch return null) orelse 1446 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 1447 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 1448 } 1449 if (std.mem.eql(u8, tab, commits_seg)) { 1450 params.scanPairs(&segments) catch return null; 1451 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1452 const base_oid = params.values.get(.base) orelse ""; 1453 const start = params.start() orelse return null; 1454 const ref = (params.ref() catch return null) orelse { 1455 const file = pathValue(segments.rest()) orelse return null; 1456 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "", base_oid) else null; 1457 }; 1458 // the message page has no diff window or file filter 1459 if (std.mem.eql(u8, segments.rest(), message_seg)) 1460 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) else null; 1461 const file = pathValue(segments.rest()) orelse return null; 1462 return repoCommitsRoute(pair, ref.kind, ref.value, start, file, base_oid); 1463 } 1464 if (std.mem.eql(u8, tab, "diff")) { 1465 params.scanPairs(&segments) catch return null; 1466 if (params.values.get(.patchrev)) |id| { 1467 if (!params.only(&.{ .start, .patchrev })) return null; 1468 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1469 } 1470 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1471 const base_oid = params.values.get(.base) orelse return null; 1472 const start = params.start() orelse return null; 1473 const ref = params.ref() catch return null; 1474 const file = pathValue(segments.rest()) orelse return null; 1475 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1476 } 1477 return null; // unknown sub-path 1478 } 1479 1480 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1481 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1482 return switch (a) { 1483 .home_users => |a_start| a_start == b.home_users, 1484 .home_repos => |a_start| a_start == b.home_repos, 1485 .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, 1486 .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, 1487 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1488 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1489 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1490 a_f.ref_kind == b.repo_files.ref_kind and 1491 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1492 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1493 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1494 a_f.line == b.repo_files.line, 1495 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1496 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1497 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1498 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1499 switch (a_c.content) { 1500 .diff => |a_d| switch (b.repo_commits.content) { 1501 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1502 .message => false, 1503 }, 1504 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1505 }, 1506 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1507 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1508 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1509 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1510 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1511 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1512 .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()), 1513 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1514 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1515 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1516 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1517 a_i.view == b.repo_issues.view and 1518 a_i.comments_start == b.repo_issues.comments_start and 1519 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1520 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1521 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1522 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1523 a_t.view == b.repo_discussions.view and 1524 a_t.comments_start == b.repo_discussions.comments_start and 1525 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1526 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1527 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1528 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1529 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1530 a_p.view == b.repo_patches.view and 1531 a_p.comments_start == b.repo_patches.comments_start and 1532 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1533 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1534 a_e.view == b.repo_events.view and 1535 a_e.kind == b.repo_events.kind and 1536 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1537 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1538 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1539 .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()), 1540 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1541 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1542 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1543 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1544 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1545 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1546 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1547 a_f.line == b.fork_files.line, 1548 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1549 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1550 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1551 switch (a_c.content) { 1552 .diff => |a_d| switch (b.fork_commits.content) { 1553 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1554 .message => false, 1555 }, 1556 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1557 }, 1558 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1559 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1560 else => true, 1561 }; 1562 } 1563 1564 // true when following a link from `b` to `a` (both repo routes) should reload 1565 // rather than stay in page 1566 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1567 if (a.parent() != .repo or b.parent() != .repo) return false; 1568 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1569 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1570 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1571 return !a.eql(b); 1572 } 1573 1574 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1575 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1576 } 1577 1578 // true when `a` and `b` are the same user paginated to a different repos 1579 // window. switching between a user's tabs is in-page, so 1580 // only a changed `start` on the repos list navigates. 1581 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1582 return switch (a) { 1583 .user_repos => |aa| switch (b) { 1584 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1585 else => false, 1586 }, 1587 .user_forks => |aa| switch (b) { 1588 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1589 else => false, 1590 }, 1591 else => false, 1592 }; 1593 } 1594 1595 // true when `a` and `b` are the same home list tab paginated to a different 1596 // window. switching between the users/repos tabs is in-page (the home page 1597 // holds both lists), so only a changed `after` on the same tab navigates. 1598 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1599 return switch (a) { 1600 .home_users => |aa| switch (b) { 1601 .home_users => |bb| aa != bb, 1602 else => false, 1603 }, 1604 .home_repos => |aa| switch (b) { 1605 .home_repos => |bb| aa != bb, 1606 else => false, 1607 }, 1608 else => false, 1609 }; 1610 } 1611 }; 1612 1613 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1614 fn isUnreserved(c: u8) bool { 1615 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1616 } 1617 1618 // percent-encode a ref name for use as a single url path segment. 1619 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1620 var out: std.Io.Writer.Allocating = .init(aa); 1621 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1622 return out.written(); 1623 } 1624 1625 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1626 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1627 return struct { 1628 pub const hex_len = hash.hexLen(repo_opts.hash); 1629 1630 // the concrete ref/oid (a null request resolves to the default branch) 1631 ref_or_oid: RoutablePage.RefOrOid, 1632 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1633 // the route layer — which splits on '/' — stores them encoded). duped 1634 // into `aa`. 1635 value: []const u8, 1636 // the commit oid it points at 1637 oid: [hex_len]u8, 1638 1639 const Self = @This(); 1640 1641 // resolve a requested ref/oid (null = the repo's default branch) to a 1642 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1643 // the url-encoded form (as it appears in the route). null when it doesn't 1644 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1645 pub fn init( 1646 repo: *rp.Repo(repo_kind, repo_opts), 1647 io: std.Io, 1648 aa: std.mem.Allocator, 1649 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1650 requested_value: []const u8, 1651 ) !?Self { 1652 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1653 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1654 var value: []const u8 = if (requested_ref_or_oid == null) 1655 requested_value 1656 else 1657 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1658 // no ref named: fall back to HEAD's branch (or its oid when detached). 1659 if (requested_ref_or_oid == null) { 1660 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1661 if (repo.head(io, &head_buf)) |head| switch (head) { 1662 .ref => |r| { 1663 ref_or_oid = .branch; 1664 value = r.name; 1665 }, 1666 .oid => |o| { 1667 ref_or_oid = .object; 1668 value = o; 1669 }, 1670 } else |_| {} 1671 } 1672 1673 var oid: [hex_len]u8 = undefined; 1674 switch (ref_or_oid) { 1675 .object => { 1676 if (value.len != hex_len) return null; 1677 @memcpy(&oid, value); 1678 }, 1679 .branch, .tag => { 1680 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1681 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1682 }, 1683 } 1684 // store url-encoded so the route layer can hold the value verbatim. 1685 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1686 } 1687 }; 1688 } 1689 1690 // where a repo page reads its on-disk repo from 1691 pub const RepoSource = struct { 1692 path: []const u8, 1693 repo_kind: rp.RepoKind, 1694 // the user's global git config, so local mode picks up their identity and 1695 // their signing and ssh settings. null on the server paths, which must not 1696 // read whoever runs the server. 1697 global_config_path: ?[]const u8 = null, 1698 1699 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1700 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1701 } 1702 1703 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 1704 if (!xit.ref.validateName(branch)) return false; 1705 return switch (self.repo_kind) { 1706 inline else => |repo_kind| blk: { 1707 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1708 defer any_repo.deinit(io, allocator); 1709 break :blk switch (any_repo) { 1710 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 1711 }; 1712 }, 1713 }; 1714 } 1715 1716 // whether the actor may change the event `id` names 1717 pub fn canModify( 1718 self: RepoSource, 1719 io: std.Io, 1720 allocator: std.mem.Allocator, 1721 actor: Actor, 1722 kind: evt.EventKind, 1723 id: *const [evt.event_id_size]u8, 1724 ) !bool { 1725 // a role that may change an event with no author needs no read 1726 if (actor.role.canModify(actor.author.email, null)) return true; 1727 1728 var arena = std.heap.ArenaAllocator.init(allocator); 1729 defer arena.deinit(); 1730 return switch (self.repo_kind) { 1731 inline else => |repo_kind| blk: { 1732 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1733 defer any_repo.deinit(io, allocator); 1734 break :blk switch (any_repo) { 1735 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 1736 }; 1737 }, 1738 }; 1739 } 1740 }; 1741 1742 // who a write acts as and the role they hold in its repo 1743 pub const Actor = struct { 1744 author: evt.CommitAuthor, 1745 // local mode has no accounts, so it has no user id 1746 user_id: ?[evt.event_id_size]u8 = null, 1747 role: evt.Repo.Role, 1748 }; 1749 1750 pub const Authorization = union(enum) { 1751 actor: Actor, 1752 login_required, 1753 repo_not_found, 1754 forbidden, 1755 }; 1756 1757 // whether the user may write to the repo `identity` names with at least 1758 // `min_role` 1759 pub fn authorizeUser( 1760 moment: evt.AdminDB.HashMap(.read_only), 1761 arena: *std.heap.ArenaAllocator, 1762 user_id: [evt.event_id_size]u8, 1763 identity: []const u8, 1764 min_role: evt.Repo.Role, 1765 ) !Authorization { 1766 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return .login_required; 1767 // a removed account's open sessions stop writing 1768 if (user.removed) return .login_required; 1769 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 1770 const repo = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, owner_repo.owner, owner_repo.name)) orelse return .repo_not_found; 1771 1772 const role = evt.Repo.roleOf(repo.repo, user_id); 1773 if (!role.atLeast(min_role)) return .forbidden; 1774 return .{ .actor = .{ 1775 .author = .{ .name = user.event.name, .email = user.event.email }, 1776 .user_id = user_id, 1777 .role = role, 1778 } }; 1779 } 1780 1781 // events created in local mode have no account behind them, so the commit 1782 // author comes from the repo's config, which the global config feeds into. 1783 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1784 1785 pub fn localAuthor( 1786 src: RepoSource, 1787 io: std.Io, 1788 allocator: std.mem.Allocator, 1789 arena: *std.heap.ArenaAllocator, 1790 ) !evt.CommitAuthor { 1791 switch (src.repo_kind) { 1792 inline else => |repo_kind| { 1793 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1794 defer any_repo.deinit(io, allocator); 1795 1796 switch (any_repo) { 1797 inline else => |*repo| { 1798 var config = try repo.listConfig(io, allocator); 1799 defer config.deinit(); 1800 1801 const user = config.sections.get("user") orelse return local_author_fallback; 1802 const name = user.get("name") orelse return local_author_fallback; 1803 const email = user.get("email") orelse return local_author_fallback; 1804 return .{ 1805 .name = try arena.allocator().dupe(u8, name), 1806 .email = try arena.allocator().dupe(u8, email), 1807 }; 1808 }, 1809 } 1810 }, 1811 } 1812 } 1813 1814 // per-connection mutable state. each SSH session / web session / local TUI 1815 // run gets its own. 1816 pub const Session = struct { 1817 data: Data = .{}, 1818 // session-lifetime allocations: login/user_id, persisted prefs, queued 1819 // actions. lives as long as the connection. 1820 arena: *std.heap.ArenaAllocator, 1821 // current page's allocations: page data and the page-scoped strings widgets 1822 // build (form actions, links). owned by Nav, swapped on each navigation, so 1823 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1824 // where a render is one-shot, this points at the same arena as `arena`. 1825 page_arena: *std.heap.ArenaAllocator, 1826 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1827 // filesystem io and the path to <server>/repos, for opening on-disk repos 1828 // during page construction. both null on wasm, which has no filesystem and 1829 // rebuilds pages from the serialized snapshot rather than from disk. 1830 io: ?std.Io = null, 1831 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1832 repos_dir: ?[]const u8 = null, 1833 // the single on-disk repo this session views, when running in local mode 1834 // (haxy invoked with no arguments inside a repo). null on the server paths, 1835 // which resolve repos from the admin db instead. 1836 local: ?RepoSource = null, 1837 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1838 // focus id -> the live TextInput, refreshed each frame by the views that own 1839 // inputs. web/wasm form handling looks widgets up here by focus id. 1840 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 1841 back: enum { unavailable, available, requested } = .unavailable, 1842 refresh_requested: bool = false, // set by input (ctrl+r) 1843 // a requested forward navigation. set this (via navigate) to move to a new 1844 // page; Nav.sync builds it and then copies it into current_page, clearing 1845 // this back to null. setting current_page directly only updates the url and 1846 // does not navigate. 1847 next_page: ?RoutablePage = null, 1848 is_terminal: bool = false, // true on remote SSH and local TUI 1849 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1850 // url. null on the web itself (no footer there). 1851 web_port: ?u16 = null, 1852 // a host operation requested by a widget 1853 host_request: ?HostRequest = null, 1854 quit_requested: bool = false, 1855 1856 pub const HostRequest = union(enum) { 1857 show_copyable_text: []const u8, 1858 sync_events, 1859 }; 1860 1861 pub const FormFeedback = union(enum) { 1862 pub const LoginFailure = enum { unknown_user, wrong_password }; 1863 pub const ThreadFailure = enum { required_title }; 1864 pub const PatchFailure = enum { 1865 required_title, 1866 invalid_target_branch, 1867 invalid_source_branch, 1868 same_branch, 1869 unrelated_branches, 1870 1871 pub fn fromError(err: anyerror) ?PatchFailure { 1872 return switch (err) { 1873 error.InvalidTargetBranch => .invalid_target_branch, 1874 error.InvalidSourceBranch => .invalid_source_branch, 1875 error.SameBranch => .same_branch, 1876 error.UnrelatedBranches => .unrelated_branches, 1877 else => null, 1878 }; 1879 } 1880 }; 1881 1882 login: struct { 1883 failure: LoginFailure, 1884 username: []const u8, 1885 }, 1886 issue: struct { 1887 failure: ThreadFailure, 1888 fields: ?struct { 1889 title: []const u8, 1890 tags: []const u8, 1891 description: []const u8, 1892 } = null, 1893 }, 1894 patch: struct { 1895 failure: PatchFailure, 1896 fields: ?struct { 1897 title: []const u8, 1898 tags: []const u8, 1899 description: []const u8, 1900 target_branch: []const u8, 1901 source_branch: []const u8 = "", 1902 source_kind: Repo.Patches.Source.Kind = .fork, 1903 } = null, 1904 }, 1905 discussion: struct { 1906 failure: ThreadFailure, 1907 fields: ?struct { 1908 title: []const u8, 1909 tags: []const u8, 1910 description: []const u8, 1911 } = null, 1912 }, 1913 }; 1914 1915 const Self = @This(); 1916 1917 // serializable data sent down to web client 1918 pub const Data = struct { 1919 user_id: ?[]const u8 = null, 1920 // the logged-in user's name, for the views that show it 1921 user_name: ?[]const u8 = null, 1922 // a failed form retained long enough to render it for correction 1923 form_feedback: ?FormFeedback = null, 1924 // a transient local event-sync error 1925 sync_failure: ?[]const u8 = null, 1926 current_page: RoutablePage = .default, 1927 // whether to render the ANSI art backdrop 1928 enable_ansi: bool = true, 1929 // the selected ANSI art. the server serializes one image for WASM so 1930 // the browser does not need the whole build-generated collection. 1931 ansi_art: []const u8 = "", 1932 // the bound git-service ports. absent in local mode, where remote urls 1933 // are not shown. 1934 git_http_port: ?u16 = null, 1935 git_ssh_port: ?u16 = null, 1936 git_ssh_prefix: []const u8 = "", 1937 // whether this session views a local repo or the server. unlike `local` 1938 // (the host-side filesystem source) this travels in the snapshot, so 1939 // the wasm side also parses elided link urls and hides the multi-user 1940 // chrome. 1941 host_kind: evt.HostKind = .server, 1942 }; 1943 1944 // a user-initiated state change. widgets enqueue these on the session during 1945 // input instead of mutating state or touching the DB themselves; the host 1946 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1947 // side effects out of the widget tree and gives every render path one place to 1948 // turn a UI action into a state change + event. 1949 pub const Action = union(enum) { 1950 toggle_ansi, 1951 }; 1952 1953 pub fn init( 1954 arena: *std.heap.ArenaAllocator, 1955 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1956 data: Data, 1957 ) !Self { 1958 var session = Self{ 1959 .data = data, 1960 .arena = arena, 1961 // until a host swaps in a page-scoped arena (Nav does this), page 1962 // allocations land in the session arena. on the web path that's the 1963 // intended behavior, since the whole arena is per-request. 1964 .page_arena = arena, 1965 .admin_repo = repo, 1966 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1967 }; 1968 try session.loadUser(); 1969 return session; 1970 } 1971 1972 // load the logged-in user's name and persisted preferences from the db 1973 pub fn loadUser(self: *Self) !void { 1974 const user_id = self.data.user_id orelse return; 1975 const moment = self.haxy_moment orelse return; 1976 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1977 self.data.user_name = user.event.name; 1978 self.data.enable_ansi = user.event.enable_ansi; 1979 } 1980 } 1981 1982 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 1983 const bytes = self.data.user_id orelse return null; 1984 if (bytes.len != evt.event_id_size) return null; 1985 var id: [evt.event_id_size]u8 = undefined; 1986 @memcpy(&id, bytes); 1987 return id; 1988 } 1989 1990 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 1991 const feedback = self.data.form_feedback orelse return null; 1992 if (std.meta.activeTag(feedback) != tag) return null; 1993 return @field(feedback, @tagName(tag)); 1994 } 1995 1996 pub fn clearFormFeedback(self: *Self) void { 1997 self.data.form_feedback = null; 1998 } 1999 2000 // the actor for a write to the repo `identity` names, or null when the 2001 // session may not write there with at least `min_role`. local mode always 2002 // writes. 2003 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2004 if (self.data.host_kind == .local) { 2005 const author = blk: { 2006 const src = self.local orelse break :blk local_author_fallback; 2007 const io = self.io orelse break :blk local_author_fallback; 2008 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2009 }; 2010 return .{ .author = author, .role = .owner }; 2011 } 2012 const user_id = self.userId() orelse return null; 2013 // the session's moment predates this input, so read the roles afresh 2014 const admin_repo = self.admin_repo orelse return null; 2015 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2016 return switch (try authorizeUser(moment, self.page_arena, user_id, identity, min_role)) { 2017 .actor => |actor| actor, 2018 .login_required, .repo_not_found, .forbidden => null, 2019 }; 2020 } 2021 2022 // queue an action for the host to drain this frame. 2023 pub fn push(self: *Self, action: Action) !void { 2024 try self.pending.append(self.arena.allocator(), action); 2025 } 2026 2027 // apply a single action's in-memory effect only (no persistence). 2028 fn apply(self: *Self, action: Action) void { 2029 switch (action) { 2030 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2031 } 2032 } 2033 2034 // drain the session's queued actions by applying them to the session. 2035 // used on the wasm path, which has no repo to persist to. 2036 pub fn applyPending(self: *Self) void { 2037 for (self.pending.items) |action| self.apply(action); 2038 self.pending.clearRetainingCapacity(); 2039 } 2040 2041 // drain the session's queued actions by applying them to the session and 2042 // writing them to the db 2043 pub fn applyAndWritePending( 2044 self: *Self, 2045 io: std.Io, 2046 allocator: std.mem.Allocator, 2047 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2048 ) !void { 2049 defer self.pending.clearRetainingCapacity(); 2050 for (self.pending.items) |action| { 2051 self.apply(action); 2052 switch (action) { 2053 .toggle_ansi => if (self.data.user_id) |user_id| { 2054 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 2055 }, 2056 } 2057 } 2058 } 2059 2060 // reload the moment from the admin repo and re-read user preferences 2061 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2062 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2063 self.haxy_moment = moment; 2064 2065 const user_id = self.data.user_id orelse return; 2066 var arena = std.heap.ArenaAllocator.init(allocator); 2067 defer arena.deinit(); 2068 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 2069 self.data.enable_ansi = user.event.enable_ansi; 2070 } 2071 } 2072 2073 // request a forward navigation to `route`; the host consumes next_page 2074 // (Nav.sync on the terminal, the wasm tick on the web). 2075 pub fn navigate(self: *Session, route: RoutablePage) !void { 2076 self.clearFormFeedback(); 2077 self.next_page = route; 2078 } 2079 }; 2080 2081 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2082 var nav = try Nav.init(allocator, session); 2083 defer nav.deinit(allocator); 2084 2085 var terminal = try term.Terminal.init(io, allocator); 2086 var terminal_live = true; 2087 defer if (terminal_live) terminal.deinit(io); 2088 2089 // set term as active so it will be properly cooked 2090 // when a panic/segfault happens 2091 term.setActive(&terminal); 2092 defer term.setActive(null); 2093 2094 while (!terminal.shouldQuit()) { 2095 const grid_changed = try terminal.render(&nav.root); 2096 2097 // process any inputs. 2098 // 2099 // if the grid didn't change, then first do a blocking 2100 // read, so the thread will sleep until further input. 2101 // after that, all remaining reads are non-blocking so 2102 // we can process the rest of the queued inputs. 2103 // 2104 // if the grid *did* change, then only do non-blocking 2105 // reads. we do not want to sleep the thread because 2106 // there may be an animation that requires more looping. 2107 var blocking = !grid_changed; 2108 while (try terminal.readKey(io, blocking)) |key| { 2109 blocking = false; 2110 try inputKey(allocator, &nav.root, key, session); 2111 } 2112 2113 if (session.host_request) |request| { 2114 session.host_request = null; 2115 switch (request) { 2116 .show_copyable_text => |copyable_text| { 2117 term.setActive(null); 2118 terminal.deinit(io); 2119 terminal_live = false; 2120 showCopyableText(io, copyable_text) catch |show_err| { 2121 terminal = try term.Terminal.init(io, allocator); 2122 terminal_live = true; 2123 term.setActive(&terminal); 2124 return show_err; 2125 }; 2126 terminal = try term.Terminal.init(io, allocator); 2127 terminal_live = true; 2128 term.setActive(&terminal); 2129 }, 2130 .sync_events => { 2131 try nav.root.build(allocator, .{ 2132 .min_size = .{ .width = null, .height = null }, 2133 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2134 }, nav.root.getFocus()); 2135 _ = try terminal.render(&nav.root); 2136 try Repo.Events.performSync(allocator, session); 2137 }, 2138 } 2139 } 2140 2141 // persist queued actions when there's a repo; local mode has none, 2142 // so just apply in-memory 2143 if (repo_maybe) |repo| { 2144 try session.applyAndWritePending(io, allocator, repo); 2145 // reload so the next navigation builds its page from the current moment 2146 try session.reloadMoment(allocator, repo); 2147 } else { 2148 session.applyPending(); 2149 } 2150 2151 // reconcile navigation: forward to a new page, or back on escape. 2152 try nav.sync(allocator, session); 2153 2154 // the quit button (on the quit tab) asks the host to tear down. 2155 if (session.quit_requested) terminal.requestQuit(); 2156 2157 try nav.root.build(allocator, .{ 2158 .min_size = .{ .width = null, .height = null }, 2159 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2160 }, nav.root.getFocus()); 2161 } 2162 } 2163 2164 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2165 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2166 null 2167 else 2168 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2169 defer if (tty) |file| file.close(io); 2170 2171 const input = if (tty) |file| file else std.Io.File.stdin(); 2172 const output = if (tty) |file| file else std.Io.File.stdout(); 2173 2174 var write_buf: [1024]u8 = undefined; 2175 var writer = output.writer(io, &write_buf); 2176 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}); 2177 try writer.interface.flush(); 2178 2179 var read_buf: [1]u8 = undefined; 2180 var reader = input.reader(io, &read_buf); 2181 while (true) { 2182 const byte = try reader.interface.takeByte(); 2183 if (byte == '\r' or byte == '\n') return; 2184 } 2185 } 2186 2187 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2188 const root_focus = root.getFocus(); 2189 const focused_before = root_focus.grandchild_id; 2190 var activate_in_page_link = false; 2191 switch (key) { 2192 // request a navigation pop; the host's Nav.sync goes back a page, or 2193 // quits when there's no history left. 2194 .escape => { 2195 session.back = .requested; 2196 return; 2197 }, 2198 .ctrl => |letter| switch (letter) { 2199 'r' => { 2200 session.refresh_requested = true; 2201 return; 2202 }, 2203 else => try root.input(allocator, key, root_focus), 2204 }, 2205 .enter => { 2206 if (root_focus.grandchild_id) |gid| { 2207 // follow a cross-page link 2208 if (crossPageLink(root_focus, gid, session.data)) |route| { 2209 return session.navigate(route); 2210 } 2211 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2212 } 2213 activate_in_page_link = true; 2214 try root.input(allocator, key, root_focus); 2215 }, 2216 .mouse => |mouse| { 2217 if (mouse.action == .press and mouse.action.press == .left) { 2218 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2219 const focus_id = hit.id; 2220 if (hit.kind == .background) { 2221 root_focus.setFocus(focus_id); 2222 return; 2223 } 2224 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2225 if (session.back == .available) session.back = .requested; 2226 return; 2227 } 2228 // follow a cross-page link 2229 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2230 return session.navigate(route); 2231 } 2232 if (rawLink(root_focus, focus_id)) |url| { 2233 if (!mouse.ctrl) try requestRawLink(session, url); 2234 return; 2235 } 2236 root_focus.setFocus(focus_id); 2237 activate_in_page_link = true; 2238 } 2239 // forward the press into the widget tree so buttons (and any 2240 // future click-aware widgets) can react. widgets that don't 2241 // care about presses ignore it. 2242 try root.input(allocator, key, root_focus); 2243 } else { 2244 try root.input(allocator, key, root_focus); 2245 } 2246 }, 2247 else => try root.input(allocator, key, root_focus), 2248 } 2249 2250 const focused_after = root_focus.grandchild_id; 2251 if (!activate_in_page_link and focused_before == focused_after) return; 2252 const focus_id = focused_after orelse return; 2253 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2254 } 2255 2256 // a link to bytes the server serves directly rather than to a page route, so 2257 // it never resolves to a RoutablePage 2258 pub const raw_link_prefix = "ax:"; 2259 2260 // a route already loaded into the current page; focusing it changes the url 2261 // without rebuilding the page 2262 pub const in_page_link_prefix = "ai:"; 2263 2264 // an in-page tab's base route, or the exact current route when it is selected 2265 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2266 const target = if (selected) session.data.current_page else route; 2267 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2268 } 2269 2270 // a button the web renderer covers with a file picker that posts the chosen 2271 // file to the url after the prefix 2272 pub const file_input_prefix = "file:"; 2273 2274 // a submit button that overrides its enclosing form's action 2275 pub const submit_action_prefix = "submit:"; 2276 2277 // the url an `ax:` link points at, or null for anything else. a host that 2278 // suppresses the anchor's own navigation needs this to follow the link. 2279 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2280 const child = root_focus.children.get(focus_id) orelse return null; 2281 const custom = switch (child.focus.kind) { 2282 .custom => |c| c, 2283 else => return null, 2284 }; 2285 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2286 return custom[raw_link_prefix.len..]; 2287 } 2288 2289 fn requestRawLink(session: *Session, url: []const u8) !void { 2290 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2291 } 2292 2293 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2294 return if (session.web_port) |port| 2295 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2296 else 2297 path; 2298 } 2299 2300 // if the focus target at focus_id is an `a:` link that should navigate (a 2301 // different parent page, or a different files directory within the repo page), 2302 // return its route; otherwise null. lets a host turn a click / enter on such a 2303 // link into a navigation rather than a focus change. 2304 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2305 const current = data.current_page; 2306 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2307 // a link to a different parent page always navigates; within a page, a 2308 // files-directory / commits-page / list-window change navigates while tab 2309 // links stay in-page. 2310 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; 2311 return null; 2312 } 2313 2314 // the route carried by an `ai:` focus target, or null for any other target 2315 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2316 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2317 } 2318 2319 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2320 const child = root_focus.children.get(focus_id) orelse return null; 2321 const custom = switch (child.focus.kind) { 2322 .custom => |c| c, 2323 else => return null, 2324 }; 2325 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2326 const path = custom[prefix.len..]; 2327 // local sessions build (and parse) links with the repo identity elided 2328 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2329 } 2330 2331 // a display author, resolved against the admin db at read time 2332 pub const Author = union(enum) { 2333 unknown, 2334 email: []const u8, // an email no user matches 2335 user_name: []const u8, 2336 2337 // the commit author line's email, resolved like initFromEmail 2338 pub fn init( 2339 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2340 arena: *std.heap.ArenaAllocator, 2341 author_line: []const u8, 2342 ) !Author { 2343 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2344 } 2345 2346 // from a record's stored author email: it resolves to the user currently 2347 // holding it, else stays the email. the result is allocated in `arena`, 2348 // so the email may borrow transient memory. 2349 pub fn initFromEmail( 2350 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2351 arena: *std.heap.ArenaAllocator, 2352 author_email: ?[]const u8, 2353 ) !Author { 2354 const email = author_email orelse return .unknown; 2355 if (haxy_moment) |moment| { 2356 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2357 return .{ .user_name = user.event.name }; 2358 } 2359 } 2360 return .{ .email = try arena.allocator().dupe(u8, email) }; 2361 } 2362 }; 2363 2364 // the "a:" link to user `name`'s page. 2365 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2366 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2367 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2368 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2369 } 2370 2371 // a focusable " author " box showing `author`, linking to their user page 2372 // when it is a known user 2373 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2374 const text = switch (author) { 2375 .unknown => "", 2376 .email, .user_name => |t| t, 2377 }; 2378 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2379 errdefer tb.deinit(allocator); 2380 tb.getFocus().mode = .all; 2381 switch (author) { 2382 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2383 else => {}, 2384 } 2385 return tb; 2386 } 2387 2388 // native-TUI navigation 2389 pub const Nav = struct { 2390 root: Widget, 2391 // backs the current page (its Page data and the page-scoped strings its 2392 // widgets build). owned here, swapped on each navigation. session.page_arena 2393 // tracks whichever of these is current so widgets allocate into it. 2394 arena: *std.heap.ArenaAllocator, 2395 history: std.ArrayList(Entry), 2396 2397 // each retained page keeps its own arena, freed when the page leaves the 2398 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2399 // long-lived session from accumulating every page it ever visited. 2400 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2401 2402 // cap on retained back-history so the chain can't grow memory without bound 2403 const max_history: usize = 16; 2404 2405 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2406 session.back = .unavailable; 2407 const arena = try allocator.create(std.heap.ArenaAllocator); 2408 arena.* = std.heap.ArenaAllocator.init(allocator); 2409 errdefer { 2410 arena.deinit(); 2411 allocator.destroy(arena); 2412 } 2413 2414 session.page_arena = arena; 2415 const route = session.data.current_page; 2416 2417 const page = try arena.allocator().create(Page); 2418 page.* = try Page.init(arena, session, route); 2419 return .{ 2420 .root = try initRoot(allocator, page, session), 2421 .arena = arena, 2422 .history = .empty, 2423 }; 2424 } 2425 2426 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2427 self.root.deinit(allocator); 2428 freeArena(allocator, self.arena); 2429 for (self.history.items) |*entry| { 2430 entry.root.deinit(allocator); 2431 freeArena(allocator, entry.arena); 2432 } 2433 self.history.deinit(allocator); 2434 } 2435 2436 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2437 arena.deinit(); 2438 allocator.destroy(arena); 2439 } 2440 2441 // reconcile the displayed root with the session's navigation state. a 2442 // forward nav (current_page moved to a different parent page) pushes the 2443 // current root and builds the new page in a fresh arena; a back request 2444 // frees the current page and restores the previous one. when escape is 2445 // pressed with no history left we switch to the quit tab instead of quitting. 2446 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2447 // refresh: rebuild the current page in place from a current moment 2448 if (session.refresh_requested) { 2449 session.refresh_requested = false; 2450 if (session.haxy_moment != null or session.local != null) { 2451 const arena = try allocator.create(std.heap.ArenaAllocator); 2452 arena.* = std.heap.ArenaAllocator.init(allocator); 2453 errdefer freeArena(allocator, arena); 2454 2455 session.page_arena = arena; 2456 2457 const route = session.data.current_page; 2458 const page = try arena.allocator().create(Page); 2459 page.* = try Page.init(arena, session, route); 2460 const new_root = try initRoot(allocator, page, session); 2461 2462 self.root.deinit(allocator); 2463 freeArena(allocator, self.arena); 2464 self.root = new_root; 2465 self.arena = arena; 2466 } 2467 return; 2468 } 2469 2470 if (session.back == .requested) { 2471 if (self.history.pop()) |entry| { 2472 session.back = if (self.history.items.len == 0) .unavailable else .available; 2473 self.root.deinit(allocator); 2474 freeArena(allocator, self.arena); 2475 self.root = entry.root; 2476 self.arena = entry.arena; 2477 session.page_arena = entry.arena; 2478 session.data.current_page = entry.route; 2479 chooseAnsiArtForNavigation(session); 2480 return; 2481 } 2482 session.back = .unavailable; 2483 // nothing to go back to; switch to the quit confirmation 2484 const root_focus = self.root.getFocus(); 2485 var iter = root_focus.children.iterator(); 2486 while (iter.next()) |entry| { 2487 switch (entry.value_ptr.focus.kind) { 2488 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2489 root_focus.setFocus(entry.key_ptr.*); 2490 // a Stack only builds its selected child, so the quit 2491 // button isn't in the focus tree until a build runs with 2492 // the quit tab selected. build once, then send a 2493 // synthetic arrow_down to drop focus from the tab onto 2494 // the button so it's ready to confirm. 2495 try self.root.build(allocator, .{ 2496 .min_size = .{ .width = null, .height = 40 }, 2497 .max_size = .{ .width = 80, .height = null }, 2498 }, root_focus); 2499 try self.root.input(allocator, .arrow_down, root_focus); 2500 break; 2501 }, 2502 else => {}, 2503 } 2504 } 2505 return; 2506 } 2507 2508 // forward navigation: navigate() set next_page to the page to move to (a 2509 // cross-page link or tab change crossing pages) 2510 if (session.next_page) |route| { 2511 session.next_page = null; 2512 if (session.haxy_moment == null and session.local == null) return; 2513 const previous_route = session.data.current_page; 2514 // the page we navigated to becomes the current page 2515 session.data.current_page = route; 2516 session.back = .available; 2517 2518 const arena = try allocator.create(std.heap.ArenaAllocator); 2519 arena.* = std.heap.ArenaAllocator.init(allocator); 2520 errdefer freeArena(allocator, arena); 2521 2522 session.page_arena = arena; 2523 2524 const page = try arena.allocator().create(Page); 2525 page.* = try Page.init(arena, session, route); 2526 const new_root = try initRoot(allocator, page, session); 2527 2528 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2529 // drop the oldest entry (freeing its widget tree and arena) once over cap 2530 if (self.history.items.len > max_history) { 2531 var oldest = self.history.orderedRemove(0); 2532 oldest.root.deinit(allocator); 2533 freeArena(allocator, oldest.arena); 2534 } 2535 self.root = new_root; 2536 self.arena = arena; 2537 } 2538 } 2539 }; 2540 2541 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2542 const page_widget: Widget = switch (page.*) { 2543 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2544 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2545 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2546 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2547 }; 2548 2549 chooseAnsiArtForNavigation(session); 2550 2551 // on the TUI/SSH, the page sits above a one-row footer showing the url 2552 var root = if (session.is_terminal) blk: { 2553 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2554 errdefer box.deinit(allocator); 2555 const bg_id = bg_blk: { 2556 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2557 errdefer background.deinit(allocator); 2558 const id = background.getFocus().id; 2559 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2560 break :bg_blk id; 2561 }; 2562 { 2563 var footer = try widget.Footer.init(allocator, session); 2564 errdefer footer.deinit(allocator); 2565 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2566 } 2567 box.getFocus().child_id = bg_id; 2568 break :blk Widget{ .box = box }; 2569 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2570 errdefer root.deinit(allocator); 2571 2572 // input-owning views build their TextInputs in init, so reset their 2573 // registrations here 2574 session.text_inputs.clearRetainingCapacity(); 2575 2576 try root.build(allocator, .{ 2577 .min_size = .{ .width = null, .height = 40 }, 2578 .max_size = .{ .width = 100, .height = null }, 2579 }, root.getFocus()); 2580 2581 return root; 2582 } 2583 2584 // native and server-side sessions choose from the embedded collection. WASM 2585 // keeps the art serialized by the server instead. 2586 fn chooseAnsiArtForNavigation(session: *Session) void { 2587 if (comptime ansi_arts.len == 0) return; 2588 const io = session.io orelse return; 2589 var random: usize = undefined; 2590 io.random(std.mem.asBytes(&random)); 2591 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2592 } 2593