╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
265 const active_patch = target_patch orelse fork_patch; 266 const title = active_patch.event.title; 267 268 // resolve the target and the newest fork revision 269 const target_branch = active_patch.event.target_branch; 270 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 271 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 272 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 273 var response = xit.net_server_receive_pack.Response.init(allocator); 274 defer response.deinit(); 275 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 276 277 // execute a transaction that receives the push and materializes its revision 278 const result = blk: { 279 const DB = rp.Repo(.xit, repo_opts).DB; 280 const State = rp.Repo(.xit, repo_opts).State; 281 const Ctx = struct { 282 core: *rp.Repo(.xit, repo_opts).Core, 283 target_core: *rp.Repo(.xit, repo_opts).Core, 284 io: std.Io, 285 allocator: std.mem.Allocator, 286 reader: *std.Io.Reader, 287 writer: *std.Io.Writer, 288 response: *xit.net_server_receive_pack.Response, 289 patch_id: [evt.event_id_size * 2]u8, 290 patch: evt.Patch.Record, 291 published: bool, 292 target_oid: [hash.hexLen(repo_opts.hash)]u8, 293 title: []const u8, 294 author: evt.CommitAuthor, 295 timestamp: u64, 296 newest: ?evt.PatchRev.WithId, 297 revision_id_maybe: *?[evt.event_id_size]u8, 298 sideband: *progress.Sideband, 299 300 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 301 // receive the branch update 302 var moment = try DB.HashMap(.read_write).init(cursor.*); 303 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 304 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); 305 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 306 if (ctx.sideband.gone) return error.ClientGone; 307 308 // copy the target history needed to preserve the merge base 309 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 310 var target_repo_moment = try ctx.target_core.latestMoment(); 311 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 312 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 313 defer objects.deinit(); 314 try objects.include(&ctx.target_oid); 315 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 316 317 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 318 const existing_revision = if (ctx.newest) |latest| 319 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 320 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 321 else 322 null; 323 324 // reuse an identical revision or record a new one 325 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 326 defer revision_arena.deinit(); 327 var events: [2]evt.EventWithId = undefined; 328 var event_count: usize = 0; 329 if (existing_revision) |latest| { 330 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 331 } else { 332 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 333 events[event_count] = prepared.event; 334 event_count += 1; 335 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 336 337 if (!ctx.published) { 338 var patch = ctx.patch.event; 339 patch.revision = prepared.revision; 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 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 365 } 366 // the patch branch's file index lands with the push 367 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 368 369 // a fork push tracks no ref updates, so it records no ranges 370 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 371 } 372 }; 373 374 try fork_repo.core.db_file.lock(io, .exclusive); 375 defer fork_repo.core.db_file.unlock(io); 376 377 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 378 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 379 .core = &fork_repo.core, 380 .target_core = &target_repo.core, 381 .io = io, 382 .allocator = allocator, 383 .reader = reader, 384 .writer = writer, 385 .response = &response, 386 .patch_id = id.*, 387 .patch = fork_patch, 388 .published = published, 389 .target_oid = target_oid, 390 .title = title, 391 .author = author, 392 .timestamp = timestamp, 393 .newest = newest, 394 .revision_id_maybe = &revision_id_maybe, 395 .sideband = &sideband, 396 }); 397 }; 398 result catch |err| { 399 response.finish(writer, @errorName(err)) catch {}; 400 if (err == error.CancelTransaction) return; 401 return err; 402 }; 403 404 if (!published) return response.finish(writer, null); 405 406 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 407 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 408 409 // best-effort update the published patch so it has the new revision 410 var refreshed = false; 411 update: { 412 const revision_id = revision_id_maybe orelse break :update; 413 var update_arena = std.heap.ArenaAllocator.init(allocator); 414 defer update_arena.deinit(); 415 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 416 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; 417 if (published_patch.removed) break :update; 418 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 419 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; 420 if (revision.removed) break :update; 421 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 422 if (published_patch.event.revision) |current| { 423 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 424 } 425 var patch = published_patch.event; 426 patch.revision = selected; 427 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 428 .id = id.*, 429 .timestamp = timestamp, 430 .author = author, 431 .event = .{ .patch = patch }, 432 }}) catch |update_err| { 433 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 434 break :update; 435 }; 436 // consume also refreshed mergeability 437 refreshed = true; 438 } 439 440 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 441 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 442 sideband.run(io, .{ .end = .writing_patch }) catch {}; 443 444 // the client may read either repo as soon as it gets the final response. 445 try response.finish(writer, null); 446 } 447