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