╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
571 572 try client_repo.push( 573 io, 574 allocator, 575 "origin", 576 "master", 577 false, 578 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 579 .command = ssh_cmd_maybe, 580 } } }, 581 ); 582 583 // the push registered admin/server; resolve its on-disk directory 584 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", false)).?; 585 defer allocator.free(server_path); 586 587 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 588 defer server_repo.deinit(io, allocator); 589 590 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 591 592 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 593 try std.testing.expectEqualStrings(&commit1, &oid_master); 594 } 595 596 fn testPushEvents( 597 comptime repo_kind: rp.RepoKind, 598 comptime transport_def: net.TransportDefinition, 599 io: std.Io, 600 allocator: std.mem.Allocator, 601 ) !void { 602 const temp_dir_name = "temp-testnet-push-events"; 603 604 // create the temp dir 605 const cwd = std.Io.Dir.cwd(); 606 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 607 if (temp_dir_or_err) |*temp_dir| { 608 temp_dir.close(io); 609 try cwd.deleteTree(io, temp_dir_name); 610 } else |_| {} 611 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 612 defer cwd.deleteTree(io, temp_dir_name) catch {}; 613 defer temp_dir.close(io); 614 615 // init server 616 var server_process = try runServer(io, allocator, temp_dir_name); 617 defer _ = server_process.kill(io); 618 619 // register the repo under admin and locate its on-disk directory 620 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 621 defer allocator.free(server_path); 622 623 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 624 const ServerRepo = rp.Repo(.xit, repo_opts); 625 var server_repo = try ServerRepo.init(io, allocator, .{ .path = server_path }); 626 defer server_repo.deinit(io, allocator); 627 628 const cwd_path = try std.process.currentPathAlloc(io, allocator); 629 defer allocator.free(cwd_path); 630 631 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 632 defer allocator.free(client_path); 633 634 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 635 defer client_repo.deinit(io, allocator); 636 637 // add remote 638 { 639 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 640 defer allocator.free(remote_url); 641 642 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 643 } 644 645 const is_ssh = switch (transport_def) { 646 .file => false, 647 .wire => |wire_kind| .ssh == wire_kind, 648 }; 649 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 650 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 651 652 var arena = std.heap.ArenaAllocator.init(allocator); 653 defer arena.deinit(); 654 655 // 656 // define test events 657 // 658 659 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 660 const issue_event_id = evt.EventWithId.randomId(prng.random()); 661 662 const events_to_push = [_]evt.EventWithId{ 663 .{ 664 .id = std.fmt.bytesToHex(issue_event_id, .lower), 665 .event = .{ 666 .issue = .{ 667 .title = "Login form clears password on validation error", 668 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 669 .tags = "bug priority-high ui", 670 }, 671 }, 672 }, 673 }; 674 675 // commit the issue on the client and push it to the server 676 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 677 try client_repo.push( 678 io, 679 allocator, 680 "origin", 681 "haxy/events", 682 false, 683 .{ .wire = .{ .ssh = .{ 684 .command = ssh_cmd_maybe, 685 } } }, 686 ); 687 688 // the push consumed the issue into the server repo's db 689 { 690 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 691 692 // get the map of issues 693 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 694 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 695 696 // get the issue out of the map 697 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 698 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 699 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 700 701 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.description); 702 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.tags); 703 } 704 705 // 706 // edit the issue 707 // 708 709 // this event edits the previous one because it has the same id 710 const events_to_push2 = [_]evt.EventWithId{ 711 .{ 712 .id = std.fmt.bytesToHex(issue_event_id, .lower), 713 .event = .{ 714 .issue = .{ 715 .title = "Login form clears password on validation error", 716 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 717 .tags = "bug priority-low ui", 718 }, 719 }, 720 }, 721 }; 722 723 // commit the edit on the client and push it to the server 724 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 725 try client_repo.push( 726 io, 727 allocator, 728 "origin", 729 "haxy/events", 730 false, 731 .{ .wire = .{ .ssh = .{ 732 .command = ssh_cmd_maybe, 733 } } }, 734 ); 735 736 // the push consumed the edit into the server repo's db 737 { 738 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 739 740 // get the map of issues 741 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 742 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 743 744 // get the issue out of the map that was edited 745 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 746 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 747 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 748 749 // the description and tags were correctly edited 750 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.description); 751 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.tags); 752 } 753 } 754 755 fn testClone( 756 comptime repo_kind: rp.RepoKind, 757 comptime transport_def: net.TransportDefinition, 758 comptime shell_out_to_git: bool, 759 io: std.Io, 760 allocator: std.mem.Allocator, 761 ) !void { 762 const temp_dir_name = "temp-testnet-clone"; 763 764 // create the temp dir 765 const cwd = std.Io.Dir.cwd(); 766 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 767 if (temp_dir_or_err) |*temp_dir| { 768 temp_dir.close(io); 769 try cwd.deleteTree(io, temp_dir_name); 770 } else |_| {} 771 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 772 defer cwd.deleteTree(io, temp_dir_name) catch {}; 773 defer temp_dir.close(io); 774 775 // init server 776 var server_process = try runServer(io, allocator, temp_dir_name); 777 defer _ = server_process.kill(io); 778 779 const cwd_path = try std.process.currentPathAlloc(io, allocator); 780 defer allocator.free(cwd_path); 781 782 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 783 defer allocator.free(temp_path); 784 785 // register the repo under admin and locate its on-disk directory 786 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 787 defer allocator.free(server_path); 788 789 // init server repo with default branch name as main 790 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 791 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 792 defer server_repo.deinit(io, allocator); 793 794 if (shell_out_to_git) { 795 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 796 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 797 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 798 } 799 800 // make a commit 801 { 802 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 803 defer hello_txt.close(io); 804 try hello_txt.writeStreamingAll(io, "hello, world!"); 805 try server_repo.add(io, allocator, &.{"hello.txt"}); 806 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 807 } 808 809 // tag first commit 810 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 811 812 // make a commit 813 { 814 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 815 defer goodbye_txt.close(io); 816 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 817 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 818 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 819 } 820 821 // export server repo 822 { 823 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 824 defer export_file.close(io); 825 } 826 827 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 828 defer allocator.free(client_path); 829 830 // get remote url 831 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 832 defer allocator.free(remote_url); 833 834 const is_ssh = switch (transport_def) { 835 .file => false, 836 .wire => |wire_kind| .ssh == wire_kind, 837 }; 838 839 if (shell_out_to_git) { 840 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 841 defer allocator.free(priv_key_path); 842 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}", .{ ssh_port, priv_key_path }); 843 defer allocator.free(ssh_config_arg); 844 845 { 846 var process = try std.process.spawn(io, .{ 847 .argv = if (is_ssh) 848 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 849 else 850 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 851 .cwd = .{ .path = temp_path }, 852 .stdin = .ignore, 853 .stdout = .ignore, 854 .stderr = .ignore, 855 }); 856 const term = try process.wait(io); 857 if (term != .exited or term.exited != 0) { 858 return error.GitCommandFailed; 859 } 860 } 861 862 // make sure shallow clone was successful 863 { 864 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 865 hello_txt.close(io); 866 } 867 868 // make a third commit on the server 869 { 870 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 871 defer extra_txt.close(io); 872 try extra_txt.writeStreamingAll(io, "extra content"); 873 try server_repo.add(io, allocator, &.{"extra.txt"}); 874 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 875 } 876 877 // pull --unshallow to deepen the clone and get the new commit 878 { 879 var process = try std.process.spawn(io, .{ 880 .argv = if (is_ssh) 881 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 882 else 883 &.{ "git", "pull", "--unshallow" }, 884 .cwd = .{ .path = client_path }, 885 .stdin = .ignore, 886 .stdout = .ignore, 887 .stderr = .ignore, 888 }); 889 const term = try process.wait(io); 890 if (term != .exited or term.exited != 0) { 891 return error.GitCommandFailed; 892 } 893 } 894 895 // make sure unshallow pull was successful 896 { 897 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 898 extra_txt.close(io); 899 } 900 901 // delete client and clone again with --shallow-since 902 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 903 904 { 905 var process = try std.process.spawn(io, .{ 906 .argv = if (is_ssh) 907 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 908 else 909 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 910 .cwd = .{ .path = temp_path }, 911 .stdin = .ignore, 912 .stdout = .ignore, 913 .stderr = .ignore, 914 }); 915 const term = try process.wait(io); 916 if (term != .exited or term.exited != 0) { 917 return error.GitCommandFailed; 918 } 919 } 920 921 // make sure shallow clone was successful 922 { 923 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 924 hello_txt.close(io); 925 } 926 927 // delete client and clone again with --shallow-exclude 928 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 929 930 { 931 var process = try std.process.spawn(io, .{ 932 .argv = if (is_ssh) 933 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 934 else 935 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 936 .cwd = .{ .path = temp_path }, 937 .stdin = .ignore, 938 .stdout = .ignore, 939 .stderr = .ignore, 940 }); 941 const term = try process.wait(io); 942 if (term != .exited or term.exited != 0) { 943 return error.GitCommandFailed; 944 } 945 } 946 947 // make sure shallow clone was successful 948 { 949 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 950 hello_txt.close(io); 951 } 952 953 // delete client and clone again with --filter=blob:none 954 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 955 956 { 957 var process = try std.process.spawn(io, .{ 958 .argv = if (is_ssh) 959 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 960 else 961 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 962 .cwd = .{ .path = temp_path }, 963 .stdin = .ignore, 964 .stdout = .ignore, 965 .stderr = .ignore, 966 }); 967 const term = try process.wait(io); 968 if (term != .exited or term.exited != 0) { 969 return error.GitCommandFailed; 970 } 971 } 972 973 // make sure partial clone was successful 974 { 975 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 976 hello_txt.close(io); 977 } 978 979 // delete client and clone again with --filter=tree:0 980 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 981 982 { 983 var process = try std.process.spawn(io, .{ 984 .argv = if (is_ssh) 985 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 986 else 987 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 988 .cwd = .{ .path = temp_path }, 989 .stdin = .ignore, 990 .stdout = .ignore, 991 .stderr = .ignore, 992 }); 993 const term = try process.wait(io); 994 if (term != .exited or term.exited != 0) { 995 return error.GitCommandFailed; 996 } 997 } 998 999 // make sure treeless clone was successful 1000 { 1001 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1002 goodbye_txt.close(io); 1003 } 1004 } else { 1005 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1006 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1007 1008 // clone repo 1009 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1010 io, 1011 allocator, 1012 remote_url, 1013 temp_path, 1014 client_path, 1015 .{ .wire = .{ .ssh = .{ 1016 .command = ssh_cmd_maybe, 1017 } } }, 1018 ); 1019 defer client_repo.deinit(io, allocator); 1020 1021 // make sure HEAD points to the right default branch 1022 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1023 const head = try client_repo.head(io, &current_branch_buffer); 1024 try std.testing.expectEqualStrings("main", head.ref.name); 1025 1026 // make sure clone was successful 1027 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1028 defer hello_txt.close(io); 1029 } 1030 } 1031 1032 fn testFetchLarge( 1033 comptime repo_kind: rp.RepoKind, 1034 comptime transport_def: net.TransportDefinition, 1035 comptime shell_out_to_git: bool, 1036 io: std.Io, 1037 allocator: std.mem.Allocator, 1038 ) !void { 1039 const temp_dir_name = "temp-testnet-fetch-large"; 1040 1041 // create the temp dir 1042 const cwd = std.Io.Dir.cwd(); 1043 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1044 if (temp_dir_or_err) |*temp_dir| { 1045 temp_dir.close(io); 1046 try cwd.deleteTree(io, temp_dir_name); 1047 } else |_| {} 1048 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1049 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1050 defer temp_dir.close(io); 1051 1052 // init server 1053 var server_process = try runServer(io, allocator, temp_dir_name); 1054 defer _ = server_process.kill(io); 1055 1056 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1057 defer allocator.free(cwd_path); 1058 1059 // register the repo under admin and locate its on-disk directory 1060 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1061 defer allocator.free(server_path); 1062 1063 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1064 defer server_repo.deinit(io, allocator); 1065 1066 var server_dir = try cwd.openDir(io, server_path, .{}); 1067 defer server_dir.close(io); 1068 1069 // copy files from current repo into server dir 1070 for (&[_][]const u8{"src"}) |dir_name| { 1071 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1072 defer src_repo_dir.close(io); 1073 1074 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1075 defer dest_repo_dir.close(io); 1076 1077 try copyDir(io, src_repo_dir, dest_repo_dir); 1078 1079 try server_repo.add(io, allocator, &.{dir_name}); 1080 } 1081 1082 // make a commit 1083 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1084 1085 // export server repo 1086 { 1087 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1088 defer export_file.close(io); 1089 } 1090 1091 if (shell_out_to_git) { 1092 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1093 } 1094 1095 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1096 defer allocator.free(client_path); 1097 1098 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1099 defer client_repo.deinit(io, allocator); 1100 1101 // add remote 1102 { 1103 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1104 defer allocator.free(remote_url); 1105 1106 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1107 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1108 } 1109 1110 const is_ssh = switch (transport_def) { 1111 .file => false, 1112 .wire => |wire_kind| .ssh == wire_kind, 1113 }; 1114 1115 if (shell_out_to_git) { 1116 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1117 defer allocator.free(priv_key_path); 1118 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}", .{ ssh_port, priv_key_path }); 1119 defer allocator.free(ssh_config_arg); 1120 1121 { 1122 var process = try std.process.spawn(io, .{ 1123 .argv = if (is_ssh) 1124 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1125 else 1126 &.{ "git", "pull", "origin", "master" }, 1127 .cwd = .{ .path = client_path }, 1128 .stdin = .ignore, 1129 .stdout = .ignore, 1130 .stderr = .ignore, 1131 }); 1132 const term = try process.wait(io); 1133 if (term != .exited or term.exited != 0) { 1134 return error.GitCommandFailed; 1135 } 1136 } 1137 1138 // make sure pull was successful 1139 { 1140 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1141 try std.testing.expectEqualStrings(&commit1, &oid_master); 1142 } 1143 1144 // make another commit on the server 1145 const commit2 = blk: { 1146 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1147 defer extra_txt.close(io); 1148 try extra_txt.writeStreamingAll(io, "extra content"); 1149 try server_repo.add(io, allocator, &.{"extra.txt"}); 1150 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1151 }; 1152 1153 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1154 { 1155 var process = try std.process.spawn(io, .{ 1156 .argv = if (is_ssh) 1157 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1158 else 1159 &.{ "git", "fetch", "origin", "master" }, 1160 .cwd = .{ .path = client_path }, 1161 .stdin = .ignore, 1162 .stdout = .ignore, 1163 .stderr = .ignore, 1164 }); 1165 const term = try process.wait(io); 1166 if (term != .exited or term.exited != 0) { 1167 return error.GitCommandFailed; 1168 } 1169 } 1170 1171 // make sure fetch with want-ref was successful 1172 { 1173 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1174 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1175 } 1176 } else { 1177 const refspecs = &.{ 1178 "+refs/heads/master:refs/heads/master", 1179 }; 1180 1181 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1182 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1183 1184 try client_repo.fetch( 1185 io, 1186 allocator, 1187 "origin", 1188 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1189 .command = ssh_cmd_maybe, 1190 } } }, 1191 ); 1192 1193 // update the working dir 1194 try client_repo.restore(io, allocator, "."); 1195 } 1196 1197 // make sure fetch was successful 1198 { 1199 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1200 try std.testing.expectEqualStrings(&commit1, &oid_master); 1201 } 1202 } 1203 1204 fn testPushLarge( 1205 comptime repo_kind: rp.RepoKind, 1206 comptime transport_def: net.TransportDefinition, 1207 comptime shell_out_to_git: bool, 1208 io: std.Io, 1209 allocator: std.mem.Allocator, 1210 ) !void { 1211 const temp_dir_name = "temp-testnet-push-large"; 1212 1213 // create the temp dir 1214 const cwd = std.Io.Dir.cwd(); 1215 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1216 if (temp_dir_or_err) |*temp_dir| { 1217 temp_dir.close(io); 1218 try cwd.deleteTree(io, temp_dir_name); 1219 } else |_| {} 1220 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1221 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1222 defer temp_dir.close(io); 1223 1224 // init server 1225 var server_process = try runServer(io, allocator, temp_dir_name); 1226 defer _ = server_process.kill(io); 1227 1228 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1229 defer allocator.free(cwd_path); 1230 1231 // register the repo under admin and locate its on-disk directory 1232 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1233 defer allocator.free(server_path); 1234 1235 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1236 defer server_repo.deinit(io, allocator); 1237 1238 // add config 1239 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1240 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1241 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1242 1243 // export server repo 1244 { 1245 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1246 defer export_file.close(io); 1247 } 1248 1249 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1250 defer allocator.free(client_path); 1251 1252 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1253 defer client_repo.deinit(io, allocator); 1254 1255 var client_dir = try cwd.openDir(io, client_path, .{}); 1256 defer client_dir.close(io); 1257 1258 { 1259 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1260 defer hello_txt.close(io); 1261 try hello_txt.writeStreamingAll(io, "hello, world!"); 1262 try client_repo.add(io, allocator, &.{"hello.txt"}); 1263 } 1264 1265 // copy files from current repo into client dir 1266 for (&[_][]const u8{"src"}) |dir_name| { 1267 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1268 defer src_repo_dir.close(io); 1269 1270 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1271 defer dest_repo_dir.close(io); 1272 1273 try copyDir(io, src_repo_dir, dest_repo_dir); 1274 1275 try client_repo.add(io, allocator, &.{dir_name}); 1276 } 1277 1278 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1279 1280 // change the files so git will send them as delta objects 1281 for (&[_][]const u8{"src"}) |dir_name| { 1282 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1283 defer dest_repo_dir.close(io); 1284 1285 { 1286 var iter = dest_repo_dir.iterate(); 1287 while (try iter.next(io)) |entry| { 1288 switch (entry.kind) { 1289 .file => { 1290 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1291 defer file.close(io); 1292 var writer = file.writer(io, &.{}); 1293 try writer.interface.writeAll("EDIT"); 1294 }, 1295 else => {}, 1296 } 1297 } 1298 } 1299 1300 try client_repo.add(io, allocator, &.{dir_name}); 1301 } 1302 1303 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1304 1305 // add remote 1306 { 1307 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1308 defer allocator.free(remote_url); 1309 1310 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1311 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1312 } 1313 1314 const is_ssh = switch (transport_def) { 1315 .file => false, 1316 .wire => |wire_kind| .ssh == wire_kind, 1317 }; 1318 1319 if (shell_out_to_git) { 1320 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1321 defer allocator.free(priv_key_path); 1322 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}", .{ ssh_port, priv_key_path }); 1323 defer allocator.free(ssh_config_arg); 1324 1325 // shell out to git so it will send delta objects 1326 var process = try std.process.spawn(io, .{ 1327 .argv = if (is_ssh) 1328 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1329 else 1330 &.{ "git", "push", "origin", "master" }, 1331 .cwd = .{ .path = client_path }, 1332 .stdin = .ignore, 1333 .stdout = .ignore, 1334 .stderr = .ignore, 1335 }); 1336 const term = try process.wait(io); 1337 if (term != .exited or term.exited != 0) { 1338 return error.GitCommandFailed; 1339 } 1340 } else { 1341 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1342 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1343 1344 try client_repo.push( 1345 io, 1346 allocator, 1347 "origin", 1348 "master", 1349 false, 1350 .{ .wire = .{ .ssh = .{ 1351 .command = ssh_cmd_maybe, 1352 } } }, 1353 ); 1354 } 1355 1356 // make sure push was successful 1357 { 1358 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1359 try std.testing.expectEqualStrings(&commit2, &oid_master); 1360 1361 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1362 defer hello_txt.close(io); 1363 } 1364 } 1365 1366 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1367 var iter = src_dir.iterate(); 1368 while (try iter.next(io)) |entry| { 1369 switch (entry.kind) { 1370 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1371 .directory => { 1372 try dest_dir.createDirPath(io, entry.name); 1373 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1374 defer dest_entry_dir.close(io); 1375 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1376 defer src_entry_dir.close(io); 1377 try copyDir(io, src_entry_dir, dest_entry_dir); 1378 }, 1379 else => {}, 1380 } 1381 } 1382 } 1383 1384 // the dev SSH public key matching the private key written by runServer 1385 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1386 1387 // create the admin event repo with a user holding the dev SSH public key 1388 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1389 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1390 defer allocator.free(cwd_path); 1391 1392 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1393 defer allocator.free(admin_repo_path); 1394 1395 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1396 defer repo.deinit(io, allocator); 1397 1398 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1399 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1400 1401 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1402 const user_id = evt.EventWithId.randomId(prng.random()); 1403 1404 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1405 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1406 1407 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1408 .id = std.fmt.bytesToHex(user_id, .lower), 1409 .event = .{ .user = .{ 1410 .name = "admin", 1411 .display_name = "Admin", 1412 .email = "admin@example.test", 1413 .password_hash = password_hash, 1414 .ssh_keys = admin_ssh_key, 1415 } }, 1416 }}); 1417 } 1418 1419 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1420 // the event store 1421 fn repoOnDiskPath( 1422 io: std.Io, 1423 allocator: std.mem.Allocator, 1424 data_dir_name: []const u8, 1425 repo_name: []const u8, 1426 create: bool, 1427 ) !?[]u8 { 1428 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1429 defer allocator.free(cwd_path); 1430 1431 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1432 defer allocator.free(admin_repo_path); 1433 1434 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1435 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1436 } 1437 1438 // build the remote URL addressing admin/<repo_name> over the given transport 1439 fn remoteUrl( 1440 comptime transport_def: net.TransportDefinition, 1441 allocator: std.mem.Allocator, 1442 repo_name: []const u8, 1443 ) ![]u8 { 1444 return switch (transport_def) { 1445 .file => unreachable, 1446 .wire => |wire_kind| switch (wire_kind) { 1447 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1448 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1449 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1450 }, 1451 }; 1452 } 1453 1454 // the ssh command the haxy git client should invoke, or null when not using 1455 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1456 // and skip writing to known_hosts. 1457 fn sshCommand( 1458 is_ssh: bool, 1459 allocator: std.mem.Allocator, 1460 cwd_path: []const u8, 1461 temp_dir_name: []const u8, 1462 ) !?[]u8 { 1463 if (!is_ssh) return null; 1464 1465 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1466 defer allocator.free(priv_key_path); 1467 1468 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}\"", .{ ssh_port, priv_key_path }); 1469 } 1470