╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
241 var fork_repo_moment = try ctx.fork_repo.core.latestMoment(); 242 const fork_state = State(.read_only){ .core = &ctx.fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 243 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 244 defer objects.deinit(); 245 try objects.include(&ctx.merge_oid); 246 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 247 } 248 249 // import the revision and mark the patch merged 250 if (!try importMergedFromFork(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.fork_repo, &ctx.patch_id, &ctx.merge_oid, ctx.expected_patch, ctx.author, ctx.timestamp)) return error.PatchOutOfDate; 251 252 // merge the selected commit 253 { 254 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 255 .kind = .full, 256 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.merge_oid }}, .algo = .patch } }, 257 .commit_metadata = .{ 258 .author = ctx.identity, 259 .committer = ctx.identity, 260 .message = ctx.expected_patch.title, 261 .timestamp = ctx.timestamp, 262 }, 263 }, ctx.target_ref, null); 264 defer merge_result.deinit(); 265 switch (merge_result.result) { 266 .conflict => return error.MergeConflict, 267 .success, .nothing, .fast_forward => {}, 268 } 269 } 270 271 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "merge patch" }); 272 } 273 }; 274 275 try target_repo.core.db_file.lock(io, .exclusive); 276 defer target_repo.core.db_file.unlock(io); 277 278 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 279 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 280 .core = &target_repo.core, 281 .fork_repo = &fork_repo, 282 .io = io, 283 .allocator = allocator, 284 .patch_id = patch_id, 285 .expected_patch = patch_record.event, 286 .target_ref = target_ref, 287 .merge_oid = merge_oid, 288 .identity = identity, 289 .author = input.author, 290 .timestamp = input.timestamp, 291 }); 292 } 293 } 294 295 // merge a published patch and remove its fork 296 pub fn mergeAndRemoveFork( 297 comptime repo_opts: rp.RepoOpts(.xit), 298 io: std.Io, 299 allocator: std.mem.Allocator, 300 repo_root_path: []const u8, 301 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 302 target_repo: *rp.Repo(.xit, repo_opts), 303 input: MergeInput, 304 ) !void { 305 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 306 try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 307 } 308 309 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 310 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 311 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 312 const name = std.mem.trimEnd(u8, line[0..open], " "); 313 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 314 return .{ .name = name, .email = line[open + 1 .. close] }; 315 } 316 317 // import a revision after detecting a client-side merge 318 fn importMerged( 319 comptime repo_opts: rp.RepoOpts(.xit), 320 state: rp.Repo(.xit, repo_opts).State(.read_write), 321 db: *evt.EventDB(repo_opts.hash), 322 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 323 io: std.Io, 324 allocator: std.mem.Allocator, 325 repo_root_path: []const u8, 326 patch_id: *const [evt.event_id_size]u8, 327 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 328 ) !void { 329 var arena = std.heap.ArenaAllocator.init(allocator); 330 defer arena.deinit(); 331 332 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 333 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 334 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 335 defer fork_repo.deinit(io, allocator); 336 337 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, merged_oid, null, null, 0); 338 } 339 340 // import the revision and mark its patch merged in the current transaction 341 fn importMergedFromFork( 342 comptime repo_opts: rp.RepoOpts(.xit), 343 state: rp.Repo(.xit, repo_opts).State(.read_write), 344 db: *evt.EventDB(repo_opts.hash), 345 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 346 io: std.Io, 347 allocator: std.mem.Allocator, 348 fork_repo: *rp.Repo(.xit, repo_opts), 349 patch_id: *const [evt.event_id_size]u8, 350 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 351 expected_patch: ?evt.Patch, 352 merged_author: ?evt.CommitAuthor, 353 merged_timestamp: u64, 354 ) !bool { 355 const DB = evt.EventDB(repo_opts.hash); 356 var arena = std.heap.ArenaAllocator.init(allocator); 357 defer arena.deinit(); 358 359 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 360 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 361 const selected = patch_record.event.revision orelse return false; 362 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 363 if (expected_patch) |expected| { 364 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 365 } 366 const revision_id = try evt.parseEventId(&selected.id); 367 if (!selected.includesOid(merged_oid)) return false; 368 369 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 370 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 371 if (!selected.matches(revision)) return false; 372 373 var fork_repo_moment = try fork_repo.core.latestMoment(); 374 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 375 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 376 if (revision.event_oid.len != event_oid.len) return false; 377 @memcpy(&event_oid, revision.event_oid); 378 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 379 defer event_object.deinit(); 380 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 381 382 var events: [2]evt.EventWithId = undefined; 383 var event_count: usize = 0; 384 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 385 var tree_entries: [2]evt.EventTreeEntry = undefined; 386 const needs_import = if (current_revision) |existing| existing.removed else true; 387 if (needs_import) { 388 tree_entries = .{ 389 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 390 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 391 }; 392 events[event_count] = .{ 393 .id = selected.id, 394 .timestamp = event_object.content.commit.metadata.timestamp, 395 .author = original_author, 396 .tree_entries = &tree_entries, 397 .event = .{ .patchrev = revision.event }, 398 }; 399 event_count += 1; 400 } else { 401 const existing = current_revision orelse unreachable; 402 if (!selected.matches(existing)) return error.InvalidPatch; 403 } 404 405 var patch = patch_record.event; 406 patch.status = .{ .merged = merged_oid }; 407 events[event_count] = .{ 408 .id = std.fmt.bytesToHex(patch_id.*, .lower), 409 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 410 .author = merged_author orelse original_author, 411 .event = .{ .patch = patch }, 412 }; 413 event_count += 1; 414 415 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 416 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 417 return true; 418 } 419 420 // find patches merged by commits received in a push 421 pub fn detectMerged( 422 comptime repo_opts: rp.RepoOpts(.xit), 423 state: rp.Repo(.xit, repo_opts).State(.read_write), 424 db: *evt.EventDB(repo_opts.hash), 425 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 426 io: std.Io, 427 allocator: std.mem.Allocator, 428 repo_root_path: []const u8, 429 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 430 error_writer: *std.Io.Writer, 431 ) !void { 432 const DB = evt.EventDB(repo_opts.hash); 433 var arena = std.heap.ArenaAllocator.init(allocator); 434 defer arena.deinit(); 435 436 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 437 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 438 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 439 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 440 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 441 const heads_prefix = "refs/heads/"; 442 for (updates) |update| { 443 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 444 std.mem.eql(u8, update.ref_name, events_ref_path) or 445 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 446 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 447 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 448 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 449 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 450 @memcpy(&old_oid, update.old_oid); 451 @memcpy(&new_oid, update.new_oid); 452 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 453 454 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 455 defer commits.deinit(); 456 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 457 try commits.include(&new_oid); 458 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 459 while (try commits.next(arena.allocator())) |commit| { 460 defer commit.deinit(); 461 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 462 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 463 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 464 var ids_iter = try ids.iterator(); 465 while (try ids_iter.next()) |cursor| { 466 const pair = try cursor.readKeyValuePair(); 467 var patch_id: [evt.event_id_size]u8 = undefined; 468 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 469 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 470 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid) catch |err| { 471 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 472 continue; 473 }; 474 } 475 } 476 } 477 } 478