╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
212 repo_id_maybe = found.event_id; 213 214 // resolve the creating user so the header can show their name to the left 215 // of the repo title. 216 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 217 owner_name = owner.event.name; 218 219 // the repo's working copy lives at <repos_dir>/<hex event id>. 220 if (session.repos_dir) |repos_dir| { 221 const hex = std.fmt.bytesToHex(found.event_id, .lower); 222 source = .{ 223 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 224 .repo_kind = .xit, 225 }; 226 } 227 } 228 229 // open the on-disk repo once and read every tab's data from it. files and 230 // commits resolve the same requested ref (the default branch when the 231 // route named none), so they end up viewing the same one and either's 232 // resolved ref can canonicalize the tab mirror urls below. no filesystem 233 // (wasm), nowhere to look, or a failed open: empty tabs. 234 const files, const commits, const refs, var issues, var patches, var discussions, const events = blk: { 235 read: { 236 const io = session.io orelse break :read; 237 const src = source orelse break :read; 238 const gpa = arena.child_allocator; 239 switch (src.repo_kind) { 240 inline else => |repo_kind| { 241 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 242 defer any_repo.deinit(io, gpa); 243 switch (any_repo) { 244 inline else => |*opened| { 245 // local mode: bring the event db up to date with the events branch 246 if (session.local != null) try evt.consume(.repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 247 break :blk .{ 248 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), 249 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), 250 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 251 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 252 try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 253 try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 254 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, events_view, events_kind, events_selected, session.local != null, session.data.sync_failure), 255 }; 256 }, 257 } 258 }, 259 } 260 } 261 const aa = arena.allocator(); 262 break :blk .{ 263 try Files.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 264 try Commits.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 265 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 266 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 267 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 268 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 269 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 270 }; 271 }; 272 issues.repo_source = source; 273 patches.repo_source = source; 274 patches.repo_id = repo_id_maybe; 275 patches.target_branch = if (files.ref_or_oid == .branch) files.ref_or_oid_value else ""; 276 discussions.repo_source = source; 277 278 // each tab mirror carries this page's route for that tab; tabs not targeted 279 // by the incoming route fall back to their root/first-page route. the files 280 // mirror carries the selected file (when the route named one) so the url 281 // keeps it before focus enters the view. 282 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 283 // the content window only applies to a selected file, so the bare 284 // directory route drops it. 285 const files_route_line = if (files.selected_file != null) files_line else 0; 286 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; 287 // the message page mirrors its own url, not the log's 288 const commits_route = switch (commits.content) { 289 .message => ui.RoutablePage.repoCommitMessageRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value), 290 .diff => |d| ui.RoutablePage.repoCommitsRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_start, d.path), 291 }; 292 const commits_route_name = (commits_route orelse return error.NotFound).repo_commits; 293 294 return .{ 295 // files and commits resolve the same ref, so either's serves the header, 296 // which points both tabs at it. 297 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag, patches_tag, discussions_tag), 298 .repo = repo, 299 .files = files, 300 .commits = commits, 301 .refs = refs, 302 .issues = issues, 303 .patches = patches, 304 .discussions = discussions, 305 .events = events, 306 .settings = Settings.init(), 307 .auth = Auth.init(), 308 .quit = Quit.init(), 309 .route_name = route_name, 310 .commits_route_name = commits_route_name, 311 .identity = Array.from(repo_identity.identity) orelse return error.NotFound, 312 }; 313 } 314 315 pub const View = struct { 316 box: wgt.Box(ui.Widget), 317 data: *const Self, 318 session: *ui.Session, 319 320 const header_index: usize = 0; 321 const stack_index: usize = 1; 322 323 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 324 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 325 errdefer box.deinit(allocator); 326 327 // build the header first so we can grab the files-tab id for the auth 328 // view (it focuses there after login). 329 { 330 var header_view = try Header.View.init(allocator, &data.header, session); 331 errdefer header_view.deinit(allocator); 332 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 333 } 334 335 { 336 var stack = try wgt.Stack(ui.Widget).init(allocator); 337 errdefer stack.deinit(allocator); 338 339 // files — the default tab: the current directory's listing. 340 { 341 var files_view = try Files.View.init(allocator, &data.files, session); 342 errdefer files_view.deinit(allocator); 343 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 344 } 345 346 // commits — the current page of the commit log. 347 { 348 var commits_view = try Commits.View.init(allocator, &data.commits, session); 349 errdefer commits_view.deinit(allocator); 350 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 351 } 352 353 // refs — the repo's branches and tags. 354 { 355 var refs_view = try Refs.View.init(allocator, &data.refs, session); 356 errdefer refs_view.deinit(allocator); 357 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 358 } 359 360 // issues — the repo's issue tracker and comment permalinks. 361 { 362 var issues_view = try Issues.View.init(allocator, &data.issues, session); 363 errdefer issues_view.deinit(allocator); 364 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 365 } 366 367 // patches and their comment permalinks. 368 { 369 var patches_view = try Patches.View.init(allocator, &data.patches, session); 370 errdefer patches_view.deinit(allocator); 371 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 372 } 373 374 // discussions and their comment permalinks. 375 { 376 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 377 errdefer discussions_view.deinit(allocator); 378 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 379 } 380 381 { 382 var events_view = try Events.View.init(allocator, &data.events, session); 383 errdefer events_view.deinit(allocator); 384 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 385 } 386 387 // the header only shows the settings tab with a login and the auth 388 // tab outside local mode, so keep the stack's children 1:1 with 389 // the tabs by skipping the same views. 390 if (session.data.user_id != null) { 391 var settings_view = try Settings.View.init(allocator, session); 392 errdefer settings_view.deinit(allocator); 393 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 394 } 395 396 if (!session.data.is_local) { 397 var auth_view = try Auth.View.init(allocator, &data.auth, session); 398 errdefer auth_view.deinit(allocator); 399 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 400 } 401 402 if (session.is_terminal) { 403 var quit_view = try Quit.View.init(allocator, session); 404 errdefer quit_view.deinit(allocator); 405 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 406 } 407 408 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 409 } 410 411 var self = View{ .box = box, .data = data, .session = session }; 412 self.getFocus().child_id = box.children.keys()[header_index]; 413 return self; 414 } 415 416 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 417 self.box.deinit(allocator); 418 } 419 420 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 421 self.clearGrid(); 422 const header = &self.box.children.values()[header_index].widget.repo_header; 423 const stack = &self.box.children.values()[stack_index].widget.stack; 424 425 // each header tab maps 1:1 to a stack child by position. mirror the 426 // selection into current_page so the host can push the matching url; 427 // all repo tabs share the .repo parent, so this stays on the page 428 // rather than navigating. 429 if (header.getSelectedIndex()) |index| { 430 stack.getFocus().child_id = stack.children.keys()[index]; 431 switch (std.meta.activeTag(stack.children.values()[index])) { 432 // files/commits carry this page's content route (directory / log 433 // page); settings/auth carry only "owner/name". 434 .repo_commits => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 435 // the refs tab mirrors this page's windowed column. 436 .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, 437 // the issues tab mirrors this page's tag filter (issue urls 438 // themselves never carry the tag). 439 .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, 440 .repo_patches => self.session.data.current_page = ui.RoutablePage.repoPatchesRoute(self.data.identity.slice(), .open, self.data.patches.tag, "") orelse self.session.data.current_page, 441 .repo_discussions => self.session.data.current_page = ui.RoutablePage.repoDiscussionsRoute(self.data.identity.slice(), self.data.discussions.tag, "") orelse self.session.data.current_page, 442 .repo_events => self.session.data.current_page = ui.RoutablePage.repoEventsRoute(self.data.identity.slice(), self.data.events.view, null, "") orelse self.session.data.current_page, 443 .home_settings => { 444 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 445 self.session.data.current_page = .{ .repo_settings = identity }; 446 }, 447 .home_auth => { 448 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 449 self.session.data.current_page = .{ .repo_auth = identity }; 450 }, 451 // the quit tab is tty-only and not a route, so leave current_page 452 // alone (nothing to mirror into the url). 453 .quit => {}, 454 // the files tab (this page's directory route) 455 else => self.session.data.current_page = .{ .repo_files = self.data.route_name }, 456 } 457 } 458 try self.box.build(allocator, constraint, root_focus); 459 } 460 461 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 462 const stack = &self.box.children.values()[stack_index].widget.stack; 463 if (self.getFocus().child_id) |child_id| { 464 if (self.box.children.getIndex(child_id)) |current_index| { 465 const child = &self.box.children.values()[current_index].widget; 466 var index = current_index; 467 468 const direction = inp.vertDirection(key); 469 470 switch (direction) { 471 .up => { 472 switch (child.*) { 473 .repo_header => { 474 try child.input(allocator, key, root_focus); 475 }, 476 .stack => { 477 if (child.stack.getSelected()) |selected_widget| { 478 const at_top = switch (selected_widget.*) { 479 .repo_files => |*v| v.getSelectedIndex() == 0, 480 .repo_commits => |*v| v.getSelectedIndex() == 0, 481 .repo_refs => |*v| v.getSelectedIndex() == 0, 482 .repo_issues => |*v| v.getSelectedIndex() == 0, 483 .repo_patches => |*v| v.getSelectedIndex() == 0, 484 .repo_discussions => |*v| v.getSelectedIndex() == 0, 485 .repo_events => |*v| v.getSelectedIndex() == 0, 486 .home_settings => |*v| v.getSelectedIndex() == 0, 487 .home_auth => |*v| v.getSelectedIndex() == 0, 488 .quit => |*v| v.getSelectedIndex() == 0, 489 else => false, 490 }; 491 if (at_top) { 492 index = header_index; 493 } else { 494 try child.input(allocator, key, root_focus); 495 } 496 } 497 }, 498 else => {}, 499 } 500 }, 501 .down => { 502 switch (child.*) { 503 .repo_header => { 504 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 505 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 506 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 507 else => {}, 508 }; 509 index = stack_index; 510 }, 511 .stack => { 512 try child.input(allocator, key, root_focus); 513 }, 514 else => {}, 515 } 516 }, 517 .none => { 518 try child.input(allocator, key, root_focus); 519 }, 520 } 521 522 if (index != current_index) { 523 root_focus.setFocus(self.box.children.keys()[index]); 524 } 525 } 526 } 527 } 528 529 pub fn clearGrid(self: *View) void { 530 self.box.clearGrid(); 531 } 532 533 pub fn getGrid(self: View) ?Grid { 534 return self.box.getGrid(); 535 } 536 537 pub fn getFocus(self: *View) *Focus { 538 return self.box.getFocus(); 539 } 540 }; 541