╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
299 } 300 // a merge: route stands for the log of what the merge brought in 301 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 302 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 303 else merged: { 304 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 305 break :merged .{ .object, merged.oid, merged.base_oid }; 306 }; 307 const files_data = if (patchrev_id.len != 0) 308 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, patchrev_id, files_dir, files_line) 309 else 310 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, ref_or_oid, ref_value, files_dir, files_line, files_find); 311 const changes_data: Changes = if (route == .repo_diff) 312 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 313 else if (patchrev_id.len != 0) diff: { 314 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 315 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 316 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, location, ref_or_oid, ref_value, commits_content, base_oid, commits_search, commits_from) }; 317 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 318 break :blk .{ 319 files_data, 320 changes_data, 321 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 322 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 323 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_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 324 try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 325 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 326 }; 327 }, 328 } 329 }, 330 } 331 } 332 const aa = arena.allocator(); 333 break :blk .{ 334 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 335 Changes{ .commits = try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content, commits_base_oid) }, 336 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 337 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 338 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 339 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 340 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 341 }; 342 }; 343 if (undo_data) |*undo| { 344 undo.can_undo = session.local != null or evt.Repo.roleOf(repo, session.userId()) == .owner; 345 undo.clear = undo_clear; 346 } 347 if (route == .repo_undo and undo_data == null) return error.NotFound; 348 issues.repo_source = source; 349 patches.repo_source = source; 350 patches.repo_id = repo_id_maybe; 351 discussions.repo_source = source; 352 353 return .{ 354 // use the files tab's resolved ref for the header 355 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues.label, patches.label, discussions.label), 356 .repo = repo, 357 .files = files, 358 .changes = changes, 359 .refs = refs, 360 .issues = issues, 361 .patches = patches, 362 .discussions = discussions, 363 .events = events, 364 .undo = undo_data, 365 .auth = Auth.init(), 366 .quit = Quit.init(), 367 }; 368 } 369 370 pub const View = struct { 371 box: wgt.Box(ui.Widget), 372 373 const header_index: usize = 0; 374 const stack_index: usize = 1; 375 376 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 377 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 378 errdefer box.deinit(allocator); 379 380 // build the header first so we can grab the files-tab id for the auth 381 // view (it focuses there after login). 382 { 383 var header_view = try Header.View.init(allocator, data, session); 384 errdefer header_view.deinit(allocator); 385 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 386 } 387 388 { 389 var stack = try wgt.Stack(ui.Widget).init(allocator); 390 errdefer stack.deinit(allocator); 391 392 // files — the default tab: the current directory's listing. 393 { 394 var files_view = try Files.View.init(allocator, &data.files, session); 395 errdefer files_view.deinit(allocator); 396 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 397 } 398 399 switch (data.changes) { 400 .diff => |*diff| { 401 var diff_view = try Diff.View.init(allocator, diff, session); 402 errdefer diff_view.deinit(allocator); 403 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 404 }, 405 .commits => |*commits| { 406 var commits_view = try Commits.View.init(allocator, commits, session); 407 errdefer commits_view.deinit(allocator); 408 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 409 }, 410 } 411 412 // refs — the repo's branches and tags. 413 { 414 var refs_view = try Refs.View.init(allocator, &data.refs, session); 415 errdefer refs_view.deinit(allocator); 416 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 417 } 418 419 // issues — the repo's issue tracker and comment permalinks. 420 { 421 var issues_view = try Issues.View.init(allocator, &data.issues, session); 422 errdefer issues_view.deinit(allocator); 423 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 424 } 425 426 // patches and their comment permalinks. 427 { 428 var patches_view = try Patches.View.init(allocator, &data.patches, session); 429 errdefer patches_view.deinit(allocator); 430 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 431 } 432 433 // discussions and their comment permalinks. 434 { 435 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 436 errdefer discussions_view.deinit(allocator); 437 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 438 } 439 440 if (session.data.host_kind == .local or data.undo != null) { 441 var events_view = try Events.View.init(allocator, &data.events, session); 442 errdefer events_view.deinit(allocator); 443 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 444 } 445 446 if (data.undo) |*undo| { 447 var undo_view = try Undo.View.init(allocator, undo, session); 448 errdefer undo_view.deinit(allocator); 449 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 450 } 451 452 // the header only shows the new repo tab with a login 453 // and the auth tab outside local mode, so keep the stack's children 1:1 with 454 // the tabs by skipping the same views. 455 if (session.data.user_id != null) { 456 const identity = try std.fmt.allocPrint(session.page_arena.allocator(), "{s}/{s}", .{ data.header.owner_name, data.header.name }); 457 const route = ui.RoutablePage{ .repo_new_repo = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 458 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 459 errdefer new_repo_view.deinit(allocator); 460 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 461 } 462 463 if (session.data.host_kind == .server) { 464 var auth_view = try Auth.View.init(allocator, &data.auth, session); 465 errdefer auth_view.deinit(allocator); 466 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 467 } 468 469 if (session.is_terminal) { 470 var quit_view = try Quit.View.init(allocator, session); 471 errdefer quit_view.deinit(allocator); 472 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 473 } 474 475 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 476 } 477 478 var self = View{ .box = box }; 479 // a page opens on its tabs, except that search results open in the 480 // tab's own search box. 481 const results = data.files.find != null or 482 (data.changes == .commits and data.changes.commits.search != null) or 483 data.issues.search != null or data.patches.search != null or data.discussions.search != null; 484 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 485 return self; 486 } 487 488 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 489 self.box.deinit(allocator); 490 } 491 492 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 493 self.clearGrid(); 494 const header = &self.box.children.values()[header_index].widget.repo_header; 495 const stack = &self.box.children.values()[stack_index].widget.stack; 496 497 // each header tab maps 1:1 to a stack child by position 498 if (header.getSelectedIndex()) |index| 499 stack.getFocus().child_id = stack.children.keys()[index]; 500 try self.box.build(allocator, constraint, root_focus); 501 } 502 503 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 504 const stack = &self.box.children.values()[stack_index].widget.stack; 505 if (self.getFocus().child_id) |child_id| { 506 if (self.box.children.getIndex(child_id)) |current_index| { 507 const child = &self.box.children.values()[current_index].widget; 508 var index = current_index; 509 510 const direction = inp.vertDirection(key); 511 512 switch (direction) { 513 .up => { 514 switch (child.*) { 515 .repo_header => { 516 try child.input(allocator, key, root_focus); 517 }, 518 .stack => { 519 if (child.stack.getSelected()) |selected_widget| { 520 if (selected_widget.atTop(root_focus)) { 521 index = header_index; 522 } else { 523 try child.input(allocator, key, root_focus); 524 } 525 } 526 }, 527 else => {}, 528 } 529 }, 530 .down => { 531 switch (child.*) { 532 .repo_header => { 533 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 534 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 535 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 536 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 537 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 538 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 539 else => {}, 540 }; 541 index = stack_index; 542 }, 543 .stack => { 544 try child.input(allocator, key, root_focus); 545 }, 546 else => {}, 547 } 548 }, 549 .none => { 550 try child.input(allocator, key, root_focus); 551 }, 552 } 553 554 if (index != current_index) { 555 root_focus.setFocus(self.box.children.keys()[index]); 556 } 557 } 558 } 559 } 560 561 pub fn clearGrid(self: *View) void { 562 self.box.clearGrid(); 563 } 564 565 pub fn getGrid(self: View) ?Grid { 566 return self.box.getGrid(); 567 } 568 569 pub fn getFocus(self: *View) *Focus { 570 return self.box.getFocus(); 571 } 572 }; 573