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