╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
145 146 if (head_oid_maybe) |*head_oid| { 147 try rf.write(.xit, repo_opts, state, ctx.io, patch_path, .{ .oid = head_oid }); 148 } else { 149 try bch.add(.xit, repo_opts, state, ctx.io, .{ .name = ref.name, .target = .none }); 150 } 151 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 152 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "create fork" }); 153 } 154 }; 155 156 try fork_repo.core.db_file.lock(io, .exclusive); 157 defer fork_repo.core.db_file.unlock(io); 158 159 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 160 try history.appendContext( 161 .{ .slot = try history.getSlot(-1) }, 162 Ctx{ .core = &fork_repo.core, .io = io }, 163 ); 164 } 165 166 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 167 try fork_repo.addConfig(io, allocator, .{ .name = "receive.denydeletes", .value = "true" }); 168 169 // create the patch event 170 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 171 .id = input.id, 172 .timestamp = input.timestamp, 173 .author = input.author, 174 .event = .{ .patch = .{ 175 .title = input.title, 176 .description = input.description, 177 .tags = input.tags, 178 } }, 179 }}); 180 181 // create the fork event 182 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 183 .id = input.id, 184 .timestamp = input.timestamp, 185 .author = input.author, 186 .event = .{ .fork = .{ 187 .user_id = &input.user_id, 188 .repo_id = &input.repo_id, 189 } }, 190 }}); 191 192 return fork_path; 193 } 194 195 pub fn receivePack( 196 comptime repo_opts: rp.RepoOpts(.xit), 197 io: std.Io, 198 allocator: std.mem.Allocator, 199 fork_repo: *rp.Repo(.xit, repo_opts), 200 target_repo: *rp.Repo(.xit, repo_opts), 201 id: *const [evt.event_id_size * 2]u8, 202 target_branch: []const u8, 203 author: evt.CommitAuthor, 204 timestamp: u64, 205 reader: *std.Io.Reader, 206 writer: *std.Io.Writer, 207 error_writer: *std.Io.Writer, 208 ) !void { 209 if (!rf.validateName(target_branch)) return error.InvalidTarget; 210 const patch_id = try evt.parseEventId(id); 211 var arena = std.heap.ArenaAllocator.init(allocator); 212 defer arena.deinit(); 213 214 // load the fork and target patch state 215 const fork_moment = evt.currentMoment(repo_opts, fork_repo) catch return error.PatchDataUnavailable; 216 const fork_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 217 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 218 error.NotFound => null, 219 else => |other| return other, 220 }; 221 const target_patch = if (target_moment) |moment| 222 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 223 else 224 null; 225 if (fork_patch.removed and target_patch == null) return error.PatchDataUnavailable; 226 if (target_patch) |patch| { 227 if (!std.mem.eql(u8, patch.author_email orelse "", author.email)) return error.PatchDataUnavailable; 228 if (patch.event.status == .merged) return error.PatchAlreadyMerged; 229 } 230 const published = target_patch != null; 231 const title = if (target_patch) |patch| patch.event.title else fork_patch.event.title; 232 233 // resolve the target and the newest fork revision 234 const target_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = target_branch })) orelse return error.TargetNotFound; 235 const newest = try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena); 236 const target_ref = try std.fmt.allocPrint(arena.allocator(), "refs/heads/{s}", .{target_branch}); 237 var revision_id_maybe: ?[evt.event_id_size]u8 = null; 238 239 // execute a transaction that receives the push and materializes its revision 240 { 241 const DB = rp.Repo(.xit, repo_opts).DB; 242 const State = rp.Repo(.xit, repo_opts).State; 243 const Ctx = struct { 244 core: *rp.Repo(.xit, repo_opts).Core, 245 target_core: *rp.Repo(.xit, repo_opts).Core, 246 io: std.Io, 247 allocator: std.mem.Allocator, 248 reader: *std.Io.Reader, 249 writer: *std.Io.Writer, 250 patch_id: [evt.event_id_size * 2]u8, 251 patch: evt.Patch.Record, 252 published: bool, 253 target_oid: [hash.hexLen(repo_opts.hash)]u8, 254 target_ref: []const u8, 255 title: []const u8, 256 author: evt.CommitAuthor, 257 timestamp: u64, 258 newest: ?evt.PatchRev.WithId, 259 revision_id_maybe: *?[evt.event_id_size]u8, 260 261 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 262 // receive the branch update 263 var moment = try DB.HashMap(.read_write).init(cursor.*); 264 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 265 try xit.net_server_receive_pack.run(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.reader, ctx.writer, .{ .allowed_ref = ref_path }); 266 267 // copy the target history needed to preserve the merge base 268 const source_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ref })) orelse return error.CancelTransaction; 269 var target_repo_moment = try ctx.target_core.latestMoment(); 270 const target_state = State(.read_only){ .core = ctx.target_core, .extra = .{ .moment = &target_repo_moment } }; 271 var objects = try obj.ObjectIterator(.xit, repo_opts).init(target_state, ctx.io, ctx.allocator, .{ .kind = .all }); 272 defer objects.deinit(); 273 try objects.include(&ctx.target_oid); 274 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 275 276 const base_oid = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), ctx.io, ctx.allocator, &ctx.target_oid, &source_oid); 277 const existing_revision = if (ctx.newest) |latest| 278 if (std.mem.eql(u8, latest.record.event.base_oid, &base_oid) and 279 std.mem.eql(u8, latest.record.event.source_oid, &source_oid) and 280 std.mem.eql(u8, latest.record.event.target_ref, ctx.target_ref)) latest else null 281 else 282 null; 283 284 // reuse an identical revision or record a new one 285 var events: [2]evt.EventWithId = undefined; 286 var event_count: usize = 0; 287 var base_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 288 var head_tree_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 289 var tree_entries: [2]evt.EventTreeEntry = undefined; 290 if (existing_revision) |latest| { 291 if (ctx.published) ctx.revision_id_maybe.* = latest.id; 292 } else { 293 var base_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &base_oid); 294 defer base_object.deinit(); 295 var source_object = try obj.Object(.xit, repo_opts).initCommit(state.readOnly(), ctx.io, ctx.allocator, &source_oid); 296 defer source_object.deinit(); 297 base_tree_oid = base_object.content.commit.tree; 298 head_tree_oid = source_object.content.commit.tree; 299 300 var revision_id: [evt.event_id_size]u8 = undefined; 301 ctx.io.random(&revision_id); 302 const revision_hex = std.fmt.bytesToHex(revision_id, .lower); 303 const revision_event: evt.PatchRev = .{ 304 .base_oid = &base_oid, 305 .source_oid = &source_oid, 306 .target_ref = ctx.target_ref, 307 .message = ctx.title, 308 }; 309 const identity = try std.fmt.allocPrint(ctx.allocator, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 310 defer ctx.allocator.free(identity); 311 const patch_oid = try evt.PatchRev.writeSquashCommit( 312 .xit, 313 repo_opts, 314 state, 315 ctx.io, 316 ctx.allocator, 317 revision_event, 318 &head_tree_oid, 319 identity, 320 identity, 321 ctx.timestamp, 322 ); 323 tree_entries = .{ 324 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 325 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 326 }; 327 events[event_count] = .{ 328 .id = revision_hex, 329 .timestamp = ctx.timestamp, 330 .author = ctx.author, 331 .tree_entries = &tree_entries, 332 .event = .{ .patchrev = revision_event }, 333 }; 334 event_count += 1; 335 ctx.revision_id_maybe.* = revision_id; 336 337 if (!ctx.published) { 338 var patch = ctx.patch.event; 339 patch.revision = .{ 340 .id = revision_hex, 341 .squash_oid = &patch_oid, 342 .source_oid = &source_oid, 343 .target_ref = ctx.target_ref, 344 }; 345 events[event_count] = .{ 346 .id = ctx.patch_id, 347 .timestamp = ctx.timestamp, 348 .author = ctx.author, 349 .event = .{ .patch = patch }, 350 }; 351 event_count += 1; 352 } 353 } 354 355 // published patches keep only revisions in the fork 356 if (ctx.published and !ctx.patch.removed) { 357 events[event_count] = .{ 358 .id = ctx.patch_id, 359 .timestamp = ctx.timestamp, 360 .author = ctx.author, 361 .event = .{ .patch = null }, 362 }; 363 event_count += 1; 364 } 365 366 // commit and index any new events 367 if (event_count > 0) { 368 try evt.commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, evt.events_ref, events[0..event_count], null); 369 if (!try evt.consumeInTransaction(.fork, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, evt.events_ref)) return error.CancelTransaction; 370 } 371 try xit.undo.writeMessage(repo_opts, state, .push); 372 } 373 }; 374 375 try fork_repo.core.db_file.lock(io, .exclusive); 376 defer fork_repo.core.db_file.unlock(io); 377 378 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 379 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 380 .core = &fork_repo.core, 381 .target_core = &target_repo.core, 382 .io = io, 383 .allocator = allocator, 384 .reader = reader, 385 .writer = writer, 386 .patch_id = id.*, 387 .patch = fork_patch, 388 .published = published, 389 .target_oid = target_oid, 390 .target_ref = target_ref, 391 .title = title, 392 .author = author, 393 .timestamp = timestamp, 394 .newest = newest, 395 .revision_id_maybe = &revision_id_maybe, 396 }) catch |err| switch (err) { 397 error.CancelTransaction => {}, 398 else => |other| return other, 399 }; 400 try writer.flush(); 401 } 402 403 // best-effort update the published patch so it has the new revision 404 { 405 const revision_id = revision_id_maybe orelse return; 406 if (!published) return; 407 var update_arena = std.heap.ArenaAllocator.init(allocator); 408 defer update_arena.deinit(); 409 const target_update_moment = evt.currentMoment(repo_opts, target_repo) catch return; 410 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; 411 if (published_patch.removed) return; 412 const fork_update_moment = evt.currentMoment(repo_opts, fork_repo) catch return; 413 const revision = (evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_update_moment, &update_arena, &revision_id) catch return) orelse return; 414 if (revision.removed) return; 415 const selected = evt.Patch.Revision.fromRecord(revision_id, revision); 416 if (published_patch.event.revision) |current| { 417 if (evt.fieldEqual(evt.Patch.Revision, current, selected)) return; 418 } 419 var patch = published_patch.event; 420 patch.revision = selected; 421 evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 422 .id = id.*, 423 .timestamp = timestamp, 424 .author = author, 425 .event = .{ .patch = patch }, 426 }}) catch |update_err| { 427 serve_common.logError(io, error_writer, "failed to update published patch {s}: {s}\n", .{ id, @errorName(update_err) }); 428 }; 429 } 430 } 431 432 // tombstone first so a missing fork never remains visible 433 pub fn remove( 434 io: std.Io, 435 allocator: std.mem.Allocator, 436 repo_root_path: []const u8, 437 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 438 id: *const [evt.event_id_size * 2]u8, 439 user_id: *const [evt.event_id_size]u8, 440 author: evt.CommitAuthor, 441 ) !void { 442 const fork_id = try evt.parseEventId(id); 443 var arena = std.heap.ArenaAllocator.init(allocator); 444 defer arena.deinit(); 445 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 446 const record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 447 if (!std.mem.eql(u8, record.event.user_id, user_id)) return error.InvalidPatchDraft; 448 if (!record.removed) try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, &fork_id, .fork, author); 449 450 const path = try forkPath(allocator, repo_root_path, id); 451 defer allocator.free(path); 452 try std.Io.Dir.cwd().deleteTree(io, path); 453 } 454