╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
842 const commit1 = blk: { 843 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 844 defer hello_txt.close(io); 845 try hello_txt.writeStreamingAll(io, "hello, world!"); 846 try client_repo.add(io, allocator, &.{"hello.txt"}); 847 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 848 }; 849 850 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 851 852 { 853 if (.windows == builtin.os.tag) { 854 std.mem.replaceScalar(u8, server_path, '\\', '/'); 855 } 856 const separator = if (server_path[0] == '/') "" else "/"; 857 858 const remote_url = switch (transport_def) { 859 .file => unreachable, 860 .wire => |wire_kind| switch (wire_kind) { 861 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 862 .raw => unreachable, 863 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 864 }, 865 }; 866 defer allocator.free(remote_url); 867 868 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 869 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 870 } 871 872 const is_ssh = switch (transport_def) { 873 .file => false, 874 .wire => |wire_kind| .ssh == wire_kind, 875 }; 876 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 877 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 878 defer allocator.free(known_hosts_path); 879 880 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 881 defer allocator.free(priv_key_path); 882 883 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 884 } else null; 885 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 886 887 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 888 defer allocator.free(receive_pack_command); 889 890 try client_repo.push( 891 io, 892 allocator, 893 "origin", 894 "master", 895 false, 896 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 897 .command = ssh_cmd_maybe, 898 .receive_pack_command = receive_pack_command, 899 } } }, 900 ); 901 902 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 903 defer server_repo.deinit(io, allocator); 904 905 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 906 907 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 908 try std.testing.expectEqualStrings(&commit1, &oid_master); 909 } 910 911 fn testClone( 912 comptime repo_kind: rp.RepoKind, 913 comptime transport_def: net.TransportDefinition, 914 comptime port: u16, 915 comptime shell_out_to_git: bool, 916 io: std.Io, 917 allocator: std.mem.Allocator, 918 ) !void { 919 const temp_dir_name = "temp-testnet-clone"; 920 921 // create the temp dir 922 const cwd = std.Io.Dir.cwd(); 923 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 924 if (temp_dir_or_err) |*temp_dir| { 925 temp_dir.close(io); 926 try cwd.deleteTree(io, temp_dir_name); 927 } else |_| {} 928 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 929 defer cwd.deleteTree(io, temp_dir_name) catch {}; 930 defer temp_dir.close(io); 931 932 // init server 933 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 934 try server.start(); 935 defer server.stop(); 936 937 const cwd_path = try std.process.currentPathAlloc(io, allocator); 938 defer allocator.free(cwd_path); 939 940 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 941 defer allocator.free(temp_path); 942 943 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 944 defer allocator.free(server_path); 945 946 // init server repo with default branch name as main 947 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 948 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 949 defer server_repo.deinit(io, allocator); 950 951 if (shell_out_to_git) { 952 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 953 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 954 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 955 } 956 957 // make a commit 958 { 959 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 960 defer hello_txt.close(io); 961 try hello_txt.writeStreamingAll(io, "hello, world!"); 962 try server_repo.add(io, allocator, &.{"hello.txt"}); 963 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 964 } 965 966 // tag first commit 967 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 968 969 // make a commit 970 { 971 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 972 defer goodbye_txt.close(io); 973 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 974 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 975 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 976 } 977 978 // export server repo 979 { 980 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 981 defer export_file.close(io); 982 } 983 984 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 985 defer allocator.free(client_path); 986 987 // get remote url 988 const remote_url = blk: { 989 if (.windows == builtin.os.tag) { 990 std.mem.replaceScalar(u8, server_path, '\\', '/'); 991 } 992 const separator = if (server_path[0] == '/') "" else "/"; 993 994 break :blk switch (transport_def) { 995 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 996 .file => try std.fmt.allocPrint(allocator, "server", .{}), // relative file paths work too 997 .wire => |wire_kind| switch (wire_kind) { 998 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 999 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1000 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1001 }, 1002 }; 1003 }; 1004 defer allocator.free(remote_url); 1005 1006 const is_ssh = switch (transport_def) { 1007 .file => false, 1008 .wire => |wire_kind| .ssh == wire_kind, 1009 }; 1010 1011 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 1012 defer allocator.free(upload_pack_command); 1013 1014 if (shell_out_to_git) { 1015 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1016 defer allocator.free(priv_key_path); 1017 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1018 defer allocator.free(ssh_config_arg); 1019 1020 { 1021 var process = try std.process.spawn(io, .{ 1022 .argv = if (is_ssh) 1023 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--depth", "1", remote_url, "client" } 1024 else 1025 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1026 .cwd = .{ .path = temp_path }, 1027 .stdin = .ignore, 1028 .stdout = .ignore, 1029 .stderr = .ignore, 1030 }); 1031 const term = try process.wait(io); 1032 if (term != .exited) { 1033 return error.GitCommandFailed; 1034 } 1035 } 1036 1037 // make sure shallow clone was successful 1038 { 1039 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1040 hello_txt.close(io); 1041 } 1042 1043 // make a third commit on the server 1044 { 1045 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1046 defer extra_txt.close(io); 1047 try extra_txt.writeStreamingAll(io, "extra content"); 1048 try server_repo.add(io, allocator, &.{"extra.txt"}); 1049 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1050 } 1051 1052 // pull --unshallow to deepen the clone and get the new commit 1053 { 1054 var process = try std.process.spawn(io, .{ 1055 .argv = if (is_ssh) 1056 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "--unshallow" } 1057 else 1058 &.{ "git", "pull", "--unshallow" }, 1059 .cwd = .{ .path = client_path }, 1060 .stdin = .ignore, 1061 .stdout = .ignore, 1062 .stderr = .ignore, 1063 }); 1064 const term = try process.wait(io); 1065 if (term != .exited) { 1066 return error.GitCommandFailed; 1067 } 1068 } 1069 1070 // make sure unshallow pull was successful 1071 { 1072 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 1073 extra_txt.close(io); 1074 } 1075 1076 // delete client and clone again with --shallow-since 1077 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1078 1079 { 1080 var process = try std.process.spawn(io, .{ 1081 .argv = if (is_ssh) 1082 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--shallow-since=2000-01-01", remote_url, "client" } 1083 else 1084 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1085 .cwd = .{ .path = temp_path }, 1086 .stdin = .ignore, 1087 .stdout = .ignore, 1088 .stderr = .ignore, 1089 }); 1090 const term = try process.wait(io); 1091 if (term != .exited) { 1092 return error.GitCommandFailed; 1093 } 1094 } 1095 1096 // make sure shallow clone was successful 1097 { 1098 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1099 hello_txt.close(io); 1100 } 1101 1102 // delete client and clone again with --shallow-exclude 1103 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1104 1105 { 1106 var process = try std.process.spawn(io, .{ 1107 .argv = if (is_ssh) 1108 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--shallow-exclude=v1", remote_url, "client" } 1109 else 1110 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1111 .cwd = .{ .path = temp_path }, 1112 .stdin = .ignore, 1113 .stdout = .ignore, 1114 .stderr = .ignore, 1115 }); 1116 const term = try process.wait(io); 1117 if (term != .exited) { 1118 return error.GitCommandFailed; 1119 } 1120 } 1121 1122 // make sure shallow clone was successful 1123 { 1124 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1125 hello_txt.close(io); 1126 } 1127 1128 // delete client and clone again with --filter=blob:none 1129 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1130 1131 { 1132 var process = try std.process.spawn(io, .{ 1133 .argv = if (is_ssh) 1134 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--filter=blob:none", remote_url, "client" } 1135 else 1136 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1137 .cwd = .{ .path = temp_path }, 1138 .stdin = .ignore, 1139 .stdout = .ignore, 1140 .stderr = .ignore, 1141 }); 1142 const term = try process.wait(io); 1143 if (term != .exited) { 1144 return error.GitCommandFailed; 1145 } 1146 } 1147 1148 // make sure partial clone was successful 1149 { 1150 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1151 hello_txt.close(io); 1152 } 1153 1154 // delete client and clone again with --filter=tree:0 1155 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1156 1157 { 1158 var process = try std.process.spawn(io, .{ 1159 .argv = if (is_ssh) 1160 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--filter=tree:0", remote_url, "client" } 1161 else 1162 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1163 .cwd = .{ .path = temp_path }, 1164 .stdin = .ignore, 1165 .stdout = .ignore, 1166 .stderr = .ignore, 1167 }); 1168 const term = try process.wait(io); 1169 if (term != .exited) { 1170 return error.GitCommandFailed; 1171 } 1172 } 1173 1174 // make sure treeless clone was successful 1175 { 1176 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1177 goodbye_txt.close(io); 1178 } 1179 } else { 1180 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1181 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1182 defer allocator.free(known_hosts_path); 1183 1184 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1185 defer allocator.free(priv_key_path); 1186 1187 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1188 } else null; 1189 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1190 1191 // clone repo 1192 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1193 io, 1194 allocator, 1195 remote_url, 1196 temp_path, 1197 client_path, 1198 .{ .wire = .{ .ssh = .{ 1199 .command = ssh_cmd_maybe, 1200 .upload_pack_command = upload_pack_command, 1201 } } }, 1202 ); 1203 defer client_repo.deinit(io, allocator); 1204 1205 // make sure HEAD points to the right default branch 1206 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1207 const head = try client_repo.head(io, &current_branch_buffer); 1208 try std.testing.expectEqualStrings("main", head.ref.name); 1209 1210 // make sure clone was successful 1211 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1212 defer hello_txt.close(io); 1213 } 1214 } 1215 1216 fn testFetchLarge( 1217 comptime repo_kind: rp.RepoKind, 1218 comptime transport_def: net.TransportDefinition, 1219 comptime port: u16, 1220 comptime shell_out_to_git: bool, 1221 io: std.Io, 1222 allocator: std.mem.Allocator, 1223 ) !void { 1224 const temp_dir_name = "temp-testnet-fetch-large"; 1225 1226 // create the temp dir 1227 const cwd = std.Io.Dir.cwd(); 1228 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1229 if (temp_dir_or_err) |*temp_dir| { 1230 temp_dir.close(io); 1231 try cwd.deleteTree(io, temp_dir_name); 1232 } else |_| {} 1233 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1234 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1235 defer temp_dir.close(io); 1236 1237 // init server 1238 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1239 try server.start(); 1240 defer server.stop(); 1241 1242 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1243 defer allocator.free(cwd_path); 1244 1245 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1246 defer allocator.free(server_path); 1247 1248 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1249 defer server_repo.deinit(io, allocator); 1250 1251 var server_dir = try cwd.openDir(io, server_path, .{}); 1252 defer server_dir.close(io); 1253 1254 // copy files from current repo into server dir 1255 for (&[_][]const u8{"src"}) |dir_name| { 1256 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1257 defer src_repo_dir.close(io); 1258 1259 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1260 defer dest_repo_dir.close(io); 1261 1262 try copyDir(io, src_repo_dir, dest_repo_dir); 1263 1264 try server_repo.add(io, allocator, &.{dir_name}); 1265 } 1266 1267 // make a commit 1268 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1269 1270 // export server repo 1271 { 1272 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1273 defer export_file.close(io); 1274 } 1275 1276 if (shell_out_to_git) { 1277 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1278 } 1279 1280 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1281 defer allocator.free(client_path); 1282 1283 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1284 defer client_repo.deinit(io, allocator); 1285 1286 // add remote 1287 { 1288 if (.windows == builtin.os.tag) { 1289 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1290 } 1291 const separator = if (server_path[0] == '/') "" else "/"; 1292 1293 const remote_url = switch (transport_def) { 1294 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1295 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1296 .wire => |wire_kind| switch (wire_kind) { 1297 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1298 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1299 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1300 }, 1301 }; 1302 defer allocator.free(remote_url); 1303 1304 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1305 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1306 } 1307 1308 const is_ssh = switch (transport_def) { 1309 .file => false, 1310 .wire => |wire_kind| .ssh == wire_kind, 1311 }; 1312 1313 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 1314 defer allocator.free(upload_pack_command); 1315 1316 if (shell_out_to_git) { 1317 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1318 defer allocator.free(priv_key_path); 1319 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1320 defer allocator.free(ssh_config_arg); 1321 1322 { 1323 var process = try std.process.spawn(io, .{ 1324 .argv = if (is_ssh) 1325 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "origin", "master" } 1326 else 1327 &.{ "git", "pull", "origin", "master" }, 1328 .cwd = .{ .path = client_path }, 1329 .stdin = .ignore, 1330 .stdout = .ignore, 1331 .stderr = .ignore, 1332 }); 1333 const term = try process.wait(io); 1334 if (term != .exited) { 1335 return error.GitCommandFailed; 1336 } 1337 } 1338 1339 // make sure pull was successful 1340 { 1341 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1342 try std.testing.expectEqualStrings(&commit1, &oid_master); 1343 } 1344 1345 // make another commit on the server 1346 const commit2 = blk: { 1347 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1348 defer extra_txt.close(io); 1349 try extra_txt.writeStreamingAll(io, "extra content"); 1350 try server_repo.add(io, allocator, &.{"extra.txt"}); 1351 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1352 }; 1353 1354 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1355 { 1356 var process = try std.process.spawn(io, .{ 1357 .argv = if (is_ssh) 1358 &.{ "git", "-c", ssh_config_arg, "fetch", "--upload-pack", upload_pack_command, "origin", "master" } 1359 else 1360 &.{ "git", "fetch", "origin", "master" }, 1361 .cwd = .{ .path = client_path }, 1362 .stdin = .ignore, 1363 .stdout = .ignore, 1364 .stderr = .ignore, 1365 }); 1366 const term = try process.wait(io); 1367 if (term != .exited) { 1368 return error.GitCommandFailed; 1369 } 1370 } 1371 1372 // make sure fetch with want-ref was successful 1373 { 1374 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1375 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1376 } 1377 } else { 1378 const refspecs = &.{ 1379 "+refs/heads/master:refs/heads/master", 1380 }; 1381 1382 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1383 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1384 defer allocator.free(known_hosts_path); 1385 1386 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1387 defer allocator.free(priv_key_path); 1388 1389 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1390 } else null; 1391 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1392 1393 try client_repo.fetch( 1394 io, 1395 allocator, 1396 "origin", 1397 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1398 .command = ssh_cmd_maybe, 1399 .upload_pack_command = upload_pack_command, 1400 } } }, 1401 ); 1402 1403 // update the working dir 1404 try client_repo.restore(io, allocator, "."); 1405 } 1406 1407 // make sure fetch was successful 1408 { 1409 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1410 try std.testing.expectEqualStrings(&commit1, &oid_master); 1411 } 1412 } 1413 1414 fn testPushLarge( 1415 comptime repo_kind: rp.RepoKind, 1416 comptime transport_def: net.TransportDefinition, 1417 comptime port: u16, 1418 comptime shell_out_to_git: bool, 1419 io: std.Io, 1420 allocator: std.mem.Allocator, 1421 ) !void { 1422 const temp_dir_name = "temp-testnet-push-large"; 1423 1424 // create the temp dir 1425 const cwd = std.Io.Dir.cwd(); 1426 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1427 if (temp_dir_or_err) |*temp_dir| { 1428 temp_dir.close(io); 1429 try cwd.deleteTree(io, temp_dir_name); 1430 } else |_| {} 1431 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1432 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1433 defer temp_dir.close(io); 1434 1435 // init server 1436 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1437 try server.start(); 1438 defer server.stop(); 1439 1440 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1441 defer allocator.free(cwd_path); 1442 1443 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1444 defer allocator.free(server_path); 1445 1446 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1447 defer server_repo.deinit(io, allocator); 1448 1449 // add config 1450 switch (transport_def) { 1451 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1452 .wire => { 1453 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1454 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1455 }, 1456 } 1457 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1458 1459 // export server repo 1460 { 1461 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1462 defer export_file.close(io); 1463 } 1464 1465 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1466 defer allocator.free(client_path); 1467 1468 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1469 defer client_repo.deinit(io, allocator); 1470 1471 var client_dir = try cwd.openDir(io, client_path, .{}); 1472 defer client_dir.close(io); 1473 1474 { 1475 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1476 defer hello_txt.close(io); 1477 try hello_txt.writeStreamingAll(io, "hello, world!"); 1478 try client_repo.add(io, allocator, &.{"hello.txt"}); 1479 } 1480 1481 // copy files from current repo into client dir 1482 for (&[_][]const u8{"src"}) |dir_name| { 1483 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1484 defer src_repo_dir.close(io); 1485 1486 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1487 defer dest_repo_dir.close(io); 1488 1489 try copyDir(io, src_repo_dir, dest_repo_dir); 1490 1491 try client_repo.add(io, allocator, &.{dir_name}); 1492 } 1493 1494 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1495 1496 // change the files so git will send them as delta objects 1497 for (&[_][]const u8{"src"}) |dir_name| { 1498 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1499 defer dest_repo_dir.close(io); 1500 1501 { 1502 var iter = dest_repo_dir.iterate(); 1503 while (try iter.next(io)) |entry| { 1504 switch (entry.kind) { 1505 .file => { 1506 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1507 defer file.close(io); 1508 var writer = file.writer(io, &.{}); 1509 try writer.interface.writeAll("EDIT"); 1510 }, 1511 else => {}, 1512 } 1513 } 1514 } 1515 1516 try client_repo.add(io, allocator, &.{dir_name}); 1517 } 1518 1519 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1520 1521 // add remote 1522 { 1523 if (.windows == builtin.os.tag) { 1524 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1525 } 1526 const separator = if (server_path[0] == '/') "" else "/"; 1527 1528 const remote_url = switch (transport_def) { 1529 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1530 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1531 .wire => |wire_kind| switch (wire_kind) { 1532 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1533 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1534 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1535 }, 1536 }; 1537 defer allocator.free(remote_url); 1538 1539 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1540 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1541 } 1542 1543 const is_ssh = switch (transport_def) { 1544 .file => false, 1545 .wire => |wire_kind| .ssh == wire_kind, 1546 }; 1547 1548 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 1549 defer allocator.free(receive_pack_command); 1550 1551 if (shell_out_to_git) { 1552 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1553 defer allocator.free(priv_key_path); 1554 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1555 defer allocator.free(ssh_config_arg); 1556 1557 // shell out to git so it will send delta objects 1558 var process = try std.process.spawn(io, .{ 1559 .argv = if (is_ssh) 1560 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" } 1561 else 1562 &.{ "git", "push", "origin", "master" }, 1563 .cwd = .{ .path = client_path }, 1564 .stdin = .ignore, 1565 .stdout = .ignore, 1566 .stderr = .ignore, 1567 }); 1568 const term = try process.wait(io); 1569 if (term != .exited) { 1570 return error.GitCommandFailed; 1571 } 1572 } else { 1573 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1574 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1575 defer allocator.free(known_hosts_path); 1576 1577 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1578 defer allocator.free(priv_key_path); 1579 1580 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1581 } else null; 1582 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1583 1584 try client_repo.push( 1585 io, 1586 allocator, 1587 "origin", 1588 "master", 1589 false, 1590 .{ .wire = .{ .ssh = .{ 1591 .command = ssh_cmd_maybe, 1592 .receive_pack_command = receive_pack_command, 1593 } } }, 1594 ); 1595 1596 if (transport_def == .file) { 1597 // update the working dir 1598 try server_repo.restore(io, allocator, "."); 1599 } 1600 } 1601 1602 // make sure push was successful 1603 { 1604 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1605 try std.testing.expectEqualStrings(&commit2, &oid_master); 1606 1607 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1608 defer hello_txt.close(io); 1609 } 1610 } 1611 1612 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1613 var iter = src_dir.iterate(); 1614 while (try iter.next(io)) |entry| { 1615 switch (entry.kind) { 1616 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1617 .directory => { 1618 try dest_dir.createDirPath(io, entry.name); 1619 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1620 defer dest_entry_dir.close(io); 1621 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1622 defer src_entry_dir.close(io); 1623 try copyDir(io, src_entry_dir, dest_entry_dir); 1624 }, 1625 else => {}, 1626 } 1627 } 1628 } 1629