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