╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
142 writer: *std.Io.Writer, 143 options: xit.net_server_receive_pack.Options, 144 author: ?evt.CommitAuthor, 145 response: *xit.net_server_receive_pack.Response, 146 repo_root_path: []const u8, 147 error_writer: *std.Io.Writer, 148 sideband: *progress.Sideband, 149 150 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 151 var moment = try DB.HashMap(.read_write).init(cursor.*); 152 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 153 154 var receive_options = ctx.options; 155 receive_options.applied_ref_updates = ctx.updates; 156 receive_options.deferred_response = ctx.response; 157 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, receive_options, ctx.sideband); 158 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 159 if (ctx.sideband.gone) return error.ClientGone; 160 161 // a repo without an events branch has no events to consume. a 162 // no-op consume must not cancel here, since the push shares the 163 // transaction and must commit regardless. 164 if (null != try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = evt.events_ref })) { 165 _ = try evt.consumeInTransaction(.repo, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 166 try pch.detectMerged(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.repo_root_path, ctx.updates.items.items, ctx.error_writer); 167 168 // the revisions the pushed branches cause belong to the push 169 _ = try pch.refreshBranchesInTransaction(.server, repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates.items.items, null, ctx.sideband); 170 } 171 172 if (ctx.sideband.gone) return error.ClientGone; 173 174 // the indexes the pushed refs invalidate belong to the push too 175 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 176 var moves: std.ArrayList(srch_cmmt.Move) = .empty; 177 defer moves.deinit(ctx.allocator); 178 for (ctx.updates.items.items) |update| try moves.append(ctx.allocator, .{ .old_oid = update.old_oid, .new_oid = update.new_oid }); 179 _ = try srch_cmmt.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, moves.items, ctx.sideband); 180 181 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, ctx.updates.items.items); 182 } 183 }; 184 185 const result = blk: { 186 try repo.core.db_file.lock(io, .exclusive); 187 defer repo.core.db_file.unlock(io); 188 189 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 190 break :blk history.appendContext( 191 .{ .slot = try history.getSlot(-1) }, 192 Ctx{ 193 .updates = &updates, 194 .core = &repo.core, 195 .io = io, 196 .allocator = allocator, 197 .reader = reader, 198 .writer = writer, 199 .options = options, 200 .author = author, 201 .response = &response, 202 .repo_root_path = repo_root_path, 203 .error_writer = error_writer, 204 .sideband = &sideband, 205 }, 206 ); 207 }; 208 result catch |err| { 209 response.finish(writer, @errorName(err)) catch {}; 210 if (err == error.CancelTransaction) return; 211 return err; 212 }; 213 214 pch.refreshBranches(.server, .xit, repo_opts, io, allocator, repo, updates.items.items, &sideband) catch |err| { 215 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 216 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, null, &sideband); 217 }; 218 try response.finish(writer, null); 219 } 220 221 pub fn receiveFork( 222 comptime repo_opts: rp.RepoOpts(.xit), 223 io: std.Io, 224 allocator: std.mem.Allocator, 225 fork_repo: *rp.Repo(.xit, repo_opts), 226 target_repo: *rp.Repo(.xit, repo_opts), 227 id: *const [evt.event_id_size * 2]u8, 228 author: evt.CommitAuthor, 229 timestamp: u64, 230 reader: *std.Io.Reader, 231 writer: *std.Io.Writer, 232 error_writer: *std.Io.Writer, 233 sess: ?*ssh.SessionCtx, 234 ) !void { 235 const patch_id = try evt.parseEventId(id); 236 var arena = std.heap.ArenaAllocator.init(allocator); 237 defer arena.deinit(); 238 239 // load the fork and target patch state 240 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 241 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 242 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 243 error.NotFound => null, 244 else => |other| return other, 245 }; 246 const target_patch = if (target_moment) |moment| 247 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 248 else 249 null; 250 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 251 if (target_patch) |patch| { 252 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 253 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 254 } 255 const published = target_patch != null; 256 const active_patch = target_patch orelse fork_patch; 257 const title = active_patch.event.title; 258 259 // resolve the target and the newest fork revision 260 const target_branch = active_patch.event.target_branch; 261 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 262 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 263 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 264 var response = xit.net_server_receive_pack.Response.init(allocator); 265 defer response.deinit(); 266 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 267 268 // execute a transaction that receives the push and materializes its revision 269 const result = blk: { 270 const DB = rp.Repo(.xit, repo_opts).DB; 271 const State = rp.Repo(.xit, repo_opts).State; 272 const Ctx = struct { 273 core: *rp.Repo(.xit, repo_opts).Core, 274 target_core: *rp.Repo(.xit, repo_opts).Core, 275 io: std.Io, 276 allocator: std.mem.Allocator, 277 reader: *std.Io.Reader, 278 writer: *std.Io.Writer, 279 response: *xit.net_server_receive_pack.Response, 280 patch_id: [evt.event_id_size * 2]u8, 281 patch: evt.Patch.Record, 282 published: bool, 283 target_oid: [hash.hexLen(repo_opts.hash)]u8, 284 title: []const u8, 285 author: evt.CommitAuthor, 286 timestamp: u64, 287 newest: ?evt.PatchRev.WithId, 288 revision_id_maybe: *?[evt.event_id_size]u8, 289 sideband: *progress.Sideband, 290 291 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 292 // receive the branch update 293 var moment = try DB.HashMap(.read_write).init(cursor.*); 294 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 295 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); 296 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 297 if (ctx.sideband.gone) return error.ClientGone; 298 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