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