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