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