╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
139 // the moment index the pushed events were consumed into 140 var moment_index: ?u64 = null; 141 const Ctx = struct { 142 updates: *xit.net_server_receive_pack.AppliedRefUpdates, 143 core: *rp.Repo(.xit, repo_opts).Core, 144 io: std.Io, 145 allocator: std.mem.Allocator, 146 reader: *std.Io.Reader, 147 writer: *std.Io.Writer, 148 options: xit.net_server_receive_pack.Options, 149 author: evt.CommitAuthor, 150 response: *xit.net_server_receive_pack.Response, 151 server: evt.Host.Server, 152 error_writer: *std.Io.Writer, 153 sideband: *progress.Sideband, 154 moment_index: *?u64, 155 156 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 157 var moment = try DB.HashMap(.read_write).init(cursor.*); 158 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 159 160 var receive_options = ctx.options; 161 receive_options.applied_ref_updates = ctx.updates; 162 receive_options.deferred_response = ctx.response; 163 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, receive_options, ctx.sideband); 164 // receive-pack refuses the clear ref alongside any other update. 165 // clearing writes nothing here, and runs once this transaction 166 // lets go of the lock. 167 const applied = ctx.updates.items.items; 168 if (applied.len == 1 and std.mem.eql(u8, applied[0].ref_name, clear_undo_ref)) return error.ClearUndoHistory; 169 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 170 if (ctx.sideband.gone) return error.ClientGone; 171 172 // a repo without an events branch has no events to consume. a 173 // no-op consume must not cancel here, since the push shares the 174 // transaction and must commit regardless. 175 if (null != try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = evt.events_ref })) { 176 ctx.moment_index.* = evt.consumeInTransaction(.repo, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref) catch |err| switch (err) { 177 error.CancelTransaction => null, 178 else => |e| return e, 179 }; 180 try pch.detectMerged(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.server.users_dir, ctx.updates.items.items, ctx.error_writer); 181 182 // the revisions the pushed branches cause belong to the push 183 _ = try pch.refreshBranchesInTransaction(.{ .server = ctx.server }, repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates.items.items, null, ctx.sideband); 184 } 185 186 if (ctx.sideband.gone) return error.ClientGone; 187 188 // the indexes the pushed refs invalidate belong to the push too 189 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 190 var moves: std.ArrayList(srch_cmmt.Move) = .empty; 191 defer moves.deinit(ctx.allocator); 192 for (ctx.updates.items.items) |update| try moves.append(ctx.allocator, .{ .old_oid = update.old_oid, .new_oid = update.new_oid }); 193 _ = try srch_cmmt.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, moves.items, ctx.sideband); 194 195 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, ctx.updates.items.items); 196 } 197 }; 198 199 const result = blk: { 200 try repo.core.db_file.lock(io, .exclusive); 201 defer repo.core.db_file.unlock(io); 202 203 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 204 break :blk history.appendContext( 205 .{ .slot = try history.getSlot(-1) }, 206 Ctx{ 207 .updates = &updates, 208 .core = &repo.core, 209 .io = io, 210 .allocator = allocator, 211 .reader = reader, 212 .writer = writer, 213 .options = options, 214 .author = author, 215 .response = &response, 216 .server = server, 217 .error_writer = error_writer, 218 .sideband = &sideband, 219 .moment_index = &moment_index, 220 }, 221 ); 222 }; 223 result catch |err| switch (err) { 224 // a refused push has already written the report saying why 225 error.CancelTransaction => return response.finish(writer, null), 226 error.ClearUndoHistory => { 227 clearUndoHistory(repo_opts, io, allocator, repo, &sideband) catch |clear_err| { 228 response.finish(writer, @errorName(clear_err)) catch {}; 229 return clear_err; 230 }; 231 return response.finish(writer, null); 232 }, 233 else => { 234 response.finish(writer, @errorName(err)) catch {}; 235 return err; 236 }, 237 }; 238 if (moment_index) |index| notify.deliverTransaction(io, allocator, server, repo_opts, repo, index, pusher_id, &sideband); 239 evt.bumpRepoActivityAt(io, allocator, server, repo.core.work_path); 240 241 pch.refreshBranches(.{ .server = server }, .xit, repo_opts, io, allocator, repo, updates.items.items, &sideband) catch |err| { 242 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 243 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, server.users_dir, null, &sideband); 244 }; 245 try response.finish(writer, null); 246 } 247 248 // discard every earlier transaction by compacting the database. the patch 249 // revisions events point at are unreachable from the refs, so they are named 250 // as extra roots to survive the collection. 251 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 { 252 // a repo that has consumed no events has no revisions to protect 253 const roots = if (evt.currentMoment(repo_opts, repo)) |moment| 254 try evt.PatchRev.gcRoots(rp.Repo(.xit, repo_opts).DB, repo_opts.hash, allocator, moment) 255 else |_| 256 &.{}; 257 defer allocator.free(roots); 258 _ = try repo.garbageCollect(io, allocator, .{ .extra_roots = roots }, sideband); 259 } 260 261 pub fn receiveFork( 262 comptime repo_opts: rp.RepoOpts(.xit), 263 io: std.Io, 264 allocator: std.mem.Allocator, 265 fork_repo: *rp.Repo(.xit, repo_opts), 266 target_repo: *rp.Repo(.xit, repo_opts), 267 server: evt.Host.Server, 268 id: *const [evt.event_id_size * 2]u8, 269 author: evt.CommitAuthor, 270 timestamp: u64, 271 reader: *std.Io.Reader, 272 writer: *std.Io.Writer, 273 error_writer: *std.Io.Writer, 274 sess: ?*ssh.SessionCtx, 275 ) !void { 276 const patch_id = try evt.parseEventId(id); 277 var arena = std.heap.ArenaAllocator.init(allocator); 278 defer arena.deinit(); 279 280 // load the fork and target patch state 281 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 282 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 283 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 284 error.NotFound => null, 285 else => |other| return other, 286 }; 287 const target_patch = if (target_moment) |moment| 288 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 289 else 290 null; 291 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 292 if (target_patch) |patch| { 293 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 294 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 295 } 296 const published = target_patch != null; 297 const active_patch = target_patch orelse fork_patch; 298 const title = active_patch.event.title; 299 300 // resolve the target and the newest fork revision 301 const target_branch = active_patch.event.target_branch; 302 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 303 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 304 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 305 var response = xit.net_server_receive_pack.Response.init(allocator); 306 defer response.deinit(); 307 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 308 309 // execute a transaction that receives the push and materializes its revision 310 const result = blk: { 311 const DB = rp.Repo(.xit, repo_opts).DB; 312 const State = rp.Repo(.xit, repo_opts).State; 313 const Ctx = struct { 314 core: *rp.Repo(.xit, repo_opts).Core, 315 target_core: *rp.Repo(.xit, repo_opts).Core, 316 io: std.Io, 317 allocator: std.mem.Allocator, 318 reader: *std.Io.Reader, 319 writer: *std.Io.Writer, 320 response: *xit.net_server_receive_pack.Response, 321 patch_id: [evt.event_id_size * 2]u8, 322 patch: evt.Patch.Record, 323 published: bool, 324 target_oid: [hash.hexLen(repo_opts.hash)]u8, 325 title: []const u8, 326 author: evt.CommitAuthor, 327 timestamp: u64, 328 newest: ?evt.PatchRev.WithId, 329 revision_id_maybe: *?[evt.event_id_size]u8, 330 sideband: *progress.Sideband, 331 332 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 333 // receive the branch update 334 var moment = try DB.HashMap(.read_write).init(cursor.*); 335 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 336 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); 337 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 338 if (ctx.sideband.gone) return error.ClientGone; 339 340 // copy the target history needed to preserve the merge base 341 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 342 var target_repo_moment = try ctx.target_core.latestMoment(); 343 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 344 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 345 defer objects.deinit(); 346 try objects.include(&ctx.target_oid); 347 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 348 349 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 350 const existing_revision = if (ctx.newest) |latest| 351 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 352 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 353 else 354 null; 355 356 // reuse an identical revision or record a new one 357 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 358 defer revision_arena.deinit(); 359 var events: [2]evt.EventWithId = undefined; 360 var event_count: usize = 0; 361 if (existing_revision) |latest| { 362 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 363 } else { 364 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 365 events[event_count] = prepared.event; 366 event_count += 1; 367 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 368 369 if (!ctx.published) { 370 var patch = ctx.patch.event; 371 patch.revision = prepared.revision; 372 events[event_count] = .{ 373 .id = ctx.patch_id, 374 .timestamp = ctx.timestamp, 375 .author = ctx.author, 376 .event = .{ .patch = patch }, 377 }; 378 event_count += 1; 379 } 380 } 381 382 // published patches keep only revisions in the fork 383 if (ctx.published and !ctx.patch.removed) { 384 events[event_count] = .{ 385 .id = ctx.patch_id, 386 .timestamp = ctx.timestamp, 387 .author = ctx.author, 388 .event = .{ .patch = null }, 389 }; 390 event_count += 1; 391 } 392 393 // commit and index any new events 394 if (event_count > 0) { 395 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 396 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 397 } 398 // the patch branch's file index lands with the push 399 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 400 401 // a fork push tracks no ref updates, so it records no ranges 402 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 403 } 404 }; 405 406 try fork_repo.core.db_file.lock(io, .exclusive); 407 defer fork_repo.core.db_file.unlock(io); 408 409 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 410 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 411 .core = &fork_repo.core, 412 .target_core = &target_repo.core, 413 .io = io, 414 .allocator = allocator, 415 .reader = reader, 416 .writer = writer, 417 .response = &response, 418 .patch_id = id.*, 419 .patch = fork_patch, 420 .published = published, 421 .target_oid = target_oid, 422 .title = title, 423 .author = author, 424 .timestamp = timestamp, 425 .newest = newest, 426 .revision_id_maybe = &revision_id_maybe, 427 .sideband = &sideband, 428 }); 429 }; 430 result catch |err| { 431 response.finish(writer, @errorName(err)) catch {}; 432 if (err == error.CancelTransaction) return; 433 return err; 434 }; 435 436 if (!published) return response.finish(writer, null); 437 438 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 439 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 440 441 // best-effort update the published patch so it has the new revision 442 var refreshed = false; 443 update: { 444 const revision_id = revision_id_maybe orelse break :update; 445 var update_arena = std.heap.ArenaAllocator.init(allocator); 446 defer update_arena.deinit(); 447 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 448 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; 449 if (published_patch.removed) break :update; 450 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 451 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; 452 if (revision.removed) break :update; 453 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 454 if (published_patch.event.revision) |current| { 455 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 456 } 457 var patch = published_patch.event; 458 patch.revision = selected; 459 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 460 .id = id.*, 461 .timestamp = timestamp, 462 .author = author, 463 .event = .{ .patch = patch }, 464 }}) catch |update_err| { 465 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 466 break :update; 467 }; 468 // consume also refreshed mergeability 469 refreshed = true; 470 } 471 472 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 473 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 474 sideband.run(io, .{ .end = .writing_patch }) catch {}; 475 476 // the client may read either repo as soon as it gets the final response. 477 try response.finish(writer, null); 478 } 479