╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
102 else => .branch, 103 }; 104 const refs_from: []const u8 = switch (route) { 105 .repo_refs => |*r| r.from.slice(), 106 else => "", 107 }; 108 // the issues tab's tag filter, the issue its window is rooted at, and the 109 // view it shows. 110 const issues_tag: []const u8 = switch (route) { 111 .repo_issues => |*i| i.tag.slice(), 112 else => "", 113 }; 114 const issues_selected: []const u8 = switch (route) { 115 .repo_issues => |*i| i.selected.slice(), 116 else => "", 117 }; 118 const issues_comment: []const u8 = switch (route) { 119 .repo_issues => |*i| i.comment.slice(), 120 else => "", 121 }; 122 const issues_theirs: []const u8 = switch (route) { 123 .repo_issues => |*i| i.theirs.slice(), 124 else => "", 125 }; 126 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 127 .repo_issues => |i| i.view, 128 else => .open, 129 }; 130 const issues_comments_start: usize = switch (route) { 131 .repo_issues => |i| i.comments_start, 132 else => 0, 133 }; 134 const patches_tag: []const u8 = switch (route) { 135 .repo_patches => |*p| p.tag.slice(), 136 else => "", 137 }; 138 const patches_selected: []const u8 = switch (route) { 139 .repo_patches => |*p| p.selected.slice(), 140 else => "", 141 }; 142 const patches_comment: []const u8 = switch (route) { 143 .repo_patches => |*p| p.comment.slice(), 144 else => "", 145 }; 146 const patches_theirs: []const u8 = switch (route) { 147 .repo_patches => |*p| p.theirs.slice(), 148 else => "", 149 }; 150 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 151 .repo_patches => |p| p.view, 152 else => .open, 153 }; 154 const patches_comments_start: usize = switch (route) { 155 .repo_patches => |p| p.comments_start, 156 else => 0, 157 }; 158 const discussions_tag: []const u8 = switch (route) { 159 .repo_discussions => |*t| t.tag.slice(), 160 else => "", 161 }; 162 const discussions_selected: []const u8 = switch (route) { 163 .repo_discussions => |*t| t.selected.slice(), 164 else => "", 165 }; 166 const discussions_comment: []const u8 = switch (route) { 167 .repo_discussions => |*t| t.comment.slice(), 168 else => "", 169 }; 170 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 171 .repo_discussions => |t| t.view, 172 else => .recent, 173 }; 174 const discussions_comments_start: usize = switch (route) { 175 .repo_discussions => |t| t.comments_start, 176 else => 0, 177 }; 178 const events_kind: ?evt.EventKind = switch (route) { 179 .repo_events => |e| e.kind, 180 else => null, 181 }; 182 const events_view: ui.RoutablePage.EventsView = switch (route) { 183 .repo_events => |e| e.view, 184 else => .active, 185 }; 186 const events_selected: []const u8 = switch (route) { 187 .repo_events => |*e| e.selected.slice(), 188 else => "", 189 }; 190 191 // where the on-disk repo lives (null keeps the views' empty fallback), plus 192 // the repo and owner-name metadata the header shows. local mode already 193 // knows all three; the server paths resolve them from the admin db. 194 var source: ?ui.RepoSource = null; 195 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 196 var repo: evt.Repo.Record = undefined; 197 var owner_name: []const u8 = undefined; 198 if (session.local) |local| { 199 source = local; 200 // local routes elide the identity, so the display name comes from the 201 // repo's directory rather than the route. 202 repo = .{ .event = .{ 203 .user_id = "", 204 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 205 .description = "", 206 } }; 207 owner_name = ""; 208 } else { 209 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 210 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 211 // a repo the session can't read doesn't exist to it 212 if (evt.Repo.roleOf(found.repo, session.userId()) == .none) return error.NotFound; 213 repo = found.repo; 214 repo_id_maybe = found.event_id; 215 216 // resolve the creating user so the header can show their name to the left 217 // of the repo title. 218 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 219 owner_name = owner.event.name; 220 221 // the repo's working copy lives at <repos_dir>/<hex event id>. 222 if (session.repos_dir) |repos_dir| { 223 const hex = std.fmt.bytesToHex(found.event_id, .lower); 224 source = .{ 225 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 226 .repo_kind = .xit, 227 }; 228 } 229 } 230 231 // open the repo once for every tab. files and changes share a ref or revision. 232 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 233 const files, const changes, const refs, var issues, var patches, var discussions, const events = blk: { 234 read: { 235 const io = session.io orelse break :read; 236 const src = source orelse break :read; 237 const gpa = arena.child_allocator; 238 switch (src.repo_kind) { 239 inline else => |repo_kind| { 240 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 241 defer any_repo.deinit(io, gpa); 242 switch (any_repo) { 243 inline else => |*opened| { 244 // local mode: bring the event db up to date with the events branch 245 if (session.local != null) { 246 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 247 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 248 } 249 const files_data = if (patchrev_id.len != 0) 250 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, patchrev_id, files_dir, files_line) 251 else 252 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, requested_ref_or_oid, requested_ref_value, files_dir, files_line); 253 const changes_data: Changes = if (route == .repo_diff) 254 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 255 else if (patchrev_id.len != 0) diff: { 256 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 257 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 258 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, location, requested_ref_or_oid, requested_ref_value, commits_content, commits_base_oid) }; 259 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 260 break :blk .{ 261 files_data, 262 changes_data, 263 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 264 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), 265 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_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 266 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), 267 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), 268 }; 269 }, 270 } 271 }, 272 } 273 } 274 const aa = arena.allocator(); 275 break :blk .{ 276 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 277 Changes{ .commits = try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content, commits_base_oid) }, 278 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 279 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 280 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 281 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 282 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 283 }; 284 }; 285 issues.repo_source = source; 286 patches.repo_source = source; 287 patches.repo_id = repo_id_maybe; 288 discussions.repo_source = source; 289 290 return .{ 291 // use the files tab's resolved ref for the header 292 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues.tag, patches.tag, discussions.tag), 293 .repo = repo, 294 .files = files, 295 .changes = changes, 296 .refs = refs, 297 .issues = issues, 298 .patches = patches, 299 .discussions = discussions, 300 .events = events, 301 .settings = Settings.init(), 302 .auth = Auth.init(), 303 .quit = Quit.init(), 304 }; 305 } 306 307 pub const View = struct { 308 box: wgt.Box(ui.Widget), 309 310 const header_index: usize = 0; 311 const stack_index: usize = 1; 312 313 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 314 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 315 errdefer box.deinit(allocator); 316 317 // build the header first so we can grab the files-tab id for the auth 318 // view (it focuses there after login). 319 { 320 var header_view = try Header.View.init(allocator, data, session); 321 errdefer header_view.deinit(allocator); 322 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 323 } 324 325 { 326 var stack = try wgt.Stack(ui.Widget).init(allocator); 327 errdefer stack.deinit(allocator); 328 329 // files — the default tab: the current directory's listing. 330 { 331 var files_view = try Files.View.init(allocator, &data.files, session); 332 errdefer files_view.deinit(allocator); 333 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 334 } 335 336 switch (data.changes) { 337 .diff => |*diff| { 338 var diff_view = try Diff.View.init(allocator, diff, session); 339 errdefer diff_view.deinit(allocator); 340 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 341 }, 342 .commits => |*commits| { 343 var commits_view = try Commits.View.init(allocator, commits, session); 344 errdefer commits_view.deinit(allocator); 345 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 346 }, 347 } 348 349 // refs — the repo's branches and tags. 350 { 351 var refs_view = try Refs.View.init(allocator, &data.refs, session); 352 errdefer refs_view.deinit(allocator); 353 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 354 } 355 356 // issues — the repo's issue tracker and comment permalinks. 357 { 358 var issues_view = try Issues.View.init(allocator, &data.issues, session); 359 errdefer issues_view.deinit(allocator); 360 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 361 } 362 363 // patches and their comment permalinks. 364 { 365 var patches_view = try Patches.View.init(allocator, &data.patches, session); 366 errdefer patches_view.deinit(allocator); 367 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 368 } 369 370 // discussions and their comment permalinks. 371 { 372 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 373 errdefer discussions_view.deinit(allocator); 374 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 375 } 376 377 { 378 var events_view = try Events.View.init(allocator, &data.events, session); 379 errdefer events_view.deinit(allocator); 380 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 381 } 382 383 // the header only shows the settings tab with a login and the auth 384 // tab outside local mode, so keep the stack's children 1:1 with 385 // the tabs by skipping the same views. 386 if (session.data.user_id != null) { 387 var settings_view = try Settings.View.init(allocator, session); 388 errdefer settings_view.deinit(allocator); 389 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 390 } 391 392 if (session.data.host_kind == .server) { 393 var auth_view = try Auth.View.init(allocator, &data.auth, session); 394 errdefer auth_view.deinit(allocator); 395 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 396 } 397 398 if (session.is_terminal) { 399 var quit_view = try Quit.View.init(allocator, session); 400 errdefer quit_view.deinit(allocator); 401 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 402 } 403 404 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 405 } 406 407 var self = View{ .box = box }; 408 self.getFocus().child_id = box.children.keys()[header_index]; 409 return self; 410 } 411 412 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 413 self.box.deinit(allocator); 414 } 415 416 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 417 self.clearGrid(); 418 const header = &self.box.children.values()[header_index].widget.repo_header; 419 const stack = &self.box.children.values()[stack_index].widget.stack; 420 421 // each header tab maps 1:1 to a stack child by position 422 if (header.getSelectedIndex()) |index| 423 stack.getFocus().child_id = stack.children.keys()[index]; 424 try self.box.build(allocator, constraint, root_focus); 425 } 426 427 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 428 const stack = &self.box.children.values()[stack_index].widget.stack; 429 if (self.getFocus().child_id) |child_id| { 430 if (self.box.children.getIndex(child_id)) |current_index| { 431 const child = &self.box.children.values()[current_index].widget; 432 var index = current_index; 433 434 const direction = inp.vertDirection(key); 435 436 switch (direction) { 437 .up => { 438 switch (child.*) { 439 .repo_header => { 440 try child.input(allocator, key, root_focus); 441 }, 442 .stack => { 443 if (child.stack.getSelected()) |selected_widget| { 444 if (selected_widget.atTop(root_focus)) { 445 index = header_index; 446 } else { 447 try child.input(allocator, key, root_focus); 448 } 449 } 450 }, 451 else => {}, 452 } 453 }, 454 .down => { 455 switch (child.*) { 456 .repo_header => { 457 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 458 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 459 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 460 else => {}, 461 }; 462 index = stack_index; 463 }, 464 .stack => { 465 try child.input(allocator, key, root_focus); 466 }, 467 else => {}, 468 } 469 }, 470 .none => { 471 try child.input(allocator, key, root_focus); 472 }, 473 } 474 475 if (index != current_index) { 476 root_focus.setFocus(self.box.children.keys()[index]); 477 } 478 } 479 } 480 } 481 482 pub fn clearGrid(self: *View) void { 483 self.box.clearGrid(); 484 } 485 486 pub fn getGrid(self: View) ?Grid { 487 return self.box.getGrid(); 488 } 489 490 pub fn getFocus(self: *View) *Focus { 491 return self.box.getFocus(); 492 } 493 }; 494