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