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