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