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