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