╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
352 }; 353 const files_data = if (patchrev_id.len != 0) 354 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 355 else 356 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, ref_or_oid, ref_value, files_dir, files_line, files_find); 357 const changes_data: Changes = if (route == .repo_diff) 358 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 359 else if (patchrev_id.len != 0) diff: { 360 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 361 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 362 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, handle, ref_or_oid, ref_value, commits_message, base_oid, commits_search, commits_from) }; 363 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 364 var discussions_data = try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view, tabViewer(handle, repo, .discuss)); 365 var issues_data = try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view, tabViewer(handle, repo, .issue)); 366 var patches_data = try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, target_branch, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view, tabViewer(handle, repo, .patch)); 367 if (repo_kind == .xit and session.data.host_kind == .server) { 368 // a server page always resolved its repo id 369 const repo_id = repo_id_maybe orelse unreachable; 370 readNotify(opened.self_repo_opts, session, arena, opened, &owner_id, &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 371 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 372 }; 373 } 374 break :blk .{ 375 files_data, 376 changes_data, 377 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 378 discussions_data, 379 issues_data, 380 patches_data, 381 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 382 }; 383 }, 384 } 385 }, 386 } 387 } 388 const aa = arena.allocator(); 389 break :blk .{ 390 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 391 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 392 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 393 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 394 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 395 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 396 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 397 }; 398 }; 399 if (route == .repo_undo and undo_data == null) return error.NotFound; 400 if ((route == .repo_settings or route == .repo_members or route == .repo_branches) and settings == null) return error.NotFound; 401 if (undo_data) |*undo| undo.clear = undo_clear; 402 discussions.repo_source = source; 403 issues.repo_source = source; 404 patches.repo_source = source; 405 patches.repo_id = repo_id_maybe; 406 407 return .{ 408 // use the files tab's resolved ref for the header 409 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.search, issues.search, patches.search), 410 .repo = repo, 411 .handle = handle, 412 .files = files, 413 .changes = changes, 414 .refs = refs, 415 .discussions = discussions, 416 .issues = issues, 417 .patches = patches, 418 .events = events, 419 .undo = undo_data, 420 .settings = settings, 421 // after the shown thread's notif is marked read 422 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 423 .quit = Quit.init(), 424 }; 425 } 426 427 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 428 fn readNotify( 429 comptime repo_opts: rp.RepoOpts(.xit), 430 session: *ui.Session, 431 arena: *std.heap.ArenaAllocator, 432 repo: *rp.Repo(.xit, repo_opts), 433 owner_id: *const [evt.event_id_size]u8, 434 repo_id: *const [evt.event_id_size]u8, 435 viewed_thread: []const u8, 436 discussions: *Discussions, 437 issues: *Issues, 438 patches: *Patches, 439 ) !void { 440 const user_id = session.userId() orelse return; 441 const io = session.io orelse return error.NoMoment; 442 const users_dir = session.users_dir orelse return error.NoMoment; 443 const gpa = arena.child_allocator; 444 445 { 446 // the owner's plain data, which holds the subscriber index 447 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 448 defer owner_repo.deinit(io, gpa); 449 const owner_moment = try notify.latestMoment(&owner_repo); 450 451 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 452 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 453 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 454 } 455 456 if (viewed_thread.len == 0) return; 457 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 458 defer user_repo.deinit(io, gpa); 459 const latest = (try notify.latestMoment(&user_repo)) orelse return; 460 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 461 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 462 if (!item.unread) return; 463 // the page reads this moment or a newer one, so a child in it is shown 464 const moment = try evt.currentMoment(repo_opts, repo); 465 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 466 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 467 // a child the page can't show leaves it unread 468 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 469 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 470 } 471 472 // set the viewer's notify button on each of a window's threads 473 fn setNotifyActions( 474 arena: *std.heap.ArenaAllocator, 475 window: anytype, 476 owner_moment: ?evt.UserDB.HashMap(.read_only), 477 repo_id: *const [evt.event_id_size]u8, 478 user_id: *const [evt.event_id_size]u8, 479 ) !void { 480 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 481 for (items) |*entry| { 482 const thread_id = try evt.parseEventId(entry.id); 483 // muting drops the user from the index, so membership alone decides 484 const subscribed = if (owner_moment) |moment| try notify.isSubscribed(moment, &notify.idOf(repo_id, &thread_id), user_id) else false; 485 entry.notify_action = if (subscribed) .mute else .subscribe; 486 } 487 window.items = items; 488 } 489 490 // the viewer a tab's comment bar sees, which is none below the tab's role 491 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 492 const viewer = handle.viewer orelse return null; 493 const tab_role = repo.event.threadRole(kind) orelse return null; 494 return if (viewer.role.atLeast(tab_role)) viewer else null; 495 } 496 497 pub const View = struct { 498 box: wgt.Box(ui.Widget), 499 500 const header_index: usize = 0; 501 const stack_index: usize = 1; 502 503 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 504 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 505 errdefer box.deinit(allocator); 506 507 // build the header first so we can grab the files-tab id for the user 508 // view (it focuses there after login). 509 { 510 var header_view = try Header.View.init(allocator, data, session); 511 errdefer header_view.deinit(allocator); 512 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 513 } 514 515 { 516 var stack = try wgt.Stack(ui.Widget).init(allocator); 517 errdefer stack.deinit(allocator); 518 519 // files — the default tab: the current directory's listing. 520 { 521 var files_view = try Files.View.init(allocator, &data.files, session); 522 errdefer files_view.deinit(allocator); 523 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 524 } 525 526 switch (data.changes) { 527 .diff => |*diff| { 528 var diff_view = try Diff.View.init(allocator, diff, session); 529 errdefer diff_view.deinit(allocator); 530 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 531 }, 532 .commits => |*commits| { 533 var commits_view = try Commits.View.init(allocator, commits, session); 534 errdefer commits_view.deinit(allocator); 535 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 536 }, 537 } 538 539 // refs — the repo's branches and tags. 540 { 541 var refs_view = try Refs.View.init(allocator, &data.refs, session); 542 errdefer refs_view.deinit(allocator); 543 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 544 } 545 546 // discussions and their comment permalinks. 547 if (data.repo.event.discuss_role != null) { 548 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 549 errdefer discussions_view.deinit(allocator); 550 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 551 } 552 553 // issues — the repo's issue tracker and comment permalinks. 554 if (data.repo.event.issue_role != null) { 555 var issues_view = try Issues.View.init(allocator, &data.issues, session); 556 errdefer issues_view.deinit(allocator); 557 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 558 } 559 560 // patches and their comment permalinks. 561 if (data.repo.event.patch_role != null) { 562 var patches_view = try Patches.View.init(allocator, &data.patches, session); 563 errdefer patches_view.deinit(allocator); 564 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 565 } 566 567 if (data.handle.canWrite()) { 568 var events_view = try Events.View.init(allocator, &data.events, session); 569 errdefer events_view.deinit(allocator); 570 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 571 } 572 573 if (data.undo) |*undo| { 574 var undo_view = try Undo.View.init(allocator, undo, session); 575 errdefer undo_view.deinit(allocator); 576 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 577 } 578 579 if (data.settings) |*settings| { 580 var settings_view = try Settings.View.init(allocator, settings, session); 581 errdefer settings_view.deinit(allocator); 582 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 583 } 584 585 // the header shows new repo with a login, and new user without one 586 // and user only outside local mode, so keep the stack's children 587 // 1:1 with the tabs by skipping the same views. 588 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 589 if (session.data.user_id != null) { 590 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 591 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 592 errdefer new_repo_view.deinit(allocator); 593 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 594 } else if (session.data.host_kind == .server) { 595 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 596 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 597 errdefer new_user_view.deinit(allocator); 598 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 599 } 600 601 if (session.data.host_kind == .server) { 602 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 603 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 604 errdefer user_view.deinit(allocator); 605 try stack.children.put(allocator, user_view.getFocus().id, user_view); 606 } 607 608 if (session.is_terminal) { 609 var quit_view = try Quit.View.init(allocator, session); 610 errdefer quit_view.deinit(allocator); 611 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 612 } 613 614 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 615 } 616 617 var self = View{ .box = box }; 618 // a page opens on its tabs, except that search results open in the 619 // tab's own search box. 620 const results = data.files.find != null or 621 (data.changes == .commits and data.changes.commits.search != null) or 622 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 623 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 624 return self; 625 } 626 627 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 628 self.box.deinit(allocator); 629 } 630 631 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 632 self.clearGrid(); 633 const header = &self.box.children.values()[header_index].widget.repo_header; 634 const stack = &self.box.children.values()[stack_index].widget.stack; 635 636 // each header tab maps 1:1 to a stack child by position 637 if (header.getSelectedIndex()) |index| 638 stack.getFocus().child_id = stack.children.keys()[index]; 639 try self.box.build(allocator, constraint, root_focus); 640 } 641 642 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 643 const stack = &self.box.children.values()[stack_index].widget.stack; 644 if (self.getFocus().child_id) |child_id| { 645 if (self.box.children.getIndex(child_id)) |current_index| { 646 const child = &self.box.children.values()[current_index].widget; 647 var index = current_index; 648 649 const direction = inp.vertDirection(key); 650 651 switch (direction) { 652 .up => { 653 switch (child.*) { 654 .repo_header => { 655 try child.input(allocator, key, root_focus); 656 }, 657 .stack => { 658 if (child.stack.getSelected()) |selected_widget| { 659 if (selected_widget.atTop(root_focus)) { 660 index = header_index; 661 } else { 662 try child.input(allocator, key, root_focus); 663 } 664 } 665 }, 666 else => {}, 667 } 668 }, 669 .down => { 670 switch (child.*) { 671 .repo_header => { 672 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 673 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 674 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 675 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 676 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 677 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 678 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 679 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 680 else => {}, 681 }; 682 index = stack_index; 683 }, 684 .stack => { 685 try child.input(allocator, key, root_focus); 686 }, 687 else => {}, 688 } 689 }, 690 .none => { 691 try child.input(allocator, key, root_focus); 692 }, 693 } 694 695 if (index != current_index) { 696 root_focus.setFocus(self.box.children.keys()[index]); 697 } 698 } 699 } 700 } 701 702 pub fn clearGrid(self: *View) void { 703 self.box.clearGrid(); 704 } 705 706 pub fn getGrid(self: View) ?Grid { 707 return self.box.getGrid(); 708 } 709 710 pub fn getFocus(self: *View) *Focus { 711 return self.box.getFocus(); 712 } 713 }; 714