╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("event.zig"); 3 const serve_common = @import("serve_common.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const hash = xit.hash; 7 const obj = xit.object; 8 const mrg = xit.merge; 9 const bch = xit.branch; 10 const rf = xit.ref; 11 12 pub const ref = rf.Ref{ .kind = .head, .name = "patch" }; 13 const ref_path = "refs/heads/patch"; 14 15 pub const Route = struct { 16 identity: []const u8, 17 id: [evt.event_id_size * 2]u8, 18 target: []const u8, 19 }; 20 21 pub fn parseRoute(route_path: []const u8) ?Route { 22 const patch_segment = "/patch:"; 23 const branch_segment = "/branch:"; 24 const patch_start = std.mem.indexOf(u8, route_path, patch_segment) orelse return null; 25 const branch_start = std.mem.indexOfPos(u8, route_path, patch_start + patch_segment.len, branch_segment) orelse return null; 26 const identity = route_path[0..patch_start]; 27 const id_text = route_path[patch_start + patch_segment.len .. branch_start]; 28 const target = route_path[branch_start + branch_segment.len ..]; 29 if (identity.len == 0 or target.len == 0 or id_text.len != evt.event_id_size * 2) return null; 30 const id_bytes = evt.parseEventId(id_text) catch return null; 31 return .{ .identity = identity, .id = std.fmt.bytesToHex(id_bytes, .lower), .target = target }; 32 } 33 34 pub fn forkPath(allocator: std.mem.Allocator, repo_root_path: []const u8, id: []const u8) ![]u8 { 35 _ = try evt.parseEventId(id); 36 return try std.fs.path.join(allocator, &.{ std.fs.path.dirname(repo_root_path) orelse ".", "forks", id }); 37 } 38 39 pub const CreateInput = struct { 40 id: [evt.event_id_size * 2]u8, 41 user_id: [evt.event_id_size]u8, 42 repo_id: [evt.event_id_size]u8, 43 title: []const u8, 44 description: []const u8, 45 tags: []const u8, 46 author: evt.CommitAuthor, 47 timestamp: u64, 48 }; 49 50 pub fn create( 51 comptime repo_opts: rp.RepoOpts(.xit), 52 io: std.Io, 53 allocator: std.mem.Allocator, 54 repo_root_path: []const u8, 55 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 56 input: CreateInput, 57 ) ![]u8 { 58 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidPatch; 59 60 // get the fork id and path 61 const fork_id = try evt.parseEventId(&input.id); 62 const fork_path = try forkPath(allocator, repo_root_path, &input.id); 63 errdefer allocator.free(fork_path); 64 65 // make sure the fork id doesn't already exist 66 var arena = std.heap.ArenaAllocator.init(allocator); 67 defer arena.deinit(); 68 const existing = if (evt.currentMoment(evt.admin_repo_opts, admin_repo)) |moment| 69 try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id) 70 else |err| switch (err) { 71 error.NotFound => null, 72 else => |other| return other, 73 }; 74 if (existing != null) return error.InvalidPatchDraft; 75 76 // get the target repo 77 const target_id = std.fmt.bytesToHex(input.repo_id, .lower); 78 const target_path = try std.fs.path.join(allocator, &.{ repo_root_path, &target_id }); 79 defer allocator.free(target_path); 80 var target_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path, .require_repo_root = true }); 81 defer target_repo.deinit(io, allocator); 82 83 // create the fork repo dir 84 const forks_path = std.fs.path.dirname(fork_path) orelse return error.InvalidPatchDraft; 85 var forks_dir = try std.Io.Dir.cwd().createDirPathOpen(io, forks_path, .{}); 86 defer forks_dir.close(io); 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 126 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 127 var moment = try DB.HashMap(.read_write).init(cursor.*); 128 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 129 const head_oid_maybe = try rf.readHeadRecurMaybe(.xit, repo_opts, state.readOnly(), ctx.io); 130 131 var path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 132 const events_path = try evt.events_ref.toPath(&path_buffer); 133 rf.remove(.xit, repo_opts, state, ctx.io, events_path) catch |err| switch (err) { 134 error.RefNotFound => {}, 135 else => |other| return other, 136 }; 137 const patch_path = try ref.toPath(&path_buffer); 138 rf.remove(.xit, repo_opts, state, ctx.io, patch_path) catch |err| switch (err) { 139 error.RefNotFound => {}, 140 else => |other| return other, 141 }; 142 143 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.materialized_key)); 144 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.last_object_id_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.writeMessage(repo_opts, state, .{ .custom = "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 = "receive.denycurrentbranch", .value = "updateinstead" }); 167 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denydeletes", .value = "true" }); 168 169 // create the patch event 170 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 171 .id = input.id, 172 .timestamp = input.timestamp, 173 .author = input.author, 174 .event = .{ .patch = .{ 175 .title = input.title, 176 .description = input.description, 177 .tags = input.tags, 178 } }, 179 }}); 180 181 // create the fork event 182 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 183 .id = input.id, 184 .timestamp = input.timestamp, 185 .author = input.author, 186 .event = .{ .fork = .{ 187 .user_id = &input.user_id, 188 .repo_id = &input.repo_id, 189 } }, 190 }}); 191 192 return fork_path; 193 } 194 195 pub fn receivePack( 196 comptime repo_opts: rp.RepoOpts(.xit), 197 io: std.Io, 198 allocator: std.mem.Allocator, 199 fork_repo: *rp.Repo(.xit, repo_opts), 200 target_repo: *rp.Repo(.xit, repo_opts), 201 id: *const [evt.event_id_size * 2]u8, 202 target_branch: []const u8, 203 author: evt.CommitAuthor, 204 timestamp: u64, 205 reader: *std.Io.Reader, 206 writer: *std.Io.Writer, 207 error_writer: *std.Io.Writer, 208 ) !void { 209 if (!rf.validateName(target_branch)) return error.InvalidTarget; 210 const patch_id = try evt.parseEventId(id); 211 var arena = std.heap.ArenaAllocator.init(allocator); 212 defer arena.deinit(); 213 214 // load the fork and target patch state 215 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 216 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 217 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 218 error.NotFound => null, 219 else => |other| return other, 220 }; 221 const target_patch = if (target_moment) |moment| 222 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 223 else 224 null; 225 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 226 if (target_patch) |patch| { 227 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 228 if (patch.event.status == .merged) return error.PatchAlreadyMerged; 229 } 230 const published = target_patch != null; 231 const title = if (target_patch) |patch| patch.event.title else fork_patch.event.title; 232 233 // resolve the target and the newest fork revision 234 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 235 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 236 const target_ref = try std.fmt.allocPrint(arena.allocator(), "refs/heads/{s}", .{target_branch}); 237 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 238 239 // execute a transaction that receives the push and materializes its revision 240 { 241 const DB = rp.Repo(.xit, repo_opts).DB; 242 const State = rp.Repo(.xit, repo_opts).State; 243 const Ctx = struct { 244 core: *rp.Repo(.xit, repo_opts).Core, 245 target_core: *rp.Repo(.xit, repo_opts).Core, 246 io: std.Io, 247 allocator: std.mem.Allocator, 248 reader: *std.Io.Reader, 249 writer: *std.Io.Writer, 250 patch_id: [evt.event_id_size * 2]u8, 251 patch: evt.Patch.Record, 252 published: bool, 253 target_oid: [hash.hexLen(repo_opts.hash)]u8, 254 target_ref: []const u8, 255 title: []const u8, 256 author: evt.CommitAuthor, 257 timestamp: u64, 258 newest: ?evt.PatchRev.WithId, 259 revision_id_maybe: *?[evt.event_id_size]u8, 260 261 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 262 // receive the branch update 263 var moment = try DB.HashMap(.read_write).init(cursor.*); 264 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 265 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, .{ .allowed_ref = ref_path }); 266 267 // copy the target history needed to preserve the merge base 268 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ref })) orelse return error.CancelTransaction; 269 var target_repo_moment = try ctx.target_core.latestMoment(); 270 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 271 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 272 defer objects.deinit(); 273 try objects.include(&ctx.target_oid); 274 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 275 276 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 277 const existing_revision = if (ctx.newest) |latest| 278 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 279 std.mem.eql(u8, latest.record.event.source_oid, &source_oid) and 280 std.mem.eql(u8, latest.record.event.target_ref, ctx.target_ref)) latest else null 281 else 282 null; 283 284 // reuse an identical revision or record a new one 285 var events: [2]evt.EventWithId = undefined; 286 var event_count: usize = 0; 287 var base_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 288 var head_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 289 var tree_entries: [2]evt.EventTreeEntry = undefined; 290 if (existing_revision) |latest| { 291 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 292 } else { 293 var base_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &base_oid); 294 defer base_object.deinit(); 295 var source_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &source_oid); 296 defer source_object.deinit(); 297 base_tree_oid = base_object.content.commit.tree; 298 head_tree_oid = source_object.content.commit.tree; 299 300 var revision_id: [evt.event_id_size]u8 = undefined; 301 ctx.io.random(&revision_id); 302 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 303 const revision_event: evt.PatchRev = .{ 304 .base_oid = &base_oid, 305 .source_oid = &source_oid, 306 .target_ref = ctx.target_ref, 307 .message = ctx.title, 308 }; 309 const identity = try std.fmt.allocPrint(ctx.allocator, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 310 defer ctx.allocator.free(identity); 311 const patch_oid = try evt.PatchRev.writeSquashCommit( 312 .xit, 313 repo_opts, 314 state, 315 ctx.io, 316 ctx.allocator, 317 revision_event, 318 &head_tree_oid, 319 identity, 320 identity, 321 ctx.timestamp, 322 ); 323 tree_entries = .{ 324 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 325 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 326 }; 327 events[event_count] = .{ 328 .id = revision_hex, 329 .timestamp = ctx.timestamp, 330 .author = ctx.author, 331 .tree_entries = &tree_entries, 332 .event = .{ .patchrev = revision_event }, 333 }; 334 event_count += 1; 335 ctx.revision_id_maybe.* = revision_id; 336 337 if (!ctx.published) { 338 var patch = ctx.patch.event; 339 patch.revision = .{ 340 .id = revision_hex, 341 .squash_oid = &patch_oid, 342 .source_oid = &source_oid, 343 .target_ref = ctx.target_ref, 344 }; 345 events[event_count] = .{ 346 .id = ctx.patch_id, 347 .timestamp = ctx.timestamp, 348 .author = ctx.author, 349 .event = .{ .patch = patch }, 350 }; 351 event_count += 1; 352 } 353 } 354 355 // published patches keep only revisions in the fork 356 if (ctx.published and !ctx.patch.removed) { 357 events[event_count] = .{ 358 .id = ctx.patch_id, 359 .timestamp = ctx.timestamp, 360 .author = ctx.author, 361 .event = .{ .patch = null }, 362 }; 363 event_count += 1; 364 } 365 366 // commit and index any new events 367 if (event_count > 0) { 368 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 369 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 370 } 371 try xit.undo.writeMessage(repo_opts, state, .push); 372 } 373 }; 374 375 try fork_repo.core.db_file.lock(io, .exclusive); 376 defer fork_repo.core.db_file.unlock(io); 377 378 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 379 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 380 .core = &fork_repo.core, 381 .target_core = &target_repo.core, 382 .io = io, 383 .allocator = allocator, 384 .reader = reader, 385 .writer = writer, 386 .patch_id = id.*, 387 .patch = fork_patch, 388 .published = published, 389 .target_oid = target_oid, 390 .target_ref = target_ref, 391 .title = title, 392 .author = author, 393 .timestamp = timestamp, 394 .newest = newest, 395 .revision_id_maybe = &revision_id_maybe, 396 }) catch |err| switch (err) { 397 error.CancelTransaction => {}, 398 else => |other| return other, 399 }; 400 try writer.flush(); 401 } 402 403 // best-effort update the published patch so it has the new revision 404 { 405 const revision_id = revision_id_maybe orelse return; 406 if (!published) return; 407 var update_arena = std.heap.ArenaAllocator.init(allocator); 408 defer update_arena.deinit(); 409 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch return; 410 const published_patch = (evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_update_moment, &update_arena, &patch_id) catch return) orelse return; 411 if (published_patch.removed) return; 412 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch return; 413 const revision = (evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_update_moment, &update_arena, &revision_id) catch return) orelse return; 414 if (revision.removed) return; 415 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 416 if (published_patch.event.revision) |current| { 417 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) return; 418 } 419 var patch = published_patch.event; 420 patch.revision = selected; 421 evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 422 .id = id.*, 423 .timestamp = timestamp, 424 .author = author, 425 .event = .{ .patch = patch }, 426 }}) catch |update_err| { 427 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 428 }; 429 } 430 } 431 432 // tombstone first so a missing fork never remains visible 433 pub fn remove( 434 io: std.Io, 435 allocator: std.mem.Allocator, 436 repo_root_path: []const u8, 437 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 438 id: *const [evt.event_id_size * 2]u8, 439 expected_user_id: ?*const [evt.event_id_size]u8, 440 author: evt.CommitAuthor, 441 ) !void { 442 const fork_id = try evt.parseEventId(id); 443 var arena = std.heap.ArenaAllocator.init(allocator); 444 defer arena.deinit(); 445 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 446 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 447 if (expected_user_id) |user_id| { 448 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 449 } 450 if (!record.removed) try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 451 452 const path = try forkPath(allocator, repo_root_path, id); 453 defer allocator.free(path); 454 try std.Io.Dir.cwd().deleteTree(io, path); 455 } 456