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