╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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("./Fork/Header.zig"); 16 pub const Files = @import("./Repo/Files.zig"); 17 pub const Commits = @import("./Repo/Commits.zig"); 18 pub const Diff = @import("./Repo/Diff.zig"); 19 pub const Patches = @import("./Repo/Patches.zig"); 20 pub const Quit = @import("./Quit.zig"); 21 22 header: Header, 23 files: Files, 24 commits: Commits, 25 patch: Patches, 26 diff: Diff, 27 // the delete tab's form, for the forker only 28 delete: ?ui.Delete, 29 user_notifications: ui.UserNotifications, 30 quit: Quit, 31 32 const Self = @This(); 33 34 pub fn init(arena: *std.heap.ArenaAllocator, session: *ui.Session, route: ui.RoutablePage) !Self { 35 const io = session.io orelse return error.NotFound; 36 const users_dir = session.users_dir orelse return error.NotFound; 37 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 38 const route_fork = route.forkRoute() orelse return error.UnexpectedRoute; 39 // the route's identity names the forker and the repo name the fork was made under 40 const identity = ui.RoutablePage.RepoIdentity.parse(route_fork.name.slice()) orelse return error.NotFound; 41 const id = evt.parseEventId(route_fork.id.slice()) catch return error.NotFound; 42 const gpa = arena.child_allocator; 43 const forker_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, identity.owner)) orelse return error.NotFound; 44 const fork_record = (try evt.readForkById(io, gpa, arena, users_dir, &forker_id, &id)) orelse return error.NotFound; 45 if (fork_record.removed) return error.NotFound; 46 const forker = if (session.userId()) |user_id| std.mem.eql(u8, &user_id, &forker_id) else false; 47 if (route == .fork_delete and !forker) return error.NotFound; 48 const target_id = try evt.parseEventId(&fork_record.event.repo_id); 49 const owner_id = try evt.parseEventId(&fork_record.event.repo_user_id); 50 51 // a deleted target, or one whose owner was deleted, leaves the fork to its forker 52 const target = try evt.readRepoById(io, gpa, arena, users_dir, &owner_id, &target_id, session.userId()); 53 // a draft is as readable as the repo it targets 54 const target_role: evt.Repo.Role = if (target) |found| role: { 55 // patches that are off take their drafts with them 56 if (found.repo.event.patch_role == null) return error.NotFound; 57 break :role found.role orelse return error.NotFound; 58 } else if (forker) .read else return error.NotFound; 59 // the url keeps the name the fork was made under, even after a rename 60 if (!std.mem.eql(u8, fork_record.event.repo_name, identity.name)) return error.NotFound; 61 const owner = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, arena, &owner_id)) orelse return error.NotFound; 62 63 const aa = arena.allocator(); 64 const target_identity = try aa.print("{s}:{s}", .{ owner.event.name, if (target) |found| found.repo.event.name else identity.name }); 65 const id_hex = std.fmt.bytesToHex(id, .lower); 66 // the fork's viewer is the target repo's 67 const handle = ui.RepoHandle{ 68 .location = .{ .fork = .{ .identity = identity.identity, .id = &id_hex } }, 69 .viewer = try ui.Viewer.init(session, arena, target_role), 70 }; 71 const fork_path = try fork.forkPath(aa, users_dir, &forker_id, &id); 72 // a fork shares its target's hash kind 73 var any_fork = try rp.AnyRepo(.xit, .{}).open(io, arena.child_allocator, .{ .path = fork_path, .require_repo_root = true }); 74 defer any_fork.deinit(io, arena.child_allocator); 75 switch (any_fork) { 76 inline else => |*fork_repo| { 77 const repo_opts = fork_repo.self_repo_opts; 78 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 79 const retained_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, arena, &id)) orelse return error.NotFound; 80 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return error.NotFound; 81 const newest_revision = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, arena); 82 var commits_base_oid = fork_oid; 83 if (newest_revision) |revision| { 84 if (revision.record.event.base_oid.len != commits_base_oid.len) return error.NotFound; 85 @memcpy(&commits_base_oid, revision.record.event.base_oid); 86 } 87 88 const target_path = try evt.repoPath(aa, users_dir, &owner_id, &target_id); 89 const target_source = ui.RepoSource{ .path = target_path, .repo_kind = .xit }; 90 var target_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (target != null) 91 rp.Repo(.xit, repo_opts).open(io, arena.child_allocator, target_source.localInitOpts()) catch null 92 else 93 null; 94 defer if (target_repo_maybe) |*target_repo| target_repo.deinit(io, arena.child_allocator); 95 96 const target_branch = retained_patch.event.target_branch; 97 98 const retained_entry = Patches.PatchWithId{ 99 .id = try aa.dupe(u8, &id_hex), 100 .record = retained_patch, 101 .author = try ui.Author.initFromEmail(haxy_moment, arena, retained_patch.author_email), 102 .draft = fork_record.event.stage == .draft, 103 .revision_oid = try aa.dupe(u8, &fork_oid), 104 .fork_exists = true, 105 .fork_identity = try aa.dupe(u8, identity.identity), 106 .target_deleted = target == null, 107 }; 108 var patch_data = try Patches.detailResult(aa, target_identity, retained_entry); 109 110 if (target_repo_maybe) |*target_repo| switch (fork_record.event.stage) { 111 .draft => if (try Patches.loadDraftEntry(.xit, repo_opts, arena, io, haxy_moment, fork_repo, target_repo, id, identity.identity)) |entry| { 112 patch_data = try Patches.detailResult(aa, target_identity, entry); 113 patch_data.repo_source = target_source; 114 }, 115 .publish => { 116 patch_data = Patches.init(.xit, repo_opts, arena, target_repo, io, haxy_moment, session, target_id, target_identity, target_branch, "", &id_hex, "", 0, "", .open, handle.viewer) catch |err| switch (err) { 117 error.NotFound => patch_data, 118 else => |other| return other, 119 }; 120 if (patch_data.selectedThread() != null) patch_data.repo_source = target_source; 121 }, 122 }; 123 patch_data.repo_id = target_id; 124 125 const requested_ref: ?ui.RoutablePage.RefOrOid = switch (route) { 126 .fork_files => |f| if (f.oid.len == 0) null else .object, 127 .fork_commits => |c| if (c.oid.len == 0) null else .object, 128 else => null, 129 }; 130 const requested_value: []const u8 = switch (route) { 131 .fork_files => |*f| f.oid.slice(), 132 .fork_commits => |*c| c.oid.slice(), 133 else => "", 134 }; 135 const files_path: []const u8 = switch (route) { 136 .fork_files => |*f| f.path.slice(), 137 else => "", 138 }; 139 const files_line: usize = switch (route) { 140 .fork_files => |f| f.line, 141 else => 0, 142 }; 143 const files_find: []const u8 = switch (route) { 144 .fork_files => |*f| f.find.slice(), 145 else => "", 146 }; 147 const commits_message = route == .fork_commits and route.fork_commits.message; 148 const files = try Files.init(.xit, repo_opts, arena, fork_repo, io, arena.child_allocator, handle, requested_ref, requested_value, files_path, files_line, files_find); 149 var commits = try Commits.init(.xit, repo_opts, arena, fork_repo, io, arena.child_allocator, haxy_moment, handle, requested_ref, requested_value, commits_message, &commits_base_oid, "", ""); 150 commits.commit_count = if (newest_revision) |revision| revision.record.commit_count else 0; 151 const diff_start: usize = switch (route) { 152 .fork_diff => |d| d.start, 153 else => 0, 154 }; 155 const diff_path = switch (route) { 156 .fork_diff => |*d| try aa.dupe(u8, d.path.slice()), 157 else => "", 158 }; 159 // a named commit's diff is against the route's base (all zeros for 160 // none), else the diff is the whole patch's 161 const diff_oid: []const u8, const diff_base_oid: []const u8 = switch (route) { 162 .fork_diff => |*d| .{ try aa.dupe(u8, d.oid.slice()), try aa.dupe(u8, d.base_oid.slice()) }, 163 else => .{ "", "" }, 164 }; 165 const diff_base: ?@TypeOf(fork_oid), const diff_head: @TypeOf(fork_oid) = if (diff_oid.len == 0) .{ commits_base_oid, fork_oid } else named: { 166 evt.PatchRev.validateOid(repo_opts.hash, diff_oid) catch return error.NotFound; 167 evt.PatchRev.validateOid(repo_opts.hash, diff_base_oid) catch return error.NotFound; 168 break :named .{ if (std.mem.allEqual(u8, diff_base_oid, '0')) null else diff_base_oid[0..fork_oid.len].*, diff_oid[0..fork_oid.len].* }; 169 }; 170 171 return .{ 172 .header = try Header.init(arena, try aa.dupe(u8, identity.name), identity.owner, &id_hex, requested_value), 173 .files = files, 174 .commits = commits, 175 .patch = patch_data, 176 .diff = .{ 177 .route = .{ .fork = .{ .identity = try aa.dupe(u8, identity.identity), .id = try aa.dupe(u8, &id_hex), .oid = diff_oid, .base_oid = diff_base_oid, .patch_base_oid = try aa.dupe(u8, &commits_base_oid) } }, 178 .path = diff_path, 179 .window = try Diff.render(.xit, repo_opts, io, arena.child_allocator, aa, fork_repo, if (diff_base) |*base| base else null, diff_head, diff_start, diff_path), 180 }, 181 .delete = if (forker) .{ .target = .{ .fork = .{ .identity = try aa.dupe(u8, identity.identity), .id = id_hex } } } else null, 182 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 183 .quit = Quit.init(), 184 }; 185 }, 186 } 187 } 188 189 pub const View = struct { 190 box: wgt.Box(ui.Widget), 191 192 const header_index: usize = 0; 193 const stack_index: usize = 1; 194 195 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 196 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 197 errdefer box.deinit(allocator); 198 { 199 var header = try Header.View.init(allocator, &data.header, data.commits.commit_count, data.delete != null, session); 200 errdefer header.deinit(allocator); 201 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .fork_header = header }, .rect = null, .min_size = null }); 202 } 203 { 204 var stack = try wgt.Stack(ui.Widget).init(allocator); 205 errdefer stack.deinit(allocator); 206 const selected = data.patch.selectedThread() orelse return error.NotFound; 207 var patch_detail = try Patches.Detail.init(allocator, &data.patch, session, selected.*, .{ .actions = data.patch.repo_source != null }); 208 errdefer patch_detail.deinit(allocator); 209 try stack.children.put(allocator, patch_detail.getFocus().id, .{ .repo_patch_detail = patch_detail }); 210 { 211 var diff = try Diff.View.init(allocator, &data.diff, session); 212 errdefer diff.deinit(allocator); 213 try stack.children.put(allocator, diff.getFocus().id, .{ .diff_view = diff }); 214 } 215 { 216 var files = try Files.View.init(allocator, &data.files, session); 217 errdefer files.deinit(allocator); 218 try stack.children.put(allocator, files.getFocus().id, .{ .repo_files = files }); 219 } 220 { 221 var commits = try Commits.View.init(allocator, &data.commits, session); 222 errdefer commits.deinit(allocator); 223 try stack.children.put(allocator, commits.getFocus().id, .{ .repo_commits = commits }); 224 } 225 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.forker_name, data.header.name }); 226 // right after commits, as in the header 227 if (data.delete) |delete| { 228 var view = try ui.Delete.View.init(allocator, delete, session, ui.RoutablePage.forkDeleteRoute(identity, data.header.id) orelse return error.RouteTooLong); 229 errdefer view.deinit(allocator); 230 try stack.children.put(allocator, view.getFocus().id, .{ .delete_form = view }); 231 } 232 if (session.data.user_id != null) { 233 const route = ui.RoutablePage.forkRepoNewRoute(identity, data.header.id) orelse return error.RouteTooLong; 234 var new_repo = try ui.NewRepo.View.init(allocator, session, route); 235 errdefer new_repo.deinit(allocator); 236 try stack.children.put(allocator, new_repo.getFocus().id, .{ .new_repo = new_repo }); 237 } else if (session.data.host_kind == .server) { 238 const route = ui.RoutablePage.forkUserNewRoute(identity, data.header.id) orelse return error.RouteTooLong; 239 var new_user = try ui.NewUser.View.init(allocator, session, route); 240 errdefer new_user.deinit(allocator); 241 try stack.children.put(allocator, new_user.getFocus().id, .{ .new_user = new_user }); 242 } 243 if (session.data.host_kind == .server) { 244 const route = ui.RoutablePage.forkUserRoute(identity, data.header.id) orelse return error.RouteTooLong; 245 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 246 errdefer user_view.deinit(allocator); 247 try stack.children.put(allocator, user_view.getFocus().id, user_view); 248 } 249 if (session.is_terminal) { 250 var quit = try Quit.View.init(allocator, session); 251 errdefer quit.deinit(allocator); 252 try stack.children.put(allocator, quit.getFocus().id, .{ .quit = quit }); 253 } 254 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 255 } 256 // a page opens on its tabs, except that search results open in the 257 // files tab's search box. 258 box.getFocus().child_id = box.children.keys()[if (data.files.find != null) stack_index else header_index]; 259 return .{ .box = box }; 260 } 261 262 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 263 self.box.deinit(allocator); 264 } 265 266 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 267 self.clearGrid(); 268 const header = &self.box.children.values()[header_index].widget.fork_header; 269 const stack = &self.box.children.values()[stack_index].widget.stack; 270 if (header.getSelectedIndex()) |index| stack.getFocus().child_id = stack.children.keys()[index]; 271 try self.box.build(allocator, constraint, root_focus); 272 } 273 274 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 275 const stack = &self.box.children.values()[stack_index].widget.stack; 276 const current_id = self.box.getFocus().child_id orelse return; 277 const current_index = self.box.children.getIndex(current_id) orelse return; 278 const child = &self.box.children.values()[current_index].widget; 279 var next_index = current_index; 280 switch (inp.vertDirection(key)) { 281 .up => switch (child.*) { 282 .fork_header => try child.input(allocator, key, root_focus), 283 .stack => if (stack.getSelected()) |selected| { 284 if (selected.atTop(root_focus)) next_index = header_index else try child.input(allocator, key, root_focus); 285 }, 286 else => {}, 287 }, 288 .down => switch (child.*) { 289 .fork_header => { 290 if (stack.getSelected()) |selected| switch (selected.*) { 291 .repo_patch_detail => |*view| if (view.focusFirst(root_focus)) return, 292 .repo_files => |*view| if (view.focusHeader(root_focus)) return, 293 .repo_commits => |*view| if (view.focusHeader(root_focus)) return, 294 .user_notifications => |*view| if (view.focusHeader(root_focus)) return, 295 else => {}, 296 }; 297 next_index = stack_index; 298 }, 299 .stack => try child.input(allocator, key, root_focus), 300 else => {}, 301 }, 302 .none => try child.input(allocator, key, root_focus), 303 } 304 if (next_index != current_index) root_focus.setFocus(self.box.children.keys()[next_index]); 305 } 306 307 pub fn clearGrid(self: *View) void { 308 self.box.clearGrid(); 309 } 310 311 pub fn getGrid(self: View) ?Grid { 312 return self.box.getGrid(); 313 } 314 315 pub fn getFocus(self: *View) *Focus { 316 return self.box.getFocus(); 317 } 318 }; 319