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