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