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