╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
888 889 const cwd_path = try std.process.currentPathAlloc(io, allocator); 890 defer allocator.free(cwd_path); 891 892 // register the repo under admin and locate its on-disk directory 893 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 894 defer allocator.free(server_path); 895 896 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 897 defer server_repo.deinit(io, allocator); 898 899 var server_dir = try cwd.openDir(io, server_path, .{}); 900 defer server_dir.close(io); 901 902 // copy files from current repo into server dir 903 for (&[_][]const u8{"src"}) |dir_name| { 904 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 905 defer src_repo_dir.close(io); 906 907 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 908 defer dest_repo_dir.close(io); 909 910 try copyDir(io, src_repo_dir, dest_repo_dir); 911 912 try server_repo.add(io, allocator, &.{dir_name}); 913 } 914 915 // make a commit 916 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 917 918 // export server repo 919 { 920 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 921 defer export_file.close(io); 922 } 923 924 if (shell_out_to_git) { 925 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 926 } 927 928 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 929 defer allocator.free(client_path); 930 931 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 932 defer client_repo.deinit(io, allocator); 933 934 // add remote 935 { 936 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 937 defer allocator.free(remote_url); 938 939 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 940 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 941 } 942 943 const is_ssh = switch (transport_def) { 944 .file => false, 945 .wire => |wire_kind| .ssh == wire_kind, 946 }; 947 948 if (shell_out_to_git) { 949 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 950 defer allocator.free(priv_key_path); 951 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 }); 952 defer allocator.free(ssh_config_arg); 953 954 { 955 var process = try std.process.spawn(io, .{ 956 .argv = if (is_ssh) 957 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 958 else 959 &.{ "git", "pull", "origin", "master" }, 960 .cwd = .{ .path = client_path }, 961 .stdin = .ignore, 962 .stdout = .ignore, 963 .stderr = .ignore, 964 }); 965 const term = try process.wait(io); 966 if (term != .exited or term.exited != 0) { 967 return error.GitCommandFailed; 968 } 969 } 970 971 // make sure pull was successful 972 { 973 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 974 try std.testing.expectEqualStrings(&commit1, &oid_master); 975 } 976 977 // make another commit on the server 978 const commit2 = blk: { 979 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 980 defer extra_txt.close(io); 981 try extra_txt.writeStreamingAll(io, "extra content"); 982 try server_repo.add(io, allocator, &.{"extra.txt"}); 983 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 984 }; 985 986 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 987 { 988 var process = try std.process.spawn(io, .{ 989 .argv = if (is_ssh) 990 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 991 else 992 &.{ "git", "fetch", "origin", "master" }, 993 .cwd = .{ .path = client_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 fetch with want-ref was successful 1005 { 1006 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1007 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1008 } 1009 } else { 1010 const refspecs = &.{ 1011 "+refs/heads/master:refs/heads/master", 1012 }; 1013 1014 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1015 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1016 1017 try client_repo.fetch( 1018 io, 1019 allocator, 1020 "origin", 1021 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1022 .command = ssh_cmd_maybe, 1023 } } }, 1024 ); 1025 1026 // update the working dir 1027 try client_repo.restore(io, allocator, "."); 1028 } 1029 1030 // make sure fetch was successful 1031 { 1032 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1033 try std.testing.expectEqualStrings(&commit1, &oid_master); 1034 } 1035 } 1036 1037 fn testPushLarge( 1038 comptime repo_kind: rp.RepoKind, 1039 comptime transport_def: net.TransportDefinition, 1040 comptime shell_out_to_git: bool, 1041 io: std.Io, 1042 allocator: std.mem.Allocator, 1043 ) !void { 1044 const temp_dir_name = "temp-testnet-push-large"; 1045 1046 // create the temp dir 1047 const cwd = std.Io.Dir.cwd(); 1048 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1049 if (temp_dir_or_err) |*temp_dir| { 1050 temp_dir.close(io); 1051 try cwd.deleteTree(io, temp_dir_name); 1052 } else |_| {} 1053 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1054 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1055 defer temp_dir.close(io); 1056 1057 // init server 1058 var server_process = try runServer(io, allocator, temp_dir_name); 1059 defer _ = server_process.kill(io); 1060 1061 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1062 defer allocator.free(cwd_path); 1063 1064 // register the repo under admin and locate its on-disk directory 1065 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1066 defer allocator.free(server_path); 1067 1068 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1069 defer server_repo.deinit(io, allocator); 1070 1071 // add config 1072 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1073 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1074 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1075 1076 // export server repo 1077 { 1078 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1079 defer export_file.close(io); 1080 } 1081 1082 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1083 defer allocator.free(client_path); 1084 1085 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1086 defer client_repo.deinit(io, allocator); 1087 1088 var client_dir = try cwd.openDir(io, client_path, .{}); 1089 defer client_dir.close(io); 1090 1091 { 1092 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1093 defer hello_txt.close(io); 1094 try hello_txt.writeStreamingAll(io, "hello, world!"); 1095 try client_repo.add(io, allocator, &.{"hello.txt"}); 1096 } 1097 1098 // copy files from current repo into client dir 1099 for (&[_][]const u8{"src"}) |dir_name| { 1100 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1101 defer src_repo_dir.close(io); 1102 1103 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1104 defer dest_repo_dir.close(io); 1105 1106 try copyDir(io, src_repo_dir, dest_repo_dir); 1107 1108 try client_repo.add(io, allocator, &.{dir_name}); 1109 } 1110 1111 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1112 1113 // change the files so git will send them as delta objects 1114 for (&[_][]const u8{"src"}) |dir_name| { 1115 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1116 defer dest_repo_dir.close(io); 1117 1118 { 1119 var iter = dest_repo_dir.iterate(); 1120 while (try iter.next(io)) |entry| { 1121 switch (entry.kind) { 1122 .file => { 1123 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1124 defer file.close(io); 1125 var writer = file.writer(io, &.{}); 1126 try writer.interface.writeAll("EDIT"); 1127 }, 1128 else => {}, 1129 } 1130 } 1131 } 1132 1133 try client_repo.add(io, allocator, &.{dir_name}); 1134 } 1135 1136 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1137 1138 // add remote 1139 { 1140 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1141 defer allocator.free(remote_url); 1142 1143 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1144 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1145 } 1146 1147 const is_ssh = switch (transport_def) { 1148 .file => false, 1149 .wire => |wire_kind| .ssh == wire_kind, 1150 }; 1151 1152 if (shell_out_to_git) { 1153 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1154 defer allocator.free(priv_key_path); 1155 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 }); 1156 defer allocator.free(ssh_config_arg); 1157 1158 // shell out to git so it will send delta objects 1159 var process = try std.process.spawn(io, .{ 1160 .argv = if (is_ssh) 1161 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1162 else 1163 &.{ "git", "push", "origin", "master" }, 1164 .cwd = .{ .path = client_path }, 1165 .stdin = .ignore, 1166 .stdout = .ignore, 1167 .stderr = .ignore, 1168 }); 1169 const term = try process.wait(io); 1170 if (term != .exited or term.exited != 0) { 1171 return error.GitCommandFailed; 1172 } 1173 } else { 1174 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1175 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1176 1177 try client_repo.push( 1178 io, 1179 allocator, 1180 "origin", 1181 "master", 1182 false, 1183 .{ .wire = .{ .ssh = .{ 1184 .command = ssh_cmd_maybe, 1185 } } }, 1186 ); 1187 } 1188 1189 // make sure push was successful 1190 { 1191 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1192 try std.testing.expectEqualStrings(&commit2, &oid_master); 1193 1194 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1195 defer hello_txt.close(io); 1196 } 1197 } 1198 1199 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1200 var iter = src_dir.iterate(); 1201 while (try iter.next(io)) |entry| { 1202 switch (entry.kind) { 1203 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1204 .directory => { 1205 try dest_dir.createDirPath(io, entry.name); 1206 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1207 defer dest_entry_dir.close(io); 1208 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1209 defer src_entry_dir.close(io); 1210 try copyDir(io, src_entry_dir, dest_entry_dir); 1211 }, 1212 else => {}, 1213 } 1214 } 1215 } 1216 1217 // the dev SSH public key matching the private key written by runServer 1218 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1219 1220 // create the admin event repo with a user holding the dev SSH public key 1221 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1222 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1223 defer allocator.free(cwd_path); 1224 1225 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1226 defer allocator.free(admin_repo_path); 1227 1228 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1229 defer repo.deinit(io, allocator); 1230 1231 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1232 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1233 1234 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1235 const user_id = evt.EventWithId.randomId(prng.random()); 1236 1237 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1238 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1239 1240 try evt.commitAndConsume(evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1241 .id = std.fmt.bytesToHex(user_id, .lower), 1242 .event = .{ .user = .{ 1243 .name = "admin", 1244 .display_name = "Admin", 1245 .email = "admin@example.test", 1246 .password_hash = password_hash, 1247 .ssh_keys = admin_ssh_key, 1248 } }, 1249 }}); 1250 } 1251 1252 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1253 // the event store 1254 fn repoOnDiskPath( 1255 io: std.Io, 1256 allocator: std.mem.Allocator, 1257 data_dir_name: []const u8, 1258 repo_name: []const u8, 1259 create: bool, 1260 ) !?[]u8 { 1261 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1262 defer allocator.free(cwd_path); 1263 1264 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1265 defer allocator.free(admin_repo_path); 1266 1267 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1268 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1269 } 1270 1271 // build the remote URL addressing admin/<repo_name> over the given transport 1272 fn remoteUrl( 1273 comptime transport_def: net.TransportDefinition, 1274 allocator: std.mem.Allocator, 1275 repo_name: []const u8, 1276 ) ![]u8 { 1277 return switch (transport_def) { 1278 .file => unreachable, 1279 .wire => |wire_kind| switch (wire_kind) { 1280 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1281 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1282 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1283 }, 1284 }; 1285 } 1286 1287 // the ssh command the haxy git client should invoke, or null when not using 1288 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1289 // and skip writing to known_hosts. 1290 fn sshCommand( 1291 is_ssh: bool, 1292 allocator: std.mem.Allocator, 1293 cwd_path: []const u8, 1294 temp_dir_name: []const u8, 1295 ) !?[]u8 { 1296 if (!is_ssh) return null; 1297 1298 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1299 defer allocator.free(priv_key_path); 1300 1301 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ ssh_port, priv_key_path }); 1302 } 1303