╭ 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 Patches = @import("./Repo/Patches.zig"); 19 pub const Settings = @import("./Settings.zig"); 20 pub const Auth = @import("./Auth.zig"); 21 pub const Quit = @import("./Quit.zig"); 22 23 header: Header, 24 files: Files, 25 commits: Commits, 26 patch: Patches, 27 settings: Settings, 28 auth: Auth, 29 quit: Quit, 30 31 const Self = @This(); 32 33 pub fn init(arena: *std.heap.ArenaAllocator, session: *ui.Session, route: ui.RoutablePage) !Self { 34 const io = session.io orelse return error.NotFound; 35 const repos_dir = session.repos_dir orelse return error.NotFound; 36 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 37 const route_fork = route.forkRoute() orelse return error.UnexpectedRoute; 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 fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, arena, &id)) orelse return error.NotFound; 41 if (fork_record.removed) return error.NotFound; 42 if (fork_record.event.repo_id.len != evt.event_id_size) return error.NotFound; 43 var target_id: [evt.event_id_size]u8 = undefined; 44 @memcpy(&target_id, fork_record.event.repo_id); 45 46 const target_record = (try evt.Repo.readById(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, arena, fork_record.event.repo_id)) orelse return error.NotFound; 47 const owner = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, arena, target_record.event.user_id)) orelse return error.NotFound; 48 if (!std.mem.eql(u8, owner.event.name, identity.owner) or !std.mem.eql(u8, target_record.event.name, identity.name)) return error.NotFound; 49 50 const aa = arena.allocator(); 51 const id_hex = std.fmt.bytesToHex(id, .lower); 52 const fork_path = try fork.forkPath(aa, repos_dir, &id_hex); 53 var fork_repo = try rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = fork_path, .require_repo_root = true }); 54 defer fork_repo.deinit(io, arena.child_allocator); 55 const fork_moment = try evt.currentMoment(.{}, &fork_repo); 56 const retained_patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, fork_moment, arena, &id)) orelse return error.NotFound; 57 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return error.NotFound; 58 var commits_base_oid = fork_oid; 59 if (try evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, fork_moment, arena)) |revision| { 60 if (revision.record.event.base_oid.len != commits_base_oid.len) return error.NotFound; 61 @memcpy(&commits_base_oid, revision.record.event.base_oid); 62 } 63 64 const target_id_hex = std.fmt.bytesToHex(target_id, .lower); 65 const target_path = try std.fs.path.join(aa, &.{ repos_dir, &target_id_hex }); 66 const target_source = ui.RepoSource{ .path = target_path, .repo_kind = .xit }; 67 var target_repo_maybe: ?rp.Repo(.xit, .{}) = if (!target_record.removed) 68 rp.Repo(.xit, .{}).open(io, arena.child_allocator, target_source.localInitOpts()) catch null 69 else 70 null; 71 defer if (target_repo_maybe) |*target_repo| target_repo.deinit(io, arena.child_allocator); 72 73 var target_branch = if (retained_patch.event.revision) |revision| revision.targetBranch() orelse "" else ""; 74 if (target_repo_maybe) |*target_repo| { 75 if (target_branch.len == 0) { 76 if (try ui.ResolvedRefOrOid(.xit, .{}).init(target_repo, io, aa, null, "")) |resolved| 77 target_branch = resolved.value; 78 } 79 } 80 81 const retained_entry = Patches.PatchWithId{ 82 .id = try aa.dupe(u8, &id_hex), 83 .record = retained_patch, 84 .author = try ui.Author.initFromEmail(haxy_moment, arena, retained_patch.author_email), 85 .draft = fork_record.event.stage == .draft, 86 .fork_oid = try aa.dupe(u8, &fork_oid), 87 .target_branch = try aa.dupe(u8, target_branch), 88 .fork_exists = true, 89 }; 90 var patch_data = try Patches.detailResult(aa, identity.identity, retained_entry); 91 92 if (target_repo_maybe) |*target_repo| switch (fork_record.event.stage) { 93 .draft => if (try Patches.loadDraftEntry(.xit, .{}, arena, io, haxy_moment, &fork_repo, target_repo, id, target_branch)) |entry| { 94 patch_data = try Patches.detailResult(aa, identity.identity, entry); 95 patch_data.repo_source = target_source; 96 }, 97 .publish => { 98 patch_data = Patches.init(.xit, .{}, arena, target_repo, io, haxy_moment, session, target_id, identity.identity, target_branch, "", &id_hex, "", 0, "", .open) catch |err| switch (err) { 99 error.NotFound => patch_data, 100 else => |other| return other, 101 }; 102 if (patch_data.selectedThread()) |entry| { 103 patch_data.repo_source = target_source; 104 if (entry.record.event.revision) |revision| { 105 if (revision.targetBranch()) |branch| target_branch = branch; 106 } 107 } 108 }, 109 }; 110 patch_data.repo_id = target_id; 111 112 const requested_ref: ?ui.RoutablePage.RefOrOid = switch (route) { 113 .fork_files => |f| if (f.oid.len == 0) null else .object, 114 .fork_commits => |c| if (c.oid.len == 0) null else .object, 115 else => null, 116 }; 117 const requested_value: []const u8 = switch (route) { 118 .fork_files => |*f| f.oid.slice(), 119 .fork_commits => |*c| c.oid.slice(), 120 else => "", 121 }; 122 const files_path: []const u8 = switch (route) { 123 .fork_files => |*f| f.path.slice(), 124 else => "", 125 }; 126 const files_line: usize = switch (route) { 127 .fork_files => |f| f.line, 128 else => 0, 129 }; 130 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 131 .fork_commits => |c| c.content, 132 else => .{ .diff = .{} }, 133 }; 134 const location = ui.RoutablePage.RepoLocation{ .fork = .{ 135 .identity = identity.identity, 136 .id = &id_hex, 137 .target_branch = target_branch, 138 } }; 139 const files = try Files.init(.xit, .{}, arena, &fork_repo, io, arena.child_allocator, location, requested_ref, requested_value, files_path, files_line); 140 const commits = try Commits.init(.xit, .{}, arena, &fork_repo, io, arena.child_allocator, haxy_moment, location, requested_ref, requested_value, commits_content, commits_base_oid); 141 142 return .{ 143 .header = try Header.init(arena, target_record.event.name, owner.event.name, &id_hex, requested_value), 144 .files = files, 145 .commits = commits, 146 .patch = patch_data, 147 .settings = Settings.init(), 148 .auth = Auth.init(), 149 .quit = Quit.init(), 150 }; 151 } 152 153 pub const View = struct { 154 box: wgt.Box(ui.Widget), 155 156 const header_index: usize = 0; 157 const stack_index: usize = 1; 158 159 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 160 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 161 errdefer box.deinit(allocator); 162 { 163 var header = try Header.View.init(allocator, &data.header, session); 164 errdefer header.deinit(allocator); 165 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .fork_header = header }, .rect = null, .min_size = null }); 166 } 167 { 168 var stack = try wgt.Stack(ui.Widget).init(allocator); 169 errdefer stack.deinit(allocator); 170 const selected = data.patch.selectedThread() orelse return error.NotFound; 171 var patch_detail = try Patches.Detail.init(allocator, &data.patch, session, selected.*, .{ .actions = data.patch.repo_source != null }); 172 errdefer patch_detail.deinit(allocator); 173 try stack.children.put(allocator, patch_detail.getFocus().id, .{ .repo_patch_detail = patch_detail }); 174 { 175 var files = try Files.View.init(allocator, &data.files, session); 176 errdefer files.deinit(allocator); 177 try stack.children.put(allocator, files.getFocus().id, .{ .repo_files = files }); 178 } 179 { 180 var commits = try Commits.View.init(allocator, &data.commits, session); 181 errdefer commits.deinit(allocator); 182 try stack.children.put(allocator, commits.getFocus().id, .{ .repo_commits = commits }); 183 } 184 if (session.data.user_id != null) { 185 var settings = try Settings.View.init(allocator, session); 186 errdefer settings.deinit(allocator); 187 try stack.children.put(allocator, settings.getFocus().id, .{ .home_settings = settings }); 188 } 189 if (!session.data.is_local) { 190 var auth = try Auth.View.init(allocator, &data.auth, session); 191 errdefer auth.deinit(allocator); 192 try stack.children.put(allocator, auth.getFocus().id, .{ .home_auth = auth }); 193 } 194 if (session.is_terminal) { 195 var quit = try Quit.View.init(allocator, session); 196 errdefer quit.deinit(allocator); 197 try stack.children.put(allocator, quit.getFocus().id, .{ .quit = quit }); 198 } 199 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 200 } 201 box.getFocus().child_id = box.children.keys()[header_index]; 202 return .{ .box = box }; 203 } 204 205 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 206 self.box.deinit(allocator); 207 } 208 209 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 210 self.clearGrid(); 211 const header = &self.box.children.values()[header_index].widget.fork_header; 212 const stack = &self.box.children.values()[stack_index].widget.stack; 213 if (header.getSelectedIndex()) |index| stack.getFocus().child_id = stack.children.keys()[index]; 214 try self.box.build(allocator, constraint, root_focus); 215 } 216 217 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 218 const stack = &self.box.children.values()[stack_index].widget.stack; 219 const current_id = self.box.getFocus().child_id orelse return; 220 const current_index = self.box.children.getIndex(current_id) orelse return; 221 const child = &self.box.children.values()[current_index].widget; 222 var next_index = current_index; 223 switch (inp.vertDirection(key)) { 224 .up => switch (child.*) { 225 .fork_header => try child.input(allocator, key, root_focus), 226 .stack => if (stack.getSelected()) |selected| { 227 const at_top = switch (selected.*) { 228 .repo_patch_detail => |*view| view.atTop(root_focus), 229 .repo_files => |*view| view.getSelectedIndex() == 0, 230 .repo_commits => |*view| view.getSelectedIndex() == 0, 231 .home_settings => |*view| view.getSelectedIndex() == 0, 232 .home_auth => |*view| view.getSelectedIndex() == 0, 233 .quit => |*view| view.getSelectedIndex() == 0, 234 else => false, 235 }; 236 if (at_top) next_index = header_index else try child.input(allocator, key, root_focus); 237 }, 238 else => {}, 239 }, 240 .down => switch (child.*) { 241 .fork_header => { 242 if (stack.getSelected()) |selected| switch (selected.*) { 243 .repo_patch_detail => |*view| if (view.focusFirst(root_focus)) return, 244 .repo_files => |*view| if (view.focusCloneUrl(root_focus)) return, 245 .repo_commits => |*view| if (view.focusCloneUrl(root_focus)) return, 246 else => {}, 247 }; 248 next_index = stack_index; 249 }, 250 .stack => try child.input(allocator, key, root_focus), 251 else => {}, 252 }, 253 .none => try child.input(allocator, key, root_focus), 254 } 255 if (next_index != current_index) root_focus.setFocus(self.box.children.keys()[next_index]); 256 } 257 258 pub fn clearGrid(self: *View) void { 259 self.box.clearGrid(); 260 } 261 262 pub fn getGrid(self: View) ?Grid { 263 return self.box.getGrid(); 264 } 265 266 pub fn getFocus(self: *View) *Focus { 267 return self.box.getFocus(); 268 } 269 }; 270