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