╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const inp = @import("./input.zig"); 13 14 pub const Header = @import("./Repo/Header.zig"); 15 pub const Files = @import("./Repo/Files.zig"); 16 pub const Commits = @import("./Repo/Commits.zig"); 17 pub const Refs = @import("./Repo/Refs.zig"); 18 pub const Issues = @import("./Repo/Issues.zig"); 19 pub const Settings = @import("./Settings.zig"); 20 pub const Auth = @import("./Auth.zig"); 21 pub const Quit = @import("./Quit.zig"); 22 23 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_identity_max_len); 24 25 header: Header, 26 repo: evt.Repo, 27 files: Files, 28 commits: Commits, 29 refs: Refs, 30 issues: Issues, 31 settings: Settings, 32 auth: Auth, 33 quit: Quit, 34 // the full files route this page renders, mirrored into current_page when 35 // the files tab is selected. 36 route_name: ui.RoutablePage.RepoFilesRoute, 37 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 38 // when the commits tab is selected. 39 commits_route_name: ui.RoutablePage.RepoCommitsRoute, 40 // "owner/name" alone, mirrored when the settings/auth tab is selected. 41 identity: Array, 42 43 const Self = @This(); 44 45 pub fn init( 46 arena: *std.heap.ArenaAllocator, 47 session: *ui.Session, 48 route: ui.RoutablePage, 49 ) !Self { 50 const DB = evt.AdminDB; 51 const hash_kind = evt.admin_repo_opts.hash; 52 53 // every repo route stores its identity as "owner/name" (or elides it in 54 // local mode); files and commits carry their remaining fields directly. 55 const name_str: []const u8 = switch (route) { 56 .repo_files => |f| f.name.slice(), 57 .repo_commits => |c| c.name.slice(), 58 .repo_settings, .repo_auth => |n| n.slice(), 59 .repo_refs => |r| r.name.slice(), 60 .repo_issues => |i| i.name.slice(), 61 else => return error.UnexpectedRoute, 62 }; 63 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 64 // the files and commits tabs share one ref/oid: whichever the incoming route 65 // names (it rides on the route's target tab), or the default branch when 66 // neither tab is targeted. building both views at it keeps switching tabs 67 // (even by key, without a reload) on the same ref. a null ref means the 68 // default branch (Files/Commits.init resolve it). the directory and the diff 69 // window only apply to their own tab. 70 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 71 .repo_files => |f| f.ref_kind, 72 .repo_commits => |c| c.ref_or_oid, 73 else => null, 74 }; 75 const requested_ref_value: []const u8 = switch (route) { 76 .repo_files => |f| f.ref_value.slice(), 77 .repo_commits => |c| c.value.slice(), 78 else => "", 79 }; 80 const files_dir = switch (route) { 81 .repo_files => |f| f.path.slice(), 82 else => "", 83 }; 84 const files_line = switch (route) { 85 .repo_files => |f| f.line, 86 else => 0, 87 }; 88 // what the commits view's pane shows for the commit it walks from: a diff 89 // window (with the file it's filtered to) or that commit's message. 90 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 91 .repo_commits => |c| c.content, 92 else => .{ .diff = .{} }, 93 }; 94 const commits_start = switch (commits_content) { 95 .diff => |d| d.start, 96 .message => 0, 97 }; 98 // the refs tab windows one column at a time: `refs_from` (a url-encoded 99 // ref name) roots `refs_kind`'s column, the other stays at its first window. 100 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 101 .repo_refs => |r| r.kind, 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_view: ui.RoutablePage.IssuesView = switch (route) { 119 .repo_issues => |i| i.view, 120 else => .open, 121 }; 122 123 // where the on-disk repo lives (null keeps the views' empty fallback), plus 124 // the repo and owner-name metadata the header shows. local mode already 125 // knows all three; the server paths resolve them from the admin db. 126 var source: ?ui.RepoSource = null; 127 var repo: evt.Repo = undefined; 128 var owner_name: []const u8 = undefined; 129 if (session.local) |local| { 130 source = local; 131 // local routes elide the identity, so the display name comes from the 132 // repo's directory rather than the route. 133 repo = .{ 134 .user_id = "", 135 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 136 .description = "", 137 }; 138 owner_name = ""; 139 } else { 140 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 141 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 142 repo = found.repo; 143 144 // resolve the creating user so the header can show their name to the left 145 // of the repo title. 146 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.user_id)) orelse return error.NotFound; 147 owner_name = owner.name; 148 149 // the repo's working copy lives at <repos_dir>/<hex event id>. 150 if (session.repos_dir) |repos_dir| { 151 const hex = std.fmt.bytesToHex(found.event_id, .lower); 152 source = .{ 153 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 154 .repo_kind = .xit, 155 }; 156 } 157 } 158 159 // open the on-disk repo once and read every tab's data from it. files and 160 // commits resolve the same requested ref (the default branch when the 161 // route named none), so they end up viewing the same one and either's 162 // resolved ref can canonicalize the tab mirror urls below. no filesystem 163 // (wasm), nowhere to look, or a failed open: empty tabs. 164 const files, const commits, const refs, var issues = blk: { 165 read: { 166 const io = session.io orelse break :read; 167 const src = source orelse break :read; 168 const gpa = arena.child_allocator; 169 switch (src.repo_kind) { 170 inline else => |repo_kind| { 171 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, .{ .path = src.path }) catch break :read; 172 defer any_repo.deinit(io, gpa); 173 switch (any_repo) { 174 inline else => |*opened| { 175 // local mode: bring the event db up to date with the events branch 176 if (session.local != null) try evt.consume(repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 177 break :blk .{ 178 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), 179 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, requested_ref_or_oid, requested_ref_value, commits_content), 180 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 181 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, repo_identity.identity, issues_tag, issues_selected, issues_view), 182 }; 183 }, 184 } 185 }, 186 } 187 } 188 const aa = arena.allocator(); 189 break :blk .{ 190 try Files.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 191 try Commits.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 192 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 193 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_view), 194 }; 195 }; 196 issues.repo_source = source; 197 198 // each tab mirror carries this page's route for that tab; tabs not targeted 199 // by the incoming route fall back to their root/first-page route. the files 200 // mirror carries the selected file (when the route named one) so the url 201 // keeps it before focus enters the view. 202 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 203 // the content window only applies to a selected file, so the bare 204 // directory route drops it. 205 const files_route_line = if (files.selected_file != null) files_line else 0; 206 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; 207 // the message page mirrors its own url, not the log's 208 const commits_route = switch (commits.content) { 209 .message => ui.RoutablePage.repoCommitMessageRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value), 210 .diff => |d| ui.RoutablePage.repoCommitsRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_start, d.path), 211 }; 212 const commits_route_name = (commits_route orelse return error.NotFound).repo_commits; 213 214 return .{ 215 // files and commits resolve the same ref, so either's serves the header, 216 // which points both tabs at it. 217 .header = try Header.init(arena, repo.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 218 .repo = repo, 219 .files = files, 220 .commits = commits, 221 .refs = refs, 222 .issues = issues, 223 .settings = Settings.init(), 224 .auth = Auth.init(), 225 .quit = Quit.init(), 226 .route_name = route_name, 227 .commits_route_name = commits_route_name, 228 .identity = Array.from(repo_identity.identity) orelse return error.NotFound, 229 }; 230 } 231 232 pub const View = struct { 233 box: wgt.Box(ui.Widget), 234 data: *const Self, 235 session: *ui.Session, 236 237 const header_index: usize = 0; 238 const stack_index: usize = 1; 239 240 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 241 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 242 errdefer box.deinit(allocator); 243 244 // build the header first so we can grab the files-tab id for the auth 245 // view (it focuses there after login). 246 var files_tab_id: usize = undefined; 247 { 248 var header_view = try Header.View.init(allocator, &data.header, session); 249 errdefer header_view.deinit(allocator); 250 files_tab_id = header_view.tab_ids.keys()[0]; 251 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 252 } 253 254 { 255 var stack = try wgt.Stack(ui.Widget).init(allocator); 256 errdefer stack.deinit(allocator); 257 258 // files — the default tab: the current directory's listing. 259 { 260 var files_view = try Files.View.init(allocator, &data.files, session); 261 errdefer files_view.deinit(allocator); 262 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 263 } 264 265 // commits — the current page of the commit log. 266 { 267 var commits_view = try Commits.View.init(allocator, &data.commits, session); 268 errdefer commits_view.deinit(allocator); 269 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 270 } 271 272 // refs — the repo's branches and tags. 273 { 274 var refs_view = try Refs.View.init(allocator, &data.refs, session); 275 errdefer refs_view.deinit(allocator); 276 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 277 } 278 279 // issues — the repo's issue tracker. 280 { 281 var issues_view = try Issues.View.init(allocator, &data.issues, session); 282 errdefer issues_view.deinit(allocator); 283 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 284 } 285 286 { 287 var settings_view = try Settings.View.init(allocator, session); 288 errdefer settings_view.deinit(allocator); 289 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 290 } 291 292 // the header has no auth tab in local mode, so keep the stack's 293 // children 1:1 with the tabs by skipping the auth view too. 294 if (!session.data.is_local) { 295 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 296 errdefer auth_view.deinit(allocator); 297 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 298 } 299 300 if (session.is_terminal) { 301 var quit_view = try Quit.View.init(allocator, session); 302 errdefer quit_view.deinit(allocator); 303 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 304 } 305 306 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 307 } 308 309 var self = View{ .box = box, .data = data, .session = session }; 310 self.getFocus().child_id = box.children.keys()[header_index]; 311 return self; 312 } 313 314 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 315 self.box.deinit(allocator); 316 } 317 318 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 319 self.clearGrid(); 320 const header = &self.box.children.values()[header_index].widget.repo_header; 321 const stack = &self.box.children.values()[stack_index].widget.stack; 322 323 // each header tab maps 1:1 to a stack child by position. mirror the 324 // selection into current_page so the host can push the matching url; 325 // all repo tabs share the .repo parent, so this stays on the page 326 // rather than navigating. 327 if (header.getSelectedIndex()) |index| { 328 stack.getFocus().child_id = stack.children.keys()[index]; 329 switch (std.meta.activeTag(stack.children.values()[index])) { 330 // files/commits carry this page's content route (directory / log 331 // page); settings/auth carry only "owner/name". 332 .repo_commits => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 333 // the refs tab mirrors this page's windowed column. 334 .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, 335 // the issues tab mirrors this page's tag filter (issue urls 336 // themselves never carry the tag). 337 .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, 338 .home_settings => { 339 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 340 self.session.data.current_page = .{ .repo_settings = identity }; 341 }, 342 .home_auth => { 343 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 344 self.session.data.current_page = .{ .repo_auth = identity }; 345 }, 346 // the quit tab is tty-only and not a route, so leave current_page 347 // alone (nothing to mirror into the url). 348 .quit => {}, 349 // the files tab (this page's directory route) 350 else => self.session.data.current_page = .{ .repo_files = self.data.route_name }, 351 } 352 } 353 try self.box.build(allocator, constraint, root_focus); 354 } 355 356 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 357 if (self.getFocus().child_id) |child_id| { 358 if (self.box.children.getIndex(child_id)) |current_index| { 359 const child = &self.box.children.values()[current_index].widget; 360 var index = current_index; 361 362 const direction = inp.vertDirection(key); 363 364 switch (direction) { 365 .up => { 366 switch (child.*) { 367 .repo_header => { 368 try child.input(allocator, key, root_focus); 369 }, 370 .stack => { 371 if (child.stack.getSelected()) |selected_widget| { 372 const at_top = switch (selected_widget.*) { 373 .repo_files => |*v| v.getSelectedIndex() == 0, 374 .repo_commits => |*v| v.getSelectedIndex() == 0, 375 .repo_refs => |*v| v.getSelectedIndex() == 0, 376 .repo_issues => |*v| v.getSelectedIndex() == 0, 377 .home_settings => |*v| v.getSelectedIndex() == 0, 378 .home_auth => |*v| v.getSelectedIndex() == 0, 379 .quit => |*v| v.getSelectedIndex() == 0, 380 else => false, 381 }; 382 if (at_top) { 383 index = header_index; 384 } else { 385 try child.input(allocator, key, root_focus); 386 } 387 } 388 }, 389 else => {}, 390 } 391 }, 392 .down => { 393 switch (child.*) { 394 .repo_header => { 395 index = stack_index; 396 }, 397 .stack => { 398 try child.input(allocator, key, root_focus); 399 }, 400 else => {}, 401 } 402 }, 403 .none => { 404 try child.input(allocator, key, root_focus); 405 }, 406 } 407 408 if (index != current_index) { 409 root_focus.setFocus(self.box.children.keys()[index]); 410 } 411 } 412 } 413 } 414 415 pub fn clearGrid(self: *View) void { 416 self.box.clearGrid(); 417 } 418 419 pub fn getGrid(self: View) ?Grid { 420 return self.box.getGrid(); 421 } 422 423 pub fn getFocus(self: *View) *Focus { 424 return self.box.getFocus(); 425 } 426 }; 427