╭ 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 obj = xit.object; 7 const mrg = xit.merge; 8 const bch = xit.branch; 9 const rf = xit.ref; 10 11 pub const ref = rf.Ref{ .kind = .head, .name = "patch" }; 12 const ref_path = "refs/heads/patch"; 13 14 pub const Route = struct { 15 identity: []const u8, 16 id: [evt.event_id_size * 2]u8, 17 target: []const u8, 18 }; 19 20 pub fn parseRoute(route_path: []const u8) ?Route { 21 const patch_segment = "/patch:"; 22 const branch_segment = "/branch:"; 23 const patch_start = std.mem.indexOf(u8, route_path, patch_segment) orelse return null; 24 const branch_start = std.mem.indexOfPos(u8, route_path, patch_start + patch_segment.len, branch_segment) orelse return null; 25 const identity = route_path[0..patch_start]; 26 const id_text = route_path[patch_start + patch_segment.len .. branch_start]; 27 const target = route_path[branch_start + branch_segment.len ..]; 28 if (identity.len == 0 or target.len == 0 or id_text.len != evt.event_id_size * 2) return null; 29 const id_bytes = evt.parseEventId(id_text) catch return null; 30 return .{ .identity = identity, .id = std.fmt.bytesToHex(id_bytes, .lower), .target = target }; 31 } 32 33 pub fn forkPath(allocator: std.mem.Allocator, repo_root_path: []const u8, id: []const u8) ![]u8 { 34 _ = try evt.parseEventId(id); 35 return try std.fs.path.join(allocator, &.{ std.fs.path.dirname(repo_root_path) orelse ".", "forks", id }); 36 } 37 38 pub const CreateInput = struct { 39 id: [evt.event_id_size * 2]u8, 40 user_id: [evt.event_id_size]u8, 41 repo_id: [evt.event_id_size]u8, 42 title: []const u8, 43 description: []const u8, 44 tags: []const u8, 45 author: evt.CommitAuthor, 46 timestamp: u64, 47 }; 48 49 pub fn create( 50 comptime repo_opts: rp.RepoOpts(.xit), 51 io: std.Io, 52 allocator: std.mem.Allocator, 53 repo_root_path: []const u8, 54 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 55 input: CreateInput, 56 ) ![]u8 { 57 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidPatch; 58 59 // get the fork id and path 60 const fork_id = try evt.parseEventId(&input.id); 61 const fork_path = try forkPath(allocator, repo_root_path, &input.id); 62 errdefer allocator.free(fork_path); 63 64 // make sure the fork id doesn't already exist 65 var arena = std.heap.ArenaAllocator.init(allocator); 66 defer arena.deinit(); 67 const existing = if (evt.currentMoment(evt.admin_repo_opts, admin_repo)) |moment| 68 try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id) 69 else |err| switch (err) { 70 error.NotFound => null, 71 else => |other| return other, 72 }; 73 if (existing != null) return error.InvalidPatchDraft; 74 75 // get the target repo 76 const target_id = std.fmt.bytesToHex(input.repo_id, .lower); 77 const target_path = try std.fs.path.join(allocator, &.{ repo_root_path, &target_id }); 78 defer allocator.free(target_path); 79 var target_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path, .require_repo_root = true }); 80 defer target_repo.deinit(io, allocator); 81 82 // create the fork repo dir 83 const forks_path = std.fs.path.dirname(fork_path) orelse return error.InvalidPatchDraft; 84 var forks_dir = try std.Io.Dir.cwd().createDirPathOpen(io, forks_path, .{}); 85 defer forks_dir.close(io); 86 const fork_name = std.fs.path.basename(fork_path); 87 forks_dir.createDir(io, fork_name, .default_dir) catch |err| switch (err) { 88 error.PathAlreadyExists => return error.InvalidPatchDraft, 89 else => |other| return other, 90 }; 91 errdefer forks_dir.deleteTree(io, fork_name) catch {}; 92 var fork_dir = try forks_dir.openDir(io, fork_name, .{}); 93 defer fork_dir.close(io); 94 var fork_repo_dir = try fork_dir.createDirPathOpen(io, ".xit", .{}); 95 defer fork_repo_dir.close(io); 96 97 // copy the target repo into the fork repo dir 98 { 99 try target_repo.core.db_file.lock(io, .shared); 100 defer target_repo.core.db_file.unlock(io); 101 try target_repo.core.chunk_store_file.lock(io, .shared); 102 defer target_repo.core.chunk_store_file.unlock(io); 103 104 // TODO: use reflinks here when the filesystem supports them 105 for ([_]struct { file: std.Io.File, name: []const u8 }{ 106 .{ .file = target_repo.core.db_file, .name = "db" }, 107 .{ .file = target_repo.core.chunk_store_file, .name = "chunks" }, 108 }) |source| { 109 const destination = try fork_repo_dir.createFile(io, source.name, .{ .exclusive = true, .read = true }); 110 defer destination.close(io); 111 var read_buffer: [64 * 1024]u8 = undefined; 112 var write_buffer: [64 * 1024]u8 = undefined; 113 var reader = source.file.reader(io, &read_buffer); 114 var writer = destination.writer(io, &write_buffer); 115 _ = try reader.interface.streamRemaining(&writer.interface); 116 try writer.interface.flush(); 117 try destination.sync(io); 118 } 119 } 120 121 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 122 defer fork_repo.deinit(io, allocator); 123 124 // clear the haxy state in the fork repo and create the patch branch 125 { 126 const DB = rp.Repo(.xit, repo_opts).DB; 127 const State = rp.Repo(.xit, repo_opts).State; 128 const Ctx = struct { 129 core: *rp.Repo(.xit, repo_opts).Core, 130 io: std.Io, 131 132 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 133 var moment = try DB.HashMap(.read_write).init(cursor.*); 134 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 135 const head_oid_maybe = try rf.readHeadRecurMaybe(.xit, repo_opts, state.readOnly(), ctx.io); 136 137 var path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 138 const events_path = try evt.events_ref.toPath(&path_buffer); 139 rf.remove(.xit, repo_opts, state, ctx.io, events_path) catch |err| switch (err) { 140 error.RefNotFound => {}, 141 else => |other| return other, 142 }; 143 const patch_path = try ref.toPath(&path_buffer); 144 rf.remove(.xit, repo_opts, state, ctx.io, patch_path) catch |err| switch (err) { 145 error.RefNotFound => {}, 146 else => |other| return other, 147 }; 148 149 _ = try moment.remove(hash.hashInt(repo_opts.hash, "haxy")); 150 _ = try moment.remove(hash.hashInt(repo_opts.hash, "haxy-last-object-id")); 151 152 if (head_oid_maybe) |*head_oid| { 153 try rf.write(.xit, repo_opts, state, ctx.io, patch_path, .{ .oid = head_oid }); 154 } else { 155 try bch.add(.xit, repo_opts, state, ctx.io, .{ .name = ref.name, .target = .none }); 156 } 157 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 158 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "create fork" }); 159 } 160 }; 161 162 try fork_repo.core.db_file.lock(io, .exclusive); 163 defer fork_repo.core.db_file.unlock(io); 164 165 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 166 try history.appendContext( 167 .{ .slot = try history.getSlot(-1) }, 168 Ctx{ .core = &fork_repo.core, .io = io }, 169 ); 170 } 171 172 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 173 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denydeletes", .value = "true" }); 174 175 // create the patch event 176 try evt.consume(.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 } }, 185 }}); 186 187 // create the fork event 188 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 189 .id = input.id, 190 .timestamp = input.timestamp, 191 .author = input.author, 192 .event = .{ .fork = .{ 193 .user_id = &input.user_id, 194 .repo_id = &input.repo_id, 195 } }, 196 }}); 197 198 return fork_path; 199 } 200 201 pub fn receivePack( 202 comptime repo_opts: rp.RepoOpts(.xit), 203 io: std.Io, 204 allocator: std.mem.Allocator, 205 fork_repo: *rp.Repo(.xit, repo_opts), 206 target_repo: *rp.Repo(.xit, repo_opts), 207 id: *const [evt.event_id_size * 2]u8, 208 target_branch: []const u8, 209 author: evt.CommitAuthor, 210 timestamp: u64, 211 reader: *std.Io.Reader, 212 writer: *std.Io.Writer, 213 ) !void { 214 if (!rf.validateName(target_branch)) return error.InvalidTarget; 215 const patch_id = try evt.parseEventId(id); 216 var arena = std.heap.ArenaAllocator.init(allocator); 217 defer arena.deinit(); 218 219 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 220 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 221 222 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 223 error.NotFound => null, 224 else => |other| return other, 225 }; 226 const target_patch = if (target_moment) |moment| 227 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 228 else 229 null; 230 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 231 if (target_patch) |patch| { 232 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 233 } 234 const published = target_patch != null; 235 const title = if (target_patch) |patch| patch.event.title else fork_patch.event.title; 236 237 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 238 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 239 const target_ref = try std.fmt.allocPrint(arena.allocator(), "refs/heads/{s}", .{target_branch}); 240 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 241 242 const DB = rp.Repo(.xit, repo_opts).DB; 243 const State = rp.Repo(.xit, repo_opts).State; 244 const Ctx = struct { 245 core: *rp.Repo(.xit, repo_opts).Core, 246 target_core: *rp.Repo(.xit, repo_opts).Core, 247 io: std.Io, 248 allocator: std.mem.Allocator, 249 reader: *std.Io.Reader, 250 writer: *std.Io.Writer, 251 patch_id: [evt.event_id_size * 2]u8, 252 patch: evt.Patch.Record, 253 published: bool, 254 target_oid: [hash.hexLen(repo_opts.hash)]u8, 255 target_ref: []const u8, 256 title: []const u8, 257 author: evt.CommitAuthor, 258 timestamp: u64, 259 newest: ?evt.PatchRev.WithId, 260 revision_id_maybe: *?[evt.event_id_size]u8, 261 262 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 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 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ref })) orelse return error.CancelTransaction; 268 var target_repo_moment = try ctx.target_core.latestMoment(); 269 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 270 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 271 defer objects.deinit(); 272 try objects.include(&ctx.target_oid); 273 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 274 275 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 276 const existing_revision = if (ctx.newest) |latest| 277 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 278 std.mem.eql(u8, latest.record.event.source_oid, &source_oid) and 279 std.mem.eql(u8, latest.record.event.target_ref, ctx.target_ref)) latest else null 280 else 281 null; 282 283 var events: [2]evt.EventWithId = undefined; 284 var event_count: usize = 0; 285 var base_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 286 var head_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 287 var tree_entries: [2]evt.EventTreeEntry = undefined; 288 if (existing_revision) |latest| { 289 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 290 } else { 291 var base_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &base_oid); 292 defer base_object.deinit(); 293 var source_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &source_oid); 294 defer source_object.deinit(); 295 base_tree_oid = base_object.content.commit.tree; 296 head_tree_oid = source_object.content.commit.tree; 297 298 var revision_id: [evt.event_id_size]u8 = undefined; 299 ctx.io.random(&revision_id); 300 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 301 tree_entries = .{ 302 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 303 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 304 }; 305 events[event_count] = .{ 306 .id = revision_hex, 307 .timestamp = ctx.timestamp, 308 .author = ctx.author, 309 .tree_entries = &tree_entries, 310 .event = .{ .patchrev = .{ 311 .base_oid = &base_oid, 312 .source_oid = &source_oid, 313 .target_ref = ctx.target_ref, 314 .message = ctx.title, 315 } }, 316 }; 317 event_count += 1; 318 ctx.revision_id_maybe.* = revision_id; 319 320 if (!ctx.published) { 321 events[event_count] = .{ 322 .id = ctx.patch_id, 323 .timestamp = ctx.timestamp, 324 .author = ctx.author, 325 .event = .{ .patch = .{ 326 .title = ctx.patch.event.title, 327 .description = ctx.patch.event.description, 328 .tags = ctx.patch.event.tags, 329 .target_patch_id = ctx.patch.event.target_patch_id, 330 .patchrev_id = revision_hex, 331 } }, 332 }; 333 event_count += 1; 334 } 335 } 336 337 if (ctx.published and !ctx.patch.removed) { 338 events[event_count] = .{ 339 .id = ctx.patch_id, 340 .timestamp = ctx.timestamp, 341 .author = ctx.author, 342 .event = .{ .patch = null }, 343 }; 344 event_count += 1; 345 } 346 if (event_count > 0) { 347 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 348 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 349 } 350 try xit.undo.writeMessage(repo_opts, state, .push); 351 try ctx.core.chunk_store_file.sync(ctx.io); 352 } 353 }; 354 355 try fork_repo.core.db_file.lock(io, .exclusive); 356 defer fork_repo.core.db_file.unlock(io); 357 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 358 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 359 .core = &fork_repo.core, 360 .target_core = &target_repo.core, 361 .io = io, 362 .allocator = allocator, 363 .reader = reader, 364 .writer = writer, 365 .patch_id = id.*, 366 .patch = fork_patch, 367 .published = published, 368 .target_oid = target_oid, 369 .target_ref = target_ref, 370 .title = title, 371 .author = author, 372 .timestamp = timestamp, 373 .newest = newest, 374 .revision_id_maybe = &revision_id_maybe, 375 }) catch |err| switch (err) { 376 error.CancelTransaction => {}, 377 else => |other| return other, 378 }; 379 try writer.flush(); 380 381 const revision_id = revision_id_maybe orelse return; 382 if (!published) return; 383 var update_arena = std.heap.ArenaAllocator.init(allocator); 384 defer update_arena.deinit(); 385 const update_moment = evt.currentMoment(repo_opts, target_repo) catch return; 386 const published_patch = (evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, update_moment, &update_arena, &patch_id) catch return) orelse return; 387 if (published_patch.removed) return; 388 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 389 if (published_patch.event.patchrev_id) |*published_revision| { 390 if (std.mem.eql(u8, published_revision, &revision_hex)) return; 391 } 392 evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 393 .id = id.*, 394 .timestamp = timestamp, 395 .author = author, 396 .event = .{ .patch = .{ 397 .title = published_patch.event.title, 398 .description = published_patch.event.description, 399 .tags = published_patch.event.tags, 400 .target_patch_id = published_patch.event.target_patch_id, 401 .patchrev_id = revision_hex, 402 } }, 403 }}) catch {}; 404 } 405 406 pub const PublishInput = struct { 407 id: [evt.event_id_size * 2]u8, 408 user_id: [evt.event_id_size]u8, 409 repo_id: [evt.event_id_size]u8, 410 author: evt.CommitAuthor, 411 timestamp: u64, 412 }; 413 414 pub fn publish( 415 comptime repo_opts: rp.RepoOpts(.xit), 416 io: std.Io, 417 allocator: std.mem.Allocator, 418 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 419 target_repo: *rp.Repo(.xit, repo_opts), 420 path: []const u8, 421 input: PublishInput, 422 ) !void { 423 const patch_id = try evt.parseEventId(&input.id); 424 var arena = std.heap.ArenaAllocator.init(allocator); 425 defer arena.deinit(); 426 427 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 428 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.InvalidPatchDraft; 429 if (fork_record.removed or 430 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 431 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 432 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 433 if (user.removed or 434 !std.mem.eql(u8, user.event.name, input.author.name) or 435 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 436 437 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = path }) catch return error.PatchDataUnavailable; 438 defer fork_repo.deinit(io, allocator); 439 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 440 const local_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 441 442 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 443 error.NotFound => null, 444 else => |other| return other, 445 }; 446 const existing = if (target_moment) |moment| 447 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 448 else 449 null; 450 if (existing) |record| { 451 if (record.removed or !std.mem.eql(u8, record.author_email orelse "", input.author.email)) return error.InvalidPatch; 452 } 453 if (local_patch.removed) { 454 if (existing == null) return error.PatchDataUnavailable; 455 return; 456 } 457 458 const revision_hex = local_patch.event.patchrev_id orelse return error.PatchNotPushed; 459 const revision_id = try evt.parseEventId(&revision_hex); 460 const revision = (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return error.PatchNotPushed; 461 if (revision.removed) return error.PatchNotPushed; 462 const metadata = existing orelse local_patch; 463 464 try evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 465 .id = input.id, 466 .timestamp = input.timestamp, 467 .author = input.author, 468 .event = .{ .patch = .{ 469 .title = metadata.event.title, 470 .description = metadata.event.description, 471 .tags = metadata.event.tags, 472 .target_patch_id = metadata.event.target_patch_id, 473 .patchrev_id = revision_hex, 474 } }, 475 }}); 476 477 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 478 .id = input.id, 479 .timestamp = input.timestamp, 480 .author = input.author, 481 .event = .{ .patch = null }, 482 }}); 483 } 484 485 // delete first so a failed tombstone can be retried safely 486 pub fn remove( 487 io: std.Io, 488 allocator: std.mem.Allocator, 489 repo_root_path: []const u8, 490 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 491 id: *const [evt.event_id_size * 2]u8, 492 user_id: *const [evt.event_id_size]u8, 493 author: evt.CommitAuthor, 494 ) !void { 495 const fork_id = try evt.parseEventId(id); 496 var arena = std.heap.ArenaAllocator.init(allocator); 497 defer arena.deinit(); 498 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 499 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 500 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 501 502 const path = try forkPath(allocator, repo_root_path, id); 503 defer allocator.free(path); 504 try std.Io.Dir.cwd().deleteTree(io, path); 505 if (!record.removed) try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 506 } 507