╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
248 249 pch.refreshBranches(.{ .server = server }, .xit, repo_opts, io, allocator, repo, updates.items.items, &sideband) catch |err| { 250 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 251 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, server.users_dir, null, &sideband); 252 }; 253 try response.finish(writer, null); 254 } 255 256 // discard every earlier transaction by compacting the database. the patch 257 // revisions events point at are unreachable from the refs, so they are named 258 // as extra roots to survive the collection. 259 fn clearUndoHistory(comptime repo_opts: rp.RepoOpts(.xit), io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, repo_opts), sideband: *progress.Sideband) !void { 260 // a repo that has consumed no events has no revisions to protect 261 const roots = if (evt.currentMoment(repo_opts, repo)) |moment| 262 try evt.PatchRev.gcRoots(rp.Repo(.xit, repo_opts).DB, repo_opts.hash, allocator, moment) 263 else |_| 264 &.{}; 265 defer allocator.free(roots); 266 _ = try repo.garbageCollect(io, allocator, .{ .extra_roots = roots }, sideband); 267 } 268 269 pub fn receiveFork( 270 comptime repo_opts: rp.RepoOpts(.xit), 271 io: std.Io, 272 allocator: std.mem.Allocator, 273 fork_repo: *rp.Repo(.xit, repo_opts), 274 target_repo: *rp.Repo(.xit, repo_opts), 275 server: evt.Host.Server, 276 id: *const [evt.event_id_size * 2]u8, 277 author: evt.CommitAuthor, 278 timestamp: u64, 279 reader: *std.Io.Reader, 280 writer: *std.Io.Writer, 281 max_repo_size: ?u64, 282 error_writer: *std.Io.Writer, 283 sess: ?*ssh.SessionCtx, 284 ) !void { 285 const patch_id = try evt.parseEventId(id); 286 var arena = std.heap.ArenaAllocator.init(allocator); 287 defer arena.deinit(); 288 289 // load the fork and target patch state 290 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 291 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 292 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 293 error.NotFound => null, 294 else => |other| return other, 295 }; 296 const target_patch = if (target_moment) |moment| 297 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 298 else 299 null; 300 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 301 if (target_patch) |patch| { 302 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 303 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 304 } 305 const published = target_patch != null; 306 const active_patch = target_patch orelse fork_patch; 307 const title = active_patch.event.title; 308 309 // resolve the target and the newest fork revision 310 const target_branch = active_patch.event.target_branch; 311 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 312 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 313 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 314 var response = xit.net_server_receive_pack.Response.init(allocator); 315 defer response.deinit(); 316 var sideband = progress.Sideband{ 317 .response = &response, 318 .writer = writer, 319 .sess = sess, 320 .size_limit = if (max_repo_size) |max| .{ .file = fork_repo.core.db_file, .max = max } else null, 321 }; 322 323 // execute a transaction that receives the push and materializes its revision 324 const result = blk: { 325 const DB = rp.Repo(.xit, repo_opts).DB; 326 const State = rp.Repo(.xit, repo_opts).State; 327 const Ctx = struct { 328 core: *rp.Repo(.xit, repo_opts).Core, 329 target_core: *rp.Repo(.xit, repo_opts).Core, 330 io: std.Io, 331 allocator: std.mem.Allocator, 332 reader: *std.Io.Reader, 333 writer: *std.Io.Writer, 334 response: *xit.net_server_receive_pack.Response, 335 patch_id: [evt.event_id_size * 2]u8, 336 patch: evt.Patch.Record, 337 published: bool, 338 target_oid: [hash.hexLen(repo_opts.hash)]u8, 339 title: []const u8, 340 author: evt.CommitAuthor, 341 timestamp: u64, 342 newest: ?evt.PatchRev.WithId, 343 revision_id_maybe: *?[evt.event_id_size]u8, 344 sideband: *progress.Sideband, 345 346 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 347 // receive the branch update 348 var moment = try DB.HashMap(.read_write).init(cursor.*); 349 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 350 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); 351 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 352 if (ctx.sideband.gone) return error.ClientGone; 353 354 // copy the target history needed to preserve the merge base, 355 // skipping what the fork already holds 356 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 357 var target_repo_moment = try ctx.target_core.latestMoment(); 358 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 359 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 360 defer objects.deinit(); 361 try pch.includeMissing(repo_opts, &objects, state.readOnly(), target_state, ctx.io, ctx.allocator, &ctx.target_oid); 362 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 363 364 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 365 const existing_revision = if (ctx.newest) |latest| 366 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 367 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 368 else 369 null; 370 371 // reuse an identical revision or record a new one 372 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 373 defer revision_arena.deinit(); 374 var events: [2]evt.EventWithId = undefined; 375 var event_count: usize = 0; 376 if (existing_revision) |latest| { 377 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 378 } else { 379 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 380 events[event_count] = prepared.event; 381 event_count += 1; 382 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 383 384 if (!ctx.published) { 385 var patch = ctx.patch.event; 386 patch.revision = prepared.revision; 387 events[event_count] = .{ 388 .id = ctx.patch_id, 389 .timestamp = ctx.timestamp, 390 .author = ctx.author, 391 .event = .{ .patch = patch }, 392 }; 393 event_count += 1; 394 } 395 } 396 397 // published patches keep only revisions in the fork 398 if (ctx.published and !ctx.patch.removed) { 399 events[event_count] = .{ 400 .id = ctx.patch_id, 401 .timestamp = ctx.timestamp, 402 .author = ctx.author, 403 .event = .{ .patch = null }, 404 }; 405 event_count += 1; 406 } 407 408 // commit and index any new events 409 if (event_count > 0) { 410 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 411 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 412 } 413 // the patch branch's file index lands with the push 414 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 415 416 // a fork push tracks no ref updates, so it records no ranges 417 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 418 // the copied target history and the index count against the limit too 419 try ctx.sideband.checkSize(ctx.io); 420 } 421 }; 422 423 try fork_repo.core.db_file.lock(io, .exclusive); 424 defer fork_repo.core.db_file.unlock(io); 425 426 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 427 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 428 .core = &fork_repo.core, 429 .target_core = &target_repo.core, 430 .io = io, 431 .allocator = allocator, 432 .reader = reader, 433 .writer = writer, 434 .response = &response, 435 .patch_id = id.*, 436 .patch = fork_patch, 437 .published = published, 438 .target_oid = target_oid, 439 .title = title, 440 .author = author, 441 .timestamp = timestamp, 442 .newest = newest, 443 .revision_id_maybe = &revision_id_maybe, 444 .sideband = &sideband, 445 }); 446 }; 447 result catch |err| switch (err) { 448 // a refused push has already written the report saying why 449 error.CancelTransaction => return response.finish(writer, null), 450 else => { 451 response.finish(writer, @errorName(err)) catch {}; 452 return err; 453 }, 454 }; 455 456 if (!published) return response.finish(writer, null); 457 458 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 459 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 460 461 // best-effort update the published patch so it has the new revision 462 var refreshed = false; 463 update: { 464 const revision_id = revision_id_maybe orelse break :update; 465 var update_arena = std.heap.ArenaAllocator.init(allocator); 466 defer update_arena.deinit(); 467 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 468 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; 469 if (published_patch.removed) break :update; 470 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 471 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; 472 if (revision.removed) break :update; 473 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 474 if (published_patch.event.revision) |current| { 475 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 476 } 477 var patch = published_patch.event; 478 patch.revision = selected; 479 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 480 .id = id.*, 481 .timestamp = timestamp, 482 .author = author, 483 .event = .{ .patch = patch }, 484 }}) catch |update_err| { 485 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 486 break :update; 487 }; 488 // consume also refreshed mergeability 489 refreshed = true; 490 } 491 492 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 493 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 494 sideband.run(io, .{ .end = .writing_patch }) catch {}; 495 496 // the client may read either repo as soon as it gets the final response. 497 try response.finish(writer, null); 498 } 499