╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
161 }; 162 } 163 } 164 165 // open the on-disk repo once and read every tab's data from it. files and 166 // commits resolve the same requested ref (the default branch when the 167 // route named none), so they end up viewing the same one and either's 168 // resolved ref can canonicalize the tab mirror urls below. no filesystem 169 // (wasm), nowhere to look, or a failed open: empty tabs. 170 const files, const commits, const refs, var issues = blk: { 171 read: { 172 const io = session.io orelse break :read; 173 const src = source orelse break :read; 174 const gpa = arena.child_allocator; 175 switch (src.repo_kind) { 176 inline else => |repo_kind| { 177 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, .{ .path = src.path }) catch break :read; 178 defer any_repo.deinit(io, gpa); 179 switch (any_repo) { 180 inline else => |*opened| { 181 // local mode: bring the event db up to date with the events branch 182 if (session.local != null) try evt.consume(repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 183 break :blk .{ 184 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_line), 185 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, repo_identity.identity, requested_ref_or_oid, requested_ref_value, commits_content), 186 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 187 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), 188 }; 189 }, 190 } 191 }, 192 } 193 } 194 const aa = arena.allocator(); 195 break :blk .{ 196 try Files.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 197 try Commits.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 198 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 199 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 200 }; 201 }; 202 issues.repo_source = source; 203 204 // each tab mirror carries this page's route for that tab; tabs not targeted 205 // by the incoming route fall back to their root/first-page route. the files 206 // mirror carries the selected file (when the route named one) so the url 207 // keeps it before focus enters the view. 208 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 209 // the content window only applies to a selected file, so the bare 210 // directory route drops it. 211 const files_route_line = if (files.selected_file != null) files_line else 0; 212 const route_name = (ui.RoutablePage.repoFilesRoute(repo_identity.identity, files.ref_or_oid, files.ref_or_oid_value, files_path, files_route_line) orelse return error.NotFound).repo_files; 213 // the message page mirrors its own url, not the log's 214 const commits_route = switch (commits.content) { 215 .message => ui.RoutablePage.repoCommitMessageRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value), 216 .diff => |d| ui.RoutablePage.repoCommitsRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_start, d.path), 217 }; 218 const commits_route_name = (commits_route orelse return error.NotFound).repo_commits; 219 220 return .{ 221 // files and commits resolve the same ref, so either's serves the header, 222 // which points both tabs at it. 223 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 224 .repo = repo, 225 .files = files, 226 .commits = commits, 227 .refs = refs, 228 .issues = issues, 229 .settings = Settings.init(), 230 .auth = Auth.init(), 231 .quit = Quit.init(), 232 .route_name = route_name, 233 .commits_route_name = commits_route_name, 234 .identity = Array.from(repo_identity.identity) orelse return error.NotFound, 235 }; 236 } 237 238 pub const View = struct { 239 box: wgt.Box(ui.Widget), 240 data: *const Self, 241 session: *ui.Session, 242 243 const header_index: usize = 0; 244 const stack_index: usize = 1; 245 246 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 247 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 248 errdefer box.deinit(allocator); 249 250 // build the header first so we can grab the files-tab id for the auth 251 // view (it focuses there after login). 252 { 253 var header_view = try Header.View.init(allocator, &data.header, session); 254 errdefer header_view.deinit(allocator); 255 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 256 } 257 258 { 259 var stack = try wgt.Stack(ui.Widget).init(allocator); 260 errdefer stack.deinit(allocator); 261 262 // files — the default tab: the current directory's listing. 263 { 264 var files_view = try Files.View.init(allocator, &data.files, session); 265 errdefer files_view.deinit(allocator); 266 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 267 } 268 269 // commits — the current page of the commit log. 270 { 271 var commits_view = try Commits.View.init(allocator, &data.commits, session); 272 errdefer commits_view.deinit(allocator); 273 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 274 } 275 276 // refs — the repo's branches and tags. 277 { 278 var refs_view = try Refs.View.init(allocator, &data.refs, session); 279 errdefer refs_view.deinit(allocator); 280 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 281 } 282 283 // issues — the repo's issue tracker and comment permalinks. 284 { 285 var issues_view = try Issues.View.init(allocator, &data.issues, session); 286 errdefer issues_view.deinit(allocator); 287 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 288 } 289 290 // the header only shows the settings tab with a login and the auth 291 // tab outside local mode, so keep the stack's children 1:1 with 292 // the tabs by skipping the same views. 293 if (session.data.user_id != null) { 294 var settings_view = try Settings.View.init(allocator, session); 295 errdefer settings_view.deinit(allocator); 296 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 297 } 298 299 if (!session.data.is_local) { 300 var auth_view = try Auth.View.init(allocator, &data.auth, session); 301 errdefer auth_view.deinit(allocator); 302 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 303 } 304 305 if (session.is_terminal) { 306 var quit_view = try Quit.View.init(allocator, session); 307 errdefer quit_view.deinit(allocator); 308 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 309 } 310 311 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 312 } 313 314 var self = View{ .box = box, .data = data, .session = session }; 315 self.getFocus().child_id = box.children.keys()[header_index]; 316 return self; 317 } 318 319 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 320 self.box.deinit(allocator); 321 } 322 323 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 324 self.clearGrid(); 325 const header = &self.box.children.values()[header_index].widget.repo_header; 326 const stack = &self.box.children.values()[stack_index].widget.stack; 327 328 // each header tab maps 1:1 to a stack child by position. mirror the 329 // selection into current_page so the host can push the matching url; 330 // all repo tabs share the .repo parent, so this stays on the page 331 // rather than navigating. 332 if (header.getSelectedIndex()) |index| { 333 stack.getFocus().child_id = stack.children.keys()[index]; 334 switch (std.meta.activeTag(stack.children.values()[index])) { 335 // files/commits carry this page's content route (directory / log 336 // page); settings/auth carry only "owner/name". 337 .repo_commits => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 338 // the refs tab mirrors this page's windowed column. 339 .repo_refs => self.session.data.current_page = ui.RoutablePage.repoRefsRoute(self.data.identity.slice(), self.data.refs.kind, self.data.refs.from) orelse self.session.data.current_page, 340 // the issues tab mirrors this page's tag filter (issue urls 341 // themselves never carry the tag). 342 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), .open, self.data.issues.tag, "") orelse self.session.data.current_page, 343 .home_settings => { 344 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 345 self.session.data.current_page = .{ .repo_settings = identity }; 346 }, 347 .home_auth => { 348 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 349 self.session.data.current_page = .{ .repo_auth = identity }; 350 }, 351 // the quit tab is tty-only and not a route, so leave current_page 352 // alone (nothing to mirror into the url). 353 .quit => {}, 354 // the files tab (this page's directory route) 355 else => self.session.data.current_page = .{ .repo_files = self.data.route_name }, 356 } 357 } 358 try self.box.build(allocator, constraint, root_focus); 359 } 360 361 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 362 const stack = &self.box.children.values()[stack_index].widget.stack; 363 if (self.getFocus().child_id) |child_id| { 364 if (self.box.children.getIndex(child_id)) |current_index| { 365 const child = &self.box.children.values()[current_index].widget; 366 var index = current_index; 367 368 const direction = inp.vertDirection(key); 369 370 switch (direction) { 371 .up => { 372 switch (child.*) { 373 .repo_header => { 374 try child.input(allocator, key, root_focus); 375 }, 376 .stack => { 377 if (child.stack.getSelected()) |selected_widget| { 378 const at_top = switch (selected_widget.*) { 379 .repo_files => |*v| v.getSelectedIndex() == 0, 380 .repo_commits => |*v| v.getSelectedIndex() == 0, 381 .repo_refs => |*v| v.getSelectedIndex() == 0, 382 .repo_issues => |*v| v.getSelectedIndex() == 0, 383 .home_settings => |*v| v.getSelectedIndex() == 0, 384 .home_auth => |*v| v.getSelectedIndex() == 0, 385 .quit => |*v| v.getSelectedIndex() == 0, 386 else => false, 387 }; 388 if (at_top) { 389 index = header_index; 390 } else { 391 try child.input(allocator, key, root_focus); 392 } 393 } 394 }, 395 else => {}, 396 } 397 }, 398 .down => { 399 switch (child.*) { 400 .repo_header => { 401 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 402 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 403 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 404 else => {}, 405 }; 406 index = stack_index; 407 }, 408 .stack => { 409 try child.input(allocator, key, root_focus); 410 }, 411 else => {}, 412 } 413 }, 414 .none => { 415 try child.input(allocator, key, root_focus); 416 }, 417 } 418 419 if (index != current_index) { 420 root_focus.setFocus(self.box.children.keys()[index]); 421 } 422 } 423 } 424 } 425 426 pub fn clearGrid(self: *View) void { 427 self.box.clearGrid(); 428 } 429 430 pub fn getGrid(self: View) ?Grid { 431 return self.box.getGrid(); 432 } 433 434 pub fn getFocus(self: *View) *Focus { 435 return self.box.getFocus(); 436 } 437 }; 438