╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
53 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 54 const location = ui.RoutablePage.RepoLocation{ .repo = repo_identity.identity }; 55 // the files and commits tabs share one ref/oid: whichever the incoming route 56 // names (it rides on the route's target tab), or the default branch when 57 // neither tab is targeted. building both views at it keeps switching tabs 58 // (even by key, without a reload) on the same ref. a null ref means the 59 // default branch (Files/Commits.init resolve it). the directory and the diff 60 // window only apply to their own tab. 61 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 62 .repo_files => |f| f.ref_kind, 63 .repo_commits => |c| c.ref_or_oid, 64 else => null, 65 }; 66 const requested_ref_value: []const u8 = switch (route) { 67 .repo_files => |*f| f.ref_value.slice(), 68 .repo_commits => |*c| c.value.slice(), 69 else => "", 70 }; 71 const files_dir = switch (route) { 72 .repo_files => |*f| f.path.slice(), 73 else => "", 74 }; 75 const files_line = switch (route) { 76 .repo_files => |f| f.line, 77 else => 0, 78 }; 79 // what the commits view's pane shows for the commit it walks from: a diff 80 // window (with the file it's filtered to) or that commit's message. 81 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 82 .repo_commits => |c| c.content, 83 else => .{ .diff = .{} }, 84 }; 85 // the refs tab windows one column at a time: `refs_from` (a url-encoded 86 // ref name) roots `refs_kind`'s column, the other stays at its first window. 87 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 88 .repo_refs => |r| r.kind, 89 else => .branch, 90 }; 91 const refs_from: []const u8 = switch (route) { 92 .repo_refs => |*r| r.from.slice(), 93 else => "", 94 }; 95 // the issues tab's tag filter, the issue its window is rooted at, and the 96 // view it shows. 97 const issues_tag: []const u8 = switch (route) { 98 .repo_issues => |*i| i.tag.slice(), 99 else => "", 100 }; 101 const issues_selected: []const u8 = switch (route) { 102 .repo_issues => |*i| i.selected.slice(), 103 else => "", 104 }; 105 const issues_comment: []const u8 = switch (route) { 106 .repo_issues => |*i| i.comment.slice(), 107 else => "", 108 }; 109 const issues_theirs: []const u8 = switch (route) { 110 .repo_issues => |*i| i.theirs.slice(), 111 else => "", 112 }; 113 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 114 .repo_issues => |i| i.view, 115 else => .open, 116 }; 117 const issues_comments_start: usize = switch (route) { 118 .repo_issues => |i| i.comments_start, 119 else => 0, 120 }; 121 const patches_tag: []const u8 = switch (route) { 122 .repo_patches => |*p| p.tag.slice(), 123 else => "", 124 }; 125 const patches_selected: []const u8 = switch (route) { 126 .repo_patches => |*p| p.selected.slice(), 127 else => "", 128 }; 129 const patches_comment: []const u8 = switch (route) { 130 .repo_patches => |*p| p.comment.slice(), 131 else => "", 132 }; 133 const patches_theirs: []const u8 = switch (route) { 134 .repo_patches => |*p| p.theirs.slice(), 135 else => "", 136 }; 137 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 138 .repo_patches => |p| p.view, 139 else => .open, 140 }; 141 const patches_comments_start: usize = switch (route) { 142 .repo_patches => |p| p.comments_start, 143 else => 0, 144 }; 145 const discussions_tag: []const u8 = switch (route) { 146 .repo_discussions => |*t| t.tag.slice(), 147 else => "", 148 }; 149 const discussions_selected: []const u8 = switch (route) { 150 .repo_discussions => |*t| t.selected.slice(), 151 else => "", 152 }; 153 const discussions_comment: []const u8 = switch (route) { 154 .repo_discussions => |*t| t.comment.slice(), 155 else => "", 156 }; 157 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 158 .repo_discussions => |t| t.view, 159 else => .recent, 160 }; 161 const discussions_comments_start: usize = switch (route) { 162 .repo_discussions => |t| t.comments_start, 163 else => 0, 164 }; 165 const events_kind: ?evt.EventKind = switch (route) { 166 .repo_events => |e| e.kind, 167 else => null, 168 }; 169 const events_view: ui.RoutablePage.EventsView = switch (route) { 170 .repo_events => |e| e.view, 171 else => .active, 172 }; 173 const events_selected: []const u8 = switch (route) { 174 .repo_events => |*e| e.selected.slice(), 175 else => "", 176 }; 177 178 // where the on-disk repo lives (null keeps the views' empty fallback), plus 179 // the repo and owner-name metadata the header shows. local mode already 180 // knows all three; the server paths resolve them from the admin db. 181 var source: ?ui.RepoSource = null; 182 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 183 var repo: evt.Repo.Record = undefined; 184 var owner_name: []const u8 = undefined; 185 if (session.local) |local| { 186 source = local; 187 // local routes elide the identity, so the display name comes from the 188 // repo's directory rather than the route. 189 repo = .{ .event = .{ 190 .user_id = "", 191 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 192 .description = "", 193 } }; 194 owner_name = ""; 195 } else { 196 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 197 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 198 repo = found.repo; 199 repo_id_maybe = found.event_id; 200 201 // resolve the creating user so the header can show their name to the left 202 // of the repo title. 203 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 204 owner_name = owner.event.name; 205 206 // the repo's working copy lives at <repos_dir>/<hex event id>. 207 if (session.repos_dir) |repos_dir| { 208 const hex = std.fmt.bytesToHex(found.event_id, .lower); 209 source = .{ 210 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 211 .repo_kind = .xit, 212 }; 213 } 214 } 215 216 // open the on-disk repo once and read every tab's data from it. files and 217 // commits resolve the same requested ref (the default branch when the 218 // route named none), so they end up viewing the same one. no filesystem 219 // (wasm), nowhere to look, or a failed open: empty tabs. 220 const files, const commits, const refs, var issues, var patches, var discussions, const events = blk: { 221 read: { 222 const io = session.io orelse break :read; 223 const src = source orelse break :read; 224 const gpa = arena.child_allocator; 225 switch (src.repo_kind) { 226 inline else => |repo_kind| { 227 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 228 defer any_repo.deinit(io, gpa); 229 switch (any_repo) { 230 inline else => |*opened| { 231 // local mode: bring the event db up to date with the events branch 232 if (session.local != null) try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 233 const files_data = 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); 234 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 235 break :blk .{ 236 files_data, 237 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, null), 238 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 239 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), 240 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), 241 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), 242 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), 243 }; 244 }, 245 } 246 }, 247 } 248 } 249 const aa = arena.allocator(); 250 break :blk .{ 251 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 252 try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 253 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 254 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 255 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 256 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 257 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 258 }; 259 }; 260 issues.repo_source = source; 261 patches.repo_source = source; 262 patches.repo_id = repo_id_maybe; 263 discussions.repo_source = source; 264 265 return .{ 266 // files and commits resolve the same ref, so either's serves the header, 267 // which points both tabs at it. 268 .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), 269 .repo = repo, 270 .files = files, 271 .commits = commits, 272 .refs = refs, 273 .issues = issues, 274 .patches = patches, 275 .discussions = discussions, 276 .events = events, 277 .settings = Settings.init(), 278 .auth = Auth.init(), 279 .quit = Quit.init(), 280 }; 281 } 282 283 pub const View = struct { 284 box: wgt.Box(ui.Widget), 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, data.commits.commit_count, 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 // patches and their comment permalinks. 334 { 335 var patches_view = try Patches.View.init(allocator, &data.patches, session); 336 errdefer patches_view.deinit(allocator); 337 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 338 } 339 340 // discussions and their comment permalinks. 341 { 342 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 343 errdefer discussions_view.deinit(allocator); 344 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 345 } 346 347 { 348 var events_view = try Events.View.init(allocator, &data.events, session); 349 errdefer events_view.deinit(allocator); 350 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 351 } 352 353 // the header only shows the settings tab with a login and the auth 354 // tab outside local mode, so keep the stack's children 1:1 with 355 // the tabs by skipping the same views. 356 if (session.data.user_id != null) { 357 var settings_view = try Settings.View.init(allocator, session); 358 errdefer settings_view.deinit(allocator); 359 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 360 } 361 362 if (session.data.host_kind == .server) { 363 var auth_view = try Auth.View.init(allocator, &data.auth, session); 364 errdefer auth_view.deinit(allocator); 365 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 366 } 367 368 if (session.is_terminal) { 369 var quit_view = try Quit.View.init(allocator, session); 370 errdefer quit_view.deinit(allocator); 371 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 372 } 373 374 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 375 } 376 377 var self = View{ .box = box }; 378 self.getFocus().child_id = box.children.keys()[header_index]; 379 return self; 380 } 381 382 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 383 self.box.deinit(allocator); 384 } 385 386 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 387 self.clearGrid(); 388 const header = &self.box.children.values()[header_index].widget.repo_header; 389 const stack = &self.box.children.values()[stack_index].widget.stack; 390 391 // each header tab maps 1:1 to a stack child by position 392 if (header.getSelectedIndex()) |index| 393 stack.getFocus().child_id = stack.children.keys()[index]; 394 try self.box.build(allocator, constraint, root_focus); 395 } 396 397 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 398 const stack = &self.box.children.values()[stack_index].widget.stack; 399 if (self.getFocus().child_id) |child_id| { 400 if (self.box.children.getIndex(child_id)) |current_index| { 401 const child = &self.box.children.values()[current_index].widget; 402 var index = current_index; 403 404 const direction = inp.vertDirection(key); 405 406 switch (direction) { 407 .up => { 408 switch (child.*) { 409 .repo_header => { 410 try child.input(allocator, key, root_focus); 411 }, 412 .stack => { 413 if (child.stack.getSelected()) |selected_widget| { 414 if (selected_widget.atTop(root_focus)) { 415 index = header_index; 416 } else { 417 try child.input(allocator, key, root_focus); 418 } 419 } 420 }, 421 else => {}, 422 } 423 }, 424 .down => { 425 switch (child.*) { 426 .repo_header => { 427 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 428 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 429 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 430 else => {}, 431 }; 432 index = stack_index; 433 }, 434 .stack => { 435 try child.input(allocator, key, root_focus); 436 }, 437 else => {}, 438 } 439 }, 440 .none => { 441 try child.input(allocator, key, root_focus); 442 }, 443 } 444 445 if (index != current_index) { 446 root_focus.setFocus(self.box.children.keys()[index]); 447 } 448 } 449 } 450 } 451 452 pub fn clearGrid(self: *View) void { 453 self.box.clearGrid(); 454 } 455 456 pub fn getGrid(self: View) ?Grid { 457 return self.box.getGrid(); 458 } 459 460 pub fn getFocus(self: *View) *Focus { 461 return self.box.getFocus(); 462 } 463 }; 464