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