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