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