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