╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
307 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 308 309 // execute a transaction that receives the push and materializes its revision 310 const result = blk: { 311 const DB = rp.Repo(.xit, repo_opts).DB; 312 const State = rp.Repo(.xit, repo_opts).State; 313 const Ctx = struct { 314 core: *rp.Repo(.xit, repo_opts).Core, 315 target_core: *rp.Repo(.xit, repo_opts).Core, 316 io: std.Io, 317 allocator: std.mem.Allocator, 318 reader: *std.Io.Reader, 319 writer: *std.Io.Writer, 320 response: *xit.net_server_receive_pack.Response, 321 patch_id: [evt.event_id_size * 2]u8, 322 patch: evt.Patch.Record, 323 published: bool, 324 target_oid: [hash.hexLen(repo_opts.hash)]u8, 325 title: []const u8, 326 author: evt.CommitAuthor, 327 timestamp: u64, 328 newest: ?evt.PatchRev.WithId, 329 revision_id_maybe: *?[evt.event_id_size]u8, 330 sideband: *progress.Sideband, 331 332 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 333 // receive the branch update 334 var moment = try DB.HashMap(.read_write).init(cursor.*); 335 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 336 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, .{ .allowed_ref = "refs/heads/" ++ fork.ref.name, .deferred_response = ctx.response }, ctx.sideband); 337 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 338 if (ctx.sideband.gone) return error.ClientGone; 339 340 // copy the target history needed to preserve the merge base 341 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 342 var target_repo_moment = try ctx.target_core.latestMoment(); 343 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 344 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 345 defer objects.deinit(); 346 try objects.include(&ctx.target_oid); 347 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 348 349 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 350 const existing_revision = if (ctx.newest) |latest| 351 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 352 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 353 else 354 null; 355 356 // reuse an identical revision or record a new one 357 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 358 defer revision_arena.deinit(); 359 var events: [2]evt.EventWithId = undefined; 360 var event_count: usize = 0; 361 if (existing_revision) |latest| { 362 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 363 } else { 364 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 365 events[event_count] = prepared.event; 366 event_count += 1; 367 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 368 369 if (!ctx.published) { 370 var patch = ctx.patch.event; 371 patch.revision = prepared.revision; 372 events[event_count] = .{ 373 .id = ctx.patch_id, 374 .timestamp = ctx.timestamp, 375 .author = ctx.author, 376 .event = .{ .patch = patch }, 377 }; 378 event_count += 1; 379 } 380 } 381 382 // published patches keep only revisions in the fork 383 if (ctx.published and !ctx.patch.removed) { 384 events[event_count] = .{ 385 .id = ctx.patch_id, 386 .timestamp = ctx.timestamp, 387 .author = ctx.author, 388 .event = .{ .patch = null }, 389 }; 390 event_count += 1; 391 } 392 393 // commit and index any new events 394 if (event_count > 0) { 395 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 396 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 397 } 398 // the patch branch's file index lands with the push 399 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 400 401 // a fork push tracks no ref updates, so it records no ranges 402 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 403 } 404 }; 405 406 try fork_repo.core.db_file.lock(io, .exclusive); 407 defer fork_repo.core.db_file.unlock(io); 408 409 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 410 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 411 .core = &fork_repo.core, 412 .target_core = &target_repo.core, 413 .io = io, 414 .allocator = allocator, 415 .reader = reader, 416 .writer = writer, 417 .response = &response, 418 .patch_id = id.*, 419 .patch = fork_patch, 420 .published = published, 421 .target_oid = target_oid, 422 .title = title, 423 .author = author, 424 .timestamp = timestamp, 425 .newest = newest, 426 .revision_id_maybe = &revision_id_maybe, 427 .sideband = &sideband, 428 }); 429 }; 430 result catch |err| { 431 response.finish(writer, @errorName(err)) catch {}; 432 if (err == error.CancelTransaction) return; 433 return err; 434 }; 435 436 if (!published) return response.finish(writer, null); 437 438 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 439 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 440 441 // best-effort update the published patch so it has the new revision 442 var refreshed = false; 443 update: { 444 const revision_id = revision_id_maybe orelse break :update; 445 var update_arena = std.heap.ArenaAllocator.init(allocator); 446 defer update_arena.deinit(); 447 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 448 const published_patch = (evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_update_moment, &update_arena, &patch_id) catch break :update) orelse break :update; 449 if (published_patch.removed) break :update; 450 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 451 const revision = (evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_update_moment, &update_arena, &revision_id) catch break :update) orelse break :update; 452 if (revision.removed) break :update; 453 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 454 if (published_patch.event.revision) |current| { 455 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 456 } 457 var patch = published_patch.event; 458 patch.revision = selected; 459 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 460 .id = id.*, 461 .timestamp = timestamp, 462 .author = author, 463 .event = .{ .patch = patch }, 464 }}) catch |update_err| { 465 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 466 break :update; 467 }; 468 // consume also refreshed mergeability 469 refreshed = true; 470 } 471 472 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 473 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 474 sideband.run(io, .{ .end = .writing_patch }) catch {}; 475 476 // the client may read either repo as soon as it gets the final response. 477 try response.finish(writer, null); 478 } 479