╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ push.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
216 writer: *std.Io.Writer, 217 response: *xit.net_server_receive_pack.Response, 218 patch_id: [evt.event_id_size * 2]u8, 219 patch: evt.Patch.Record, 220 published: bool, 221 target_oid: [hash.hexLen(repo_opts.hash)]u8, 222 title: []const u8, 223 author: evt.CommitAuthor, 224 timestamp: u64, 225 newest: ?evt.PatchRev.WithId, 226 revision_id_maybe: *?[evt.event_id_size]u8, 227 228 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 229 // receive the branch update 230 var moment = try DB.HashMap(.read_write).init(cursor.*); 231 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 232 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 }); 233 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 234 235 // copy the target history needed to preserve the merge base 236 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 237 var target_repo_moment = try ctx.target_core.latestMoment(); 238 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 239 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 240 defer objects.deinit(); 241 try objects.include(&ctx.target_oid); 242 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 243 244 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 245 const existing_revision = if (ctx.newest) |latest| 246 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 247 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 248 else 249 null; 250 251 // reuse an identical revision or record a new one 252 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 253 defer revision_arena.deinit(); 254 var events: [2]evt.EventWithId = undefined; 255 var event_count: usize = 0; 256 if (existing_revision) |latest| { 257 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 258 } else { 259 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 260 events[event_count] = prepared.event; 261 event_count += 1; 262 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 263 264 if (!ctx.published) { 265 var patch = ctx.patch.event; 266 patch.revision = prepared.revision; 267 events[event_count] = .{ 268 .id = ctx.patch_id, 269 .timestamp = ctx.timestamp, 270 .author = ctx.author, 271 .event = .{ .patch = patch }, 272 }; 273 event_count += 1; 274 } 275 } 276 277 // published patches keep only revisions in the fork 278 if (ctx.published and !ctx.patch.removed) { 279 events[event_count] = .{ 280 .id = ctx.patch_id, 281 .timestamp = ctx.timestamp, 282 .author = ctx.author, 283 .event = .{ .patch = null }, 284 }; 285 event_count += 1; 286 } 287 288 // commit and index any new events 289 if (event_count > 0) { 290 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 291 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 292 } 293 try xit.undo.writeMessage(repo_opts, state, .push); 294 } 295 }; 296 297 try fork_repo.core.db_file.lock(io, .exclusive); 298 defer fork_repo.core.db_file.unlock(io); 299 300 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 301 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 302 .core = &fork_repo.core, 303 .target_core = &target_repo.core, 304 .io = io, 305 .allocator = allocator, 306 .reader = reader, 307 .writer = writer, 308 .response = &response, 309 .patch_id = id.*, 310 .patch = fork_patch, 311 .published = published, 312 .target_oid = target_oid, 313 .title = title, 314 .author = author, 315 .timestamp = timestamp, 316 .newest = newest, 317 .revision_id_maybe = &revision_id_maybe, 318 }); 319 }; 320 result catch |err| { 321 response.finish(writer, @errorName(err)) catch {}; 322 if (err == error.CancelTransaction) return; 323 return err; 324 }; 325 326 var progress = PushProgress{ .response = &response, .writer = writer, .label = "Updating patches" }; 327 progress.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 328 329 // best-effort update the published patch so it has the new revision 330 var refreshed = false; 331 update: { 332 const revision_id = revision_id_maybe orelse break :update; 333 if (!published) break :update; 334 var update_arena = std.heap.ArenaAllocator.init(allocator); 335 defer update_arena.deinit(); 336 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 337 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; 338 if (published_patch.removed) break :update; 339 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 340 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; 341 if (revision.removed) break :update; 342 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 343 if (published_patch.event.revision) |current| { 344 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 345 } 346 var patch = published_patch.event; 347 patch.revision = selected; 348 evt.consume(.server, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 349 .id = id.*, 350 .timestamp = timestamp, 351 .author = author, 352 .event = .{ .patch = patch }, 353 }}) catch |update_err| { 354 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 355 break :update; 356 }; 357 // consume also refreshed mergeability 358 refreshed = true; 359 } 360 361 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, patch_id); 362 progress.run(io, .{ .complete_one = .writing_patch }) catch {}; 363 progress.run(io, .{ .end = .writing_patch }) catch {}; 364 365 // the client may read either repo as soon as it gets the final response. 366 try response.finish(writer, null); 367 } 368