╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
204 .repo_discussions => |t| t.view, 205 else => .recent, 206 }; 207 const discussions_comments_start: usize = switch (route) { 208 .repo_discussions => |t| t.comments_start, 209 else => 0, 210 }; 211 const events_kind: ?evt.EventKind = switch (route) { 212 .repo_events => |e| e.kind, 213 else => null, 214 }; 215 const events_view: ui.RoutablePage.EventsView = switch (route) { 216 .repo_events => |e| e.view, 217 else => .active, 218 }; 219 const events_moment: ?u64 = switch (route) { 220 .repo_events => |e| e.moment, 221 else => null, 222 }; 223 const events_selected: []const u8 = switch (route) { 224 .repo_events => |*e| e.selected.slice(), 225 else => "", 226 }; 227 228 // where the on-disk repo lives (null keeps the views' empty fallback), plus 229 // the repo and owner-name metadata the header shows. local mode already 230 // knows all three; the server paths resolve them from the admin db. 231 var source: ?ui.RepoSource = null; 232 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 233 var repo: evt.Repo.Record = undefined; 234 var owner_name: []const u8 = undefined; 235 if (session.local) |local| { 236 source = local; 237 // local routes elide the identity, so the display name comes from the 238 // repo's directory rather than the route. 239 repo = .{ .event = .{ 240 .user_id = "", 241 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 242 .description = "", 243 } }; 244 owner_name = ""; 245 } else { 246 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 247 const io = session.io orelse return error.NoMoment; 248 const users_dir = session.users_dir orelse return error.NoMoment; 249 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, users_dir, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 250 // a repo the session can't read doesn't exist to it 251 if (evt.Repo.roleOf(found.repo, session.userId()) == .none) return error.NotFound; 252 repo = found.repo; 253 repo_id_maybe = found.event_id; 254 255 // resolve the creating user so the header can show their name to the left 256 // of the repo title. 257 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 258 owner_name = owner.event.name; 259 260 // the repo's working copy lives in its owner's dir. 261 source = .{ 262 .path = try evt.repoPath(arena.allocator(), users_dir, repo.event.user_id, &found.event_id), 263 .repo_kind = .xit, 264 }; 265 } 266 267 const handle = ui.RepoHandle{ 268 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 269 .viewer = try ui.Viewer.init(session, arena, repo), 270 }; 271 272 // open the repo once for every tab. files and changes share a ref or revision. 273 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 274 // the events tab needs write access, like undo 275 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 276 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 277 const undo_clear = route == .repo_undo and route.repo_undo.clear; 278 // only xit repos have undo history, so it's read in their branch below 279 var undo_data: ?Undo = null; 280 const files, const changes, const refs, var issues, var patches, var discussions, const events = blk: { 281 read: { 282 const io = session.io orelse break :read; 283 const src = source orelse break :read; 284 const gpa = arena.child_allocator; 285 switch (src.repo_kind) { 286 inline else => |repo_kind| { 287 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 288 defer any_repo.deinit(io, gpa); 289 switch (any_repo) { 290 inline else => |*opened| { 291 // local mode: bring the event db up to date with the events branch 292 if (session.local != null) { 293 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 294 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 295 } 296 // tabs switch in-page, so every tab's data is read here 297 if (repo_kind == .xit and handle.canWrite()) { 298 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 299 error.OutOfMemory => return err, 300 else => .{ .identity = repo_identity.identity, .handle = handle, .failure = @errorName(err) }, 301 }; 302 } 303 // a merge: route stands for the log of what the merge brought in 304 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 305 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 306 else merged: { 307 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 308 break :merged .{ .object, merged.oid, merged.base_oid }; 309 }; 310 const files_data = if (patchrev_id.len != 0) 311 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 312 else 313 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); 314 const changes_data: Changes = if (route == .repo_diff) 315 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 316 else if (patchrev_id.len != 0) diff: { 317 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 318 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 319 } 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_content, base_oid, commits_search, commits_from) }; 320 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 321 break :blk .{ 322 files_data, 323 changes_data, 324 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 325 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, handle.viewer), 326 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, handle.viewer), 327 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, handle.viewer), 328 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), 329 }; 330 }, 331 } 332 }, 333 } 334 } 335 const aa = arena.allocator(); 336 break :blk .{ 337 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 338 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content, commits_base_oid) }, 339 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 340 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 341 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 342 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 343 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 344 }; 345 }; 346 if (route == .repo_undo and undo_data == null) return error.NotFound; 347 if (undo_data) |*undo| undo.clear = undo_clear; 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 .handle = handle, 358 .files = files, 359 .changes = changes, 360 .refs = refs, 361 .issues = issues, 362 .patches = patches, 363 .discussions = discussions, 364 .events = events, 365 .undo = undo_data, 366 .auth = Auth.init(), 367 .quit = Quit.init(), 368 }; 369 } 370 371 pub const View = struct { 372 box: wgt.Box(ui.Widget), 373 374 const header_index: usize = 0; 375 const stack_index: usize = 1; 376 377 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 378 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 379 errdefer box.deinit(allocator); 380 381 // build the header first so we can grab the files-tab id for the auth 382 // view (it focuses there after login). 383 { 384 var header_view = try Header.View.init(allocator, data, session); 385 errdefer header_view.deinit(allocator); 386 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 387 } 388 389 { 390 var stack = try wgt.Stack(ui.Widget).init(allocator); 391 errdefer stack.deinit(allocator); 392 393 // files — the default tab: the current directory's listing. 394 { 395 var files_view = try Files.View.init(allocator, &data.files, session); 396 errdefer files_view.deinit(allocator); 397 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 398 } 399 400 switch (data.changes) { 401 .diff => |*diff| { 402 var diff_view = try Diff.View.init(allocator, diff, session); 403 errdefer diff_view.deinit(allocator); 404 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 405 }, 406 .commits => |*commits| { 407 var commits_view = try Commits.View.init(allocator, commits, session); 408 errdefer commits_view.deinit(allocator); 409 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 410 }, 411 } 412 413 // refs — the repo's branches and tags. 414 { 415 var refs_view = try Refs.View.init(allocator, &data.refs, session); 416 errdefer refs_view.deinit(allocator); 417 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 418 } 419 420 // issues — the repo's issue tracker and comment permalinks. 421 { 422 var issues_view = try Issues.View.init(allocator, &data.issues, session); 423 errdefer issues_view.deinit(allocator); 424 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 425 } 426 427 // patches and their comment permalinks. 428 { 429 var patches_view = try Patches.View.init(allocator, &data.patches, session); 430 errdefer patches_view.deinit(allocator); 431 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 432 } 433 434 // discussions and their comment permalinks. 435 { 436 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 437 errdefer discussions_view.deinit(allocator); 438 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 439 } 440 441 if (data.handle.canWrite()) { 442 var events_view = try Events.View.init(allocator, &data.events, session); 443 errdefer events_view.deinit(allocator); 444 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 445 } 446 447 if (data.undo) |*undo| { 448 var undo_view = try Undo.View.init(allocator, undo, session); 449 errdefer undo_view.deinit(allocator); 450 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 451 } 452 453 // the header only shows the new repo tab with a login 454 // and the auth tab outside local mode, so keep the stack's children 1:1 with 455 // the tabs by skipping the same views. 456 if (session.data.user_id != null) { 457 const identity = try std.fmt.allocPrint(session.page_arena.allocator(), "{s}/{s}", .{ data.header.owner_name, data.header.name }); 458 const route = ui.RoutablePage{ .repo_new_repo = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 459 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 460 errdefer new_repo_view.deinit(allocator); 461 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 462 } 463 464 if (session.data.host_kind == .server) { 465 var auth_view = try Auth.View.init(allocator, &data.auth, session); 466 errdefer auth_view.deinit(allocator); 467 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 468 } 469 470 if (session.is_terminal) { 471 var quit_view = try Quit.View.init(allocator, session); 472 errdefer quit_view.deinit(allocator); 473 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 474 } 475 476 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 477 } 478 479 var self = View{ .box = box }; 480 // a page opens on its tabs, except that search results open in the 481 // tab's own search box. 482 const results = data.files.find != null or 483 (data.changes == .commits and data.changes.commits.search != null) or 484 data.issues.search != null or data.patches.search != null or data.discussions.search != null; 485 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 486 return self; 487 } 488 489 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 490 self.box.deinit(allocator); 491 } 492 493 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 494 self.clearGrid(); 495 const header = &self.box.children.values()[header_index].widget.repo_header; 496 const stack = &self.box.children.values()[stack_index].widget.stack; 497 498 // each header tab maps 1:1 to a stack child by position 499 if (header.getSelectedIndex()) |index| 500 stack.getFocus().child_id = stack.children.keys()[index]; 501 try self.box.build(allocator, constraint, root_focus); 502 } 503 504 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 505 const stack = &self.box.children.values()[stack_index].widget.stack; 506 if (self.getFocus().child_id) |child_id| { 507 if (self.box.children.getIndex(child_id)) |current_index| { 508 const child = &self.box.children.values()[current_index].widget; 509 var index = current_index; 510 511 const direction = inp.vertDirection(key); 512 513 switch (direction) { 514 .up => { 515 switch (child.*) { 516 .repo_header => { 517 try child.input(allocator, key, root_focus); 518 }, 519 .stack => { 520 if (child.stack.getSelected()) |selected_widget| { 521 if (selected_widget.atTop(root_focus)) { 522 index = header_index; 523 } else { 524 try child.input(allocator, key, root_focus); 525 } 526 } 527 }, 528 else => {}, 529 } 530 }, 531 .down => { 532 switch (child.*) { 533 .repo_header => { 534 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 535 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 536 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 537 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 538 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 539 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 540 else => {}, 541 }; 542 index = stack_index; 543 }, 544 .stack => { 545 try child.input(allocator, key, root_focus); 546 }, 547 else => {}, 548 } 549 }, 550 .none => { 551 try child.input(allocator, key, root_focus); 552 }, 553 } 554 555 if (index != current_index) { 556 root_focus.setFocus(self.box.children.keys()[index]); 557 } 558 } 559 } 560 } 561 562 pub fn clearGrid(self: *View) void { 563 self.box.clearGrid(); 564 } 565 566 pub fn getGrid(self: View) ?Grid { 567 return self.box.getGrid(); 568 } 569 570 pub fn getFocus(self: *View) *Focus { 571 return self.box.getFocus(); 572 } 573 }; 574