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