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