╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
6 user_id: [evt.event_id_size * 2]u8, 7 name: []const u8, 8 description: []const u8, 9 read_access: Access = .private, 10 // the least role that may write in each tab, null turns the tab off 11 discuss_role: ?Role = .read, 12 issue_role: ?Role = .read, 13 patch_role: ?Role = null, 14 // newline-separated names of the branches a push may not delete or force push 15 protected_branches: []const u8 = "", 16 17 // what the db stores: the event's data plus the commit-derived fields 18 pub const Record = struct { 19 event: Self, 20 removed: bool = false, 21 created_order: u64 = 0, 22 updated_order: u64 = 0, 23 }; 24 25 const Self = @This(); 26 27 pub const Access = enum { 28 private, 29 public, 30 }; 31 32 // what a user may do in a repo, ordered so a higher role includes the lower 33 // ones. holding no role is a null, not a value. 34 pub const Role = enum { 35 read, 36 write, 37 owner, 38 39 pub fn atLeast(self: Role, min: Role) bool { 40 return @backingInt(self) >= @backingInt(min); 41 } 42 43 // whether a user may change an event: anyone's with the write role, 44 // otherwise only one they authored 45 pub fn canModify(self: Role, user_email: []const u8, author_email_maybe: ?[]const u8) bool { 46 if (self.atLeast(.write)) return true; 47 const author_email = author_email_maybe orelse return false; 48 return std.mem.eql(u8, user_email, author_email); 49 } 50 }; 51 52 pub const name_max_len = 32; 53 // how many branches a repo may protect, so the branches tab stays one short page 54 pub const protected_branch_max_count = 50; 55 56 // the protected branch names 57 pub fn protectedBranchNames(self: Self, allocator: std.mem.Allocator) ![]const []const u8 { 58 var names: std.ArrayList([]const u8) = .empty; 59 var lines = std.mem.splitScalar(u8, self.protected_branches, '\n'); 60 while (lines.next()) |line| { 61 if (line.len > 0) try names.append(allocator, line); 62 } 63 return try names.toOwnedSlice(allocator); 64 } 65 66 // the role a kind's tab needs for a write, or null when the tab is off or the 67 // kind has no tab 68 pub fn threadRole(self: Self, kind: evt.EventKind) ?Role { 69 return switch (kind) { 70 .discuss => self.discuss_role, 71 .issue => self.issue_role, 72 .patch => self.patch_role, 73 else => null, 74 }; 75 } 76 77 // the role `user_id_maybe` holds in the repo, or null when they hold none: its 78 // creator owns it, and anyone else gets their grant, else the base role. 79 // `user_moment` is the owner's, which holds the grants. a null user is logged 80 // out. 81 pub fn roleOf( 82 comptime DB: type, 83 comptime hash_kind: hash.HashKind, 84 user_moment: DB.HashMap(.read_only), 85 arena: *std.heap.ArenaAllocator, 86 record: Record, 87 repo_id: *const [evt.event_id_size]u8, 88 user_id_maybe: ?[evt.event_id_size]u8, 89 ) !?Role { 90 // what read access gives everyone, logged in or not 91 const base: ?Role = if (record.event.read_access == .public) .read else null; 92 const user_id = user_id_maybe orelse return base; 93 if (std.mem.eql(u8, &record.event.user_id, &std.fmt.bytesToHex(user_id, .lower))) return .owner; 94 95 // a grant is never below the base role 96 return (try evt.Grant.readRole(DB, hash_kind, user_moment, arena, repo_id, &user_id)) orelse base; 97 } 98 99 // the moment keys `evt.merge` reads and writes for this kind 100 pub const merge_policy: evt.MergePolicy = .target_wins; 101 pub const record_map_key = "event-id->repo"; 102 pub const all_id_set_key = "repo-id-set"; 103 pub const name_index_key = "name->repo-id"; 104 105 pub fn validateName(name: []const u8) !void { 106 if (name.len == 0) return error.NameEmpty; 107 if (name.len > name_max_len) return error.NameTooLong; 108 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 109 110 for (name) |c| { 111 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 112 return error.InvalidName; 113 } 114 } 115 116 pub fn consume( 117 comptime DB: type, 118 comptime hash_kind: hash.HashKind, 119 haxy_moment: DB.HashMap(.read_write), 120 event_id: *const [evt.event_id_size]u8, 121 record_maybe: ?Record, 122 arena: *std.heap.ArenaAllocator, 123 _: ?[]const u8, 124 ) !void { 125 const repo_key = hash.hashInt(hash_kind, event_id); 126 127 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 128 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 129 130 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 131 const name_to_repo_id = try DB.SortedMap(.read_write).init(name_to_repo_id_cursor); 132 133 var existing_record_maybe: ?Record = null; 134 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 135 if (existing_cursor_maybe) |existing_cursor| { 136 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 137 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 138 } 139 140 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 141 142 if (!record_to_write.removed) { 143 _ = try evt.parseEventId(&record_to_write.event.user_id); 144 try validateName(record_to_write.event.name); 145 } 146 147 if (existing_record_maybe) |existing_record| { 148 // updates preserve the original creation metadata 149 record_to_write.created_order = existing_record.created_order; 150 151 // drop the old active index; active values are re-added below 152 if (!existing_record.removed) { 153 _ = try name_to_repo_id.remove(existing_record.event.name); 154 } 155 } 156 157 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 158 const repo = try DB.HashMap(.read_write).init(repo_cursor); 159 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 160 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 161 162 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 163 164 // the id set retains removed records so merges can carry removals 165 if (existing_cursor_maybe == null) { 166 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 167 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 168 try repo_id_set.put(&order_key); 169 } 170 171 if (!record_to_write.removed) { 172 try name_to_repo_id.put(record_to_write.event.name, .{ .bytes = event_id }); 173 } 174 } 175 176 // a repo plus its event id (the id is the on-disk repo directory name, so the 177 // caller can locate the working repo under <server>/repos/<hex event id>). 178 pub const RepoWithId = struct { 179 repo: Record, 180 event_id: [evt.event_id_size]u8, 181 }; 182 183 // a repo as one viewer sees it. without a role, the viewer may not see it. 184 pub const RepoWithRole = struct { 185 repo: Record, 186 event_id: [evt.event_id_size]u8, 187 role: ?Role, 188 }; 189 190 // read a repo by its name from its owner's user repo 191 pub fn readByName( 192 comptime DB: type, 193 comptime hash_kind: hash.HashKind, 194 user_moment: DB.HashMap(.read_only), 195 arena: *std.heap.ArenaAllocator, 196 name: []const u8, 197 ) !?RepoWithId { 198 const name_to_repo_id_cursor = try user_moment.getCursor(hash.hashInt(hash_kind, name_index_key)) orelse return null; 199 const name_to_repo_id = try DB.SortedMap(.read_only).init(name_to_repo_id_cursor); 200 const repo_id_cursor = try name_to_repo_id.getCursor(name) orelse return null; 201 var repo_id: [evt.event_id_size]u8 = undefined; 202 _ = try repo_id_cursor.readBytes(&repo_id); 203 const repo = (try readById(DB, hash_kind, user_moment, arena, &repo_id)) orelse return null; 204 return .{ .repo = repo, .event_id = repo_id }; 205 } 206 207 // a deleted repo reads as missing 208 pub fn readById( 209 comptime DB: type, 210 comptime hash_kind: hash.HashKind, 211 haxy_moment: DB.HashMap(.read_only), 212 arena: *std.heap.ArenaAllocator, 213 id: []const u8, 214 ) !?Record { 215 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 216 const records = try DB.HashMap(.read_only).init(records_cursor); 217 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 218 const record = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 219 return if (record.removed) null else record; 220 } 221