╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
208 try repo.core.db_file.lock(io, .exclusive); 209 defer repo.core.db_file.unlock(io); 210 211 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 212 break :blk history.appendContext( 213 .{ .slot = try history.getSlot(-1) }, 214 Ctx{ 215 .updates = &updates, 216 .core = &repo.core, 217 .io = io, 218 .allocator = allocator, 219 .reader = reader, 220 .writer = writer, 221 .options = options, 222 .author = author, 223 .response = &response, 224 .server = server, 225 .error_writer = error_writer, 226 .sideband = &sideband, 227 .moment_index = &moment_index, 228 }, 229 ); 230 }; 231 result catch |err| switch (err) { 232 // a refused push has already written the report saying why 233 error.CancelTransaction => return response.finish(writer, null), 234 error.ClearUndoHistory => { 235 clearUndoHistory(repo_opts, io, allocator, repo, &sideband) catch |clear_err| { 236 response.finish(writer, @errorName(clear_err)) catch {}; 237 return clear_err; 238 }; 239 return response.finish(writer, null); 240 }, 241 else => { 242 response.finish(writer, @errorName(err)) catch {}; 243 return err; 244 }, 245 }; 246 if (moment_index) |index| notify.deliverTransaction(io, allocator, server, repo_opts, repo, index, pusher_id, &sideband); 247 evt.bumpRepoActivityAt(io, allocator, server, repo.core.work_path); 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 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 356 var target_repo_moment = try ctx.target_core.latestMoment(); 357 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 358 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 359 defer objects.deinit(); 360 try objects.include(&ctx.target_oid); 361 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 362 363 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 364 const existing_revision = if (ctx.newest) |latest| 365 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 366 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 367 else 368 null; 369 370 // reuse an identical revision or record a new one 371 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 372 defer revision_arena.deinit(); 373 var events: [2]evt.EventWithId = undefined; 374 var event_count: usize = 0; 375 if (existing_revision) |latest| { 376 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 377 } else { 378 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 379 events[event_count] = prepared.event; 380 event_count += 1; 381 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 382 383 if (!ctx.published) { 384 var patch = ctx.patch.event; 385 patch.revision = prepared.revision; 386 events[event_count] = .{ 387 .id = ctx.patch_id, 388 .timestamp = ctx.timestamp, 389 .author = ctx.author, 390 .event = .{ .patch = patch }, 391 }; 392 event_count += 1; 393 } 394 } 395 396 // published patches keep only revisions in the fork 397 if (ctx.published and !ctx.patch.removed) { 398 events[event_count] = .{ 399 .id = ctx.patch_id, 400 .timestamp = ctx.timestamp, 401 .author = ctx.author, 402 .event = .{ .patch = null }, 403 }; 404 event_count += 1; 405 } 406 407 // commit and index any new events 408 if (event_count > 0) { 409 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 410 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 411 } 412 // the patch branch's file index lands with the push 413 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 414 415 // a fork push tracks no ref updates, so it records no ranges 416 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 417 // the copied target history and the index count against the limit too 418 try ctx.sideband.checkSize(ctx.io); 419 } 420 }; 421 422 try fork_repo.core.db_file.lock(io, .exclusive); 423 defer fork_repo.core.db_file.unlock(io); 424 425 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 426 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 427 .core = &fork_repo.core, 428 .target_core = &target_repo.core, 429 .io = io, 430 .allocator = allocator, 431 .reader = reader, 432 .writer = writer, 433 .response = &response, 434 .patch_id = id.*, 435 .patch = fork_patch, 436 .published = published, 437 .target_oid = target_oid, 438 .title = title, 439 .author = author, 440 .timestamp = timestamp, 441 .newest = newest, 442 .revision_id_maybe = &revision_id_maybe, 443 .sideband = &sideband, 444 }); 445 }; 446 result catch |err| switch (err) { 447 // a refused push has already written the report saying why 448 error.CancelTransaction => return response.finish(writer, null), 449 else => { 450 response.finish(writer, @errorName(err)) catch {}; 451 return err; 452 }, 453 }; 454 455 if (!published) return response.finish(writer, null); 456 457 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 458 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 459 460 // best-effort update the published patch so it has the new revision 461 var refreshed = false; 462 update: { 463 const revision_id = revision_id_maybe orelse break :update; 464 var update_arena = std.heap.ArenaAllocator.init(allocator); 465 defer update_arena.deinit(); 466 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 467 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; 468 if (published_patch.removed) break :update; 469 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 470 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; 471 if (revision.removed) break :update; 472 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 473 if (published_patch.event.revision) |current| { 474 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 475 } 476 var patch = published_patch.event; 477 patch.revision = selected; 478 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 479 .id = id.*, 480 .timestamp = timestamp, 481 .author = author, 482 .event = .{ .patch = patch }, 483 }}) catch |update_err| { 484 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 485 break :update; 486 }; 487 // consume also refreshed mergeability 488 refreshed = true; 489 } 490 491 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 492 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 493 sideband.run(io, .{ .end = .writing_patch }) catch {}; 494 495 // the client may read either repo as soon as it gets the final response. 496 try response.finish(writer, null); 497 } 498