╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const inp = @import("./input.zig"); 13 14 pub const Header = @import("./Repo/Header.zig"); 15 pub const Files = @import("./Repo/Files.zig"); 16 pub const Commits = @import("./Repo/Commits.zig"); 17 pub const Refs = @import("./Repo/Refs.zig"); 18 pub const Issues = @import("./Repo/Issues.zig"); 19 pub const Comments = @import("./Repo/Comments.zig"); 20 pub const Settings = @import("./Settings.zig"); 21 pub const Auth = @import("./Auth.zig"); 22 pub const Quit = @import("./Quit.zig"); 23 24 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_identity_max_len); 25 26 header: Header, 27 repo: evt.Repo.Record, 28 files: Files, 29 commits: Commits, 30 refs: Refs, 31 issues: Issues, 32 comments: ?Comments, 33 settings: Settings, 34 auth: Auth, 35 quit: Quit, 36 // the full files route this page renders, mirrored into current_page when 37 // the files tab is selected. 38 route_name: ui.RoutablePage.RepoFilesRoute, 39 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 40 // when the commits tab is selected. 41 commits_route_name: ui.RoutablePage.RepoCommitsRoute, 42 // "owner/name" alone, mirrored when the settings/auth tab is selected. 43 identity: Array, 44 45 const Self = @This(); 46 47 pub fn init( 48 arena: *std.heap.ArenaAllocator, 49 session: *ui.Session, 50 route: ui.RoutablePage, 51 ) !Self { 52 const DB = evt.AdminDB; 53 const hash_kind = evt.admin_repo_opts.hash; 54 55 // every repo route stores its identity as "owner/name" (or elides it in 56 // local mode); files and commits carry their remaining fields directly. 57 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 58 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 59 // the files and commits tabs share one ref/oid: whichever the incoming route 60 // names (it rides on the route's target tab), or the default branch when 61 // neither tab is targeted. building both views at it keeps switching tabs 62 // (even by key, without a reload) on the same ref. a null ref means the 63 // default branch (Files/Commits.init resolve it). the directory and the diff 64 // window only apply to their own tab. 65 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 66 .repo_files => |f| f.ref_kind, 67 .repo_commits => |c| c.ref_or_oid, 68 else => null, 69 }; 70 const requested_ref_value: []const u8 = switch (route) { 71 .repo_files => |*f| f.ref_value.slice(), 72 .repo_commits => |*c| c.value.slice(), 73 else => "", 74 }; 75 const files_dir = switch (route) { 76 .repo_files => |*f| f.path.slice(), 77 else => "", 78 }; 79 const files_line = switch (route) { 80 .repo_files => |f| f.line, 81 else => 0, 82 }; 83 // what the commits view's pane shows for the commit it walks from: a diff 84 // window (with the file it's filtered to) or that commit's message. 85 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 86 .repo_commits => |c| c.content, 87 else => .{ .diff = .{} }, 88 }; 89 const commits_start = switch (commits_content) { 90 .diff => |d| d.start, 91 .message => 0, 92 }; 93 // the refs tab windows one column at a time: `refs_from` (a url-encoded 94 // ref name) roots `refs_kind`'s column, the other stays at its first window. 95 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 96 .repo_refs => |r| r.kind, 97 else => .branch, 98 }; 99 const refs_from: []const u8 = switch (route) { 100 .repo_refs => |*r| r.from.slice(), 101 else => "", 102 }; 103 // the issues tab's tag filter, the issue its window is rooted at, and the 104 // view it shows. 105 const issues_tag: []const u8 = switch (route) { 106 .repo_issues => |*i| i.tag.slice(), 107 else => "", 108 }; 109 const issues_selected: []const u8 = switch (route) { 110 .repo_issues => |*i| i.selected.slice(), 111 else => "", 112 }; 113 const issues_theirs: []const u8 = switch (route) { 114 .repo_issues => |*i| i.theirs.slice(), 115 else => "", 116 }; 117 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 118 .repo_issues => |i| i.view, 119 else => .open, 120 }; 121 const issues_comments_start: usize = switch (route) { 122 .repo_issues => |i| i.comments_start, 123 else => 0, 124 }; 125 126 // where the on-disk repo lives (null keeps the views' empty fallback), plus 127 // the repo and owner-name metadata the header shows. local mode already 128 // knows all three; the server paths resolve them from the admin db. 129 var source: ?ui.RepoSource = null; 130 var repo: evt.Repo.Record = undefined; 131 var owner_name: []const u8 = undefined; 132 if (session.local) |local| { 133 source = local; 134 // local routes elide the identity, so the display name comes from the 135 // repo's directory rather than the route. 136 repo = .{ .event = .{ 137 .user_id = "", 138 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 139 .description = "", 140 } }; 141 owner_name = ""; 142 } else { 143 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 144 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 145 repo = found.repo; 146 147 // resolve the creating user so the header can show their name to the left 148 // of the repo title. 149 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 150 owner_name = owner.event.name; 151 152 // the repo's working copy lives at <repos_dir>/<hex event id>. 153 if (session.repos_dir) |repos_dir| { 154 const hex = std.fmt.bytesToHex(found.event_id, .lower); 155 source = .{ 156 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 157 .repo_kind = .xit, 158 }; 159 } 160 } 161 162 // open the on-disk repo once and read every tab's data from it. files and 163 // commits resolve the same requested ref (the default branch when the 164 // route named none), so they end up viewing the same one and either's 165 // resolved ref can canonicalize the tab mirror urls below. no filesystem 166 // (wasm), nowhere to look, or a failed open: empty tabs. 167 const files, const commits, const refs, var issues, const comments = blk: { 168 read: { 169 const io = session.io orelse break :read; 170 const src = source orelse break :read; 171 const gpa = arena.child_allocator; 172 switch (src.repo_kind) { 173 inline else => |repo_kind| { 174 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, .{ .path = src.path }) catch break :read; 175 defer any_repo.deinit(io, gpa); 176 switch (any_repo) { 177 inline else => |*opened| { 178 // local mode: bring the event db up to date with the events branch 179 if (session.local != null) try evt.consume(repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 180 const comments = switch (route) { 181 .repo_comments => |*c| try Comments.initFromRepo(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, c.thread.slice(), c.selected.slice(), c.start), 182 else => null, 183 }; 184 break :blk .{ 185 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_line), 186 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, repo_identity.identity, requested_ref_or_oid, requested_ref_value, commits_content), 187 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 188 if (comments == null) 189 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_tag, issues_selected, issues_comments_start, issues_theirs, issues_view) 190 else 191 try Issues.emptyResult(arena.allocator(), repo_identity.identity, "", "", 0, "", .open), 192 comments, 193 }; 194 }, 195 } 196 }, 197 } 198 } 199 const aa = arena.allocator(); 200 break :blk .{ 201 try Files.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 202 try Commits.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 203 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 204 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comments_start, issues_theirs, issues_view), 205 null, 206 }; 207 }; 208 if (std.meta.activeTag(route) == .repo_comments and comments == null) return error.NotFound; 209 issues.repo_source = source; 210 211 // each tab mirror carries this page's route for that tab; tabs not targeted 212 // by the incoming route fall back to their root/first-page route. the files 213 // mirror carries the selected file (when the route named one) so the url 214 // keeps it before focus enters the view. 215 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 216 // the content window only applies to a selected file, so the bare 217 // directory route drops it. 218 const files_route_line = if (files.selected_file != null) files_line else 0; 219 const route_name = (ui.RoutablePage.repoFilesRoute(repo_identity.identity, files.ref_or_oid, files.ref_or_oid_value, files_path, files_route_line) orelse return error.NotFound).repo_files; 220 // the message page mirrors its own url, not the log's 221 const commits_route = switch (commits.content) { 222 .message => ui.RoutablePage.repoCommitMessageRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value), 223 .diff => |d| ui.RoutablePage.repoCommitsRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_start, d.path), 224 }; 225 const commits_route_name = (commits_route orelse return error.NotFound).repo_commits; 226 227 return .{ 228 // files and commits resolve the same ref, so either's serves the header, 229 // which points both tabs at it. 230 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 231 .repo = repo, 232 .files = files, 233 .commits = commits, 234 .refs = refs, 235 .issues = issues, 236 .comments = comments, 237 .settings = Settings.init(), 238 .auth = Auth.init(), 239 .quit = Quit.init(), 240 .route_name = route_name, 241 .commits_route_name = commits_route_name, 242 .identity = Array.from(repo_identity.identity) orelse return error.NotFound, 243 }; 244 } 245 246 pub const View = struct { 247 box: wgt.Box(ui.Widget), 248 data: *const Self, 249 session: *ui.Session, 250 251 const header_index: usize = 0; 252 const stack_index: usize = 1; 253 254 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 255 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 256 errdefer box.deinit(allocator); 257 258 // build the header first so we can grab the files-tab id for the auth 259 // view (it focuses there after login). 260 { 261 var header_view = try Header.View.init(allocator, &data.header, session); 262 errdefer header_view.deinit(allocator); 263 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 264 } 265 266 { 267 var stack = try wgt.Stack(ui.Widget).init(allocator); 268 errdefer stack.deinit(allocator); 269 270 // files — the default tab: the current directory's listing. 271 { 272 var files_view = try Files.View.init(allocator, &data.files, session); 273 errdefer files_view.deinit(allocator); 274 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 275 } 276 277 // commits — the current page of the commit log. 278 { 279 var commits_view = try Commits.View.init(allocator, &data.commits, session); 280 errdefer commits_view.deinit(allocator); 281 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 282 } 283 284 // refs — the repo's branches and tags. 285 { 286 var refs_view = try Refs.View.init(allocator, &data.refs, session); 287 errdefer refs_view.deinit(allocator); 288 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 289 } 290 291 // issues — the repo's issue tracker or a comment permalink. 292 { 293 if (data.comments) |*comments| { 294 var comments_view = try Comments.View.init(allocator, comments, session); 295 errdefer comments_view.deinit(allocator); 296 try stack.children.put(allocator, comments_view.getFocus().id, .{ .repo_comments = comments_view }); 297 } else { 298 var issues_view = try Issues.View.init(allocator, &data.issues, session); 299 errdefer issues_view.deinit(allocator); 300 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 301 } 302 } 303 304 // the header only shows the settings tab with a login and the auth 305 // tab outside local mode, so keep the stack's children 1:1 with 306 // the tabs by skipping the same views. 307 if (session.data.user_id != null) { 308 var settings_view = try Settings.View.init(allocator, session); 309 errdefer settings_view.deinit(allocator); 310 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 311 } 312 313 if (!session.data.is_local) { 314 var auth_view = try Auth.View.init(allocator, &data.auth, session); 315 errdefer auth_view.deinit(allocator); 316 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 317 } 318 319 if (session.is_terminal) { 320 var quit_view = try Quit.View.init(allocator, session); 321 errdefer quit_view.deinit(allocator); 322 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 323 } 324 325 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 326 } 327 328 var self = View{ .box = box, .data = data, .session = session }; 329 self.getFocus().child_id = box.children.keys()[header_index]; 330 return self; 331 } 332 333 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 334 self.box.deinit(allocator); 335 } 336 337 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 338 self.clearGrid(); 339 const header = &self.box.children.values()[header_index].widget.repo_header; 340 const stack = &self.box.children.values()[stack_index].widget.stack; 341 342 // each header tab maps 1:1 to a stack child by position. mirror the 343 // selection into current_page so the host can push the matching url; 344 // all repo tabs share the .repo parent, so this stays on the page 345 // rather than navigating. 346 if (header.getSelectedIndex()) |index| { 347 stack.getFocus().child_id = stack.children.keys()[index]; 348 switch (std.meta.activeTag(stack.children.values()[index])) { 349 // files/commits carry this page's content route (directory / log 350 // page); settings/auth carry only "owner/name". 351 .repo_commits => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 352 // the refs tab mirrors this page's windowed column. 353 .repo_refs => self.session.data.current_page = ui.RoutablePage.repoRefsRoute(self.data.identity.slice(), self.data.refs.kind, self.data.refs.from) orelse self.session.data.current_page, 354 // the issues tab mirrors this page's tag filter (issue urls 355 // themselves never carry the tag). 356 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), .open, self.data.issues.tag, "") orelse self.session.data.current_page, 357 // the comment view mirrors its own content route when built. 358 .repo_comments => {}, 359 .home_settings => { 360 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 361 self.session.data.current_page = .{ .repo_settings = identity }; 362 }, 363 .home_auth => { 364 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 365 self.session.data.current_page = .{ .repo_auth = identity }; 366 }, 367 // the quit tab is tty-only and not a route, so leave current_page 368 // alone (nothing to mirror into the url). 369 .quit => {}, 370 // the files tab (this page's directory route) 371 else => self.session.data.current_page = .{ .repo_files = self.data.route_name }, 372 } 373 } 374 try self.box.build(allocator, constraint, root_focus); 375 } 376 377 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 378 if (self.getFocus().child_id) |child_id| { 379 if (self.box.children.getIndex(child_id)) |current_index| { 380 const child = &self.box.children.values()[current_index].widget; 381 var index = current_index; 382 383 const direction = inp.vertDirection(key); 384 385 switch (direction) { 386 .up => { 387 switch (child.*) { 388 .repo_header => { 389 try child.input(allocator, key, root_focus); 390 }, 391 .stack => { 392 if (child.stack.getSelected()) |selected_widget| { 393 const at_top = switch (selected_widget.*) { 394 .repo_files => |*v| v.getSelectedIndex() == 0, 395 .repo_commits => |*v| v.getSelectedIndex() == 0, 396 .repo_refs => |*v| v.getSelectedIndex() == 0, 397 .repo_issues => |*v| v.getSelectedIndex() == 0, 398 .repo_comments => |*v| v.getSelectedIndex() == 0, 399 .home_settings => |*v| v.getSelectedIndex() == 0, 400 .home_auth => |*v| v.getSelectedIndex() == 0, 401 .quit => |*v| v.getSelectedIndex() == 0, 402 else => false, 403 }; 404 if (at_top) { 405 index = header_index; 406 } else { 407 try child.input(allocator, key, root_focus); 408 } 409 } 410 }, 411 else => {}, 412 } 413 }, 414 .down => { 415 switch (child.*) { 416 .repo_header => { 417 index = stack_index; 418 }, 419 .stack => { 420 try child.input(allocator, key, root_focus); 421 }, 422 else => {}, 423 } 424 }, 425 .none => { 426 try child.input(allocator, key, root_focus); 427 }, 428 } 429 430 if (index != current_index) { 431 root_focus.setFocus(self.box.children.keys()[index]); 432 } 433 } 434 } 435 } 436 437 pub fn clearGrid(self: *View) void { 438 self.box.clearGrid(); 439 } 440 441 pub fn getGrid(self: View) ?Grid { 442 return self.box.getGrid(); 443 } 444 445 pub fn getFocus(self: *View) *Focus { 446 return self.box.getFocus(); 447 } 448 }; 449