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