╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
149 .fork_diff => |*d| try aa.dupe(u8, d.path.slice()), 150 else => "", 151 }; 152 // a named commit's diff is against the route's base (all zeros for 153 // none), else the diff is the whole patch's 154 const diff_oid: []const u8, const diff_base_oid: []const u8 = switch (route) { 155 .fork_diff => |*d| .{ try aa.dupe(u8, d.oid.slice()), try aa.dupe(u8, d.base_oid.slice()) }, 156 else => .{ "", "" }, 157 }; 158 const diff_base: ?@TypeOf(fork_oid), const diff_head: @TypeOf(fork_oid) = if (diff_oid.len == 0) .{ commits_base_oid, fork_oid } else named: { 159 evt.PatchRev.validateOid(repo_opts.hash, diff_oid) catch return error.NotFound; 160 evt.PatchRev.validateOid(repo_opts.hash, diff_base_oid) catch return error.NotFound; 161 break :named .{ if (std.mem.allEqual(u8, diff_base_oid, '0')) null else diff_base_oid[0..fork_oid.len].*, diff_oid[0..fork_oid.len].* }; 162 }; 163 164 return .{ 165 .header = try Header.init(arena, target_record.event.name, identity.owner, &id_hex, requested_value), 166 .files = files, 167 .commits = commits, 168 .patch = patch_data, 169 .diff = .{ 170 .route = .{ .fork = .{ .identity = try aa.dupe(u8, identity.identity), .id = try aa.dupe(u8, &id_hex), .oid = diff_oid, .base_oid = diff_base_oid } }, 171 .path = diff_path, 172 .window = try Diff.render(.xit, repo_opts, io, arena.child_allocator, aa, fork_repo, if (diff_base) |*base| base else null, diff_head, diff_start, diff_path), 173 }, 174 .auth = Auth.init(), 175 .quit = Quit.init(), 176 }; 177 }, 178 } 179 } 180 181 pub const View = struct { 182 box: wgt.Box(ui.Widget), 183 184 const header_index: usize = 0; 185 const stack_index: usize = 1; 186 187 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 188 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 189 errdefer box.deinit(allocator); 190 { 191 var header = try Header.View.init(allocator, &data.header, data.commits.commit_count, session); 192 errdefer header.deinit(allocator); 193 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .fork_header = header }, .rect = null, .min_size = null }); 194 } 195 { 196 var stack = try wgt.Stack(ui.Widget).init(allocator); 197 errdefer stack.deinit(allocator); 198 const selected = data.patch.selectedThread() orelse return error.NotFound; 199 var patch_detail = try Patches.Detail.init(allocator, &data.patch, session, selected.*, .{ .actions = data.patch.repo_source != null }); 200 errdefer patch_detail.deinit(allocator); 201 try stack.children.put(allocator, patch_detail.getFocus().id, .{ .repo_patch_detail = patch_detail }); 202 { 203 var diff = try Diff.View.init(allocator, &data.diff, session); 204 errdefer diff.deinit(allocator); 205 try stack.children.put(allocator, diff.getFocus().id, .{ .diff_view = diff }); 206 } 207 { 208 var files = try Files.View.init(allocator, &data.files, session); 209 errdefer files.deinit(allocator); 210 try stack.children.put(allocator, files.getFocus().id, .{ .repo_files = files }); 211 } 212 { 213 var commits = try Commits.View.init(allocator, &data.commits, session); 214 errdefer commits.deinit(allocator); 215 try stack.children.put(allocator, commits.getFocus().id, .{ .repo_commits = commits }); 216 } 217 const identity = try std.fmt.allocPrint(session.page_arena.allocator(), "{s}/{s}", .{ data.header.forker_name, data.header.name }); 218 if (session.data.user_id != null) { 219 const route = ui.RoutablePage.forkNewRepoRoute(identity, data.header.id) orelse return error.RouteTooLong; 220 var new_repo = try ui.NewRepo.View.init(allocator, session, route); 221 errdefer new_repo.deinit(allocator); 222 try stack.children.put(allocator, new_repo.getFocus().id, .{ .new_repo = new_repo }); 223 } else if (session.data.host_kind == .server) { 224 const route = ui.RoutablePage.forkNewUserRoute(identity, data.header.id) orelse return error.RouteTooLong; 225 var new_user = try ui.NewUser.View.init(allocator, session, route); 226 errdefer new_user.deinit(allocator); 227 try stack.children.put(allocator, new_user.getFocus().id, .{ .new_user = new_user }); 228 } 229 if (session.data.host_kind == .server) { 230 var auth = try Auth.View.init(allocator, &data.auth, session); 231 errdefer auth.deinit(allocator); 232 try stack.children.put(allocator, auth.getFocus().id, .{ .home_auth = auth }); 233 } 234 if (session.is_terminal) { 235 var quit = try Quit.View.init(allocator, session); 236 errdefer quit.deinit(allocator); 237 try stack.children.put(allocator, quit.getFocus().id, .{ .quit = quit }); 238 } 239 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 240 } 241 // a page opens on its tabs, except that search results open in the 242 // files tab's search box. 243 box.getFocus().child_id = box.children.keys()[if (data.files.find != null) stack_index else header_index]; 244 return .{ .box = box }; 245 } 246 247 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 248 self.box.deinit(allocator); 249 } 250 251 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 252 self.clearGrid(); 253 const header = &self.box.children.values()[header_index].widget.fork_header; 254 const stack = &self.box.children.values()[stack_index].widget.stack; 255 if (header.getSelectedIndex()) |index| stack.getFocus().child_id = stack.children.keys()[index]; 256 try self.box.build(allocator, constraint, root_focus); 257 } 258 259 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 260 const stack = &self.box.children.values()[stack_index].widget.stack; 261 const current_id = self.box.getFocus().child_id orelse return; 262 const current_index = self.box.children.getIndex(current_id) orelse return; 263 const child = &self.box.children.values()[current_index].widget; 264 var next_index = current_index; 265 switch (inp.vertDirection(key)) { 266 .up => switch (child.*) { 267 .fork_header => try child.input(allocator, key, root_focus), 268 .stack => if (stack.getSelected()) |selected| { 269 if (selected.atTop(root_focus)) next_index = header_index else try child.input(allocator, key, root_focus); 270 }, 271 else => {}, 272 }, 273 .down => switch (child.*) { 274 .fork_header => { 275 if (stack.getSelected()) |selected| switch (selected.*) { 276 .repo_patch_detail => |*view| if (view.focusFirst(root_focus)) return, 277 .repo_files => |*view| if (view.focusHeader(root_focus)) return, 278 .repo_commits => |*view| if (view.focusHeader(root_focus)) return, 279 else => {}, 280 }; 281 next_index = stack_index; 282 }, 283 .stack => try child.input(allocator, key, root_focus), 284 else => {}, 285 }, 286 .none => try child.input(allocator, key, root_focus), 287 } 288 if (next_index != current_index) root_focus.setFocus(self.box.children.keys()[next_index]); 289 } 290 291 pub fn clearGrid(self: *View) void { 292 self.box.clearGrid(); 293 } 294 295 pub fn getGrid(self: View) ?Grid { 296 return self.box.getGrid(); 297 } 298 299 pub fn getFocus(self: *View) *Focus { 300 return self.box.getFocus(); 301 } 302 }; 303