╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
8 const bch = xit.branch; 9 const rf = xit.ref; 10 11 pub const ref = rf.Ref{ .kind = .head, .name = "patch" }; 12 13 // the action creating a fork records 14 pub const undo_action = "haxy/fork"; 15 16 pub const Route = struct { 17 identity: []const u8, 18 id: [evt.event_id_size * 2]u8, 19 }; 20 21 // parse a "<forker>:<repo>+<patch id>" fork path 22 pub fn parseRoute(route_path: []const u8) ?Route { 23 const plus = std.mem.indexOfScalar(u8, route_path, '+') orelse return null; 24 const identity = route_path[0..plus]; 25 const id_text = route_path[plus + 1 ..]; 26 if (identity.len == 0 or id_text.len != evt.event_id_size * 2) return null; 27 const id_bytes = evt.parseEventId(id_text) catch return null; 28 return .{ .identity = identity, .id = std.fmt.bytesToHex(id_bytes, .lower) }; 29 } 30 31 // a fork's path, inside its forker's dir 32 pub fn forkPath(allocator: std.mem.Allocator, users_dir: []const u8, forker_id: *const [evt.event_id_size]u8, fork_id: *const [evt.event_id_size]u8) ![]u8 { 33 return try std.Io.Dir.path.join(allocator, &.{ users_dir, &std.fmt.bytesToHex(forker_id.*, .lower), "forks", &std.fmt.bytesToHex(fork_id.*, .lower) }); 34 } 35 36 const forker_section = "haxy"; 37 const forker_prefix = "fork-"; 38 39 // the target repo config name recording who forked a published patch 40 pub fn forkerConfigName(patch_id: *const [evt.event_id_size]u8) [forker_section.len + 1 + forker_prefix.len + evt.event_id_size * 2]u8 { 41 return (forker_section ++ "." ++ forker_prefix).* ++ std.fmt.bytesToHex(patch_id.*, .lower); 42 } 43 44 // the forker of a published patch from the target repo's config, or null when it has no fork 45 pub fn readForkerId(sections: *const xit.config.Sections, patch_id: *const [evt.event_id_size]u8) !?[evt.event_id_size]u8 { 46 const section = sections.get(forker_section) orelse return null; 47 const name = forkerConfigName(patch_id); 48 return try evt.parseEventId(section.get(name[forker_section.len + 1 ..]) orelse return null); 49 } 50 51 pub const CreateInput = struct { 52 id: [evt.event_id_size * 2]u8, 53 user_id: [evt.event_id_size]u8, 54 repo_id: [evt.event_id_size]u8, 55 repo_user_id: [evt.event_id_size]u8, 56 title: []const u8, 57 description: []const u8, 58 labels: []const u8, 59 target_branch: []const u8, 60 author: evt.CommitAuthor, 61 timestamp: u64, 62 }; 63 64 pub fn create( 65 comptime any_opts: rp.AnyRepoOpts(.xit), 66 io: std.Io, 67 allocator: std.mem.Allocator, 68 users_dir: []const u8, 69 input: CreateInput, 70 ) ![]u8 { 71 if (!evt.Patch.fieldsValid(input.title, input.labels)) return error.InvalidPatch; 72 if (!evt.Patch.branchValid(input.target_branch)) return error.InvalidTargetBranch; 73 74 // get the fork id and path 75 const fork_id = try evt.parseEventId(&input.id); 76 const fork_path = try forkPath(allocator, users_dir, &input.user_id, &fork_id); 77 errdefer allocator.free(fork_path); 78 79 // make sure the fork id doesn't already exist 80 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &input.user_id)) orelse return error.InvalidPatchDraft; 81 defer user_repo.deinit(io, allocator); 82 var arena = std.heap.ArenaAllocator.init(allocator); 83 defer arena.deinit(); 84 const existing = if (try evt.userMoment(&user_repo)) |moment| 85 try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, &fork_id) 86 else 87 null; 88 if (existing != null) return error.InvalidPatchDraft; 89 90 // the fork copies the target's db, so it takes the target's hash kind 91 const target_path = try evt.repoPath(allocator, users_dir, &input.repo_user_id, &input.repo_id); 92 defer allocator.free(target_path); 93 var any_target = try rp.AnyRepo(.xit, any_opts).open(io, allocator, .{ .path = target_path, .require_repo_root = true }); 94 defer any_target.deinit(io, allocator); 95 switch (any_target) { 96 inline else => |*target_repo| try copyTarget(target_repo.self_repo_opts, io, allocator, users_dir, target_repo, &user_repo, fork_path, input), 97 } 98 return fork_path; 99 } 100 101 // copy the target into a new fork at `fork_path` and record its patch and fork events 102 fn copyTarget( 103 comptime repo_opts: rp.RepoOpts(.xit), 104 io: std.Io, 105 allocator: std.mem.Allocator, 106 users_dir: []const u8, 107 target_repo: *rp.Repo(.xit, repo_opts), 108 user_repo: *rp.Repo(.xit, evt.user_repo_opts), 109 fork_path: []const u8, 110 input: CreateInput, 111 ) !void { 112 if ((try target_repo.readRef(io, .{ .kind = .head, .name = input.target_branch })) == null) return error.TargetNotFound; 113 114 // create the fork repo dir 115 const forks_path = std.Io.Dir.path.dirname(fork_path) orelse return error.InvalidPatchDraft; 116 var forks_dir = try std.Io.Dir.cwd().createDirPathOpen(io, forks_path, .{}); 117 defer forks_dir.close(io); 118 const fork_name = std.Io.Dir.path.basename(fork_path); 119 forks_dir.createDir(io, fork_name, .default_dir) catch |err| switch (err) { 120 error.PathAlreadyExists => return error.InvalidPatchDraft, 121 else => |other| return other, 122 }; 123 errdefer forks_dir.deleteTree(io, fork_name) catch {}; 124 var fork_dir = try forks_dir.openDir(io, fork_name, .{}); 125 defer fork_dir.close(io); 126 var fork_repo_dir = try fork_dir.createDirPathOpen(io, ".xit", .{}); 127 defer fork_repo_dir.close(io); 128 129 // copy the target repo into the fork repo dir 130 { 131 try target_repo.core.db_file.lock(io, .shared); 132 defer target_repo.core.db_file.unlock(io); 133 134 // TODO: use reflink here when the filesystem supports it 135 const destination = try fork_repo_dir.createFile(io, "db", .{ .exclusive = true, .read = true }); 136 defer destination.close(io); 137 var read_buffer: [64 * 1024]u8 = undefined; 138 var write_buffer: [64 * 1024]u8 = undefined; 139 var reader = target_repo.core.db_file.reader(io, &read_buffer); 140 var writer = destination.writer(io, &write_buffer); 141 _ = try reader.interface.streamRemaining(&writer.interface); 142 try writer.interface.flush(); 143 try destination.sync(io); 144 } 145 146 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 147 defer fork_repo.deinit(io, allocator); 148 149 // clear the haxy state in the fork repo and create the patch branch 150 { 151 const DB = rp.Repo(.xit, repo_opts).DB; 152 const State = rp.Repo(.xit, repo_opts).State; 153 const Ctx = struct { 154 core: *rp.Repo(.xit, repo_opts).Core, 155 io: std.Io, 156 allocator: std.mem.Allocator, 157 158 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 159 var moment = try DB.HashMap(.read_write).init(cursor.*); 160 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 161 const head_oid_maybe = try rf.readHeadRecurMaybe(.xit, repo_opts, state.readOnly(), ctx.io); 162 163 var path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 164 const events_path = try evt.events_ref.toPath(&path_buffer); 165 rf.remove(.xit, repo_opts, state, ctx.io, events_path) catch |err| switch (err) { 166 error.RefNotFound => {}, 167 else => |other| return other, 168 }; 169 const patch_path = try ref.toPath(&path_buffer); 170 rf.remove(.xit, repo_opts, state, ctx.io, patch_path) catch |err| switch (err) { 171 error.RefNotFound => {}, 172 else => |other| return other, 173 }; 174 175 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.history_key)); 176 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.last_object_id_key)); 177 // forks aren't searched, so the copied versions would linger 178 // with nothing to prune them 179 _ = try moment.remove(hash.hashInt(repo_opts.hash, srch_cmmt.index_key)); 180 181 if (head_oid_maybe) |*head_oid| { 182 try rf.write(.xit, repo_opts, state, ctx.io, patch_path, .{ .oid = head_oid }); 183 } else { 184 try bch.add(.xit, repo_opts, state, ctx.io, .{ .name = ref.name, .target = .none }); 185 } 186 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 187 188 var config = try xit.config.Config(.xit, repo_opts).init(state.readOnly(), ctx.io, ctx.allocator); 189 defer config.deinit(); 190 try config.add(state, ctx.io, .{ .name = "receive.denydeletes", .value = "true" }); 191 192 // the copied db carries the target's forker entries 193 if (config.local_sections.get(forker_section)) |section| { 194 var names_arena = std.heap.ArenaAllocator.init(ctx.allocator); 195 defer names_arena.deinit(); 196 const na = names_arena.allocator(); 197 var names: std.ArrayList([]const u8) = .empty; 198 for (section.keys()) |key| { 199 if (std.mem.startsWith(u8, key, forker_prefix)) try names.append(na, try na.print(forker_section ++ ".{s}", .{key})); 200 } 201 for (names.items) |name| try config.remove(state, ctx.io, .{ .name = name }); 202 } 203 204 // the copied db carries the target's file index, so the patch 205 // branch's entry starts from one of those rather than walking 206 // the tree 207 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, null); 208 209 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 210 } 211 }; 212 213 try fork_repo.core.db_file.lock(io, .exclusive); 214 defer fork_repo.core.db_file.unlock(io); 215 216 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 217 try history.appendContext( 218 .{ .slot = try history.getSlot(-1) }, 219 Ctx{ .core = &fork_repo.core, .io = io, .allocator = allocator }, 220 ); 221 } 222 223 // create the patch event 224 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 225 .id = input.id, 226 .timestamp = input.timestamp, 227 .author = input.author, 228 .event = .{ .patch = .{ 229 .title = input.title, 230 .description = input.description, 231 .labels = input.labels, 232 .target_branch = input.target_branch, 233 } }, 234 }}); 235 236 // create the fork event 237 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, user_repo, evt.events_ref, &.{.{ 238 .id = input.id, 239 .timestamp = input.timestamp, 240 .author = input.author, 241 .event = .{ .fork = .{ 242 .repo_id = std.fmt.bytesToHex(input.repo_id, .lower), 243 .repo_user_id = std.fmt.bytesToHex(input.repo_user_id, .lower), 244 } }, 245 }}); 246 } 247 248 // tombstone first so a missing fork never remains visible 249 pub fn remove( 250 io: std.Io, 251 allocator: std.mem.Allocator, 252 users_dir: []const u8, 253 forker_id: *const [evt.event_id_size]u8, 254 id: *const [evt.event_id_size * 2]u8, 255 author: evt.CommitAuthor, 256 ) !void { 257 const fork_id = try evt.parseEventId(id); 258 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, forker_id)) orelse return error.InvalidPatchDraft; 259 defer user_repo.deinit(io, allocator); 260 var arena = std.heap.ArenaAllocator.init(allocator); 261 defer arena.deinit(); 262 const moment = (try evt.userMoment(&user_repo)) orelse return error.InvalidPatchDraft; 263 const record = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 264 if (!record.removed) try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, &fork_id, .fork, author); 265 266 const path = try forkPath(allocator, users_dir, forker_id, &fork_id); 267 defer allocator.free(path); 268 try std.Io.Dir.cwd().deleteTree(io, path); 269 } 270