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