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