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