╭ 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 Patches = @import("./Repo/Patches.zig"); 20 pub const Discussions = @import("./Repo/Discussions.zig"); 21 pub const Comment = @import("./Repo/Comment.zig"); 22 pub const Events = @import("./Repo/Events.zig"); 23 pub const Settings = @import("./Settings.zig"); 24 pub const Auth = @import("./Auth.zig"); 25 pub const Quit = @import("./Quit.zig"); 26 27 header: Header, 28 repo: evt.Repo.Record, 29 files: Files, 30 commits: Commits, 31 refs: Refs, 32 issues: Issues, 33 patches: Patches, 34 discussions: Discussions, 35 events: Events, 36 settings: Settings, 37 auth: Auth, 38 quit: Quit, 39 40 const Self = @This(); 41 42 pub fn init( 43 arena: *std.heap.ArenaAllocator, 44 session: *ui.Session, 45 route: ui.RoutablePage, 46 ) !Self { 47 const DB = evt.AdminDB; 48 const hash_kind = evt.admin_repo_opts.hash; 49 50 // every repo route stores its identity as "owner/name" (or elides it in 51 // local mode); files and commits carry their remaining fields directly. 52 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 53 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 54 const location = ui.RoutablePage.RepoLocation{ .repo = repo_identity.identity }; 55 // the files and commits tabs share one ref/oid: whichever the incoming route 56 // names (it rides on the route's target tab), or the default branch when 57 // neither tab is targeted. building both views at it keeps switching tabs 58 // (even by key, without a reload) on the same ref. a null ref means the 59 // default branch (Files/Commits.init resolve it). the directory and the diff 60 // window only apply to their own tab. 61 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 62 .repo_files => |f| f.ref_kind, 63 .repo_commits => |c| c.ref_or_oid, 64 else => null, 65 }; 66 const requested_ref_value: []const u8 = switch (route) { 67 .repo_files => |*f| f.ref_value.slice(), 68 .repo_commits => |*c| c.value.slice(), 69 else => "", 70 }; 71 const files_dir = switch (route) { 72 .repo_files => |*f| f.path.slice(), 73 else => "", 74 }; 75 const files_line = switch (route) { 76 .repo_files => |f| f.line, 77 else => 0, 78 }; 79 // what the commits view's pane shows for the commit it walks from: a diff 80 // window (with the file it's filtered to) or that commit's message. 81 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 82 .repo_commits => |c| c.content, 83 else => .{ .diff = .{} }, 84 }; 85 // the refs tab windows one column at a time: `refs_from` (a url-encoded 86 // ref name) roots `refs_kind`'s column, the other stays at its first window. 87 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 88 .repo_refs => |r| r.kind, 89 else => .branch, 90 }; 91 const refs_from: []const u8 = switch (route) { 92 .repo_refs => |*r| r.from.slice(), 93 else => "", 94 }; 95 // the issues tab's tag filter, the issue its window is rooted at, and the 96 // view it shows. 97 const issues_tag: []const u8 = switch (route) { 98 .repo_issues => |*i| i.tag.slice(), 99 else => "", 100 }; 101 const issues_selected: []const u8 = switch (route) { 102 .repo_issues => |*i| i.selected.slice(), 103 else => "", 104 }; 105 const issues_comment: []const u8 = switch (route) { 106 .repo_issues => |*i| i.comment.slice(), 107 else => "", 108 }; 109 const issues_theirs: []const u8 = switch (route) { 110 .repo_issues => |*i| i.theirs.slice(), 111 else => "", 112 }; 113 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 114 .repo_issues => |i| i.view, 115 else => .open, 116 }; 117 const issues_comments_start: usize = switch (route) { 118 .repo_issues => |i| i.comments_start, 119 else => 0, 120 }; 121 const patches_tag: []const u8 = switch (route) { 122 .repo_patches => |*p| p.tag.slice(), 123 else => "", 124 }; 125 const patches_selected: []const u8 = switch (route) { 126 .repo_patches => |*p| p.selected.slice(), 127 else => "", 128 }; 129 const patches_comment: []const u8 = switch (route) { 130 .repo_patches => |*p| p.comment.slice(), 131 else => "", 132 }; 133 const patches_theirs: []const u8 = switch (route) { 134 .repo_patches => |*p| p.theirs.slice(), 135 else => "", 136 }; 137 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 138 .repo_patches => |p| p.view, 139 else => .open, 140 }; 141 const patches_comments_start: usize = switch (route) { 142 .repo_patches => |p| p.comments_start, 143 else => 0, 144 }; 145 const discussions_tag: []const u8 = switch (route) { 146 .repo_discussions => |*t| t.tag.slice(), 147 else => "", 148 }; 149 const discussions_selected: []const u8 = switch (route) { 150 .repo_discussions => |*t| t.selected.slice(), 151 else => "", 152 }; 153 const discussions_comment: []const u8 = switch (route) { 154 .repo_discussions => |*t| t.comment.slice(), 155 else => "", 156 }; 157 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 158 .repo_discussions => |t| t.view, 159 else => .recent, 160 }; 161 const discussions_comments_start: usize = switch (route) { 162 .repo_discussions => |t| t.comments_start, 163 else => 0, 164 }; 165 const events_kind: ?evt.EventKind = switch (route) { 166 .repo_events => |e| e.kind, 167 else => null, 168 }; 169 const events_view: ui.RoutablePage.EventsView = switch (route) { 170 .repo_events => |e| e.view, 171 else => .active, 172 }; 173 const events_selected: []const u8 = switch (route) { 174 .repo_events => |*e| e.selected.slice(), 175 else => "", 176 }; 177 178 // where the on-disk repo lives (null keeps the views' empty fallback), plus 179 // the repo and owner-name metadata the header shows. local mode already 180 // knows all three; the server paths resolve them from the admin db. 181 var source: ?ui.RepoSource = null; 182 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 183 var repo: evt.Repo.Record = undefined; 184 var owner_name: []const u8 = undefined; 185 if (session.local) |local| { 186 source = local; 187 // local routes elide the identity, so the display name comes from the 188 // repo's directory rather than the route. 189 repo = .{ .event = .{ 190 .user_id = "", 191 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 192 .description = "", 193 } }; 194 owner_name = ""; 195 } else { 196 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 197 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 198 repo = found.repo; 199 repo_id_maybe = found.event_id; 200 201 // resolve the creating user so the header can show their name to the left 202 // of the repo title. 203 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 204 owner_name = owner.event.name; 205 206 // the repo's working copy lives at <repos_dir>/<hex event id>. 207 if (session.repos_dir) |repos_dir| { 208 const hex = std.fmt.bytesToHex(found.event_id, .lower); 209 source = .{ 210 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 211 .repo_kind = .xit, 212 }; 213 } 214 } 215 216 // open the on-disk repo once and read every tab's data from it. files and 217 // commits resolve the same requested ref (the default branch when the 218 // route named none), so they end up viewing the same one. no filesystem 219 // (wasm), nowhere to look, or a failed open: empty tabs. 220 const files, const commits, const refs, var issues, var patches, var discussions, const events = blk: { 221 read: { 222 const io = session.io orelse break :read; 223 const src = source orelse break :read; 224 const gpa = arena.child_allocator; 225 switch (src.repo_kind) { 226 inline else => |repo_kind| { 227 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 228 defer any_repo.deinit(io, gpa); 229 switch (any_repo) { 230 inline else => |*opened| { 231 // local mode: bring the event db up to date with the events branch 232 if (session.local != null) { 233 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 234 try @import("../patch.zig").refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null); 235 } 236 const files_data = try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, requested_ref_or_oid, requested_ref_value, files_dir, files_line); 237 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 238 break :blk .{ 239 files_data, 240 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, location, requested_ref_or_oid, requested_ref_value, commits_content, null), 241 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 242 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), 243 try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, target_branch, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 244 try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 245 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, events_view, events_kind, events_selected, session.local != null, session.data.sync_failure), 246 }; 247 }, 248 } 249 }, 250 } 251 } 252 const aa = arena.allocator(); 253 break :blk .{ 254 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 255 try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 256 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 257 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 258 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 259 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 260 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 261 }; 262 }; 263 issues.repo_source = source; 264 patches.repo_source = source; 265 patches.repo_id = repo_id_maybe; 266 discussions.repo_source = source; 267 268 return .{ 269 // files and commits resolve the same ref, so either's serves the header, 270 // which points both tabs at it. 271 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues.tag, patches.tag, discussions.tag), 272 .repo = repo, 273 .files = files, 274 .commits = commits, 275 .refs = refs, 276 .issues = issues, 277 .patches = patches, 278 .discussions = discussions, 279 .events = events, 280 .settings = Settings.init(), 281 .auth = Auth.init(), 282 .quit = Quit.init(), 283 }; 284 } 285 286 pub const View = struct { 287 box: wgt.Box(ui.Widget), 288 289 const header_index: usize = 0; 290 const stack_index: usize = 1; 291 292 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 293 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 294 errdefer box.deinit(allocator); 295 296 // build the header first so we can grab the files-tab id for the auth 297 // view (it focuses there after login). 298 { 299 var header_view = try Header.View.init(allocator, &data.header, data.commits.commit_count, session); 300 errdefer header_view.deinit(allocator); 301 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 302 } 303 304 { 305 var stack = try wgt.Stack(ui.Widget).init(allocator); 306 errdefer stack.deinit(allocator); 307 308 // files — the default tab: the current directory's listing. 309 { 310 var files_view = try Files.View.init(allocator, &data.files, session); 311 errdefer files_view.deinit(allocator); 312 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 313 } 314 315 // commits — the current page of the commit log. 316 { 317 var commits_view = try Commits.View.init(allocator, &data.commits, session); 318 errdefer commits_view.deinit(allocator); 319 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 320 } 321 322 // refs — the repo's branches and tags. 323 { 324 var refs_view = try Refs.View.init(allocator, &data.refs, session); 325 errdefer refs_view.deinit(allocator); 326 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 327 } 328 329 // issues — the repo's issue tracker and comment permalinks. 330 { 331 var issues_view = try Issues.View.init(allocator, &data.issues, session); 332 errdefer issues_view.deinit(allocator); 333 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 334 } 335 336 // patches and their comment permalinks. 337 { 338 var patches_view = try Patches.View.init(allocator, &data.patches, session); 339 errdefer patches_view.deinit(allocator); 340 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 341 } 342 343 // discussions and their comment permalinks. 344 { 345 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 346 errdefer discussions_view.deinit(allocator); 347 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 348 } 349 350 { 351 var events_view = try Events.View.init(allocator, &data.events, session); 352 errdefer events_view.deinit(allocator); 353 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 354 } 355 356 // the header only shows the settings tab with a login and the auth 357 // tab outside local mode, so keep the stack's children 1:1 with 358 // the tabs by skipping the same views. 359 if (session.data.user_id != null) { 360 var settings_view = try Settings.View.init(allocator, session); 361 errdefer settings_view.deinit(allocator); 362 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 363 } 364 365 if (session.data.host_kind == .server) { 366 var auth_view = try Auth.View.init(allocator, &data.auth, session); 367 errdefer auth_view.deinit(allocator); 368 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 369 } 370 371 if (session.is_terminal) { 372 var quit_view = try Quit.View.init(allocator, session); 373 errdefer quit_view.deinit(allocator); 374 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 375 } 376 377 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 378 } 379 380 var self = View{ .box = box }; 381 self.getFocus().child_id = box.children.keys()[header_index]; 382 return self; 383 } 384 385 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 386 self.box.deinit(allocator); 387 } 388 389 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 390 self.clearGrid(); 391 const header = &self.box.children.values()[header_index].widget.repo_header; 392 const stack = &self.box.children.values()[stack_index].widget.stack; 393 394 // each header tab maps 1:1 to a stack child by position 395 if (header.getSelectedIndex()) |index| 396 stack.getFocus().child_id = stack.children.keys()[index]; 397 try self.box.build(allocator, constraint, root_focus); 398 } 399 400 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 401 const stack = &self.box.children.values()[stack_index].widget.stack; 402 if (self.getFocus().child_id) |child_id| { 403 if (self.box.children.getIndex(child_id)) |current_index| { 404 const child = &self.box.children.values()[current_index].widget; 405 var index = current_index; 406 407 const direction = inp.vertDirection(key); 408 409 switch (direction) { 410 .up => { 411 switch (child.*) { 412 .repo_header => { 413 try child.input(allocator, key, root_focus); 414 }, 415 .stack => { 416 if (child.stack.getSelected()) |selected_widget| { 417 if (selected_widget.atTop(root_focus)) { 418 index = header_index; 419 } else { 420 try child.input(allocator, key, root_focus); 421 } 422 } 423 }, 424 else => {}, 425 } 426 }, 427 .down => { 428 switch (child.*) { 429 .repo_header => { 430 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 431 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 432 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 433 else => {}, 434 }; 435 index = stack_index; 436 }, 437 .stack => { 438 try child.input(allocator, key, root_focus); 439 }, 440 else => {}, 441 } 442 }, 443 .none => { 444 try child.input(allocator, key, root_focus); 445 }, 446 } 447 448 if (index != current_index) { 449 root_focus.setFocus(self.box.children.keys()[index]); 450 } 451 } 452 } 453 } 454 455 pub fn clearGrid(self: *View) void { 456 self.box.clearGrid(); 457 } 458 459 pub fn getGrid(self: View) ?Grid { 460 return self.box.getGrid(); 461 } 462 463 pub fn getFocus(self: *View) *Focus { 464 return self.box.getFocus(); 465 } 466 }; 467