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