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