╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
288 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 289 // receive the branch update 290 var moment = try DB.HashMap(.read_write).init(cursor.*); 291 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 292 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.progress); 293 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 294 if (ctx.progress.gone) return error.ClientGone; 295 296 // copy the target history needed to preserve the merge base 297 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 298 var target_repo_moment = try ctx.target_core.latestMoment(); 299 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 300 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 301 defer objects.deinit(); 302 try objects.include(&ctx.target_oid); 303 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 304 305 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 306 const existing_revision = if (ctx.newest) |latest| 307 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 308 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 309 else 310 null; 311 312 // reuse an identical revision or record a new one 313 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 314 defer revision_arena.deinit(); 315 var events: [2]evt.EventWithId = undefined; 316 var event_count: usize = 0; 317 if (existing_revision) |latest| { 318 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 319 } else { 320 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 321 events[event_count] = prepared.event; 322 event_count += 1; 323 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 324 325 if (!ctx.published) { 326 var patch = ctx.patch.event; 327 patch.revision = prepared.revision; 328 events[event_count] = .{ 329 .id = ctx.patch_id, 330 .timestamp = ctx.timestamp, 331 .author = ctx.author, 332 .event = .{ .patch = patch }, 333 }; 334 event_count += 1; 335 } 336 } 337 338 // published patches keep only revisions in the fork 339 if (ctx.published and !ctx.patch.removed) { 340 events[event_count] = .{ 341 .id = ctx.patch_id, 342 .timestamp = ctx.timestamp, 343 .author = ctx.author, 344 .event = .{ .patch = null }, 345 }; 346 event_count += 1; 347 } 348 349 // commit and index any new events 350 if (event_count > 0) { 351 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 352 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 353 } 354 // a fork push tracks no ref updates, so it records no ranges 355 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 356 } 357 }; 358 359 try fork_repo.core.db_file.lock(io, .exclusive); 360 defer fork_repo.core.db_file.unlock(io); 361 362 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 363 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 364 .core = &fork_repo.core, 365 .target_core = &target_repo.core, 366 .io = io, 367 .allocator = allocator, 368 .reader = reader, 369 .writer = writer, 370 .response = &response, 371 .patch_id = id.*, 372 .patch = fork_patch, 373 .published = published, 374 .target_oid = target_oid, 375 .title = title, 376 .author = author, 377 .timestamp = timestamp, 378 .newest = newest, 379 .revision_id_maybe = &revision_id_maybe, 380 .progress = &progress, 381 }); 382 }; 383 result catch |err| { 384 response.finish(writer, @errorName(err)) catch {}; 385 if (err == error.CancelTransaction) return; 386 return err; 387 }; 388 389 find.refresh(repo_opts, io, allocator, fork_repo) catch |err| { 390 serve_common.logError(io, error_writer, "failed to refresh file index: {s}\n", .{@errorName(err)}); 391 }; 392 if (!published) return response.finish(writer, null); 393 394 progress.run(io, .{ .text = "Updating patches" }) catch {}; 395 progress.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 396 397 // best-effort update the published patch so it has the new revision 398 var refreshed = false; 399 update: { 400 const revision_id = revision_id_maybe orelse break :update; 401 var update_arena = std.heap.ArenaAllocator.init(allocator); 402 defer update_arena.deinit(); 403 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 404 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; 405 if (published_patch.removed) break :update; 406 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 407 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; 408 if (revision.removed) break :update; 409 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 410 if (published_patch.event.revision) |current| { 411 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 412 } 413 var patch = published_patch.event; 414 patch.revision = selected; 415 evt.consume(.server, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 416 .id = id.*, 417 .timestamp = timestamp, 418 .author = author, 419 .event = .{ .patch = patch }, 420 }}) catch |update_err| { 421 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 422 break :update; 423 }; 424 // consume also refreshed mergeability 425 refreshed = true; 426 } 427 428 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, patch_id); 429 progress.run(io, .{ .complete_one = .writing_patch }) catch {}; 430 progress.run(io, .{ .end = .writing_patch }) catch {}; 431 432 // the client may read either repo as soon as it gets the final response. 433 try response.finish(writer, null); 434 } 435