╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("event.zig"); 3 const find = @import("find.zig"); 4 const srch_cmmt = @import("search_commit.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 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 pub fn parseRoute(route_path: []const u8) ?Route { 22 const patch_segment = "/patch:"; 23 const patch_start = std.mem.indexOf(u8, route_path, patch_segment) orelse return null; 24 const identity = route_path[0..patch_start]; 25 const id_text = route_path[patch_start + patch_segment.len ..]; 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 pub fn forkPath(allocator: std.mem.Allocator, repo_root_path: []const u8, id: []const u8) ![]u8 { 32 _ = try evt.parseEventId(id); 33 return try std.fs.path.join(allocator, &.{ std.fs.path.dirname(repo_root_path) orelse ".", "forks", id }); 34 } 35 36 pub const CreateInput = struct { 37 id: [evt.event_id_size * 2]u8, 38 user_id: [evt.event_id_size]u8, 39 repo_id: [evt.event_id_size]u8, 40 title: []const u8, 41 description: []const u8, 42 tags: []const u8, 43 target_branch: []const u8, 44 author: evt.CommitAuthor, 45 timestamp: u64, 46 }; 47 48 pub fn create( 49 comptime repo_opts: rp.RepoOpts(.xit), 50 io: std.Io, 51 allocator: std.mem.Allocator, 52 repo_root_path: []const u8, 53 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 54 input: CreateInput, 55 ) ![]u8 { 56 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidPatch; 57 if (!evt.Patch.branchValid(input.target_branch)) return error.InvalidTargetBranch; 58 59 // get the fork id and path 60 const fork_id = try evt.parseEventId(&input.id); 61 const fork_path = try forkPath(allocator, repo_root_path, &input.id); 62 errdefer allocator.free(fork_path); 63 64 // make sure the fork id doesn't already exist 65 var arena = std.heap.ArenaAllocator.init(allocator); 66 defer arena.deinit(); 67 const existing = if (evt.currentMoment(evt.admin_repo_opts, admin_repo)) |moment| 68 try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id) 69 else |err| switch (err) { 70 error.NotFound => null, 71 else => |other| return other, 72 }; 73 if (existing != null) return error.InvalidPatchDraft; 74 75 // get the target repo 76 const target_id = std.fmt.bytesToHex(input.repo_id, .lower); 77 const target_path = try std.fs.path.join(allocator, &.{ repo_root_path, &target_id }); 78 defer allocator.free(target_path); 79 var target_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path, .require_repo_root = true }); 80 defer target_repo.deinit(io, allocator); 81 if ((try target_repo.readRef(io, .{ .kind = .head, .name = input.target_branch })) == null) return error.TargetNotFound; 82 83 // create the fork repo dir 84 const forks_path = std.fs.path.dirname(fork_path) orelse return error.InvalidPatchDraft; 85 var forks_dir = try std.Io.Dir.cwd().createDirPathOpen(io, forks_path, .{}); 86 defer forks_dir.close(io); 87 const fork_name = std.fs.path.basename(fork_path); 88 forks_dir.createDir(io, fork_name, .default_dir) catch |err| switch (err) { 89 error.PathAlreadyExists => return error.InvalidPatchDraft, 90 else => |other| return other, 91 }; 92 errdefer forks_dir.deleteTree(io, fork_name) catch {}; 93 var fork_dir = try forks_dir.openDir(io, fork_name, .{}); 94 defer fork_dir.close(io); 95 var fork_repo_dir = try fork_dir.createDirPathOpen(io, ".xit", .{}); 96 defer fork_repo_dir.close(io); 97 98 // copy the target repo into the fork repo dir 99 { 100 try target_repo.core.db_file.lock(io, .shared); 101 defer target_repo.core.db_file.unlock(io); 102 103 // TODO: use reflink here when the filesystem supports it 104 const destination = try fork_repo_dir.createFile(io, "db", .{ .exclusive = true, .read = true }); 105 defer destination.close(io); 106 var read_buffer: [64 * 1024]u8 = undefined; 107 var write_buffer: [64 * 1024]u8 = undefined; 108 var reader = target_repo.core.db_file.reader(io, &read_buffer); 109 var writer = destination.writer(io, &write_buffer); 110 _ = try reader.interface.streamRemaining(&writer.interface); 111 try writer.interface.flush(); 112 try destination.sync(io); 113 } 114 115 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 116 defer fork_repo.deinit(io, allocator); 117 118 // clear the haxy state in the fork repo and create the patch branch 119 { 120 const DB = rp.Repo(.xit, repo_opts).DB; 121 const State = rp.Repo(.xit, repo_opts).State; 122 const Ctx = struct { 123 core: *rp.Repo(.xit, repo_opts).Core, 124 io: std.Io, 125 allocator: std.mem.Allocator, 126 127 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 128 var moment = try DB.HashMap(.read_write).init(cursor.*); 129 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 130 const head_oid_maybe = try rf.readHeadRecurMaybe(.xit, repo_opts, state.readOnly(), ctx.io); 131 132 var path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 133 const events_path = try evt.events_ref.toPath(&path_buffer); 134 rf.remove(.xit, repo_opts, state, ctx.io, events_path) catch |err| switch (err) { 135 error.RefNotFound => {}, 136 else => |other| return other, 137 }; 138 const patch_path = try ref.toPath(&path_buffer); 139 rf.remove(.xit, repo_opts, state, ctx.io, patch_path) catch |err| switch (err) { 140 error.RefNotFound => {}, 141 else => |other| return other, 142 }; 143 144 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.history_key)); 145 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.last_object_id_key)); 146 // forks aren't searched, so the copied versions would linger 147 // with nothing to prune them 148 _ = try moment.remove(hash.hashInt(repo_opts.hash, srch_cmmt.index_key)); 149 150 if (head_oid_maybe) |*head_oid| { 151 try rf.write(.xit, repo_opts, state, ctx.io, patch_path, .{ .oid = head_oid }); 152 } else { 153 try bch.add(.xit, repo_opts, state, ctx.io, .{ .name = ref.name, .target = .none }); 154 } 155 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 156 157 var config = try xit.config.Config(.xit, repo_opts).init(state.readOnly(), ctx.io, ctx.allocator); 158 defer config.deinit(); 159 try config.add(state, ctx.io, .{ .name = "receive.denydeletes", .value = "true" }); 160 161 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 162 } 163 }; 164 165 try fork_repo.core.db_file.lock(io, .exclusive); 166 defer fork_repo.core.db_file.unlock(io); 167 168 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 169 try history.appendContext( 170 .{ .slot = try history.getSlot(-1) }, 171 Ctx{ .core = &fork_repo.core, .io = io, .allocator = allocator }, 172 ); 173 } 174 175 // the copied db carries the target's file index, so the patch branch's 176 // entry starts from one of those rather than walking the tree 177 find.refresh(repo_opts, io, allocator, &fork_repo) catch |err| { 178 std.log.warn("failed to refresh file index: {s}", .{@errorName(err)}); 179 }; 180 181 // create the patch event 182 try evt.consume(.server, .fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 183 .id = input.id, 184 .timestamp = input.timestamp, 185 .author = input.author, 186 .event = .{ .patch = .{ 187 .title = input.title, 188 .description = input.description, 189 .tags = input.tags, 190 .target_branch = input.target_branch, 191 } }, 192 }}); 193 194 // create the fork event 195 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 196 .id = input.id, 197 .timestamp = input.timestamp, 198 .author = input.author, 199 .event = .{ .fork = .{ 200 .user_id = &input.user_id, 201 .repo_id = &input.repo_id, 202 } }, 203 }}); 204 205 return fork_path; 206 } 207 208 // tombstone first so a missing fork never remains visible 209 pub fn remove( 210 io: std.Io, 211 allocator: std.mem.Allocator, 212 repo_root_path: []const u8, 213 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 214 id: *const [evt.event_id_size * 2]u8, 215 expected_user_id: ?*const [evt.event_id_size]u8, 216 author: evt.CommitAuthor, 217 ) !void { 218 const fork_id = try evt.parseEventId(id); 219 var arena = std.heap.ArenaAllocator.init(allocator); 220 defer arena.deinit(); 221 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 222 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 223 if (expected_user_id) |user_id| { 224 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 225 } 226 if (!record.removed) try evt.remove(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 227 228 const path = try forkPath(allocator, repo_root_path, id); 229 defer allocator.free(path); 230 try std.Io.Dir.cwd().deleteTree(io, path); 231 } 232