╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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(.server, .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(.server, .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(.server, .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