╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
56 // what the payload may take up: the record holds a timestamp and the 57 // action kind in the same fixed buffer 58 const payload_max_len = repo_opts.max_read_size - @sizeOf(i64) - undo_action.len - 1; 59 60 var arena = std.heap.ArenaAllocator.init(allocator); 61 defer arena.deinit(); 62 const aa = arena.allocator(); 63 64 const author_json: std.json.ObjectMap = try .init(aa, &.{ "username", "email" }, &.{ .{ .string = author.name }, .{ .string = author.email } }); 65 var payload: std.json.ObjectMap = try .init(aa, &.{"author"}, &.{.{ .object = author_json }}); 66 67 // where each ref landed, so the undo tab can say what the push did to it. 68 // an oid of all zeros means the ref has no side there, which is how a 69 // create and a remove are told apart 70 var refs: std.json.Array = .init(aa); 71 for (updates) |update| { 72 const entry: std.json.ObjectMap = try .init(aa, &.{ "name", "old", "new" }, &.{ 73 .{ .string = update.ref_name }, 74 .{ .string = if (zeroOid(update.old_oid)) "" else update.old_oid }, 75 .{ .string = if (zeroOid(update.new_oid)) "" else update.new_oid }, 76 }); 77 try refs.append(.{ .object = entry }); 78 try payload.put(aa, "refs", .{ .array = refs }); 79 80 // the record is written into one fixed buffer, so a ref that no 81 // longer fits is left out rather than failing the push 82 if (try payloadLen(payload) > payload_max_len) { 83 _ = refs.pop(); 84 break; 85 } 86 } 87 88 // the array is stored by value, so the one the payload holds still counts 89 // the entry that was popped until it is replaced here 90 try payload.put(aa, "refs", .{ .array = refs }); 91 92 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action, .payload = payload } }); 93 } 94 95 // the oid a ref update sends when it has no side: all zeros 96 fn zeroOid(oid: []const u8) bool { 97 for (oid) |char| if (char != '0') return false; 98 return true; 99 } 100 101 // how long the record's payload would be, to keep it within the buffer 102 fn payloadLen(payload: std.json.ObjectMap) !u64 { 103 var discarding = std.Io.Writer.Discarding.init(&.{}); 104 try std.json.Stringify.value(std.json.Value{ .object = payload }, .{}, &discarding.writer); 105 return discarding.fullCount(); 106 } 107 108 // serve a receive-pack and consume any events it pushed to the events branch, 109 // all in one transaction: the push and the views derived from it commit 110 // atomically, and a failed consume cancels the push 111 pub fn receivePackAndConsume( 112 comptime repo_opts: rp.RepoOpts(.xit), 113 io: std.Io, 114 allocator: std.mem.Allocator, 115 repo: *rp.Repo(.xit, repo_opts), 116 reader: *std.Io.Reader, 117 writer: *std.Io.Writer, 118 options: xit.net_server_receive_pack.Options, 119 author: evt.CommitAuthor, 120 pusher_id: *const [evt.event_id_size]u8, 121 server: evt.Host.Server, 122 error_writer: *std.Io.Writer, 123 sess: ?*ssh.SessionCtx, 124 ) !void { 125 const DB = rp.Repo(.xit, repo_opts).DB; 126 const State = rp.Repo(.xit, repo_opts).State; 127 128 var response = xit.net_server_receive_pack.Response.init(allocator); 129 defer response.deinit(); 130 131 var updates = xit.net_server_receive_pack.AppliedRefUpdates.init(allocator); 132 defer updates.deinit(); 133 134 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 135 // the moment index the pushed events were consumed into 136 var moment_index: ?u64 = null; 137 const Ctx = struct { 138 updates: *xit.net_server_receive_pack.AppliedRefUpdates, 139 core: *rp.Repo(.xit, repo_opts).Core, 140 io: std.Io, 141 allocator: std.mem.Allocator, 142 reader: *std.Io.Reader, 143 writer: *std.Io.Writer, 144 options: xit.net_server_receive_pack.Options, 145 author: evt.CommitAuthor, 146 response: *xit.net_server_receive_pack.Response, 147 server: evt.Host.Server, 148 error_writer: *std.Io.Writer, 149 sideband: *progress.Sideband, 150 moment_index: *?u64, 151 152 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 153 var moment = try DB.HashMap(.read_write).init(cursor.*); 154 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 155 156 var receive_options = ctx.options; 157 receive_options.applied_ref_updates = ctx.updates; 158 receive_options.deferred_response = ctx.response; 159 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, receive_options, ctx.sideband); 160 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 161 if (ctx.sideband.gone) return error.ClientGone; 162 163 // a repo without an events branch has no events to consume. a 164 // no-op consume must not cancel here, since the push shares the 165 // transaction and must commit regardless. 166 if (null != try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = evt.events_ref })) { 167 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) { 168 error.CancelTransaction => null, 169 else => |e| return e, 170 }; 171 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); 172 173 // the revisions the pushed branches cause belong to the push 174 _ = try pch.refreshBranchesInTransaction(.{ .server = ctx.server }, repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates.items.items, null, ctx.sideband); 175 } 176 177 if (ctx.sideband.gone) return error.ClientGone; 178 179 // the indexes the pushed refs invalidate belong to the push too 180 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 181 var moves: std.ArrayList(srch_cmmt.Move) = .empty; 182 defer moves.deinit(ctx.allocator); 183 for (ctx.updates.items.items) |update| try moves.append(ctx.allocator, .{ .old_oid = update.old_oid, .new_oid = update.new_oid }); 184 _ = try srch_cmmt.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, moves.items, ctx.sideband); 185 186 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, ctx.updates.items.items); 187 } 188 }; 189 190 const result = blk: { 191 try repo.core.db_file.lock(io, .exclusive); 192 defer repo.core.db_file.unlock(io); 193 194 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 195 break :blk history.appendContext( 196 .{ .slot = try history.getSlot(-1) }, 197 Ctx{ 198 .updates = &updates, 199 .core = &repo.core, 200 .io = io, 201 .allocator = allocator, 202 .reader = reader, 203 .writer = writer, 204 .options = options, 205 .author = author, 206 .response = &response, 207 .server = server, 208 .error_writer = error_writer, 209 .sideband = &sideband, 210 .moment_index = &moment_index, 211 }, 212 ); 213 }; 214 result catch |err| { 215 response.finish(writer, @errorName(err)) catch {}; 216 if (err == error.CancelTransaction) return; 217 return err; 218 }; 219 if (moment_index) |index| notify.deliverTransaction(io, allocator, server, repo_opts, repo, index, pusher_id, &sideband); 220 evt.bumpRepoActivityAt(io, allocator, server, repo.core.work_path); 221 222 pch.refreshBranches(.{ .server = server }, .xit, repo_opts, io, allocator, repo, updates.items.items, &sideband) catch |err| { 223 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 224 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, server.users_dir, null, &sideband); 225 }; 226 try response.finish(writer, null); 227 } 228 229 pub fn receiveFork( 230 comptime repo_opts: rp.RepoOpts(.xit), 231 io: std.Io, 232 allocator: std.mem.Allocator, 233 fork_repo: *rp.Repo(.xit, repo_opts), 234 target_repo: *rp.Repo(.xit, repo_opts), 235 server: evt.Host.Server, 236 id: *const [evt.event_id_size * 2]u8, 237 author: evt.CommitAuthor, 238 timestamp: u64, 239 reader: *std.Io.Reader, 240 writer: *std.Io.Writer, 241 error_writer: *std.Io.Writer, 242 sess: ?*ssh.SessionCtx, 243 ) !void { 244 const patch_id = try evt.parseEventId(id); 245 var arena = std.heap.ArenaAllocator.init(allocator); 246 defer arena.deinit(); 247 248 // load the fork and target patch state 249 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 250 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 251 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 252 error.NotFound => null, 253 else => |other| return other, 254 }; 255 const target_patch = if (target_moment) |moment| 256 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 257 else 258 null; 259 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 260 if (target_patch) |patch| { 261 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 262 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 263 } 264 const published = target_patch != null; 265 const active_patch = target_patch orelse fork_patch; 266 const title = active_patch.event.title; 267 268 // resolve the target and the newest fork revision 269 const target_branch = active_patch.event.target_branch; 270 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 271 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 272 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 273 var response = xit.net_server_receive_pack.Response.init(allocator); 274 defer response.deinit(); 275 var sideband = progress.Sideband{ .response = &response, .writer = writer, .sess = sess }; 276 277 // execute a transaction that receives the push and materializes its revision 278 const result = blk: { 279 const DB = rp.Repo(.xit, repo_opts).DB; 280 const State = rp.Repo(.xit, repo_opts).State; 281 const Ctx = struct { 282 core: *rp.Repo(.xit, repo_opts).Core, 283 target_core: *rp.Repo(.xit, repo_opts).Core, 284 io: std.Io, 285 allocator: std.mem.Allocator, 286 reader: *std.Io.Reader, 287 writer: *std.Io.Writer, 288 response: *xit.net_server_receive_pack.Response, 289 patch_id: [evt.event_id_size * 2]u8, 290 patch: evt.Patch.Record, 291 published: bool, 292 target_oid: [hash.hexLen(repo_opts.hash)]u8, 293 title: []const u8, 294 author: evt.CommitAuthor, 295 timestamp: u64, 296 newest: ?evt.PatchRev.WithId, 297 revision_id_maybe: *?[evt.event_id_size]u8, 298 sideband: *progress.Sideband, 299 300 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 301 // receive the branch update 302 var moment = try DB.HashMap(.read_write).init(cursor.*); 303 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 304 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); 305 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 306 if (ctx.sideband.gone) return error.ClientGone; 307 308 // copy the target history needed to preserve the merge base 309 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 310 var target_repo_moment = try ctx.target_core.latestMoment(); 311 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 312 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 313 defer objects.deinit(); 314 try objects.include(&ctx.target_oid); 315 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, ctx.sideband); 316 317 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 318 const existing_revision = if (ctx.newest) |latest| 319 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 320 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 321 else 322 null; 323 324 // reuse an identical revision or record a new one 325 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 326 defer revision_arena.deinit(); 327 var events: [2]evt.EventWithId = undefined; 328 var event_count: usize = 0; 329 if (existing_revision) |latest| { 330 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 331 } else { 332 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 333 events[event_count] = prepared.event; 334 event_count += 1; 335 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 336 337 if (!ctx.published) { 338 var patch = ctx.patch.event; 339 patch.revision = prepared.revision; 340 events[event_count] = .{ 341 .id = ctx.patch_id, 342 .timestamp = ctx.timestamp, 343 .author = ctx.author, 344 .event = .{ .patch = patch }, 345 }; 346 event_count += 1; 347 } 348 } 349 350 // published patches keep only revisions in the fork 351 if (ctx.published and !ctx.patch.removed) { 352 events[event_count] = .{ 353 .id = ctx.patch_id, 354 .timestamp = ctx.timestamp, 355 .author = ctx.author, 356 .event = .{ .patch = null }, 357 }; 358 event_count += 1; 359 } 360 361 // commit and index any new events 362 if (event_count > 0) { 363 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 364 _ = try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 365 } 366 // the patch branch's file index lands with the push 367 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.sideband); 368 369 // a fork push tracks no ref updates, so it records no ranges 370 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author, &.{}); 371 } 372 }; 373 374 try fork_repo.core.db_file.lock(io, .exclusive); 375 defer fork_repo.core.db_file.unlock(io); 376 377 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 378 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 379 .core = &fork_repo.core, 380 .target_core = &target_repo.core, 381 .io = io, 382 .allocator = allocator, 383 .reader = reader, 384 .writer = writer, 385 .response = &response, 386 .patch_id = id.*, 387 .patch = fork_patch, 388 .published = published, 389 .target_oid = target_oid, 390 .title = title, 391 .author = author, 392 .timestamp = timestamp, 393 .newest = newest, 394 .revision_id_maybe = &revision_id_maybe, 395 .sideband = &sideband, 396 }); 397 }; 398 result catch |err| { 399 response.finish(writer, @errorName(err)) catch {}; 400 if (err == error.CancelTransaction) return; 401 return err; 402 }; 403 404 if (!published) return response.finish(writer, null); 405 406 sideband.run(io, .{ .text = "Updating patches" }) catch {}; 407 sideband.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 408 409 // best-effort update the published patch so it has the new revision 410 var refreshed = false; 411 update: { 412 const revision_id = revision_id_maybe orelse break :update; 413 var update_arena = std.heap.ArenaAllocator.init(allocator); 414 defer update_arena.deinit(); 415 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 416 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; 417 if (published_patch.removed) break :update; 418 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 419 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; 420 if (revision.removed) break :update; 421 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 422 if (published_patch.event.revision) |current| { 423 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 424 } 425 var patch = published_patch.event; 426 patch.revision = selected; 427 evt.consume(.{ .server = server }, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 428 .id = id.*, 429 .timestamp = timestamp, 430 .author = author, 431 .event = .{ .patch = patch }, 432 }}) catch |update_err| { 433 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 434 break :update; 435 }; 436 // consume also refreshed mergeability 437 refreshed = true; 438 } 439 440 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, server.users_dir, patch_id); 441 sideband.run(io, .{ .complete_one = .writing_patch }) catch {}; 442 sideband.run(io, .{ .end = .writing_patch }) catch {}; 443 444 // the client may read either repo as soon as it gets the final response. 445 try response.finish(writer, null); 446 } 447