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