╭ 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 ) !void { 89 var author_json: std.json.ObjectMap = if (author) |user| 90 try .init(allocator, &.{ "username", "email" }, &.{ .{ .string = user.name }, .{ .string = user.email } }) 91 else 92 .empty; 93 defer author_json.deinit(allocator); 94 var payload: std.json.ObjectMap = try .init(allocator, &.{"author"}, &.{if (author != null) .{ .object = author_json } else .null}); 95 defer payload.deinit(allocator); 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 // serve a receive-pack and consume any events it pushed to the events branch, 100 // all in one transaction: the push and the views derived from it commit 101 // atomically, and a failed consume cancels the push 102 pub fn receivePackAndConsume( 103 comptime repo_opts: rp.RepoOpts(.xit), 104 io: std.Io, 105 allocator: std.mem.Allocator, 106 repo: *rp.Repo(.xit, repo_opts), 107 reader: *std.Io.Reader, 108 writer: *std.Io.Writer, 109 options: xit.net_server_receive_pack.Options, 110 author: ?evt.CommitAuthor, 111 repo_root_path: []const u8, 112 error_writer: *std.Io.Writer, 113 ) !void { 114 const DB = rp.Repo(.xit, repo_opts).DB; 115 const State = rp.Repo(.xit, repo_opts).State; 116 117 var response = xit.net_server_receive_pack.Response.init(allocator); 118 defer response.deinit(); 119 120 var updates = xit.net_server_receive_pack.AppliedRefUpdates.init(allocator); 121 defer updates.deinit(); 122 123 var progress = PushProgress{ .response = &response, .writer = writer }; 124 const Ctx = struct { 125 updates: *xit.net_server_receive_pack.AppliedRefUpdates, 126 core: *rp.Repo(.xit, repo_opts).Core, 127 io: std.Io, 128 allocator: std.mem.Allocator, 129 reader: *std.Io.Reader, 130 writer: *std.Io.Writer, 131 options: xit.net_server_receive_pack.Options, 132 author: ?evt.CommitAuthor, 133 response: *xit.net_server_receive_pack.Response, 134 repo_root_path: []const u8, 135 error_writer: *std.Io.Writer, 136 progress: *PushProgress, 137 138 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 139 var moment = try DB.HashMap(.read_write).init(cursor.*); 140 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 141 142 var receive_options = ctx.options; 143 receive_options.applied_ref_updates = ctx.updates; 144 receive_options.deferred_response = ctx.response; 145 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, receive_options); 146 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 147 148 // a repo without an events branch has no events to consume. a 149 // no-op consume must not cancel here, since the push shares the 150 // transaction and must commit regardless. 151 if (null != try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = evt.events_ref })) { 152 _ = try evt.consumeInTransaction(.repo, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref); 153 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); 154 155 // the revisions the pushed branches cause belong to the push 156 _ = try pch.refreshBranchesInTransaction(.server, repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates.items.items, null, ctx.progress); 157 } 158 159 // the indexes the pushed refs invalidate belong to the push too 160 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator); 161 _ = try srch_cmmt.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, ctx.updates.items.items); 162 163 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author); 164 } 165 }; 166 167 const result = blk: { 168 try repo.core.db_file.lock(io, .exclusive); 169 defer repo.core.db_file.unlock(io); 170 171 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 172 break :blk history.appendContext( 173 .{ .slot = try history.getSlot(-1) }, 174 Ctx{ 175 .updates = &updates, 176 .core = &repo.core, 177 .io = io, 178 .allocator = allocator, 179 .reader = reader, 180 .writer = writer, 181 .options = options, 182 .author = author, 183 .response = &response, 184 .repo_root_path = repo_root_path, 185 .error_writer = error_writer, 186 .progress = &progress, 187 }, 188 ); 189 }; 190 result catch |err| { 191 response.finish(writer, @errorName(err)) catch {}; 192 if (err == error.CancelTransaction) return; 193 return err; 194 }; 195 196 pch.refreshBranches(.server, .xit, repo_opts, io, allocator, repo, updates.items.items, &progress) catch |err| { 197 serve_common.logError(io, error_writer, "failed to refresh branch patches: {s}\n", .{@errorName(err)}); 198 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, null, &progress); 199 }; 200 try response.finish(writer, null); 201 } 202 203 pub fn receiveFork( 204 comptime repo_opts: rp.RepoOpts(.xit), 205 io: std.Io, 206 allocator: std.mem.Allocator, 207 fork_repo: *rp.Repo(.xit, repo_opts), 208 target_repo: *rp.Repo(.xit, repo_opts), 209 id: *const [evt.event_id_size * 2]u8, 210 author: evt.CommitAuthor, 211 timestamp: u64, 212 reader: *std.Io.Reader, 213 writer: *std.Io.Writer, 214 error_writer: *std.Io.Writer, 215 ) !void { 216 const patch_id = try evt.parseEventId(id); 217 var arena = std.heap.ArenaAllocator.init(allocator); 218 defer arena.deinit(); 219 220 // load the fork and target patch state 221 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 222 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 223 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 224 error.NotFound => null, 225 else => |other| return other, 226 }; 227 const target_patch = if (target_moment) |moment| 228 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 229 else 230 null; 231 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 232 if (target_patch) |patch| { 233 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 234 if (patch.event.status.kind() == .merged) return error.PatchAlreadyMerged; 235 } 236 const published = target_patch != null; 237 const active_patch = target_patch orelse fork_patch; 238 const title = active_patch.event.title; 239 240 // resolve the target and the newest fork revision 241 const target_branch = active_patch.event.target_branch; 242 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 243 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 244 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 245 var response = xit.net_server_receive_pack.Response.init(allocator); 246 defer response.deinit(); 247 248 // execute a transaction that receives the push and materializes its revision 249 const result = blk: { 250 const DB = rp.Repo(.xit, repo_opts).DB; 251 const State = rp.Repo(.xit, repo_opts).State; 252 const Ctx = struct { 253 core: *rp.Repo(.xit, repo_opts).Core, 254 target_core: *rp.Repo(.xit, repo_opts).Core, 255 io: std.Io, 256 allocator: std.mem.Allocator, 257 reader: *std.Io.Reader, 258 writer: *std.Io.Writer, 259 response: *xit.net_server_receive_pack.Response, 260 patch_id: [evt.event_id_size * 2]u8, 261 patch: evt.Patch.Record, 262 published: bool, 263 target_oid: [hash.hexLen(repo_opts.hash)]u8, 264 title: []const u8, 265 author: evt.CommitAuthor, 266 timestamp: u64, 267 newest: ?evt.PatchRev.WithId, 268 revision_id_maybe: *?[evt.event_id_size]u8, 269 270 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 271 // receive the branch update 272 var moment = try DB.HashMap(.read_write).init(cursor.*); 273 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 274 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 }); 275 try writeReceivedPatches(repo_opts, state, ctx.io, ctx.allocator, ctx.response, ctx.writer); 276 277 // copy the target history needed to preserve the merge base 278 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = fork.ref })) orelse return error.CancelTransaction; 279 var target_repo_moment = try ctx.target_core.latestMoment(); 280 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 281 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 282 defer objects.deinit(); 283 try objects.include(&ctx.target_oid); 284 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 285 286 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 287 const existing_revision = if (ctx.newest) |latest| 288 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 289 std.mem.eql(u8, latest.record.event.source_oid, &source_oid)) latest else null 290 else 291 null; 292 293 // reuse an identical revision or record a new one 294 var revision_arena = std.heap.ArenaAllocator.init(ctx.allocator); 295 defer revision_arena.deinit(); 296 var events: [2]evt.EventWithId = undefined; 297 var event_count: usize = 0; 298 if (existing_revision) |latest| { 299 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 300 } else { 301 const prepared = try evt.PatchRev.prepare(.xit, repo_opts, state, ctx.io, &revision_arena, &base_oid, &source_oid, ctx.title, ctx.author, ctx.timestamp); 302 events[event_count] = prepared.event; 303 event_count += 1; 304 ctx.revision_id_maybe.* = try evt.parseEventId(&prepared.event.id); 305 306 if (!ctx.published) { 307 var patch = ctx.patch.event; 308 patch.revision = prepared.revision; 309 events[event_count] = .{ 310 .id = ctx.patch_id, 311 .timestamp = ctx.timestamp, 312 .author = ctx.author, 313 .event = .{ .patch = patch }, 314 }; 315 event_count += 1; 316 } 317 } 318 319 // published patches keep only revisions in the fork 320 if (ctx.published and !ctx.patch.removed) { 321 events[event_count] = .{ 322 .id = ctx.patch_id, 323 .timestamp = ctx.timestamp, 324 .author = ctx.author, 325 .event = .{ .patch = null }, 326 }; 327 event_count += 1; 328 } 329 330 // commit and index any new events 331 if (event_count > 0) { 332 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 333 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 334 } 335 try writeUndo(repo_opts, state, ctx.io, ctx.allocator, ctx.author); 336 } 337 }; 338 339 try fork_repo.core.db_file.lock(io, .exclusive); 340 defer fork_repo.core.db_file.unlock(io); 341 342 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 343 break :blk history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 344 .core = &fork_repo.core, 345 .target_core = &target_repo.core, 346 .io = io, 347 .allocator = allocator, 348 .reader = reader, 349 .writer = writer, 350 .response = &response, 351 .patch_id = id.*, 352 .patch = fork_patch, 353 .published = published, 354 .target_oid = target_oid, 355 .title = title, 356 .author = author, 357 .timestamp = timestamp, 358 .newest = newest, 359 .revision_id_maybe = &revision_id_maybe, 360 }); 361 }; 362 result catch |err| { 363 response.finish(writer, @errorName(err)) catch {}; 364 if (err == error.CancelTransaction) return; 365 return err; 366 }; 367 368 find.refresh(repo_opts, io, allocator, fork_repo) catch |err| { 369 serve_common.logError(io, error_writer, "failed to refresh file index: {s}\n", .{@errorName(err)}); 370 }; 371 if (!published) return response.finish(writer, null); 372 373 var progress = PushProgress{ .response = &response, .writer = writer, .label = "Updating patches" }; 374 progress.run(io, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 1 } }) catch {}; 375 376 // best-effort update the published patch so it has the new revision 377 var refreshed = false; 378 update: { 379 const revision_id = revision_id_maybe orelse break :update; 380 var update_arena = std.heap.ArenaAllocator.init(allocator); 381 defer update_arena.deinit(); 382 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch break :update; 383 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; 384 if (published_patch.removed) break :update; 385 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch break :update; 386 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; 387 if (revision.removed) break :update; 388 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 389 if (published_patch.event.revision) |current| { 390 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) break :update; 391 } 392 var patch = published_patch.event; 393 patch.revision = selected; 394 evt.consume(.server, .repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 395 .id = id.*, 396 .timestamp = timestamp, 397 .author = author, 398 .event = .{ .patch = patch }, 399 }}) catch |update_err| { 400 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 401 break :update; 402 }; 403 // consume also refreshed mergeability 404 refreshed = true; 405 } 406 407 if (!refreshed) pch.refreshMergeability(repo_opts, io, allocator, target_repo, patch_id); 408 progress.run(io, .{ .complete_one = .writing_patch }) catch {}; 409 progress.run(io, .{ .end = .writing_patch }) catch {}; 410 411 // the client may read either repo as soon as it gets the final response. 412 try response.finish(writer, null); 413 } 414