╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 repo = found.repo; 212 repo_id_maybe = found.event_id; 213 214 // resolve the creating user so the header can show their name to the left 215 // of the repo title. 216 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 217 owner_name = owner.event.name; 218 219 // the repo's working copy lives at <repos_dir>/<hex event id>. 220 if (session.repos_dir) |repos_dir| { 221 const hex = std.fmt.bytesToHex(found.event_id, .lower); 222 source = .{ 223 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 224 .repo_kind = .xit, 225 }; 226 } 227 } 228 229 // open the repo once for every tab. files and changes share a ref or revision. 230 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 231 const files, const changes, const refs, var issues, var patches, var discussions, const events = blk: { 232 read: { 233 const io = session.io orelse break :read; 234 const src = source orelse break :read; 235 const gpa = arena.child_allocator; 236 switch (src.repo_kind) { 237 inline else => |repo_kind| { 238 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 239 defer any_repo.deinit(io, gpa); 240 switch (any_repo) { 241 inline else => |*opened| { 242 // local mode: bring the event db up to date with the events branch 243 if (session.local != null) { 244 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 245 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 246 } 247 const files_data = if (patchrev_id.len != 0) 248 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, patchrev_id, files_dir, files_line) 249 else 250 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); 251 const changes_data: Changes = if (route == .repo_diff) 252 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 253 else if (patchrev_id.len != 0) diff: { 254 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 255 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 256 } 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) }; 257 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 258 break :blk .{ 259 files_data, 260 changes_data, 261 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 262 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), 263 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), 264 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), 265 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), 266 }; 267 }, 268 } 269 }, 270 } 271 } 272 const aa = arena.allocator(); 273 break :blk .{ 274 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 275 Changes{ .commits = try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content, commits_base_oid) }, 276 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 277 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 278 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 279 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 280 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 281 }; 282 }; 283 issues.repo_source = source; 284 patches.repo_source = source; 285 patches.repo_id = repo_id_maybe; 286 discussions.repo_source = source; 287 288 return .{ 289 // use the files tab's resolved ref for the header 290 .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), 291 .repo = repo, 292 .files = files, 293 .changes = changes, 294 .refs = refs, 295 .issues = issues, 296 .patches = patches, 297 .discussions = discussions, 298 .events = events, 299 .settings = Settings.init(), 300 .auth = Auth.init(), 301 .quit = Quit.init(), 302 }; 303 } 304 305 pub const View = struct { 306 box: wgt.Box(ui.Widget), 307 308 const header_index: usize = 0; 309 const stack_index: usize = 1; 310 311 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 312 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 313 errdefer box.deinit(allocator); 314 315 // build the header first so we can grab the files-tab id for the auth 316 // view (it focuses there after login). 317 { 318 var header_view = try Header.View.init(allocator, data, session); 319 errdefer header_view.deinit(allocator); 320 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 321 } 322 323 { 324 var stack = try wgt.Stack(ui.Widget).init(allocator); 325 errdefer stack.deinit(allocator); 326 327 // files — the default tab: the current directory's listing. 328 { 329 var files_view = try Files.View.init(allocator, &data.files, session); 330 errdefer files_view.deinit(allocator); 331 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 332 } 333 334 switch (data.changes) { 335 .diff => |*diff| { 336 var diff_view = try Diff.View.init(allocator, diff, session); 337 errdefer diff_view.deinit(allocator); 338 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 339 }, 340 .commits => |*commits| { 341 var commits_view = try Commits.View.init(allocator, commits, session); 342 errdefer commits_view.deinit(allocator); 343 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 344 }, 345 } 346 347 // refs — the repo's branches and tags. 348 { 349 var refs_view = try Refs.View.init(allocator, &data.refs, session); 350 errdefer refs_view.deinit(allocator); 351 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 352 } 353 354 // issues — the repo's issue tracker and comment permalinks. 355 { 356 var issues_view = try Issues.View.init(allocator, &data.issues, session); 357 errdefer issues_view.deinit(allocator); 358 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 359 } 360 361 // patches and their comment permalinks. 362 { 363 var patches_view = try Patches.View.init(allocator, &data.patches, session); 364 errdefer patches_view.deinit(allocator); 365 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 366 } 367 368 // discussions and their comment permalinks. 369 { 370 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 371 errdefer discussions_view.deinit(allocator); 372 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 373 } 374 375 { 376 var events_view = try Events.View.init(allocator, &data.events, session); 377 errdefer events_view.deinit(allocator); 378 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 379 } 380 381 // the header only shows the settings tab with a login and the auth 382 // tab outside local mode, so keep the stack's children 1:1 with 383 // the tabs by skipping the same views. 384 if (session.data.user_id != null) { 385 var settings_view = try Settings.View.init(allocator, session); 386 errdefer settings_view.deinit(allocator); 387 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 388 } 389 390 if (session.data.host_kind == .server) { 391 var auth_view = try Auth.View.init(allocator, &data.auth, session); 392 errdefer auth_view.deinit(allocator); 393 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 394 } 395 396 if (session.is_terminal) { 397 var quit_view = try Quit.View.init(allocator, session); 398 errdefer quit_view.deinit(allocator); 399 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 400 } 401 402 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 403 } 404 405 var self = View{ .box = box }; 406 self.getFocus().child_id = box.children.keys()[header_index]; 407 return self; 408 } 409 410 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 411 self.box.deinit(allocator); 412 } 413 414 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 415 self.clearGrid(); 416 const header = &self.box.children.values()[header_index].widget.repo_header; 417 const stack = &self.box.children.values()[stack_index].widget.stack; 418 419 // each header tab maps 1:1 to a stack child by position 420 if (header.getSelectedIndex()) |index| 421 stack.getFocus().child_id = stack.children.keys()[index]; 422 try self.box.build(allocator, constraint, root_focus); 423 } 424 425 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 426 const stack = &self.box.children.values()[stack_index].widget.stack; 427 if (self.getFocus().child_id) |child_id| { 428 if (self.box.children.getIndex(child_id)) |current_index| { 429 const child = &self.box.children.values()[current_index].widget; 430 var index = current_index; 431 432 const direction = inp.vertDirection(key); 433 434 switch (direction) { 435 .up => { 436 switch (child.*) { 437 .repo_header => { 438 try child.input(allocator, key, root_focus); 439 }, 440 .stack => { 441 if (child.stack.getSelected()) |selected_widget| { 442 if (selected_widget.atTop(root_focus)) { 443 index = header_index; 444 } else { 445 try child.input(allocator, key, root_focus); 446 } 447 } 448 }, 449 else => {}, 450 } 451 }, 452 .down => { 453 switch (child.*) { 454 .repo_header => { 455 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 456 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 457 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 458 else => {}, 459 }; 460 index = stack_index; 461 }, 462 .stack => { 463 try child.input(allocator, key, root_focus); 464 }, 465 else => {}, 466 } 467 }, 468 .none => { 469 try child.input(allocator, key, root_focus); 470 }, 471 } 472 473 if (index != current_index) { 474 root_focus.setFocus(self.box.children.keys()[index]); 475 } 476 } 477 } 478 } 479 480 pub fn clearGrid(self: *View) void { 481 self.box.clearGrid(); 482 } 483 484 pub fn getGrid(self: View) ?Grid { 485 return self.box.getGrid(); 486 } 487 488 pub fn getFocus(self: *View) *Focus { 489 return self.box.getFocus(); 490 } 491 }; 492