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