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