╭ Branches.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const Comment = @import("./Comment.zig"); 7 const xit = @import("xit"); 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 // the repo's protected branches, which only its owners see 18 identity: []const u8, 19 names: []const []const u8, 20 21 const Self = @This(); 22 23 pub const Action = enum { protect, unprotect }; 24 25 pub fn init(arena: *std.heap.ArenaAllocator, identity: []const u8, repo: evt.Repo) !Self { 26 const aa = arena.allocator(); 27 return .{ 28 .identity = try aa.dupe(u8, identity), 29 .names = try repo.protectedBranchNames(aa), 30 }; 31 } 32 33 // protect or unprotect `branch` in the repo `identity` names as `actor`, one 34 // of its owners. returns why a protect wrote nothing, or null once written 35 pub fn perform( 36 io: std.Io, 37 allocator: std.mem.Allocator, 38 users_dir: []const u8, 39 actor: ui.Actor, 40 identity: []const u8, 41 action: Action, 42 branch: []const u8, 43 ) !?ui.Session.FormFeedback.BranchesFailure { 44 const owner_id = actor.repo_user_id orelse return error.NotFound; 45 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 46 var arena = std.heap.ArenaAllocator.init(allocator); 47 defer arena.deinit(); 48 const repo_id = blk: { 49 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &owner_id)) orelse return error.NotFound; 50 defer user_repo.deinit(io, allocator); 51 const moment = (try evt.userMoment(&user_repo)) orelse return error.NotFound; 52 const found = (try evt.Repo.readByName(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, parsed.name)) orelse return error.NotFound; 53 break :blk found.event_id; 54 }; 55 56 // only an existing branch can be protected, and its row links to it, so 57 // its encoded name must fit in a route 58 if (action == .protect) { 59 if ((try ui.urlEncodeRef(arena.allocator(), branch)).len > ui.RoutablePage.ref_route_max_len) return .too_long; 60 const path = try evt.repoPath(allocator, users_dir, &owner_id, &repo_id); 61 defer allocator.free(path); 62 const source = ui.RepoSource{ .path = path, .repo_kind = .xit }; 63 if (!try source.hasBranch(io, allocator, branch)) return .not_found; 64 } 65 evt.protectBranch(io, allocator, users_dir, &owner_id, &repo_id, branch, action == .protect, actor.author) catch |err| { 66 if (ui.Session.FormFeedback.BranchesFailure.fromError(err)) |failure| return failure; 67 return err; 68 }; 69 return null; 70 } 71 72 pub const View = struct { 73 scroll: wgt.Scroll(ui.Widget), 74 data: *const Self, 75 session: *ui.Session, 76 77 const about_index = 0; 78 const add_index = 1; 79 80 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 81 const aa = session.page_arena.allocator(); 82 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 83 errdefer box.deinit(allocator); 84 const route = ui.RoutablePage.repoBranchesRoute(data.identity) orelse return error.RouteTooLong; 85 const url = try route.toUrl(session.page_arena); 86 87 { 88 var about = try ui.widget.initAbout(allocator, &.{ 89 .{ .text = "protected branches can't be deleted or force pushed.\n" }, 90 .{ .text = "owners", .style = .{ .bold = true } }, 91 .{ .text = " can push directly to them.\n" }, 92 .{ .text = "members", .style = .{ .bold = true } }, 93 .{ .text = " can only change them by merging a patch." }, 94 }); 95 errdefer about.deinit(allocator); 96 try box.children.put(allocator, about.getFocus().id, .{ .widget = .{ .box = about }, .rect = null, .min_size = null }); 97 } 98 { 99 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 100 errdefer add.deinit(allocator); 101 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/protect", .{url}) }; 102 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " add a protected branch " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 103 errdefer name.deinit(allocator); 104 name.getFocus().mode = .all; 105 if (session.formFeedback(.repo_branches)) |saved| try name.setContent(allocator, saved.name); 106 add.getFocus().child_id = name.getFocus().id; 107 try add.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 108 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 109 } 110 111 // the branch rows follow the add row in the same box 112 for (data.names) |branch| { 113 const encoded = try ui.urlEncodeRef(aa, branch); 114 var row_route = route; 115 row_route.repo_branches.branch = ui.RoutablePage.Array(ui.RoutablePage.ref_route_max_len).from(encoded) orelse return error.RouteTooLong; 116 const row_url = try row_route.toUrl(session.page_arena); 117 118 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 119 errdefer row_box.deinit(allocator); 120 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 121 { 122 const files_route = ui.RoutablePage.repoFilesRoute(data.identity, .branch, encoded, "", 0) orelse return error.RouteTooLong; 123 var name = try Comment.linkBox(allocator, session, branch, files_route); 124 errdefer name.deinit(allocator); 125 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 126 } 127 { 128 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 129 errdefer remove.deinit(allocator); 130 remove.getFocus().mode = .all; 131 remove.getFocus().kind = .{ .custom = try aa.print("submit:{s}/unprotect", .{row_url}) }; 132 try row_box.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 133 } 134 row_box.getFocus().child_id = row_box.children.keys()[0]; 135 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 136 } 137 138 box.getFocus().child_id = box.children.keys()[about_index]; 139 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 140 errdefer center.deinit(allocator); 141 return .{ 142 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 143 .data = data, 144 .session = session, 145 }; 146 } 147 148 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 149 self.scroll.deinit(allocator); 150 } 151 152 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 153 return &self.scroll.child.center.child.box; 154 } 155 156 fn addBox(self: *View) *wgt.Box(ui.Widget) { 157 return &self.contentBox().children.values()[add_index].widget.box; 158 } 159 160 fn nameInput(self: *View) *wgt.TextInput { 161 return &self.addBox().children.values()[0].widget.text_input; 162 } 163 164 fn addActive(self: *View) bool { 165 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 166 } 167 168 fn aboutBox(self: *View) *wgt.Box(ui.Widget) { 169 return &self.contentBox().children.values()[about_index].widget.box; 170 } 171 172 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 173 self.clearGrid(); 174 try self.labelAddInput(root_focus); 175 ui.widget.markAboutFocused(self.aboutBox(), root_focus); 176 // the window's height as a minimum centers rows that fit 177 try self.scroll.build(allocator, .{ 178 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 179 .max_size = constraint.max_size, 180 }, root_focus); 181 } 182 183 // label the add input for this frame and register it for the web form 184 // handling 185 fn labelAddInput(self: *View, root_focus: *Focus) !void { 186 const name = self.nameInput(); 187 const failure = if (self.session.formFeedback(.repo_branches)) |saved| saved.failure else null; 188 name.options.top_label.text = if (failure) |value| switch (value) { 189 .not_found => " add a protected branch (not found) ", 190 .already_added => " add a protected branch (already added) ", 191 .too_many => " add a protected branch (too many branches) ", 192 .too_long => " add a protected branch (name too long) ", 193 } else " add a protected branch "; 194 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 195 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 196 } 197 198 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 199 // the add row keeps every key but the vertical ones, which move between rows 200 if (self.addActive() and inp.vertDirection(key) == .none) { 201 if (key == .enter) { 202 const name = try self.nameInput().text(allocator); 203 defer allocator.free(name); 204 try self.act(allocator, .protect, name); 205 } else try self.nameInput().input(allocator, key, root_focus); 206 return; 207 } 208 209 const rows = self.contentBox(); 210 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 211 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 212 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 213 return; 214 } 215 switch (key) { 216 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 217 else => { 218 // the text and the add row sit above the rows, and a row's name is a link the host follows 219 if (current <= add_index) return; 220 const row_box = &rows.children.values()[current].widget.box; 221 const button_id = row_box.getFocus().child_id orelse return; 222 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 223 try self.act(allocator, .unprotect, self.data.names[current - add_index - 1]); 224 }, 225 } 226 } 227 228 // the terminal performs the action itself; the web posts the form instead 229 fn act(self: *View, allocator: std.mem.Allocator, action: Action, branch: []const u8) !void { 230 if (comptime wasm) return; 231 const io = self.session.io orelse return; 232 const users_dir = self.session.users_dir orelse return; 233 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 234 if (try perform(io, allocator, users_dir, actor, self.data.identity, action, branch)) |failure| { 235 self.session.data.form_feedback = .{ .repo_branches = .{ .failure = failure, .name = try self.session.arena.allocator().dupe(u8, branch) } }; 236 return; 237 } 238 try self.session.navigate(ui.RoutablePage.repoBranchesRoute(self.data.identity) orelse return error.RouteTooLong); 239 } 240 241 pub fn clearGrid(self: *View) void { 242 self.scroll.clearGrid(); 243 } 244 245 pub fn getGrid(self: View) ?Grid { 246 return self.scroll.getGrid(); 247 } 248 249 pub fn getFocus(self: *View) *Focus { 250 return self.scroll.getFocus(); 251 } 252 253 // up leaves the view from the explanatory text 254 pub fn atTop(self: *View) bool { 255 return self.contentBox().getFocus().child_id == self.aboutBox().getFocus().id; 256 } 257 }; 258