╭ 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 Comment = @import("./Repo/Comment.zig"); 20 pub const Settings = @import("./Settings.zig"); 21 pub const Auth = @import("./Auth.zig"); 22 pub const Quit = @import("./Quit.zig"); 23 24 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_identity_max_len); 25 26 header: Header, 27 repo: evt.Repo.Record, 28 files: Files, 29 commits: Commits, 30 refs: Refs, 31 issues: Issues, 32 settings: Settings, 33 auth: Auth, 34 quit: Quit, 35 // the full files route this page renders, mirrored into current_page when 36 // the files tab is selected. 37 route_name: ui.RoutablePage.RepoFilesRoute, 38 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 39 // when the commits tab is selected. 40 commits_route_name: ui.RoutablePage.RepoCommitsRoute, 41 // "owner/name" alone, mirrored when the settings/auth tab is selected. 42 identity: Array, 43 44 const Self = @This(); 45 46 pub fn init( 47 arena: *std.heap.ArenaAllocator, 48 session: *ui.Session, 49 route: ui.RoutablePage, 50 ) !Self { 51 const DB = evt.AdminDB; 52 const hash_kind = evt.admin_repo_opts.hash; 53 54 // every repo route stores its identity as "owner/name" (or elides it in 55 // local mode); files and commits carry their remaining fields directly. 56 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 57 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 58 // the files and commits tabs share one ref/oid: whichever the incoming route 59 // names (it rides on the route's target tab), or the default branch when 60 // neither tab is targeted. building both views at it keeps switching tabs 61 // (even by key, without a reload) on the same ref. a null ref means the 62 // default branch (Files/Commits.init resolve it). the directory and the diff 63 // window only apply to their own tab. 64 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 65 .repo_files => |f| f.ref_kind, 66 .repo_commits => |c| c.ref_or_oid, 67 else => null, 68 }; 69 const requested_ref_value: []const u8 = switch (route) { 70 .repo_files => |*f| f.ref_value.slice(), 71 .repo_commits => |*c| c.value.slice(), 72 else => "", 73 }; 74 const files_dir = switch (route) { 75 .repo_files => |*f| f.path.slice(), 76 else => "", 77 }; 78 const files_line = switch (route) { 79 .repo_files => |f| f.line, 80 else => 0, 81 }; 82 // what the commits view's pane shows for the commit it walks from: a diff 83 // window (with the file it's filtered to) or that commit's message. 84 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 85 .repo_commits => |c| c.content, 86 else => .{ .diff = .{} }, 87 }; 88 const commits_start = switch (commits_content) { 89 .diff => |d| d.start, 90 .message => 0, 91 }; 92 // the refs tab windows one column at a time: `refs_from` (a url-encoded 93 // ref name) roots `refs_kind`'s column, the other stays at its first window. 94 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 95 .repo_refs => |r| r.kind, 96 else => .branch, 97 }; 98 const refs_from: []const u8 = switch (route) { 99 .repo_refs => |*r| r.from.slice(), 100 else => "", 101 }; 102 // the issues tab's tag filter, the issue its window is rooted at, and the 103 // view it shows. 104 const issues_tag: []const u8 = switch (route) { 105 .repo_issues => |*i| i.tag.slice(), 106 else => "", 107 }; 108 const issues_selected: []const u8 = switch (route) { 109 .repo_issues => |*i| i.selected.slice(), 110 else => "", 111 }; 112 const issues_comment: []const u8 = switch (route) { 113 .repo_issues => |*i| i.comment.slice(), 114 else => "", 115 }; 116 const issues_theirs: []const u8 = switch (route) { 117 .repo_issues => |*i| i.theirs.slice(), 118 else => "", 119 }; 120 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 121 .repo_issues => |i| i.view, 122 else => .open, 123 }; 124 const issues_comments_start: usize = switch (route) { 125 .repo_issues => |i| i.comments_start, 126 else => 0, 127 }; 128 129 // where the on-disk repo lives (null keeps the views' empty fallback), plus 130 // the repo and owner-name metadata the header shows. local mode already 131 // knows all three; the server paths resolve them from the admin db. 132 var source: ?ui.RepoSource = null; 133 var repo: evt.Repo.Record = undefined; 134 var owner_name: []const u8 = undefined; 135 if (session.local) |local| { 136 source = local; 137 // local routes elide the identity, so the display name comes from the 138 // repo's directory rather than the route. 139 repo = .{ .event = .{ 140 .user_id = "", 141 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 142 .description = "", 143 } }; 144 owner_name = ""; 145 } else { 146 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 147 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 148 repo = found.repo; 149 150 // resolve the creating user so the header can show their name to the left 151 // of the repo title. 152 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 153 owner_name = owner.event.name; 154 155 // the repo's working copy lives at <repos_dir>/<hex event id>. 156 if (session.repos_dir) |repos_dir| { 157 const hex = std.fmt.bytesToHex(found.event_id, .lower); 158 source = .{ 159 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 160 .repo_kind = .xit, 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 if (self.getFocus().child_id) |child_id| { 363 if (self.box.children.getIndex(child_id)) |current_index| { 364 const child = &self.box.children.values()[current_index].widget; 365 var index = current_index; 366 367 const direction = inp.vertDirection(key); 368 369 switch (direction) { 370 .up => { 371 switch (child.*) { 372 .repo_header => { 373 try child.input(allocator, key, root_focus); 374 }, 375 .stack => { 376 if (child.stack.getSelected()) |selected_widget| { 377 const at_top = switch (selected_widget.*) { 378 .repo_files => |*v| v.getSelectedIndex() == 0, 379 .repo_commits => |*v| v.getSelectedIndex() == 0, 380 .repo_refs => |*v| v.getSelectedIndex() == 0, 381 .repo_issues => |*v| v.getSelectedIndex() == 0, 382 .home_settings => |*v| v.getSelectedIndex() == 0, 383 .home_auth => |*v| v.getSelectedIndex() == 0, 384 .quit => |*v| v.getSelectedIndex() == 0, 385 else => false, 386 }; 387 if (at_top) { 388 index = header_index; 389 } else { 390 try child.input(allocator, key, root_focus); 391 } 392 } 393 }, 394 else => {}, 395 } 396 }, 397 .down => { 398 switch (child.*) { 399 .repo_header => { 400 index = stack_index; 401 }, 402 .stack => { 403 try child.input(allocator, key, root_focus); 404 }, 405 else => {}, 406 } 407 }, 408 .none => { 409 try child.input(allocator, key, root_focus); 410 }, 411 } 412 413 if (index != current_index) { 414 root_focus.setFocus(self.box.children.keys()[index]); 415 } 416 } 417 } 418 } 419 420 pub fn clearGrid(self: *View) void { 421 self.box.clearGrid(); 422 } 423 424 pub fn getGrid(self: View) ?Grid { 425 return self.box.getGrid(); 426 } 427 428 pub fn getFocus(self: *View) *Focus { 429 return self.box.getFocus(); 430 } 431 }; 432