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