╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
89 90 // the array is stored by value, so the one the payload holds still counts 91 // the entry that was popped until it is replaced here 92 try payload.put(aa, "refs", .{ .array = refs }); 93 94 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action, .payload = payload } }); 95 } 96 97 // the oid a ref update sends when it has no side: all zeros 98 fn zeroOid(oid: []const u8) bool { 99 for (oid) |char| if (char != '0') return false; 100 return true; 101 } 102 103 // how long the record's payload would be, to keep it within the buffer 104 fn payloadLen(payload: std.json.ObjectMap) !u64 { 105 var discarding = std.Io.Writer.Discarding.init(&.{}); 106 try std.json.Stringify.value(std.json.Value{ .object = payload }, .{}, &discarding.writer); 107 return discarding.fullCount(); 108 } 109 110 // serve a receive-pack and consume any events it pushed to the events branch, 111 // all in one transaction: the push and the views derived from it commit 112 // atomically, and a failed consume cancels the push 113 pub fn receivePackAndConsume( 114 comptime repo_opts: rp.RepoOpts(.xit), 115 io: std.Io, 116 allocator: std.mem.Allocator, 117 repo: *rp.Repo(.xit, repo_opts), 118 reader: *std.Io.Reader, 119 writer: *std.Io.Writer, 120 options: xit.net_server_receive_pack.Options, 121 author: ?evt.CommitAuthor, 122 server: evt.Host.Server, 123 error_writer: *std.Io.Writer, 124 sess: ?*ssh.SessionCtx, 125 ) !void { 126 const DB = rp.Repo(.xit, repo_opts).DB; 127 const State = rp.Repo(.xit, repo_opts).State; 128 129 var response = xit.net_server_receive_pack.Response.init(allocator); 130 defer response.deinit(); 131 132 var updates = xit.net_server_receive_pack.AppliedRefUpdates.init(allocator); 133 defer updates.deinit(); 134 135 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 136 const Ctx = struct { 137 updates: *xit.net_server_receive_pack.AppliedRefUpdates, 138 core: *rp.Repo(.xit, repo_opts).Core, 139 io: std.Io, 140 allocator: std.mem.Allocator, 141 reader: *std.Io.Reader, 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 server: evt.Host.Server, 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.server.users_dir, ctx.updates.items.items, ctx.error_writer); 167 168 // the revisions the pushed branches cause belong to the push 169 _ = try pch.refreshBranchesInTransaction(.{ .server = ctx.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 .server = server, 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 evt.bumpRepoActivityAt(io, allocator, server, repo.core.work_path); 214 215 pch.refreshBranches(.{ .server = server }, .xit, repo_opts, io, allocator, repo, updates.items.items, &sideband) catch |err| { 216 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 217 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, server.users_dir, null, &sideband); 218 }; 219 try response.finish(writer, null); 220 } 221 222 pub fn receiveFork( 223 comptime repo_opts: rp.RepoOpts(.xit), 224 io: std.Io, 225 allocator: std.mem.Allocator, 226 fork_repo: *rp.Repo(.xit, repo_opts), 227 target_repo: *rp.Repo(.xit, repo_opts), 228 server: evt.Host.Server, 229 id: *const [evt.event_id_size * 2]u8, 230 author: evt.CommitAuthor, 231 timestamp: u64, 232 reader: *std.Io.Reader, 233 writer: *std.Io.Writer, 234 error_writer: *std.Io.Writer, 235 sess: ?*ssh.SessionCtx, 236 ) !void { 237 const patch_id = try evt.parseEventId(id); 238 var arena = std.heap.ArenaAllocator.init(allocator); 239 defer arena.deinit(); 240 241 // load the fork and target patch state 242 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 243 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 244 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 245 error.NotFound => null, 246 else => |other| return other, 247 }; 248 const target_patch = if (target_moment) |moment| 249 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 250 else 251 null; 252 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 253 if (target_patch) |patch| { 254 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 255 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 256 } 257 const published = target_patch != null; 258 const active_patch = target_patch orelse fork_patch; 259 const title = active_patch.event.title; 260 261 // resolve the target and the newest fork revision 262 const target_branch = active_patch.event.target_branch; 263 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 264 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 265 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 266 var response = xit.net_server_receive_pack.Response.init(allocator); 267 defer response.deinit(); 268 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 269 270 // execute a transaction that receives the push and materializes its revision 271 const result = blk: { 272 const DB = rp.Repo(.xit, repo_opts).DB; 273 const State = rp.Repo(.xit, repo_opts).State; 274 const Ctx = struct { 275 core: *rp.Repo(.xit, repo_opts).Core, 276 target_core: *rp.Repo(.xit, repo_opts).Core, 277 io: std.Io, 278 allocator: std.mem.Allocator, 279 reader: *std.Io.Reader, 280 writer: *std.Io.Writer, 281 response: *xit.net_server_receive_pack.Response, 282 patch_id: [evt.event_id_size * 2]u8, 283 patch: evt.Patch.Record, 284 published: bool, 285 target_oid: [hash.hexLen(repo_opts.hash)]u8, 286 title: []const u8, 287 author: evt.CommitAuthor, 288 timestamp: u64, 289 newest: ?evt.PatchRev.WithId, 290 revision_id_maybe: *?[evt.event_id_size]u8, 291 sideband: *progress.Sideband, 292 293 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 294 // receive the branch update 295 var moment = try DB.HashMap(.read_write).init(cursor.*); 296 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 297 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); 298 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 299 if (ctx.sideband.gone) return error.ClientGone; 300 301 // copy the target history needed to preserve the merge base 302 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 303 var target_repo_moment = try ctx.target_core.latestMoment(); 304 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 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