╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
244 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 245 246 // execute a transaction that receives the push and materializes its revision 247 { 248 const DB = rp.Repo(.xit, repo_opts).DB; 249 const State = rp.Repo(.xit, repo_opts).State; 250 const Ctx = struct { 251 core: *rp.Repo(.xit, repo_opts).Core, 252 target_core: *rp.Repo(.xit, repo_opts).Core, 253 io: std.Io, 254 allocator: std.mem.Allocator, 255 reader: *std.Io.Reader, 256 writer: *std.Io.Writer, 257 patch_id: [evt.event_id_size * 2]u8, 258 patch: evt.Patch.Record, 259 posted: bool, 260 target_oid: [hash.hexLen(repo_opts.hash)]u8, 261 target_ref: []const u8, 262 title: []const u8, 263 author: evt.CommitAuthor, 264 timestamp: u64, 265 newest: ?evt.PatchRev.WithId, 266 revision_id_maybe: *?[evt.event_id_size]u8, 267 268 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 269 // receive the branch update 270 var moment = try DB.HashMap(.read_write).init(cursor.*); 271 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 272 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, .{ .allowed_ref = ref_path }); 273 274 // copy the target history needed to preserve the merge base 275 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ref })) orelse return error.CancelTransaction; 276 var target_repo_moment = try ctx.target_core.latestMoment(); 277 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 278 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 279 defer objects.deinit(); 280 try objects.include(&ctx.target_oid); 281 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 282 283 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 284 const existing_revision = if (ctx.newest) |latest| 285 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 286 std.mem.eql(u8, latest.record.event.source_oid, &source_oid) and 287 std.mem.eql(u8, latest.record.event.target_ref, ctx.target_ref)) latest else null 288 else 289 null; 290 291 // reuse an identical revision or record a new one 292 var events: [2]evt.EventWithId = undefined; 293 var event_count: usize = 0; 294 var base_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 295 var head_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 296 var tree_entries: [2]evt.EventTreeEntry = undefined; 297 if (existing_revision) |latest| { 298 if (ctx.posted) ctx.revision_id_maybe.* = latest.id; 299 } else { 300 var base_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &base_oid); 301 defer base_object.deinit(); 302 var source_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &source_oid); 303 defer source_object.deinit(); 304 base_tree_oid = base_object.content.commit.tree; 305 head_tree_oid = source_object.content.commit.tree; 306 307 var revision_id: [evt.event_id_size]u8 = undefined; 308 ctx.io.random(&revision_id); 309 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 310 const revision_event: evt.PatchRev = .{ 311 .base_oid = &base_oid, 312 .source_oid = &source_oid, 313 .target_ref = ctx.target_ref, 314 .message = ctx.title, 315 }; 316 const identity = try std.fmt.allocPrint(ctx.allocator, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 317 defer ctx.allocator.free(identity); 318 const patch_oid = try evt.PatchRev.writeSquashCommit( 319 .xit, 320 repo_opts, 321 state, 322 ctx.io, 323 ctx.allocator, 324 revision_event, 325 &head_tree_oid, 326 identity, 327 identity, 328 ctx.timestamp, 329 ); 330 tree_entries = .{ 331 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 332 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 333 }; 334 events[event_count] = .{ 335 .id = revision_hex, 336 .timestamp = ctx.timestamp, 337 .author = ctx.author, 338 .tree_entries = &tree_entries, 339 .event = .{ .patchrev = revision_event }, 340 }; 341 event_count += 1; 342 ctx.revision_id_maybe.* = revision_id; 343 344 if (!ctx.posted) { 345 var patch = ctx.patch.event; 346 patch.revision = .{ 347 .id = revision_hex, 348 .squash_oid = &patch_oid, 349 .source_oid = &source_oid, 350 .target_ref = ctx.target_ref, 351 }; 352 events[event_count] = .{ 353 .id = ctx.patch_id, 354 .timestamp = ctx.timestamp, 355 .author = ctx.author, 356 .event = .{ .patch = patch }, 357 }; 358 event_count += 1; 359 } 360 } 361 362 // posted patches keep only revisions in the fork 363 if (ctx.posted and !ctx.patch.removed) { 364 events[event_count] = .{ 365 .id = ctx.patch_id, 366 .timestamp = ctx.timestamp, 367 .author = ctx.author, 368 .event = .{ .patch = null }, 369 }; 370 event_count += 1; 371 } 372 373 // commit and index any new events 374 if (event_count > 0) { 375 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 376 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 377 } 378 try xit.undo.writeMessage(repo_opts, state, .push); 379 try ctx.core.chunk_store_file.sync(ctx.io); 380 } 381 }; 382 383 try fork_repo.core.db_file.lock(io, .exclusive); 384 defer fork_repo.core.db_file.unlock(io); 385 386 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 387 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 388 .core = &fork_repo.core, 389 .target_core = &target_repo.core, 390 .io = io, 391 .allocator = allocator, 392 .reader = reader, 393 .writer = writer, 394 .patch_id = id.*, 395 .patch = fork_patch, 396 .posted = posted, 397 .target_oid = target_oid, 398 .target_ref = target_ref, 399 .title = title, 400 .author = author, 401 .timestamp = timestamp, 402 .newest = newest, 403 .revision_id_maybe = &revision_id_maybe, 404 }) catch |err| switch (err) { 405 error.CancelTransaction => {}, 406 else => |other| return other, 407 }; 408 try writer.flush(); 409 } 410 411 // best-effort update the posted patch so it has the new revision 412 { 413 const revision_id = revision_id_maybe orelse return; 414 if (!posted) return; 415 var update_arena = std.heap.ArenaAllocator.init(allocator); 416 defer update_arena.deinit(); 417 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch return; 418 const posted_patch = (evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_update_moment, &update_arena, &patch_id) catch return) orelse return; 419 if (posted_patch.removed) return; 420 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch return; 421 const revision = (evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_update_moment, &update_arena, &revision_id) catch return) orelse return; 422 if (revision.removed) return; 423 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 424 if (posted_patch.event.revision) |current| { 425 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) return; 426 } 427 var patch = posted_patch.event; 428 patch.revision = selected; 429 evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 430 .id = id.*, 431 .timestamp = timestamp, 432 .author = author, 433 .event = .{ .patch = patch }, 434 }}) catch |update_err| { 435 serve_common.logError(io, error_writer, "failed to update posted patch {s}: {s}\n", .{ id, @errorName(update_err) }); 436 }; 437 } 438 } 439 440 // delete first so a failed tombstone can be retried safely 441 pub fn remove( 442 io: std.Io, 443 allocator: std.mem.Allocator, 444 repo_root_path: []const u8, 445 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 446 id: *const [evt.event_id_size * 2]u8, 447 user_id: *const [evt.event_id_size]u8, 448 author: evt.CommitAuthor, 449 ) !void { 450 const fork_id = try evt.parseEventId(id); 451 var arena = std.heap.ArenaAllocator.init(allocator); 452 defer arena.deinit(); 453 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 454 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 455 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 456 457 const path = try forkPath(allocator, repo_root_path, id); 458 defer allocator.free(path); 459 try std.Io.Dir.cwd().deleteTree(io, path); 460 if (!record.removed) try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 461 } 462