╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
530 .target_branch = "master", 531 .author = .{ .name = "admin", .email = "admin@example.test" }, 532 .timestamp = 1, 533 }); 534 }; 535 defer allocator.free(draft_path); 536 537 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 538 defer allocator.free(client_path); 539 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 540 defer allocator.free(ssh_cmd); 541 var client = try rp.Repo(.xit, .{ .is_test = true }).clone( 542 io, 543 allocator, 544 "git@localhost:admin/target", 545 client_path, 546 client_path, 547 null, 548 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 549 ); 550 defer client.deinit(io, allocator); 551 552 const source_oid = blk: { 553 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 554 defer file.close(io); 555 try file.writeStreamingAll(io, "fork contents\n"); 556 try client.add(io, allocator, &.{"feature.txt"}); 557 break :blk try client.commit(io, allocator, .{ .message = "add feature" }); 558 }; 559 560 const remote_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 561 defer allocator.free(remote_url); 562 try client.addRemote(io, allocator, .{ .name = "patch", .value = remote_url }); 563 try client.push(io, allocator, "patch", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 564 565 var first_revision_id: [evt.event_id_size]u8 = undefined; 566 { 567 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 568 defer draft.deinit(io, allocator); 569 try std.testing.expect(try draft.isBare(io, allocator)); 570 var arena = std.heap.ArenaAllocator.init(allocator); 571 defer arena.deinit(); 572 const moment = try evt.currentMoment(.{}, &draft); 573 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 574 try std.testing.expect(!patch.removed); 575 try std.testing.expectEqualStrings(&source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 576 const selected = patch.event.revision orelse return error.NotFound; 577 try std.testing.expectEqualStrings("master", patch.event.target_branch); 578 try std.testing.expectEqualStrings(&source_oid, selected.source_oid); 579 first_revision_id = try evt.parseEventId(&selected.id); 580 const revision = (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &first_revision_id)) orelse return error.NotFound; 581 try std.testing.expectEqualStrings(&source_oid, revision.event.source_oid); 582 } 583 584 // 585 // publish, then push another revision 586 // 587 588 { 589 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 590 defer admin.deinit(io, allocator); 591 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 592 defer target.deinit(io, allocator); 593 try target.addBranch(io, .{ .name = "feature" }); 594 try pch.publish(.{}, io, allocator, &admin, &target, draft_path, .{ 595 .id = fork_id_hex, 596 .user_id = user_id, 597 .repo_id = repo_id, 598 .author = .{ .name = "admin", .email = "admin@example.test" }, 599 .timestamp = 2, 600 }); 601 var arena = std.heap.ArenaAllocator.init(allocator); 602 defer arena.deinit(); 603 const moment = try evt.currentMoment(.{}, &target); 604 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 605 try evt.Patch.update(.xit, .{}, io, allocator, &target, &fork_id, .{ .fields = .{ 606 .title = "edited feature", 607 .description = patch.event.description, 608 .tags = patch.event.tags, 609 .target_branch = "feature", 610 } }, .{ .name = "admin", .email = "admin@example.test" }); 611 } 612 613 const feature_remote_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 614 defer allocator.free(feature_remote_url); 615 try client.addRemote(io, allocator, .{ .name = "patch-to-feature", .value = feature_remote_url }); 616 617 const second_source_oid = blk: { 618 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 619 defer file.close(io); 620 try file.writeStreamingAll(io, "updated fork contents\n"); 621 try client.add(io, allocator, &.{"feature.txt"}); 622 break :blk try client.commit(io, allocator, .{ .message = "revise feature" }); 623 }; 624 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 625 626 var second_revision_id: [evt.event_id_size]u8 = undefined; 627 var second_patch_oid: [hash.hexLen(.sha1)]u8 = undefined; 628 { 629 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 630 defer draft.deinit(io, allocator); 631 var arena = std.heap.ArenaAllocator.init(allocator); 632 defer arena.deinit(); 633 const moment = try evt.currentMoment(.{}, &draft); 634 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 635 try std.testing.expect(patch.removed); 636 const newest = (try evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, &arena)) orelse return error.NotFound; 637 second_revision_id = newest.id; 638 @memcpy(&second_patch_oid, newest.record.patch_oid); 639 try std.testing.expect(!std.mem.eql(u8, &first_revision_id, &second_revision_id)); 640 try std.testing.expectEqualStrings(&second_source_oid, newest.record.event.source_oid); 641 try std.testing.expectEqualStrings("edited feature", newest.record.event.message); 642 } 643 { 644 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 645 defer target.deinit(io, allocator); 646 var arena = std.heap.ArenaAllocator.init(allocator); 647 defer arena.deinit(); 648 const moment = try evt.currentMoment(.{}, &target); 649 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 650 try std.testing.expectEqualStrings("edited feature", patch.event.title); 651 const selected = patch.event.revision orelse return error.NotFound; 652 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(second_revision_id, .lower), &selected.id); 653 try std.testing.expectEqualStrings(&second_source_oid, selected.source_oid); 654 try std.testing.expectEqualStrings("feature", patch.event.target_branch); 655 } 656 657 // 658 // reject extra refs without applying the patch update 659 // 660 661 const third_source_oid = blk: { 662 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 663 defer file.close(io); 664 try file.writeStreamingAll(io, "rejected fork contents\n"); 665 try client.add(io, allocator, &.{"feature.txt"}); 666 break :blk try client.commit(io, allocator, .{ .message = "rejected revision" }); 667 }; 668 const extra_ref_rejected = if (client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ 669 .refspecs = &.{"refs/heads/master:refs/heads/extra"}, 670 .wire = .{ .ssh = .{ .command = ssh_cmd } }, 671 })) |_| false else |_| true; 672 try std.testing.expect(extra_ref_rejected); 673 { 674 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 675 defer draft.deinit(io, allocator); 676 try std.testing.expectEqualStrings(&second_source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 677 try std.testing.expectEqual(null, try draft.readRef(io, .{ .kind = .head, .name = "extra" })); 678 var arena = std.heap.ArenaAllocator.init(allocator); 679 defer arena.deinit(); 680 const moment = try evt.currentMoment(.{}, &draft); 681 const newest = (try evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, &arena)) orelse return error.NotFound; 682 try std.testing.expectEqualSlices(u8, &second_revision_id, &newest.id); 683 } 684 685 // 686 // a removed target patch stays removed after another push 687 // 688 689 { 690 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 691 defer target.deinit(io, allocator); 692 try evt.remove(.repo, .xit, .{}, io, allocator, &target, &fork_id, .patch, .{ .name = "admin", .email = "admin@example.test" }); 693 } 694 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 695 { 696 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 697 defer target.deinit(io, allocator); 698 var arena = std.heap.ArenaAllocator.init(allocator); 699 defer arena.deinit(); 700 const moment = try evt.currentMoment(.{}, &target); 701 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 702 try std.testing.expect(patch.removed); 703 } 704 705 // 706 // restore the patch, merge its source oid, and push the merge 707 // 708 709 { 710 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 711 defer target.deinit(io, allocator); 712 var arena = std.heap.ArenaAllocator.init(allocator); 713 defer arena.deinit(); 714 const moment = try evt.currentMoment(.{}, &target); 715 const removed = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 716 try evt.consume(.repo, .xit, .{}, io, allocator, &target, evt.events_ref, &.{.{ 717 .id = fork_id_hex, 718 .timestamp = 4, 719 .author = .{ .name = "admin", .email = "admin@example.test" }, 720 .event = .{ .patch = removed.event }, 721 }}); 722 } 723 const merge_parents = [_][hash.hexLen(.sha1)]u8{ third_source_oid, second_source_oid }; 724 _ = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 725 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 726 { 727 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 728 defer target.deinit(io, allocator); 729 var arena = std.heap.ArenaAllocator.init(allocator); 730 defer arena.deinit(); 731 const moment = try evt.currentMoment(.{}, &target); 732 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 733 try std.testing.expectEqual(.merged, patch.event.status.kind()); 734 const merged_oid = switch (patch.event.status) { 735 .merged => |oid| oid, 736 else => return error.InvalidPatch, 737 }; 738 try std.testing.expectEqualStrings(&second_source_oid, merged_oid); 739 const imported = (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &second_revision_id)) orelse return error.NotFound; 740 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 741 } 742 743 const other_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/other/patch:{s}", .{&fork_id_hex}); 744 defer allocator.free(other_url); 745 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 746 const wrong_repo_rejected = if (client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } })) |_| false else |_| true; 747 try std.testing.expect(wrong_repo_rejected); 748 749 // 750 // the fork remains cloneable after the target repo is removed 751 // 752 753 { 754 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 755 defer admin.deinit(io, allocator); 756 try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 757 } 758 const clone_path = try std.fs.path.join(allocator, &.{ temp_path, "fork-clone" }); 759 defer allocator.free(clone_path); 760 var fork_clone = try rp.Repo(.xit, .{ .is_test = true }).clone( 761 io, 762 allocator, 763 remote_url, 764 clone_path, 765 clone_path, 766 null, 767 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 768 ); 769 defer fork_clone.deinit(io, allocator); 770 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 771 772 { 773 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 774 defer admin.deinit(io, allocator); 775 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 776 .{ 777 .id = std.fmt.bytesToHex(repo_id, .lower), 778 .timestamp = 2, 779 .author = .{ .name = "admin", .email = "admin@example.test" }, 780 .event = .{ .repo = .{ 781 .user_id = &user_id, 782 .name = "target", 783 .description = "", 784 } }, 785 }, 786 .{ 787 .id = std.fmt.bytesToHex(user_id, .lower), 788 .timestamp = 2, 789 .author = .{ .name = "admin", .email = "admin@example.test" }, 790 .event = .{ .user = .{ 791 .name = "admin", 792 .email = "admin@example.test", 793 .password_hash = "", 794 .ssh_keys = "", 795 } }, 796 }, 797 }); 798 } 799 800 const denied_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 801 defer allocator.free(denied_url); 802 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 803 const unauthorized_rejected = if (client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } })) |_| false else |_| true; 804 try std.testing.expect(unauthorized_rejected); 805 } 806 807 fn testPushCreatesMissingRepo( 808 comptime repo_kind: rp.RepoKind, 809 comptime transport_def: net.TransportDefinition, 810 comptime port: u16, 811 io: std.Io, 812 allocator: std.mem.Allocator, 813 ) !void { 814 var temp = std.testing.tmpDir(.{}); 815 defer temp.cleanup(); 816 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 817 defer allocator.free(temp_path); 818 819 var server_process = try runServer(io, allocator, temp_path, port); 820 defer _ = server_process.kill(io); 821 822 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 823 defer allocator.free(client_path); 824 825 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 826 defer client_repo.deinit(io, allocator); 827 828 const commit1 = blk: { 829 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 830 defer hello_txt.close(io); 831 try hello_txt.writeStreamingAll(io, "hello, world!"); 832 try client_repo.add(io, allocator, &.{"hello.txt"}); 833 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 834 }; 835 836 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 837 838 // the repo is not pre-registered; the push should mint the repo event under 839 // admin and create the repo on disk 840 { 841 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 842 defer allocator.free(remote_url); 843 844 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 845 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 846 } 847 848 const is_ssh = switch (transport_def) { 849 .file => false, 850 .wire => |wire_kind| .ssh == wire_kind, 851 }; 852 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 853 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 854 855 try client_repo.push( 856 io, 857 allocator, 858 "origin", 859 "master", 860 false, 861 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 862 .command = ssh_cmd_maybe, 863 } } }, 864 ); 865 866 // the push registered admin/server; resolve its on-disk directory 867 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", false)).?; 868 defer allocator.free(server_path); 869 870 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 871 defer server_repo.deinit(io, allocator); 872 try std.testing.expect(try server_repo.isBare(io, allocator)); 873 874 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 875 876 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 877 try std.testing.expectEqualStrings(&commit1, &oid_master); 878 } 879 880 fn testPushEvents( 881 comptime repo_kind: rp.RepoKind, 882 comptime transport_def: net.TransportDefinition, 883 comptime port: u16, 884 io: std.Io, 885 allocator: std.mem.Allocator, 886 ) !void { 887 // create the temp dir 888 var temp = std.testing.tmpDir(.{}); 889 defer temp.cleanup(); 890 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 891 defer allocator.free(temp_path); 892 893 // init server 894 var server_process = try runServer(io, allocator, temp_path, port); 895 defer _ = server_process.kill(io); 896 897 // register the repo under admin and locate its on-disk directory 898 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 899 defer allocator.free(server_path); 900 901 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 902 const ServerRepo = rp.Repo(.xit, repo_opts); 903 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 904 defer server_repo.deinit(io, allocator); 905 906 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 907 defer allocator.free(client_path); 908 909 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 910 defer client_repo.deinit(io, allocator); 911 912 // add remote 913 { 914 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 915 defer allocator.free(remote_url); 916 917 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 918 } 919 920 const is_ssh = switch (transport_def) { 921 .file => false, 922 .wire => |wire_kind| .ssh == wire_kind, 923 }; 924 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 925 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 926 927 var arena = std.heap.ArenaAllocator.init(allocator); 928 defer arena.deinit(); 929 930 // 931 // define test events 932 // 933 934 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 935 const issue_event_id = evt.EventWithId.randomId(prng.random()); 936 937 const events_to_push = [_]evt.EventWithId{ 938 .{ 939 .id = std.fmt.bytesToHex(issue_event_id, .lower), 940 .author = .{ .name = "haxy", .email = "user@haxy" }, 941 .event = .{ 942 .issue = .{ 943 .title = "Login form clears password on validation error", 944 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 945 .tags = "bug priority-high ui", 946 }, 947 }, 948 }, 949 }; 950 951 // commit the issue on the client and push it to the server 952 try evt.consume(.repo, repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 953 try client_repo.push( 954 io, 955 allocator, 956 "origin", 957 "haxy/events", 958 false, 959 .{ .wire = .{ .ssh = .{ 960 .command = ssh_cmd_maybe, 961 } } }, 962 ); 963 964 // the push consumed the issue into the server repo's db 965 { 966 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 967 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 968 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 969 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 970 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 971 972 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 973 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.event.tags); 974 } 975 976 // 977 // edit the issue 978 // 979 980 // this event edits the previous one because it has the same id 981 const events_to_push2 = [_]evt.EventWithId{ 982 .{ 983 .id = std.fmt.bytesToHex(issue_event_id, .lower), 984 .author = .{ .name = "haxy", .email = "user@haxy" }, 985 .event = .{ 986 .issue = .{ 987 .title = "Login form clears password on validation error", 988 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 989 .tags = "bug priority-low ui", 990 }, 991 }, 992 }, 993 }; 994 995 // commit the edit on the client and push it to the server 996 try evt.consume(.repo, repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 997 try client_repo.push( 998 io, 999 allocator, 1000 "origin", 1001 "haxy/events", 1002 false, 1003 .{ .wire = .{ .ssh = .{ 1004 .command = ssh_cmd_maybe, 1005 } } }, 1006 ); 1007 1008 // the push consumed the edit into the server repo's db 1009 { 1010 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1011 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1012 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1013 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1014 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1015 1016 // the description and tags were correctly edited 1017 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1018 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.event.tags); 1019 } 1020 } 1021 1022 fn testClone( 1023 comptime repo_kind: rp.RepoKind, 1024 comptime transport_def: net.TransportDefinition, 1025 comptime shell_out_to_git: bool, 1026 comptime port: u16, 1027 io: std.Io, 1028 allocator: std.mem.Allocator, 1029 ) !void { 1030 // create the temp dir 1031 var temp = std.testing.tmpDir(.{}); 1032 defer temp.cleanup(); 1033 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1034 defer allocator.free(temp_path); 1035 1036 // init server 1037 var server_process = try runServer(io, allocator, temp_path, port); 1038 defer _ = server_process.kill(io); 1039 1040 // register the repo under admin and locate its on-disk directory 1041 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1042 defer allocator.free(server_path); 1043 1044 // init server repo with default branch name as main 1045 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1046 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .bare = true, .path = server_path, .create_default_branch = "main" }); 1047 defer server_repo.deinit(io, allocator); 1048 1049 if (shell_out_to_git) { 1050 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1051 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1052 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1053 } 1054 1055 // make a commit 1056 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1057 1058 // tag first commit 1059 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1060 1061 // make a commit 1062 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1063 1064 // export server repo 1065 { 1066 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1067 defer export_file.close(io); 1068 } 1069 1070 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1071 defer allocator.free(client_path); 1072 1073 // get remote url 1074 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1075 defer allocator.free(remote_url); 1076 1077 const is_ssh = switch (transport_def) { 1078 .file => false, 1079 .wire => |wire_kind| .ssh == wire_kind, 1080 }; 1081 1082 if (shell_out_to_git) { 1083 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1084 defer allocator.free(priv_key_path); 1085 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1086 defer allocator.free(ssh_config_arg); 1087 1088 { 1089 var process = try std.process.spawn(io, .{ 1090 .argv = if (is_ssh) 1091 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 1092 else 1093 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1094 .cwd = .{ .path = temp_path }, 1095 .stdin = .ignore, 1096 .stdout = .ignore, 1097 .stderr = .ignore, 1098 }); 1099 const term = try process.wait(io); 1100 if (term != .exited or term.exited != 0) { 1101 return error.GitCommandFailed; 1102 } 1103 } 1104 1105 // make sure shallow clone was successful 1106 { 1107 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1108 hello_txt.close(io); 1109 } 1110 1111 // make a third commit on the server 1112 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1113 1114 // pull --unshallow to deepen the clone and get the new commit 1115 { 1116 var process = try std.process.spawn(io, .{ 1117 .argv = if (is_ssh) 1118 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 1119 else 1120 &.{ "git", "pull", "--unshallow" }, 1121 .cwd = .{ .path = client_path }, 1122 .stdin = .ignore, 1123 .stdout = .ignore, 1124 .stderr = .ignore, 1125 }); 1126 const term = try process.wait(io); 1127 if (term != .exited or term.exited != 0) { 1128 return error.GitCommandFailed; 1129 } 1130 } 1131 1132 // make sure unshallow pull was successful 1133 { 1134 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1135 extra_txt.close(io); 1136 } 1137 1138 // delete client and clone again with --shallow-since 1139 try temp.dir.deleteTree(io, "client"); 1140 1141 { 1142 var process = try std.process.spawn(io, .{ 1143 .argv = if (is_ssh) 1144 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 1145 else 1146 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1147 .cwd = .{ .path = temp_path }, 1148 .stdin = .ignore, 1149 .stdout = .ignore, 1150 .stderr = .ignore, 1151 }); 1152 const term = try process.wait(io); 1153 if (term != .exited or term.exited != 0) { 1154 return error.GitCommandFailed; 1155 } 1156 } 1157 1158 // make sure shallow clone was successful 1159 { 1160 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1161 hello_txt.close(io); 1162 } 1163 1164 // delete client and clone again with --shallow-exclude 1165 try temp.dir.deleteTree(io, "client"); 1166 1167 { 1168 var process = try std.process.spawn(io, .{ 1169 .argv = if (is_ssh) 1170 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 1171 else 1172 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1173 .cwd = .{ .path = temp_path }, 1174 .stdin = .ignore, 1175 .stdout = .ignore, 1176 .stderr = .ignore, 1177 }); 1178 const term = try process.wait(io); 1179 if (term != .exited or term.exited != 0) { 1180 return error.GitCommandFailed; 1181 } 1182 } 1183 1184 // make sure shallow clone was successful 1185 { 1186 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1187 hello_txt.close(io); 1188 } 1189 1190 // delete client and clone again with --filter=blob:none 1191 try temp.dir.deleteTree(io, "client"); 1192 1193 { 1194 var process = try std.process.spawn(io, .{ 1195 .argv = if (is_ssh) 1196 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 1197 else 1198 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1199 .cwd = .{ .path = temp_path }, 1200 .stdin = .ignore, 1201 .stdout = .ignore, 1202 .stderr = .ignore, 1203 }); 1204 const term = try process.wait(io); 1205 if (term != .exited or term.exited != 0) { 1206 return error.GitCommandFailed; 1207 } 1208 } 1209 1210 // make sure partial clone was successful 1211 { 1212 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1213 hello_txt.close(io); 1214 } 1215 1216 // delete client and clone again with --filter=tree:0 1217 try temp.dir.deleteTree(io, "client"); 1218 1219 { 1220 var process = try std.process.spawn(io, .{ 1221 .argv = if (is_ssh) 1222 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 1223 else 1224 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1225 .cwd = .{ .path = temp_path }, 1226 .stdin = .ignore, 1227 .stdout = .ignore, 1228 .stderr = .ignore, 1229 }); 1230 const term = try process.wait(io); 1231 if (term != .exited or term.exited != 0) { 1232 return error.GitCommandFailed; 1233 } 1234 } 1235 1236 // make sure treeless clone was successful 1237 { 1238 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1239 goodbye_txt.close(io); 1240 } 1241 } else { 1242 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1243 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1244 1245 // clone repo 1246 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1247 io, 1248 allocator, 1249 remote_url, 1250 temp_path, 1251 client_path, 1252 null, 1253 .{ .transport = .{ .wire = .{ .ssh = .{ 1254 .command = ssh_cmd_maybe, 1255 } } } }, 1256 ); 1257 defer client_repo.deinit(io, allocator); 1258 1259 // make sure HEAD points to the right default branch 1260 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1261 const head = try client_repo.head(io, &current_branch_buffer); 1262 try std.testing.expectEqualStrings("main", head.ref.name); 1263 1264 // make sure clone was successful 1265 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1266 defer hello_txt.close(io); 1267 } 1268 } 1269 1270 fn testFetchLarge( 1271 comptime repo_kind: rp.RepoKind, 1272 comptime transport_def: net.TransportDefinition, 1273 comptime shell_out_to_git: bool, 1274 comptime port: u16, 1275 io: std.Io, 1276 allocator: std.mem.Allocator, 1277 ) !void { 1278 // create the temp dir 1279 var temp = std.testing.tmpDir(.{}); 1280 defer temp.cleanup(); 1281 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1282 defer allocator.free(temp_path); 1283 1284 // init server 1285 var server_process = try runServer(io, allocator, temp_path, port); 1286 defer _ = server_process.kill(io); 1287 1288 // register the repo under admin and locate its on-disk directory 1289 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1290 defer allocator.free(server_path); 1291 1292 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1293 defer server_repo.deinit(io, allocator); 1294 1295 // make a commit with files from the current repo 1296 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1297 1298 // export server repo 1299 { 1300 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1301 defer export_file.close(io); 1302 } 1303 1304 if (shell_out_to_git) { 1305 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1306 } 1307 1308 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1309 defer allocator.free(client_path); 1310 1311 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1312 defer client_repo.deinit(io, allocator); 1313 1314 // add remote 1315 { 1316 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1317 defer allocator.free(remote_url); 1318 1319 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1320 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1321 } 1322 1323 const is_ssh = switch (transport_def) { 1324 .file => false, 1325 .wire => |wire_kind| .ssh == wire_kind, 1326 }; 1327 1328 if (shell_out_to_git) { 1329 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1330 defer allocator.free(priv_key_path); 1331 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1332 defer allocator.free(ssh_config_arg); 1333 1334 { 1335 var process = try std.process.spawn(io, .{ 1336 .argv = if (is_ssh) 1337 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1338 else 1339 &.{ "git", "pull", "origin", "master" }, 1340 .cwd = .{ .path = client_path }, 1341 .stdin = .ignore, 1342 .stdout = .ignore, 1343 .stderr = .ignore, 1344 }); 1345 const term = try process.wait(io); 1346 if (term != .exited or term.exited != 0) { 1347 return error.GitCommandFailed; 1348 } 1349 } 1350 1351 // make sure pull was successful 1352 { 1353 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1354 try std.testing.expectEqualStrings(&commit1, &oid_master); 1355 } 1356 1357 // make another commit on the server 1358 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1359 1360 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1361 { 1362 var process = try std.process.spawn(io, .{ 1363 .argv = if (is_ssh) 1364 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1365 else 1366 &.{ "git", "fetch", "origin", "master" }, 1367 .cwd = .{ .path = client_path }, 1368 .stdin = .ignore, 1369 .stdout = .ignore, 1370 .stderr = .ignore, 1371 }); 1372 const term = try process.wait(io); 1373 if (term != .exited or term.exited != 0) { 1374 return error.GitCommandFailed; 1375 } 1376 } 1377 1378 // make sure fetch with want-ref was successful 1379 { 1380 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1381 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1382 } 1383 } else { 1384 const refspecs = &.{ 1385 "+refs/heads/master:refs/heads/master", 1386 }; 1387 1388 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1389 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1390 1391 try client_repo.fetch( 1392 io, 1393 allocator, 1394 "origin", 1395 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1396 .command = ssh_cmd_maybe, 1397 } } }, 1398 ); 1399 1400 // update the working dir 1401 try client_repo.restore(io, allocator, "."); 1402 } 1403 1404 // make sure fetch was successful 1405 { 1406 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1407 try std.testing.expectEqualStrings(&commit1, &oid_master); 1408 } 1409 } 1410 1411 fn testPushLarge( 1412 comptime repo_kind: rp.RepoKind, 1413 comptime transport_def: net.TransportDefinition, 1414 comptime shell_out_to_git: bool, 1415 comptime port: u16, 1416 io: std.Io, 1417 allocator: std.mem.Allocator, 1418 ) !void { 1419 // create the temp dir 1420 var temp = std.testing.tmpDir(.{}); 1421 defer temp.cleanup(); 1422 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1423 defer allocator.free(temp_path); 1424 1425 // init server 1426 var server_process = try runServer(io, allocator, temp_path, port); 1427 defer _ = server_process.kill(io); 1428 1429 // register the repo under admin and locate its on-disk directory 1430 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1431 defer allocator.free(server_path); 1432 1433 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1434 defer server_repo.deinit(io, allocator); 1435 1436 // add config 1437 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1438 1439 // export server repo 1440 { 1441 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1442 defer export_file.close(io); 1443 } 1444 1445 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1446 defer allocator.free(client_path); 1447 1448 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1449 defer client_repo.deinit(io, allocator); 1450 1451 var client_dir = try temp.dir.openDir(io, "client", .{}); 1452 defer client_dir.close(io); 1453 1454 { 1455 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1456 defer hello_txt.close(io); 1457 try hello_txt.writeStreamingAll(io, "hello, world!"); 1458 try client_repo.add(io, allocator, &.{"hello.txt"}); 1459 } 1460 1461 // copy files from current repo into client dir 1462 const cwd = std.Io.Dir.cwd(); 1463 for (&[_][]const u8{"src"}) |dir_name| { 1464 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1465 defer src_repo_dir.close(io); 1466 1467 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1468 defer dest_repo_dir.close(io); 1469 1470 try copyDir(io, src_repo_dir, dest_repo_dir); 1471 1472 try client_repo.add(io, allocator, &.{dir_name}); 1473 } 1474 1475 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1476 1477 // change the files so git will send them as delta objects 1478 for (&[_][]const u8{"src"}) |dir_name| { 1479 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1480 defer dest_repo_dir.close(io); 1481 1482 { 1483 var iter = dest_repo_dir.iterate(); 1484 while (try iter.next(io)) |entry| { 1485 switch (entry.kind) { 1486 .file => { 1487 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1488 defer file.close(io); 1489 var writer = file.writer(io, &.{}); 1490 try writer.interface.writeAll("EDIT"); 1491 }, 1492 else => {}, 1493 } 1494 } 1495 } 1496 1497 try client_repo.add(io, allocator, &.{dir_name}); 1498 } 1499 1500 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1501 1502 // add remote 1503 { 1504 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1505 defer allocator.free(remote_url); 1506 1507 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1508 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1509 } 1510 1511 const is_ssh = switch (transport_def) { 1512 .file => false, 1513 .wire => |wire_kind| .ssh == wire_kind, 1514 }; 1515 1516 if (shell_out_to_git) { 1517 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1518 defer allocator.free(priv_key_path); 1519 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1520 defer allocator.free(ssh_config_arg); 1521 1522 // shell out to git so it will send delta objects 1523 var process = try std.process.spawn(io, .{ 1524 .argv = if (is_ssh) 1525 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1526 else 1527 &.{ "git", "push", "origin", "master" }, 1528 .cwd = .{ .path = client_path }, 1529 .stdin = .ignore, 1530 .stdout = .ignore, 1531 .stderr = .ignore, 1532 }); 1533 const term = try process.wait(io); 1534 if (term != .exited or term.exited != 0) { 1535 return error.GitCommandFailed; 1536 } 1537 } else { 1538 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1539 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1540 1541 try client_repo.push( 1542 io, 1543 allocator, 1544 "origin", 1545 "master", 1546 false, 1547 .{ .wire = .{ .ssh = .{ 1548 .command = ssh_cmd_maybe, 1549 } } }, 1550 ); 1551 } 1552 1553 // make sure push was successful 1554 { 1555 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1556 try std.testing.expectEqualStrings(&commit2, &oid_master); 1557 1558 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1559 } 1560 } 1561 1562 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1563 var iter = src_dir.iterate(); 1564 while (try iter.next(io)) |entry| { 1565 switch (entry.kind) { 1566 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1567 .directory => { 1568 try dest_dir.createDirPath(io, entry.name); 1569 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1570 defer dest_entry_dir.close(io); 1571 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1572 defer src_entry_dir.close(io); 1573 try copyDir(io, src_entry_dir, dest_entry_dir); 1574 }, 1575 else => {}, 1576 } 1577 } 1578 } 1579 1580 // the dev SSH public key matching the private key written by runServer 1581 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1582 1583 // create the admin event repo with a user holding the dev SSH public key 1584 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1585 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1586 defer allocator.free(admin_repo_path); 1587 1588 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1589 defer repo.deinit(io, allocator); 1590 1591 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1592 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1593 1594 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1595 const user_id = evt.EventWithId.randomId(prng.random()); 1596 1597 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1598 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1599 1600 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1601 .id = std.fmt.bytesToHex(user_id, .lower), 1602 .author = .{ .name = "admin", .email = "admin@example.test" }, 1603 .event = .{ .user = .{ 1604 .name = "admin", 1605 .email = "admin@example.test", 1606 .password_hash = password_hash, 1607 .ssh_keys = admin_ssh_key, 1608 } }, 1609 }}); 1610 } 1611 1612 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1613 // the event store 1614 fn repoOnDiskPath( 1615 io: std.Io, 1616 allocator: std.mem.Allocator, 1617 data_path: []const u8, 1618 repo_name: []const u8, 1619 create: bool, 1620 ) !?[]u8 { 1621 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1622 defer allocator.free(admin_repo_path); 1623 1624 const event_id_hex = (try evt.resolveOrCreateRepo( 1625 io, 1626 allocator, 1627 admin_repo_path, 1628 "admin", 1629 repo_name, 1630 if (create) .{ .read_access = .public } else null, 1631 )) orelse return null; 1632 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1633 } 1634 1635 // build the remote URL addressing admin/<repo_name> over the given transport 1636 fn remoteUrl( 1637 comptime transport_def: net.TransportDefinition, 1638 allocator: std.mem.Allocator, 1639 repo_name: []const u8, 1640 port: u16, 1641 ) ![]u8 { 1642 return switch (transport_def) { 1643 .file => unreachable, 1644 .wire => |wire_kind| switch (wire_kind) { 1645 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1646 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1647 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1648 }, 1649 }; 1650 } 1651 1652 // the ssh command the haxy git client should invoke, or null when not using 1653 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1654 // and skip writing to known_hosts. 1655 fn sshCommand( 1656 is_ssh: bool, 1657 allocator: std.mem.Allocator, 1658 temp_path: []const u8, 1659 port: u16, 1660 ) !?[]u8 { 1661 if (!is_ssh) return null; 1662 1663 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1664 defer allocator.free(priv_key_path); 1665 1666 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1667 } 1668 1669 const ServerFiles = union(enum) { 1670 files: []const struct { path: []const u8, content: []const u8 }, 1671 dirs: []const []const u8, 1672 }; 1673 1674 /// commit files without writing the server's index or worktree. 1675 fn commitServer(repo: anytype, io: std.Io, allocator: std.mem.Allocator, files: ServerFiles, metadata: xit.object.CommitMetadata(repo.self_repo_opts.hash)) ![xit.hash.hexLen(repo.self_repo_opts.hash)]u8 { 1676 const R = @TypeOf(repo.*); 1677 const opts = repo.self_repo_opts; 1678 const Index = xit.index.Index(.xit, opts); 1679 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1680 const Ctx = struct { 1681 core: *R.Core, 1682 io: std.Io, 1683 allocator: std.mem.Allocator, 1684 files: ServerFiles, 1685 metadata: xit.object.CommitMetadata(opts.hash), 1686 result: *[xit.hash.hexLen(opts.hash)]u8, 1687 1688 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1689 var reader = std.Io.Reader.fixed(content); 1690 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1691 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1692 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1693 defer ctx.allocator.free(path_parts); 1694 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1695 } 1696 1697 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1698 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1699 defer dir.close(ctx.io); 1700 var entries = dir.iterate(); 1701 while (try entries.next(ctx.io)) |entry| { 1702 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1703 defer ctx.allocator.free(child); 1704 switch (entry.kind) { 1705 .directory => try ctx.addDir(state, index, child), 1706 .file => { 1707 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1708 defer ctx.allocator.free(content); 1709 try ctx.addFile(state, index, child, content); 1710 }, 1711 else => {}, 1712 } 1713 } 1714 } 1715 1716 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1717 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1718 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1719 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1720 var index = if (head_oid) |oid| try Index.initFromCommit(state.readOnly(), ctx.io, ctx.allocator, &oid) else try Index.init(state.readOnly(), ctx.io, ctx.allocator); 1721 defer index.deinit(); 1722 switch (ctx.files) { 1723 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1724 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1725 } 1726 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1727 defer tree.deinit(); 1728 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1729 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1730 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1731 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1732 } 1733 }; 1734 try repo.core.db_file.lock(io, .exclusive); 1735 defer repo.core.db_file.unlock(io); 1736 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1737 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1738 .core = &repo.core, 1739 .io = io, 1740 .allocator = allocator, 1741 .files = files, 1742 .metadata = metadata, 1743 .result = &result, 1744 }); 1745 return result; 1746 } 1747