╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const pch = @import("../patch.zig"); 4 const notify = @import("../notify.zig"); 5 const ui = @import("../ui.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const hash = xit.hash; 9 const xitui = xit.xitui; 10 const wgt = xitui.widget; 11 const layout = xitui.layout; 12 const Key = xitui.input.Key; 13 const Grid = xitui.grid.Grid; 14 const Focus = xitui.focus.Focus; 15 const inp = @import("./input.zig"); 16 17 pub const Header = @import("./Repo/Header.zig"); 18 pub const Files = @import("./Repo/Files.zig"); 19 pub const Markdown = @import("./Repo/Markdown.zig"); 20 pub const Commits = @import("./Repo/Commits.zig"); 21 pub const Diff = @import("./Repo/Diff.zig"); 22 pub const Refs = @import("./Repo/Refs.zig"); 23 pub const Discussions = @import("./Repo/Discussions.zig"); 24 pub const Issues = @import("./Repo/Issues.zig"); 25 pub const Patches = @import("./Repo/Patches.zig"); 26 pub const Comment = @import("./Repo/Comment.zig"); 27 pub const Undo = @import("./Repo/Undo.zig"); 28 pub const Events = @import("./Repo/Events.zig"); 29 pub const Settings = @import("./Repo/Settings.zig"); 30 pub const Members = @import("./Repo/Members.zig"); 31 pub const Branches = @import("./Repo/Branches.zig"); 32 pub const Quit = @import("./Quit.zig"); 33 34 header: Header, 35 repo: evt.Repo.Record, 36 handle: ui.RepoHandle, 37 files: Files, 38 changes: Changes, 39 refs: Refs, 40 discussions: Discussions, 41 issues: Issues, 42 patches: Patches, 43 events: Events, 44 undo: ?Undo = null, 45 settings: ?Settings = null, 46 user_notifications: ui.UserNotifications, 47 quit: Quit, 48 49 const Self = @This(); 50 51 const Changes = union(enum) { 52 commits: Commits, 53 diff: Diff, 54 }; 55 56 pub fn init( 57 arena: *std.heap.ArenaAllocator, 58 session: *ui.Session, 59 route: ui.RoutablePage, 60 ) !Self { 61 const DB = evt.AdminDB; 62 const hash_kind = evt.admin_repo_opts.hash; 63 64 // every repo route stores its identity as "owner:name" (or elides it in local mode) 65 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 66 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 67 // files, commits and diff share the requested ref, or the default branch. 68 // directories and hunk windows only apply to their own tab. 69 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 70 .repo_files => |f| f.ref_kind, 71 .repo_commits => |c| c.ref_or_oid, 72 .repo_diff => |d| d.ref_or_oid, 73 else => null, 74 }; 75 const requested_ref_value: []const u8 = switch (route) { 76 .repo_files => |*f| f.ref_value.slice(), 77 .repo_commits => |*c| c.value.slice(), 78 .repo_diff => |*d| d.value.slice(), 79 else => "", 80 }; 81 const patchrev_id: []const u8 = switch (route) { 82 .repo_files => |*f| f.patchrev_id.slice(), 83 .repo_diff => |*d| d.patchrev_id.slice(), 84 else => "", 85 }; 86 const files_dir = switch (route) { 87 .repo_files => |*f| f.path.slice(), 88 else => "", 89 }; 90 const files_line = switch (route) { 91 .repo_files => |f| f.line, 92 else => 0, 93 }; 94 const files_find: []const u8 = switch (route) { 95 .repo_files => |*f| f.find.slice(), 96 else => "", 97 }; 98 // the commits view's pane shows the whole message of the commit it walks from 99 const commits_message = route == .repo_commits and route.repo_commits.message; 100 const commits_base_oid: []const u8 = switch (route) { 101 .repo_commits => |*c| c.base_oid.slice(), 102 else => "", 103 }; 104 const commits_search: []const u8 = switch (route) { 105 .repo_commits => |*c| c.search.slice(), 106 else => "", 107 }; 108 const commits_from: []const u8 = switch (route) { 109 .repo_commits => |*c| c.from.slice(), 110 else => "", 111 }; 112 const commits_merge: []const u8 = switch (route) { 113 .repo_commits => |*c| c.merge.slice(), 114 else => "", 115 }; 116 // the refs tab windows one column at a time: `refs_from` (a url-encoded 117 // ref name) roots `refs_kind`'s column, the other stays at its first window. 118 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 119 .repo_refs => |r| r.kind, 120 else => .branch, 121 }; 122 const refs_from: []const u8 = switch (route) { 123 .repo_refs => |*r| r.from.slice(), 124 else => "", 125 }; 126 const refs_search: []const u8 = switch (route) { 127 .repo_refs => |*r| r.search.slice(), 128 else => "", 129 }; 130 const discussions_search: []const u8 = switch (route) { 131 .repo_discussions => |*t| t.search.slice(), 132 else => "", 133 }; 134 const discussions_selected: []const u8 = switch (route) { 135 .repo_discussions => |*t| t.selected.slice(), 136 else => "", 137 }; 138 const discussions_comment: []const u8 = switch (route) { 139 .repo_discussions => |*t| t.comment.slice(), 140 else => "", 141 }; 142 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 143 .repo_discussions => |t| t.view, 144 else => .recent, 145 }; 146 const discussions_comments_start: usize = switch (route) { 147 .repo_discussions => |t| t.comments_start, 148 else => 0, 149 }; 150 // the issues tab's search, the issue its window is rooted at, and the view 151 // it shows. 152 const issues_search: []const u8 = switch (route) { 153 .repo_issues => |*i| i.search.slice(), 154 else => "", 155 }; 156 const issues_selected: []const u8 = switch (route) { 157 .repo_issues => |*i| i.selected.slice(), 158 else => "", 159 }; 160 const issues_comment: []const u8 = switch (route) { 161 .repo_issues => |*i| i.comment.slice(), 162 else => "", 163 }; 164 const issues_theirs: []const u8 = switch (route) { 165 .repo_issues => |*i| i.theirs.slice(), 166 else => "", 167 }; 168 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 169 .repo_issues => |i| i.view, 170 else => .open, 171 }; 172 const issues_comments_start: usize = switch (route) { 173 .repo_issues => |i| i.comments_start, 174 else => 0, 175 }; 176 const patches_search: []const u8 = switch (route) { 177 .repo_patches => |*p| p.search.slice(), 178 else => "", 179 }; 180 const patches_selected: []const u8 = switch (route) { 181 .repo_patches => |*p| p.selected.slice(), 182 else => "", 183 }; 184 const patches_comment: []const u8 = switch (route) { 185 .repo_patches => |*p| p.comment.slice(), 186 else => "", 187 }; 188 const patches_theirs: []const u8 = switch (route) { 189 .repo_patches => |*p| p.theirs.slice(), 190 else => "", 191 }; 192 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 193 .repo_patches => |p| p.view, 194 else => .open, 195 }; 196 const patches_comments_start: usize = switch (route) { 197 .repo_patches => |p| p.comments_start, 198 else => 0, 199 }; 200 const events_kind: ?evt.EventKind = switch (route) { 201 .repo_events => |e| e.kind, 202 else => null, 203 }; 204 const events_view: ui.RoutablePage.EventsView = switch (route) { 205 .repo_events => |e| e.view, 206 else => .active, 207 }; 208 const events_moment: ?u64 = switch (route) { 209 .repo_events => |e| e.moment, 210 else => null, 211 }; 212 const events_selected: []const u8 = switch (route) { 213 .repo_events => |*e| e.selected.slice(), 214 else => "", 215 }; 216 const members_route: ui.RoutablePage.RepoMembersRoute = switch (route) { 217 .repo_members => |r| r, 218 else => .{ .name = .{} }, 219 }; 220 // the thread the route shows, whose notif viewing it reads 221 const viewed_thread: []const u8 = switch (route) { 222 .repo_issues => |*i| if (i.view == .conflicts) "" else i.selected.slice(), 223 .repo_patches => |*p| if (p.view == .conflicts) "" else p.selected.slice(), 224 .repo_discussions => |*t| t.selected.slice(), 225 else => "", 226 }; 227 228 // where the on-disk repo lives (null keeps the views' empty fallback), plus 229 // the repo and owner-name metadata the header shows. local mode already 230 // knows all three; the server paths resolve them from the admin db. 231 var source: ?ui.RepoSource = null; 232 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 233 // local mode has no owner 234 var owner_id: [evt.event_id_size]u8 = @splat(0); 235 var repo: evt.Repo.Record = undefined; 236 // local mode owns its repo 237 var role: evt.Repo.Role = .owner; 238 var owner_name: []const u8 = undefined; 239 if (session.local) |local| { 240 source = local; 241 // local routes elide the identity, so the display name comes from the 242 // repo's directory rather than the route. 243 repo = .{ 244 .event = .{ 245 .user_id = @splat('0'), 246 .name = try arena.allocator().dupe(u8, std.Io.Dir.path.basename(local.path)), 247 .description = "", 248 // local mode shows every tab 249 .discuss_role = .read, 250 .issue_role = .read, 251 .patch_role = .read, 252 }, 253 }; 254 owner_name = ""; 255 } else { 256 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 257 const io = session.io orelse return error.NoMoment; 258 const users_dir = session.users_dir orelse return error.NoMoment; 259 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, users_dir, repo_identity.owner, repo_identity.name, session.userId())) orelse return error.NotFound; 260 // a repo the session can't read doesn't exist to it 261 role = found.role orelse return error.NotFound; 262 repo = found.repo; 263 repo_id_maybe = found.event_id; 264 265 // resolve the creating user so the header can show their name to the left 266 // of the repo title. 267 owner_id = try evt.parseEventId(&repo.event.user_id); 268 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, &owner_id)) orelse return error.NotFound; 269 owner_name = owner.event.name; 270 271 // the repo's working copy lives in its owner's dir. 272 source = .{ 273 .path = try evt.repoPath(arena.allocator(), users_dir, &owner_id, &found.event_id), 274 .repo_kind = .xit, 275 }; 276 } 277 278 const handle = ui.RepoHandle{ 279 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 280 .viewer = try ui.Viewer.init(session, arena, role), 281 .read_access = repo.event.read_access, 282 }; 283 284 // open the repo once for every tab. files and changes share a ref or revision. 285 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 286 // the events tab needs write access, like undo 287 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 288 // a tab that is off doesn't exist 289 const route_thread_kind: ?evt.EventKind = switch (route) { 290 .repo_discussions => .discuss, 291 .repo_issues => .issue, 292 .repo_patches => .patch, 293 else => null, 294 }; 295 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 296 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 297 const undo_clear = route == .repo_undo and route.repo_undo.clear; 298 // only xit repos have undo history, so it's read in their branch below 299 var undo_data: ?Undo = null; 300 // only the owner sees the settings, which show the repo's hash kind 301 var settings: ?Settings = null; 302 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 303 read: { 304 const io = session.io orelse break :read; 305 const src = source orelse break :read; 306 const gpa = arena.child_allocator; 307 switch (src.repo_kind) { 308 inline else => |repo_kind| { 309 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 310 defer any_repo.deinit(io, gpa); 311 switch (any_repo) { 312 inline else => |*opened| { 313 // local mode: bring the event db up to date with the events branch 314 if (session.local != null) { 315 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 316 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 317 } 318 if (session.data.host_kind == .server and handle.canUndo()) { 319 // a server page always resolved its repo id 320 const repo_id = repo_id_maybe orelse unreachable; 321 settings = .{ 322 .identity = try arena.allocator().dupe(u8, repo_identity.identity), 323 .name = repo.event.name, 324 .description = repo.event.description, 325 .access = repo.event.read_access, 326 .discuss_role = repo.event.discuss_role, 327 .issue_role = repo.event.issue_role, 328 .patch_role = repo.event.patch_role, 329 .hash_kind = opened.self_repo_opts.hash, 330 .view = switch (route) { 331 .repo_members => .members, 332 .repo_branches => .branches, 333 else => .settings, 334 }, 335 .members = try Members.init(io, gpa, arena, session.haxy_moment orelse return error.NoMoment, session.users_dir orelse return error.NoMoment, repo_identity.identity, &owner_id, &repo_id, members_route.start, if (members_route.confirm) |action| .{ .action = action, .name = members_route.user.slice() } else null), 336 .branches = try Branches.init(arena, repo_identity.identity, repo.event), 337 }; 338 } 339 // tabs switch in-page, so every tab's data is read here 340 if (repo_kind == .xit and handle.canWrite()) { 341 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 342 error.OutOfMemory => return err, 343 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 344 }; 345 } 346 // a merge: route stands for the log of what the merge brought in 347 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 348 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 349 else merged: { 350 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 351 break :merged .{ .object, merged.oid, merged.base_oid }; 352 }; 353 const files_data = if (patchrev_id.len != 0) 354 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 355 else 356 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, ref_or_oid, ref_value, files_dir, files_line, files_find); 357 const changes_data: Changes = if (route == .repo_diff) 358 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 359 else if (patchrev_id.len != 0) diff: { 360 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 361 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 362 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, handle, ref_or_oid, ref_value, commits_message, base_oid, commits_search, commits_from) }; 363 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 364 var discussions_data = try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view, tabViewer(handle, repo, .discuss)); 365 var issues_data = try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view, tabViewer(handle, repo, .issue)); 366 var patches_data = 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_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view, tabViewer(handle, repo, .patch)); 367 if (repo_kind == .xit and session.data.host_kind == .server) { 368 // a server page always resolved its repo id 369 const repo_id = repo_id_maybe orelse unreachable; 370 readNotify(opened.self_repo_opts, session, arena, opened, &owner_id, &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 371 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 372 }; 373 } 374 break :blk .{ 375 files_data, 376 changes_data, 377 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 378 discussions_data, 379 issues_data, 380 patches_data, 381 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 382 }; 383 }, 384 } 385 }, 386 } 387 } 388 const aa = arena.allocator(); 389 break :blk .{ 390 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 391 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 392 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 393 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 394 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 395 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 396 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 397 }; 398 }; 399 if (route == .repo_undo and undo_data == null) return error.NotFound; 400 if ((route == .repo_settings or route == .repo_members or route == .repo_branches) and settings == null) return error.NotFound; 401 if (undo_data) |*undo| undo.clear = undo_clear; 402 discussions.repo_source = source; 403 issues.repo_source = source; 404 patches.repo_source = source; 405 patches.repo_id = repo_id_maybe; 406 407 return .{ 408 // use the files tab's resolved ref for the header 409 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.search, issues.search, patches.search), 410 .repo = repo, 411 .handle = handle, 412 .files = files, 413 .changes = changes, 414 .refs = refs, 415 .discussions = discussions, 416 .issues = issues, 417 .patches = patches, 418 .events = events, 419 .undo = undo_data, 420 .settings = settings, 421 // after the shown thread's notif is marked read 422 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 423 .quit = Quit.init(), 424 }; 425 } 426 427 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 428 fn readNotify( 429 comptime repo_opts: rp.RepoOpts(.xit), 430 session: *ui.Session, 431 arena: *std.heap.ArenaAllocator, 432 repo: *rp.Repo(.xit, repo_opts), 433 owner_id: *const [evt.event_id_size]u8, 434 repo_id: *const [evt.event_id_size]u8, 435 viewed_thread: []const u8, 436 discussions: *Discussions, 437 issues: *Issues, 438 patches: *Patches, 439 ) !void { 440 const user_id = session.userId() orelse return; 441 const io = session.io orelse return error.NoMoment; 442 const users_dir = session.users_dir orelse return error.NoMoment; 443 const gpa = arena.child_allocator; 444 445 { 446 // the owner's plain data, which holds the subscriber index 447 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 448 defer owner_repo.deinit(io, gpa); 449 const owner_moment = try notify.latestMoment(&owner_repo); 450 451 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 452 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 453 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 454 } 455 456 if (viewed_thread.len == 0) return; 457 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 458 defer user_repo.deinit(io, gpa); 459 const latest = (try notify.latestMoment(&user_repo)) orelse return; 460 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 461 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 462 if (!item.unread) return; 463 // the page reads this moment or a newer one, so a child in it is shown 464 const moment = try evt.currentMoment(repo_opts, repo); 465 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 466 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 467 // a child the page can't show leaves it unread 468 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 469 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 470 } 471 472 // set the viewer's notify button on each of a window's threads 473 fn setNotifyActions( 474 arena: *std.heap.ArenaAllocator, 475 window: anytype, 476 owner_moment: ?evt.UserDB.HashMap(.read_only), 477 repo_id: *const [evt.event_id_size]u8, 478 user_id: *const [evt.event_id_size]u8, 479 ) !void { 480 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 481 for (items) |*entry| { 482 const thread_id = try evt.parseEventId(entry.id); 483 // muting drops the user from the index, so membership alone decides 484 const subscribed = if (owner_moment) |moment| try notify.isSubscribed(moment, &notify.idOf(repo_id, &thread_id), user_id) else false; 485 entry.notify_action = if (subscribed) .mute else .subscribe; 486 } 487 window.items = items; 488 } 489 490 // the viewer a tab's comment bar sees, which is none below the tab's role 491 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 492 const viewer = handle.viewer orelse return null; 493 const tab_role = repo.event.threadRole(kind) orelse return null; 494 return if (viewer.role.atLeast(tab_role)) viewer else null; 495 } 496 497 pub const View = struct { 498 box: wgt.Box(ui.Widget), 499 500 const header_index: usize = 0; 501 const stack_index: usize = 1; 502 503 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 504 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 505 errdefer box.deinit(allocator); 506 507 // build the header first so we can grab the files-tab id for the user 508 // view (it focuses there after login). 509 { 510 var header_view = try Header.View.init(allocator, data, session); 511 errdefer header_view.deinit(allocator); 512 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 513 } 514 515 { 516 var stack = try wgt.Stack(ui.Widget).init(allocator); 517 errdefer stack.deinit(allocator); 518 519 // files — the default tab: the current directory's listing. 520 { 521 var files_view = try Files.View.init(allocator, &data.files, session); 522 errdefer files_view.deinit(allocator); 523 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 524 } 525 526 switch (data.changes) { 527 .diff => |*diff| { 528 var diff_view = try Diff.View.init(allocator, diff, session); 529 errdefer diff_view.deinit(allocator); 530 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 531 }, 532 .commits => |*commits| { 533 var commits_view = try Commits.View.init(allocator, commits, session); 534 errdefer commits_view.deinit(allocator); 535 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 536 }, 537 } 538 539 // refs — the repo's branches and tags. 540 { 541 var refs_view = try Refs.View.init(allocator, &data.refs, session); 542 errdefer refs_view.deinit(allocator); 543 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 544 } 545 546 // discussions and their comment permalinks. 547 if (data.repo.event.discuss_role != null) { 548 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 549 errdefer discussions_view.deinit(allocator); 550 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 551 } 552 553 // issues — the repo's issue tracker and comment permalinks. 554 if (data.repo.event.issue_role != null) { 555 var issues_view = try Issues.View.init(allocator, &data.issues, session); 556 errdefer issues_view.deinit(allocator); 557 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 558 } 559 560 // patches and their comment permalinks. 561 if (data.repo.event.patch_role != null) { 562 var patches_view = try Patches.View.init(allocator, &data.patches, session); 563 errdefer patches_view.deinit(allocator); 564 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 565 } 566 567 if (data.handle.canWrite()) { 568 var events_view = try Events.View.init(allocator, &data.events, session); 569 errdefer events_view.deinit(allocator); 570 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 571 } 572 573 if (data.undo) |*undo| { 574 var undo_view = try Undo.View.init(allocator, undo, session); 575 errdefer undo_view.deinit(allocator); 576 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 577 } 578 579 if (data.settings) |*settings| { 580 var settings_view = try Settings.View.init(allocator, settings, session); 581 errdefer settings_view.deinit(allocator); 582 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 583 } 584 585 // the header shows new repo with a login, and new user without one 586 // and user only outside local mode, so keep the stack's children 587 // 1:1 with the tabs by skipping the same views. 588 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 589 if (session.data.user_id != null) { 590 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 591 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 592 errdefer new_repo_view.deinit(allocator); 593 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 594 } else if (session.data.host_kind == .server) { 595 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 596 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 597 errdefer new_user_view.deinit(allocator); 598 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 599 } 600 601 if (session.data.host_kind == .server) { 602 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 603 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 604 errdefer user_view.deinit(allocator); 605 try stack.children.put(allocator, user_view.getFocus().id, user_view); 606 } 607 608 if (session.is_terminal) { 609 var quit_view = try Quit.View.init(allocator, session); 610 errdefer quit_view.deinit(allocator); 611 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 612 } 613 614 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 615 } 616 617 var self = View{ .box = box }; 618 // a page opens on its tabs, except that search results open in the 619 // tab's own search box. 620 const results = data.files.find != null or 621 (data.changes == .commits and data.changes.commits.search != null) or 622 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 623 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 624 return self; 625 } 626 627 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 628 self.box.deinit(allocator); 629 } 630 631 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 632 self.clearGrid(); 633 const header = &self.box.children.values()[header_index].widget.repo_header; 634 const stack = &self.box.children.values()[stack_index].widget.stack; 635 636 // each header tab maps 1:1 to a stack child by position 637 if (header.getSelectedIndex()) |index| 638 stack.getFocus().child_id = stack.children.keys()[index]; 639 try self.box.build(allocator, constraint, root_focus); 640 } 641 642 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 643 const stack = &self.box.children.values()[stack_index].widget.stack; 644 if (self.getFocus().child_id) |child_id| { 645 if (self.box.children.getIndex(child_id)) |current_index| { 646 const child = &self.box.children.values()[current_index].widget; 647 var index = current_index; 648 649 const direction = inp.vertDirection(key); 650 651 switch (direction) { 652 .up => { 653 switch (child.*) { 654 .repo_header => { 655 try child.input(allocator, key, root_focus); 656 }, 657 .stack => { 658 if (child.stack.getSelected()) |selected_widget| { 659 if (selected_widget.atTop(root_focus)) { 660 index = header_index; 661 } else { 662 try child.input(allocator, key, root_focus); 663 } 664 } 665 }, 666 else => {}, 667 } 668 }, 669 .down => { 670 switch (child.*) { 671 .repo_header => { 672 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 673 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 674 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 675 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 676 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 677 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 678 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 679 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 680 else => {}, 681 }; 682 index = stack_index; 683 }, 684 .stack => { 685 try child.input(allocator, key, root_focus); 686 }, 687 else => {}, 688 } 689 }, 690 .none => { 691 try child.input(allocator, key, root_focus); 692 }, 693 } 694 695 if (index != current_index) { 696 root_focus.setFocus(self.box.children.keys()[index]); 697 } 698 } 699 } 700 } 701 702 pub fn clearGrid(self: *View) void { 703 self.box.clearGrid(); 704 } 705 706 pub fn getGrid(self: View) ?Grid { 707 return self.box.getGrid(); 708 } 709 710 pub fn getFocus(self: *View) *Focus { 711 return self.box.getFocus(); 712 } 713 }; 714