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