╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
314 io: std.Io, 315 allocator: std.mem.Allocator, 316 repo: *Repo, 317 oid: *const [hash.hexLen(repo_opts.hash)]u8, 318 ) ![hash.hexLen(repo_opts.hash)]u8 { 319 var moment = try repo.core.latestMoment(); 320 const state = Repo.State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 321 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 322 defer object.deinit(); 323 return switch (object.content) { 324 .commit => |commit| commit.tree, 325 else => error.InvalidObject, 326 }; 327 } 328 329 fn consumePatchWithRevision( 330 comptime role: evt.RepoRole, 331 io: std.Io, 332 allocator: std.mem.Allocator, 333 repo: *Repo, 334 ref: rf.Ref, 335 revision_id: [evt.event_id_size]u8, 336 revision: evt.PatchRev, 337 tree_entries: []const evt.EventTreeEntry, 338 revision_timestamp: u64, 339 patch_id: [evt.event_id_size]u8, 340 patch_value: evt.Patch, 341 patch_timestamp: u64, 342 ) !void { 343 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 344 .id = std.fmt.bytesToHex(revision_id, .lower), 345 .timestamp = revision_timestamp, 346 .author = author, 347 .tree_entries = tree_entries, 348 .event = .{ .patchrev = revision }, 349 }}); 350 351 var arena = std.heap.ArenaAllocator.init(allocator); 352 defer arena.deinit(); 353 const moment = try evt.currentMoment(repo_opts, repo); 354 const record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &revision_id)) orelse return error.NotFound; 355 var patch = patch_value; 356 patch.revision = evt.Patch.Revision.fromRecord(revision_id, record); 357 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 358 .id = std.fmt.bytesToHex(patch_id, .lower), 359 .timestamp = patch_timestamp, 360 .author = author, 361 .event = .{ .patch = patch }, 362 }}); 363 } 364 365 test "patch event conflicts, stacking, and gc" { 366 const io = std.testing.io; 367 const allocator = std.testing.allocator; 368 369 // create the temp dir 370 var temp = std.testing.tmpDir(.{}); 371 defer temp.cleanup(); 372 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 373 defer allocator.free(temp_path); 374 375 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 376 defer allocator.free(upstream_path); 377 const target_path = try std.fs.path.join(allocator, &.{ temp_path, "target" }); 378 defer allocator.free(target_path); 379 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 380 defer allocator.free(source_path); 381 382 // 383 // create the upstream repo and its base commit 384 // 385 386 const base_oid = blk: { 387 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 388 defer upstream.deinit(io, allocator); 389 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 390 defer file.close(io); 391 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 392 try upstream.add(io, allocator, &.{"main.zig"}); 393 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 394 }; 395 396 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 397 defer target.deinit(io, allocator); 398 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 399 400 // 401 // fork the target and commit the proposed change 402 // 403 404 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 405 defer source.deinit(io, allocator); 406 407 const source_oid = blk: { 408 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 409 defer file.close(io); 410 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 411 try source.add(io, allocator, &.{"feature.zig"}); 412 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 413 }; 414 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 415 416 // 417 // copy the proposed objects without creating a target ref 418 // 419 420 { 421 var source_moment = try source.core.latestMoment(); 422 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 423 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 424 defer objects.deinit(); 425 try objects.include(&source_oid); 426 try target.copyObjects(.xit, repo_opts, &objects, io, null); 427 } 428 429 // 430 // define the patch revision and patch 431 // 432 433 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 434 const patchrev_id = evt.EventWithId.randomId(prng.random()); 435 const patch_id = evt.EventWithId.randomId(prng.random()); 436 const patch = evt.Patch{ 437 .title = "add the answer", 438 .description = "adds a reusable answer constant", 439 .tags = "enhancement", 440 .target_branch = "master", 441 }; 442 const tree_entries = [_]evt.EventTreeEntry{ 443 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 444 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 445 }; 446 447 // reject invalid branches even when the event bypasses patch creation 448 { 449 const cases = [_]struct { source: ?[]const u8, target: []const u8, expected_error: anyerror }{ 450 .{ .source = "master", .target = "master", .expected_error = error.SameBranch }, 451 .{ .source = null, .target = evt.events_ref.name, .expected_error = error.InvalidTargetBranch }, 452 }; 453 for (cases) |case| { 454 var invalid = patch; 455 invalid.source_branch = case.source; 456 invalid.target_branch = case.target; 457 try std.testing.expectError(case.expected_error, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 458 .id = std.fmt.bytesToHex(patch_id, .lower), 459 .timestamp = 3, 460 .author = author, 461 .event = .{ .patch = invalid }, 462 }})); 463 try std.testing.expectEqual(null, try target.readRef(io, evt.events_ref)); 464 } 465 } 466 467 // 468 // consume the patch revision and patch 469 // 470 471 { 472 try consumePatchWithRevision( 473 .repo, 474 io, 475 allocator, 476 &target, 477 evt.events_ref, 478 patchrev_id, 479 .{ .base_oid = &base_oid, .source_oid = &source_oid, .message = patch.title }, 480 &tree_entries, 481 3, 482 patch_id, 483 patch, 484 4, 485 ); 486 } 487 488 // 489 // check the records and squash commit 490 // 491 492 var arena = std.heap.ArenaAllocator.init(allocator); 493 defer arena.deinit(); 494 const moment = try evt.currentMoment(repo_opts, &target); 495 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 496 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 497 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 498 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 499 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 500 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 501 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 502 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 503 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 504 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 505 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 506 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 507 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 508 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 509 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 510 511 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 512 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 513 { 514 var repo_moment = try target.core.latestMoment(); 515 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 516 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 517 defer patch_object.deinit(); 518 switch (patch_object.content) { 519 .commit => |commit| { 520 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 521 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 522 try std.testing.expectEqual(1, parents.len); 523 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 524 }, 525 else => return error.InvalidObject, 526 } 527 } 528 529 // 530 // update metadata without creating another patch revision 531 // 532 533 var updated_patch = patch; 534 updated_patch.revision = .{ 535 .id = std.fmt.bytesToHex(patchrev_id, .lower), 536 .squash_oid = &initial_patch_oid, 537 .source_oid = &source_oid, 538 }; 539 updated_patch.title = "add a reusable answer"; 540 { 541 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 542 .id = std.fmt.bytesToHex(patch_id, .lower), 543 .timestamp = 5, 544 .author = author, 545 .event = .{ .patch = updated_patch }, 546 }}); 547 } 548 549 _ = arena.reset(.retain_capacity); 550 const updated_moment = try evt.currentMoment(repo_opts, &target); 551 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 552 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 553 554 // 555 // branch the events history before creating competing revisions 556 // 557 558 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 559 { 560 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 561 defer result.deinit(); 562 try target.addBranch(io, .{ .name = side_events_ref.name }); 563 } 564 565 // 566 // create two competing code revisions 567 // 568 569 const source_a_oid = blk: { 570 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 571 defer file.close(io); 572 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 573 try source.add(io, allocator, &.{"feature.zig"}); 574 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 575 }; 576 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 577 const source_b_oid = blk: { 578 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 579 defer file.close(io); 580 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 581 try source.add(io, allocator, &.{"feature.zig"}); 582 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 583 }; 584 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 585 { 586 var source_moment = try source.core.latestMoment(); 587 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 588 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 589 defer objects.deinit(); 590 try objects.include(&source_b_oid); 591 try target.copyObjects(.xit, repo_opts, &objects, io, null); 592 } 593 594 // 595 // select the first revision on the events branch 596 // 597 598 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 599 const target_entries = [_]evt.EventTreeEntry{ 600 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 601 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 602 }; 603 var target_patch = updated_patch; 604 target_patch.status = .closed; 605 { 606 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 607 .base_oid = &base_oid, 608 .source_oid = &source_a_oid, 609 .message = updated_patch.title, 610 }, &target_entries, 8, patch_id, target_patch, 9); 611 } 612 613 // 614 // select the second revision on the side branch 615 // 616 617 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 618 const parent_entries = [_]evt.EventTreeEntry{ 619 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 620 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 621 }; 622 var parent_patch = updated_patch; 623 parent_patch.status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(patchrev_b_id, .lower) } }; 624 { 625 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 626 .base_oid = &base_oid, 627 .source_oid = &source_b_oid, 628 .message = updated_patch.title, 629 }, &parent_entries, 10, patch_id, parent_patch, 11); 630 } 631 632 // 633 // merge the competing patch revisions 634 // 635 636 { 637 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 638 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 639 } 640 641 // 642 // check the selected revision and conflict 643 // 644 645 _ = arena.reset(.retain_capacity); 646 const merged_moment = try evt.currentMoment(repo_opts, &target); 647 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 648 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 649 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 650 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 651 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 652 653 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 654 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 655 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 656 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 657 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 658 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 659 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 660 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 661 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 662 663 // 664 // create a patch stacked on the selected revision 665 // 666 667 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 668 const child_id = evt.EventWithId.randomId(prng.random()); 669 const child_entries = [_]evt.EventTreeEntry{ 670 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 671 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 672 }; 673 { 674 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 675 .base_oid = merged_patchrev.patch_oid, 676 .source_oid = &source_b_oid, 677 .message = "stack another answer change", 678 }, &child_entries, 12, child_id, .{ 679 .title = "stack another answer change", 680 .description = "depends on the first patch", 681 .tags = "enhancement", 682 .target_branch = "master", 683 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 684 }, 13); 685 } 686 687 // 688 // check the squash commit and reverse index 689 // 690 691 _ = arena.reset(.retain_capacity); 692 const stacked_moment = try evt.currentMoment(repo_opts, &target); 693 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 694 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 695 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 696 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 697 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 698 @memcpy(&child_oid, child_patchrev.patch_oid); 699 { 700 var child_repo_moment = try target.core.latestMoment(); 701 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 702 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 703 defer child_object.deinit(); 704 const child_commit = switch (child_object.content) { 705 .commit => |commit| commit, 706 else => return error.InvalidObject, 707 }; 708 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 709 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 710 } 711 712 const targets_cursor = try stacked_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.target_patch_id_to_patch_id_set_key)) orelse return error.NotFound; 713 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 714 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 715 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 716 try std.testing.expectEqual(1, try children.count()); 717 718 // 719 // close and merge the same revision concurrently 720 // 721 722 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 723 { 724 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 725 defer result.deinit(); 726 try target.addBranch(io, .{ .name = status_side_ref.name }); 727 } 728 var closed_child = child.event; 729 closed_child.status = .closed; 730 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 731 .id = std.fmt.bytesToHex(child_id, .lower), 732 .timestamp = 14, 733 .author = author, 734 .event = .{ .patch = closed_child }, 735 }}); 736 var merged_child = child.event; 737 merged_child.status = .{ .merged = .{ .revision = .source, .patchrev_id = (child.event.revision orelse return error.NotFound).id } }; 738 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 739 .id = std.fmt.bytesToHex(child_id, .lower), 740 .timestamp = 15, 741 .author = author, 742 .event = .{ .patch = merged_child }, 743 }}); 744 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 745 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 746 747 _ = arena.reset(.retain_capacity); 748 const status_moment = try evt.currentMoment(repo_opts, &target); 749 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 750 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 751 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 752 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 753 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 754 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 755 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 756 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 757 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 758 try std.testing.expectEqual(null, child_conflict); 759 760 var reopened_child = merged_child_record.event; 761 reopened_child.status = .open; 762 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 763 .id = std.fmt.bytesToHex(child_id, .lower), 764 .timestamp = 16, 765 .author = author, 766 .event = .{ .patch = reopened_child }, 767 }})); 768 769 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 770 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 771 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 772 .timestamp = 16, 773 .author = author, 774 .event = .{ .patch = .{ 775 .title = "invalid merged patch", 776 .description = "has no revision", 777 .tags = "enhancement", 778 .target_branch = "master", 779 .status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(invalid_merged_id, .lower) } }, 780 } }, 781 }})); 782 783 // 784 // retain the ref-free commits during garbage collection 785 // 786 787 { 788 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 789 defer allocator.free(roots); 790 try std.testing.expectEqual(4, roots.len); 791 _ = try target.garbageCollect(io, allocator, .{ .extra_roots = roots }); 792 793 var after_gc_moment = try target.core.latestMoment(); 794 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 795 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 796 retained.deinit(); 797 } 798 } 799 800 test "patch lifecycle" { 801 try patchLifecycle(.squash); 802 try patchLifecycle(.source); 803 } 804 805 fn patchLifecycle(merge_revision: evt.Patch.MergeRevision) !void { 806 const io = std.testing.io; 807 const allocator = std.testing.allocator; 808 809 var temp = std.testing.tmpDir(.{}); 810 defer temp.cleanup(); 811 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 812 defer allocator.free(temp_path); 813 814 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 815 defer allocator.free(repos_dir); 816 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 817 defer allocator.free(admin_path); 818 const upstream_path = try std.fs.path.join(allocator, &.{ temp_path, "upstream" }); 819 defer allocator.free(upstream_path); 820 const source_path = try std.fs.path.join(allocator, &.{ temp_path, "source" }); 821 defer allocator.free(source_path); 822 823 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 824 const user_id = evt.EventWithId.randomId(prng.random()); 825 const repo_id = evt.EventWithId.randomId(prng.random()); 826 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 827 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 828 defer allocator.free(target_path); 829 830 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 831 defer admin.deinit(io, allocator); 832 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 833 .{ 834 .id = std.fmt.bytesToHex(user_id, .lower), 835 .timestamp = 1, 836 .author = author, 837 .event = .{ .user = .{ 838 .name = author.name, 839 .email = author.email, 840 .password_hash = "", 841 } }, 842 }, 843 .{ 844 .id = repo_id_hex, 845 .timestamp = 1, 846 .author = author, 847 .event = .{ .repo = .{ 848 .user_id = &user_id, 849 .name = "repo", 850 .description = "", 851 } }, 852 }, 853 }); 854 855 // 856 // create the target repo 857 // 858 859 { 860 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 861 defer upstream.deinit(io, allocator); 862 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 863 defer file.close(io); 864 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 865 try upstream.add(io, allocator, &.{"main.zig"}); 866 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 867 } 868 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 869 defer target.deinit(io, allocator); 870 871 // 872 // create the patch draft in the fork 873 // 874 875 const patch_id = evt.EventWithId.randomId(prng.random()); 876 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 877 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 878 .target_branch = "master", 879 .id = patch_id_hex, 880 .user_id = user_id, 881 .repo_id = repo_id, 882 .title = "add the answer", 883 .tags = "enhancement", 884 .description = "adds a reusable answer constant", 885 .author = author, 886 .timestamp = 2, 887 }); 888 defer allocator.free(draft_path); 889 890 // 891 // create and record the proposed revision in the fork 892 // 893 894 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 895 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 896 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 897 defer source.deinit(io, allocator); 898 const source_oid = blk: { 899 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 900 defer file.close(io); 901 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 902 try source.add(io, allocator, &.{"feature.zig"}); 903 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 904 }; 905 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 906 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 907 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 908 const first_entries = [_]evt.EventTreeEntry{ 909 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 910 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 911 }; 912 { 913 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 914 defer draft.deinit(io, allocator); 915 var arena = std.heap.ArenaAllocator.init(allocator); 916 defer arena.deinit(); 917 918 const initial_moment = try evt.currentMoment(repo_opts, &draft); 919 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 920 try std.testing.expect(!initial_patch.removed); 921 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 922 try std.testing.expectEqual(null, initial_patch.event.revision); 923 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 924 925 var source_moment = try source.core.latestMoment(); 926 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 927 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 928 defer objects.deinit(); 929 try objects.include(&source_oid); 930 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 931 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 932 .base_oid = &base_oid, 933 .source_oid = &source_oid, 934 .message = "add the answer", 935 }, &first_entries, 3, patch_id, .{ 936 .title = "add the answer", 937 .tags = "enhancement", 938 .description = "adds a reusable answer constant", 939 .target_branch = "master", 940 }, 3); 941 942 _ = arena.reset(.retain_capacity); 943 const moment = try evt.currentMoment(repo_opts, &draft); 944 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 945 try std.testing.expect(!patch.removed); 946 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 947 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 948 } 949 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 950 951 // draft mergeability checks leave the target repo untouched 952 const before_draft_check = (try target.core.latestMoment()).cursor.slot(); 953 pch.refreshMergeability(repo_opts, io, allocator, &target, patch_id); 954 try std.testing.expectEqualDeep(before_draft_check, (try target.core.latestMoment()).cursor.slot()); 955 956 // 957 // publish another draft before its first push 958 // 959 960 const unpushed_id = evt.EventWithId.randomId(prng.random()); 961 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 962 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 963 .id = unpushed_id_hex, 964 .user_id = user_id, 965 .repo_id = repo_id, 966 .title = "explain the answer", 967 .tags = "documentation", 968 .description = "describes the existing answer", 969 .target_branch = "master", 970 .author = author, 971 .timestamp = 3, 972 }); 973 defer allocator.free(unpushed_path); 974 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 975 .id = unpushed_id_hex, 976 .user_id = user_id, 977 .repo_id = repo_id, 978 .author = author, 979 .timestamp = 4, 980 }); 981 { 982 var verify_arena = std.heap.ArenaAllocator.init(allocator); 983 defer verify_arena.deinit(); 984 const target_moment = try evt.currentMoment(repo_opts, &target); 985 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 986 try std.testing.expectEqual(null, unpushed.event.revision); 987 988 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 989 defer unpushed_repo.deinit(io, allocator); 990 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 991 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 992 try std.testing.expect(removed.removed); 993 } 994 995 // 996 // edit the patch metadata in the fork 997 // 998 999 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 1000 .id = patch_id_hex, 1001 .user_id = user_id, 1002 .repo_id = repo_id, 1003 .title = "explain the answer", 1004 .tags = "enhancement documentation", 1005 .description = "adds and explains a reusable answer constant", 1006 .target_branch = "master", 1007 .author = author, 1008 .timestamp = 4, 1009 })); 1010 1011 // 1012 // publish the metadata and revision pointer 1013 // 1014 1015 const before_publish = try target.core.db.rootCursor().count(); 1016 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1017 .id = patch_id_hex, 1018 .user_id = user_id, 1019 .repo_id = repo_id, 1020 .author = author, 1021 .timestamp = 5, 1022 }); 1023 try std.testing.expectEqual(before_publish + 1, try target.core.db.rootCursor().count()); 1024 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 1025 .id = patch_id_hex, 1026 .user_id = user_id, 1027 .repo_id = repo_id, 1028 .author = author, 1029 .timestamp = 6, 1030 }); 1031 1032 var arena = std.heap.ArenaAllocator.init(allocator); 1033 defer arena.deinit(); 1034 const moment = try evt.currentMoment(repo_opts, &target); 1035 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1036 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1037 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1038 try std.testing.expectEqual(.open, patch.event.status); 1039 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1040 1041 _ = arena.reset(.retain_capacity); 1042 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 1043 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 1044 try std.testing.expect(!published_fork.removed); 1045 try std.testing.expectEqual(.publish, published_fork.event.stage); 1046 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 1047 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 1048 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 1049 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1050 { 1051 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1052 defer draft.deinit(io, allocator); 1053 _ = arena.reset(.retain_capacity); 1054 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1055 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1056 try std.testing.expect(removed.removed); 1057 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1058 @memcpy(&squash_oid, revision.patch_oid); 1059 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1060 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1061 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1062 @memcpy(&event_oid, revision.event_oid); 1063 var repo_moment = try draft.core.latestMoment(); 1064 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1065 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1066 defer event_object.deinit(); 1067 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1068 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1069 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1070 } 1071 1072 // 1073 // merge the selected revision into the target 1074 // 1075 1076 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1077 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 1078 .id = patch_id_hex, 1079 .revision = merge_revision, 1080 .author = author, 1081 .timestamp = 5, 1082 })); 1083 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1084 1085 // run the cache scenario once; both lifecycle runs still perform a merge 1086 if (merge_revision == .source) { 1087 try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); 1088 } 1089 1090 const selected_oid = switch (merge_revision) { 1091 .squash => squash_oid, 1092 .source => source_oid, 1093 }; 1094 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 1095 .id = patch_id_hex, 1096 .revision = merge_revision, 1097 .author = author, 1098 .timestamp = 5, 1099 }); 1100 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1101 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1102 1103 _ = arena.reset(.retain_capacity); 1104 const merged_moment = try evt.currentMoment(repo_opts, &target); 1105 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1106 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1107 const merged_status = switch (merged.event.status) { 1108 .merged => |value| value, 1109 else => return error.InvalidPatch, 1110 }; 1111 try std.testing.expectEqual(merge_revision, merged_status.revision); 1112 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1113 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1114 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1115 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1116 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1117 1118 _ = arena.reset(.retain_capacity); 1119 const removed_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, try evt.currentMoment(evt.admin_repo_opts, &admin), &arena, &patch_id)) orelse return error.NotFound; 1120 try std.testing.expect(removed_fork.removed); 1121 } 1122 1123 fn indexedForkCount( 1124 moment: evt.AdminDB.HashMap(.read_only), 1125 index_key: []const u8, 1126 parent_id: []const u8, 1127 ) !u64 { 1128 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 1129 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 1130 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 1131 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 1132 return try ids.count(); 1133 } 1134 1135 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1136 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1137 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1138 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1139 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1140 return try ids.count(); 1141 } 1142 1143 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.StatusKind) !u64 { 1144 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 1145 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 1146 var key_buffer: evt.Patch.TagStatusKey = undefined; 1147 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 1148 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1149 return try ids.count(); 1150 } 1151 1152 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1153 var arena = std.heap.ArenaAllocator.init(allocator); 1154 defer arena.deinit(); 1155 var moment = try repo.core.latestMoment(); 1156 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1157 } 1158 1159 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1160 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1161 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1162 const checks = try Repo.DB.HashMap(.read_only).init((try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))) orelse return error.NotFound); 1163 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1164 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1165 for ([_][]const u8{ "source", "squash" }) |field| { 1166 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1167 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1168 } 1169 } 1170 1171 fn testMergeability( 1172 target: *Repo, 1173 io: std.Io, 1174 allocator: std.mem.Allocator, 1175 repos_dir: []const u8, 1176 id: *const [evt.event_id_size]u8, 1177 base: *const [hash.hexLen(repo_opts.hash)]u8, 1178 source: *const [hash.hexLen(repo_opts.hash)]u8, 1179 ) !void { 1180 var arena = std.heap.ArenaAllocator.init(allocator); 1181 defer arena.deinit(); 1182 const events = try evt.currentMoment(repo_opts, target); 1183 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1184 const patch = record.event; 1185 1186 // publication checks both methods, and reads and cache hits do no work 1187 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1188 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1189 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1190 const before = (try target.core.latestMoment()).cursor.slot(); 1191 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1192 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1193 { 1194 var moment = try target.core.latestMoment(); 1195 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1196 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1197 } 1198 1199 // a target update invalidates the old result before refresh 1200 { 1201 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1202 defer file.close(io); 1203 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1204 } 1205 try target.add(io, allocator, &.{"feature.zig"}); 1206 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1207 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1208 var squash_input = input; 1209 squash_input.revision = .squash; 1210 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1211 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1212 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1213 1214 // local edits and server comments leave checks alone 1215 var edit = evt.Patch.Update{ .fields = .{ 1216 .title = "edit the answer", 1217 .tags = patch.tags, 1218 .description = patch.description, 1219 .target_branch = patch.target_branch, 1220 } }; 1221 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1222 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1223 _ = try evt.Comment.create(.server, .xit, repo_opts, io, allocator, target, &input.id, &input.id, "reviewing the answer", author); 1224 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1225 1226 // a server patch edit refreshes its mergeability 1227 edit.fields.title = patch.title; 1228 const before_edit = try target.core.db.rootCursor().count(); 1229 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, target, id, edit, author); 1230 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1231 const events_before = try target.readRef(io, evt.events_ref); 1232 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1233 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1234 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1235 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); 1236 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1237 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1238 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1239 1240 // deleting and restoring the target cannot reuse the conflict result 1241 { 1242 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1243 defer detached.deinit(); 1244 } 1245 try target.removeBranch(io, .{ .name = "master" }); 1246 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1247 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1248 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1249 try target.addBranch(io, .{ .name = "master" }); 1250 { 1251 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1252 defer restored.deinit(); 1253 } 1254 pch.refreshMergeability(repo_opts, io, allocator, target, null); 1255 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1256 1257 // a missing fork disables merging without changing the accepted events 1258 const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); 1259 defer allocator.free(draft_path); 1260 const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); 1261 defer allocator.free(away_path); 1262 { 1263 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1264 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1265 { 1266 const log_level = std.testing.log_level; 1267 std.testing.log_level = .err; 1268 defer std.testing.log_level = log_level; 1269 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); 1270 } 1271 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1272 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1273 } 1274 pch.refreshMergeability(repo_opts, io, allocator, target, id.*); 1275 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1276 1277 // a changed revision is unknown even if its target has not moved 1278 var changed = patch; 1279 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1280 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1281 } 1282