╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
129 130 var path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 131 const events_path = try evt.events_ref.toPath(&path_buffer); 132 rf.remove(.xit, repo_opts, state, ctx.io, events_path) catch |err| switch (err) { 133 error.RefNotFound => {}, 134 else => |other| return other, 135 }; 136 const patch_path = try ref.toPath(&path_buffer); 137 rf.remove(.xit, repo_opts, state, ctx.io, patch_path) catch |err| switch (err) { 138 error.RefNotFound => {}, 139 else => |other| return other, 140 }; 141 142 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.materialized_key)); 143 _ = try moment.remove(hash.hashInt(repo_opts.hash, evt.last_object_id_key)); 144 145 if (head_oid_maybe) |*head_oid| { 146 try rf.write(.xit, repo_opts, state, ctx.io, patch_path, .{ .oid = head_oid }); 147 } else { 148 try bch.add(.xit, repo_opts, state, ctx.io, .{ .name = ref.name, .target = .none }); 149 } 150 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 151 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "create fork" }); 152 } 153 }; 154 155 try fork_repo.core.db_file.lock(io, .exclusive); 156 defer fork_repo.core.db_file.unlock(io); 157 158 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 159 try history.appendContext( 160 .{ .slot = try history.getSlot(-1) }, 161 Ctx{ .core = &fork_repo.core, .io = io }, 162 ); 163 } 164 165 try fork_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }); 166 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denydeletes", .value = "true" }); 167 168 // create the patch event 169 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 170 .id = input.id, 171 .timestamp = input.timestamp, 172 .author = input.author, 173 .event = .{ .patch = .{ 174 .title = input.title, 175 .description = input.description, 176 .tags = input.tags, 177 .target_branch = input.target_branch, 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 author: evt.CommitAuthor, 203 timestamp: u64, 204 reader: *std.Io.Reader, 205 writer: *std.Io.Writer, 206 error_writer: *std.Io.Writer, 207 ) !void { 208 const patch_id = try evt.parseEventId(id); 209 var arena = std.heap.ArenaAllocator.init(allocator); 210 defer arena.deinit(); 211 212 // load the fork and target patch state 213 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 214 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 215 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 216 error.NotFound => null, 217 else => |other| return other, 218 }; 219 const target_patch = if (target_moment) |moment| 220 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 221 else 222 null; 223 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 224 if (target_patch) |patch| { 225 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 226 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 227 } 228 const published = target_patch != null; 229 const active_patch = target_patch orelse fork_patch; 230 const title = active_patch.event.title; 231 232 // resolve the target and the newest fork revision 233 const target_branch = active_patch.event.target_branch; 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 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 237 238 // execute a transaction that receives the push and materializes its revision 239 { 240 const DB = rp.Repo(.xit, repo_opts).DB; 241 const State = rp.Repo(.xit, repo_opts).State; 242 const Ctx = struct { 243 core: *rp.Repo(.xit, repo_opts).Core, 244 target_core: *rp.Repo(.xit, repo_opts).Core, 245 io: std.Io, 246 allocator: std.mem.Allocator, 247 reader: *std.Io.Reader, 248 writer: *std.Io.Writer, 249 patch_id: [evt.event_id_size * 2]u8, 250 patch: evt.Patch.Record, 251 published: bool, 252 target_oid: [hash.hexLen(repo_opts.hash)]u8, 253 title: []const u8, 254 author: evt.CommitAuthor, 255 timestamp: u64, 256 newest: ?evt.PatchRev.WithId, 257 revision_id_maybe: *?[evt.event_id_size]u8, 258 259 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 260 // receive the branch update 261 var moment = try DB.HashMap(.read_write).init(cursor.*); 262 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 263 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, .{ .allowed_ref = ref_path }); 264 265 // copy the target history needed to preserve the merge base 266 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ref })) orelse return error.CancelTransaction; 267 var target_repo_moment = try ctx.target_core.latestMoment(); 268 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 269 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 270 defer objects.deinit(); 271 try objects.include(&ctx.target_oid); 272 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 273 274 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 275 const existing_revision = if (ctx.newest) |latest| 276 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 277 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 278 else 279 null; 280 281 // reuse an identical revision or record a new one 282 var events: [2]evt.EventWithId = undefined; 283 var event_count: usize = 0; 284 var base_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 285 var head_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 286 var tree_entries: [2]evt.EventTreeEntry = undefined; 287 if (existing_revision) |latest| { 288 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 289 } else { 290 var base_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &base_oid); 291 defer base_object.deinit(); 292 var source_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &source_oid); 293 defer source_object.deinit(); 294 base_tree_oid = base_object.content.commit.tree; 295 head_tree_oid = source_object.content.commit.tree; 296 297 var revision_id: [evt.event_id_size]u8 = undefined; 298 ctx.io.random(&revision_id); 299 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 300 const revision_event: evt.PatchRev = .{ 301 .base_oid = &base_oid, 302 .source_oid = &source_oid, 303 .message = ctx.title, 304 }; 305 const identity = try std.fmt.allocPrint(ctx.allocator, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 306 defer ctx.allocator.free(identity); 307 const patch_oid = try evt.PatchRev.writeSquashCommit( 308 .xit, 309 repo_opts, 310 state, 311 ctx.io, 312 ctx.allocator, 313 revision_event, 314 &head_tree_oid, 315 identity, 316 identity, 317 ctx.timestamp, 318 ); 319 tree_entries = .{ 320 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 321 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 322 }; 323 events[event_count] = .{ 324 .id = revision_hex, 325 .timestamp = ctx.timestamp, 326 .author = ctx.author, 327 .tree_entries = &tree_entries, 328 .event = .{ .patchrev = revision_event }, 329 }; 330 event_count += 1; 331 ctx.revision_id_maybe.* = revision_id; 332 333 if (!ctx.published) { 334 var patch = ctx.patch.event; 335 patch.revision = .{ 336 .id = revision_hex, 337 .squash_oid = &patch_oid, 338 .source_oid = &source_oid, 339 }; 340 events[event_count] = .{ 341 .id = ctx.patch_id, 342 .timestamp = ctx.timestamp, 343 .author = ctx.author, 344 .event = .{ .patch = patch }, 345 }; 346 event_count += 1; 347 } 348 } 349 350 // published patches keep only revisions in the fork 351 if (ctx.published and !ctx.patch.removed) { 352 events[event_count] = .{ 353 .id = ctx.patch_id, 354 .timestamp = ctx.timestamp, 355 .author = ctx.author, 356 .event = .{ .patch = null }, 357 }; 358 event_count += 1; 359 } 360 361 // commit and index any new events 362 if (event_count > 0) { 363 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 364 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 365 } 366 try xit.undo.writeMessage(repo_opts, state, .push); 367 } 368 }; 369 370 try fork_repo.core.db_file.lock(io, .exclusive); 371 defer fork_repo.core.db_file.unlock(io); 372 373 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 374 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 375 .core = &fork_repo.core, 376 .target_core = &target_repo.core, 377 .io = io, 378 .allocator = allocator, 379 .reader = reader, 380 .writer = writer, 381 .patch_id = id.*, 382 .patch = fork_patch, 383 .published = published, 384 .target_oid = target_oid, 385 .title = title, 386 .author = author, 387 .timestamp = timestamp, 388 .newest = newest, 389 .revision_id_maybe = &revision_id_maybe, 390 }) catch |err| switch (err) { 391 error.CancelTransaction => {}, 392 else => |other| return other, 393 }; 394 try writer.flush(); 395 } 396 397 // best-effort update the published patch so it has the new revision 398 { 399 const revision_id = revision_id_maybe orelse return; 400 if (!published) return; 401 var update_arena = std.heap.ArenaAllocator.init(allocator); 402 defer update_arena.deinit(); 403 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch return; 404 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; 405 if (published_patch.removed) return; 406 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch return; 407 const revision = (evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_update_moment, &update_arena, &revision_id) catch return) orelse return; 408 if (revision.removed) return; 409 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 410 if (published_patch.event.revision) |current| { 411 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) return; 412 } 413 var patch = published_patch.event; 414 patch.revision = selected; 415 evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 416 .id = id.*, 417 .timestamp = timestamp, 418 .author = author, 419 .event = .{ .patch = patch }, 420 }}) catch |update_err| { 421 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 422 }; 423 } 424 } 425 426 // tombstone first so a missing fork never remains visible 427 pub fn remove( 428 io: std.Io, 429 allocator: std.mem.Allocator, 430 repo_root_path: []const u8, 431 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 432 id: *const [evt.event_id_size * 2]u8, 433 expected_user_id: ?*const [evt.event_id_size]u8, 434 author: evt.CommitAuthor, 435 ) !void { 436 const fork_id = try evt.parseEventId(id); 437 var arena = std.heap.ArenaAllocator.init(allocator); 438 defer arena.deinit(); 439 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 440 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 441 if (expected_user_id) |user_id| { 442 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 443 } 444 if (!record.removed) try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 445 446 const path = try forkPath(allocator, repo_root_path, id); 447 defer allocator.free(path); 448 try std.Io.Dir.cwd().deleteTree(io, path); 449 } 450