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