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