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