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