╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1132 1133 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 1134 defer allocator.free(temp_path); 1135 1136 // register the repo under admin and locate its on-disk directory 1137 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1138 defer allocator.free(server_path); 1139 1140 // init server repo with default branch name as main 1141 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1142 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 1143 defer server_repo.deinit(io, allocator); 1144 1145 if (shell_out_to_git) { 1146 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1147 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1148 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1149 } 1150 1151 // make a commit 1152 { 1153 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1154 defer hello_txt.close(io); 1155 try hello_txt.writeStreamingAll(io, "hello, world!"); 1156 try server_repo.add(io, allocator, &.{"hello.txt"}); 1157 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1158 } 1159 1160 // tag first commit 1161 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1162 1163 // make a commit 1164 { 1165 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 1166 defer goodbye_txt.close(io); 1167 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 1168 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 1169 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 1170 } 1171 1172 // export server repo 1173 { 1174 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1175 defer export_file.close(io); 1176 } 1177 1178 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1179 defer allocator.free(client_path); 1180 1181 // get remote url 1182 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1183 defer allocator.free(remote_url); 1184 1185 const is_ssh = switch (transport_def) { 1186 .file => false, 1187 .wire => |wire_kind| .ssh == wire_kind, 1188 }; 1189 1190 if (shell_out_to_git) { 1191 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1192 defer allocator.free(priv_key_path); 1193 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1194 defer allocator.free(ssh_config_arg); 1195 1196 { 1197 var process = try std.process.spawn(io, .{ 1198 .argv = if (is_ssh) 1199 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 1200 else 1201 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1202 .cwd = .{ .path = temp_path }, 1203 .stdin = .ignore, 1204 .stdout = .ignore, 1205 .stderr = .ignore, 1206 }); 1207 const term = try process.wait(io); 1208 if (term != .exited or term.exited != 0) { 1209 return error.GitCommandFailed; 1210 } 1211 } 1212 1213 // make sure shallow clone was successful 1214 { 1215 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1216 hello_txt.close(io); 1217 } 1218 1219 // make a third commit on the server 1220 { 1221 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1222 defer extra_txt.close(io); 1223 try extra_txt.writeStreamingAll(io, "extra content"); 1224 try server_repo.add(io, allocator, &.{"extra.txt"}); 1225 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1226 } 1227 1228 // pull --unshallow to deepen the clone and get the new commit 1229 { 1230 var process = try std.process.spawn(io, .{ 1231 .argv = if (is_ssh) 1232 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 1233 else 1234 &.{ "git", "pull", "--unshallow" }, 1235 .cwd = .{ .path = client_path }, 1236 .stdin = .ignore, 1237 .stdout = .ignore, 1238 .stderr = .ignore, 1239 }); 1240 const term = try process.wait(io); 1241 if (term != .exited or term.exited != 0) { 1242 return error.GitCommandFailed; 1243 } 1244 } 1245 1246 // make sure unshallow pull was successful 1247 { 1248 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 1249 extra_txt.close(io); 1250 } 1251 1252 // delete client and clone again with --shallow-since 1253 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1254 1255 { 1256 var process = try std.process.spawn(io, .{ 1257 .argv = if (is_ssh) 1258 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 1259 else 1260 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1261 .cwd = .{ .path = temp_path }, 1262 .stdin = .ignore, 1263 .stdout = .ignore, 1264 .stderr = .ignore, 1265 }); 1266 const term = try process.wait(io); 1267 if (term != .exited or term.exited != 0) { 1268 return error.GitCommandFailed; 1269 } 1270 } 1271 1272 // make sure shallow clone was successful 1273 { 1274 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1275 hello_txt.close(io); 1276 } 1277 1278 // delete client and clone again with --shallow-exclude 1279 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1280 1281 { 1282 var process = try std.process.spawn(io, .{ 1283 .argv = if (is_ssh) 1284 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 1285 else 1286 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1287 .cwd = .{ .path = temp_path }, 1288 .stdin = .ignore, 1289 .stdout = .ignore, 1290 .stderr = .ignore, 1291 }); 1292 const term = try process.wait(io); 1293 if (term != .exited or term.exited != 0) { 1294 return error.GitCommandFailed; 1295 } 1296 } 1297 1298 // make sure shallow clone was successful 1299 { 1300 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1301 hello_txt.close(io); 1302 } 1303 1304 // delete client and clone again with --filter=blob:none 1305 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1306 1307 { 1308 var process = try std.process.spawn(io, .{ 1309 .argv = if (is_ssh) 1310 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 1311 else 1312 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1313 .cwd = .{ .path = temp_path }, 1314 .stdin = .ignore, 1315 .stdout = .ignore, 1316 .stderr = .ignore, 1317 }); 1318 const term = try process.wait(io); 1319 if (term != .exited or term.exited != 0) { 1320 return error.GitCommandFailed; 1321 } 1322 } 1323 1324 // make sure partial clone was successful 1325 { 1326 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1327 hello_txt.close(io); 1328 } 1329 1330 // delete client and clone again with --filter=tree:0 1331 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1332 1333 { 1334 var process = try std.process.spawn(io, .{ 1335 .argv = if (is_ssh) 1336 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 1337 else 1338 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1339 .cwd = .{ .path = temp_path }, 1340 .stdin = .ignore, 1341 .stdout = .ignore, 1342 .stderr = .ignore, 1343 }); 1344 const term = try process.wait(io); 1345 if (term != .exited or term.exited != 0) { 1346 return error.GitCommandFailed; 1347 } 1348 } 1349 1350 // make sure treeless clone was successful 1351 { 1352 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1353 goodbye_txt.close(io); 1354 } 1355 } else { 1356 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1357 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1358 1359 // clone repo 1360 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1361 io, 1362 allocator, 1363 remote_url, 1364 temp_path, 1365 client_path, 1366 null, 1367 .{ .wire = .{ .ssh = .{ 1368 .command = ssh_cmd_maybe, 1369 } } }, 1370 ); 1371 defer client_repo.deinit(io, allocator); 1372 1373 // make sure HEAD points to the right default branch 1374 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1375 const head = try client_repo.head(io, &current_branch_buffer); 1376 try std.testing.expectEqualStrings("main", head.ref.name); 1377 1378 // make sure clone was successful 1379 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1380 defer hello_txt.close(io); 1381 } 1382 } 1383 1384 fn testFetchLarge( 1385 comptime repo_kind: rp.RepoKind, 1386 comptime transport_def: net.TransportDefinition, 1387 comptime shell_out_to_git: bool, 1388 comptime port: u16, 1389 io: std.Io, 1390 allocator: std.mem.Allocator, 1391 ) !void { 1392 const temp_dir_name = "temp-testnet-fetch-large"; 1393 1394 // create the temp dir 1395 const cwd = std.Io.Dir.cwd(); 1396 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1397 if (temp_dir_or_err) |*temp_dir| { 1398 temp_dir.close(io); 1399 try cwd.deleteTree(io, temp_dir_name); 1400 } else |_| {} 1401 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1402 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1403 defer temp_dir.close(io); 1404 1405 // init server 1406 var server_process = try runServer(io, allocator, temp_dir_name, port); 1407 defer _ = server_process.kill(io); 1408 1409 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1410 defer allocator.free(cwd_path); 1411 1412 // register the repo under admin and locate its on-disk directory 1413 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1414 defer allocator.free(server_path); 1415 1416 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1417 defer server_repo.deinit(io, allocator); 1418 1419 var server_dir = try cwd.openDir(io, server_path, .{}); 1420 defer server_dir.close(io); 1421 1422 // copy files from current repo into server dir 1423 for (&[_][]const u8{"src"}) |dir_name| { 1424 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1425 defer src_repo_dir.close(io); 1426 1427 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1428 defer dest_repo_dir.close(io); 1429 1430 try copyDir(io, src_repo_dir, dest_repo_dir); 1431 1432 try server_repo.add(io, allocator, &.{dir_name}); 1433 } 1434 1435 // make a commit 1436 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1437 1438 // export server repo 1439 { 1440 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1441 defer export_file.close(io); 1442 } 1443 1444 if (shell_out_to_git) { 1445 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1446 } 1447 1448 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1449 defer allocator.free(client_path); 1450 1451 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1452 defer client_repo.deinit(io, allocator); 1453 1454 // add remote 1455 { 1456 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1457 defer allocator.free(remote_url); 1458 1459 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1460 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1461 } 1462 1463 const is_ssh = switch (transport_def) { 1464 .file => false, 1465 .wire => |wire_kind| .ssh == wire_kind, 1466 }; 1467 1468 if (shell_out_to_git) { 1469 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1470 defer allocator.free(priv_key_path); 1471 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1472 defer allocator.free(ssh_config_arg); 1473 1474 { 1475 var process = try std.process.spawn(io, .{ 1476 .argv = if (is_ssh) 1477 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1478 else 1479 &.{ "git", "pull", "origin", "master" }, 1480 .cwd = .{ .path = client_path }, 1481 .stdin = .ignore, 1482 .stdout = .ignore, 1483 .stderr = .ignore, 1484 }); 1485 const term = try process.wait(io); 1486 if (term != .exited or term.exited != 0) { 1487 return error.GitCommandFailed; 1488 } 1489 } 1490 1491 // make sure pull was successful 1492 { 1493 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1494 try std.testing.expectEqualStrings(&commit1, &oid_master); 1495 } 1496 1497 // make another commit on the server 1498 const commit2 = blk: { 1499 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1500 defer extra_txt.close(io); 1501 try extra_txt.writeStreamingAll(io, "extra content"); 1502 try server_repo.add(io, allocator, &.{"extra.txt"}); 1503 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1504 }; 1505 1506 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1507 { 1508 var process = try std.process.spawn(io, .{ 1509 .argv = if (is_ssh) 1510 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1511 else 1512 &.{ "git", "fetch", "origin", "master" }, 1513 .cwd = .{ .path = client_path }, 1514 .stdin = .ignore, 1515 .stdout = .ignore, 1516 .stderr = .ignore, 1517 }); 1518 const term = try process.wait(io); 1519 if (term != .exited or term.exited != 0) { 1520 return error.GitCommandFailed; 1521 } 1522 } 1523 1524 // make sure fetch with want-ref was successful 1525 { 1526 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1527 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1528 } 1529 } else { 1530 const refspecs = &.{ 1531 "+refs/heads/master:refs/heads/master", 1532 }; 1533 1534 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1535 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1536 1537 try client_repo.fetch( 1538 io, 1539 allocator, 1540 "origin", 1541 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1542 .command = ssh_cmd_maybe, 1543 } } }, 1544 ); 1545 1546 // update the working dir 1547 try client_repo.restore(io, allocator, "."); 1548 } 1549 1550 // make sure fetch was successful 1551 { 1552 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1553 try std.testing.expectEqualStrings(&commit1, &oid_master); 1554 } 1555 } 1556 1557 fn testPushLarge( 1558 comptime repo_kind: rp.RepoKind, 1559 comptime transport_def: net.TransportDefinition, 1560 comptime shell_out_to_git: bool, 1561 comptime port: u16, 1562 io: std.Io, 1563 allocator: std.mem.Allocator, 1564 ) !void { 1565 const temp_dir_name = "temp-testnet-push-large"; 1566 1567 // create the temp dir 1568 const cwd = std.Io.Dir.cwd(); 1569 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1570 if (temp_dir_or_err) |*temp_dir| { 1571 temp_dir.close(io); 1572 try cwd.deleteTree(io, temp_dir_name); 1573 } else |_| {} 1574 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1575 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1576 defer temp_dir.close(io); 1577 1578 // init server 1579 var server_process = try runServer(io, allocator, temp_dir_name, port); 1580 defer _ = server_process.kill(io); 1581 1582 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1583 defer allocator.free(cwd_path); 1584 1585 // register the repo under admin and locate its on-disk directory 1586 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1587 defer allocator.free(server_path); 1588 1589 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1590 defer server_repo.deinit(io, allocator); 1591 1592 // add config 1593 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1594 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1595 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1596 1597 // export server repo 1598 { 1599 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1600 defer export_file.close(io); 1601 } 1602 1603 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1604 defer allocator.free(client_path); 1605 1606 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1607 defer client_repo.deinit(io, allocator); 1608 1609 var client_dir = try cwd.openDir(io, client_path, .{}); 1610 defer client_dir.close(io); 1611 1612 { 1613 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1614 defer hello_txt.close(io); 1615 try hello_txt.writeStreamingAll(io, "hello, world!"); 1616 try client_repo.add(io, allocator, &.{"hello.txt"}); 1617 } 1618 1619 // copy files from current repo into client dir 1620 for (&[_][]const u8{"src"}) |dir_name| { 1621 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1622 defer src_repo_dir.close(io); 1623 1624 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1625 defer dest_repo_dir.close(io); 1626 1627 try copyDir(io, src_repo_dir, dest_repo_dir); 1628 1629 try client_repo.add(io, allocator, &.{dir_name}); 1630 } 1631 1632 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1633 1634 // change the files so git will send them as delta objects 1635 for (&[_][]const u8{"src"}) |dir_name| { 1636 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1637 defer dest_repo_dir.close(io); 1638 1639 { 1640 var iter = dest_repo_dir.iterate(); 1641 while (try iter.next(io)) |entry| { 1642 switch (entry.kind) { 1643 .file => { 1644 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1645 defer file.close(io); 1646 var writer = file.writer(io, &.{}); 1647 try writer.interface.writeAll("EDIT"); 1648 }, 1649 else => {}, 1650 } 1651 } 1652 } 1653 1654 try client_repo.add(io, allocator, &.{dir_name}); 1655 } 1656 1657 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1658 1659 // add remote 1660 { 1661 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1662 defer allocator.free(remote_url); 1663 1664 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1665 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1666 } 1667 1668 const is_ssh = switch (transport_def) { 1669 .file => false, 1670 .wire => |wire_kind| .ssh == wire_kind, 1671 }; 1672 1673 if (shell_out_to_git) { 1674 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1675 defer allocator.free(priv_key_path); 1676 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1677 defer allocator.free(ssh_config_arg); 1678 1679 // shell out to git so it will send delta objects 1680 var process = try std.process.spawn(io, .{ 1681 .argv = if (is_ssh) 1682 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1683 else 1684 &.{ "git", "push", "origin", "master" }, 1685 .cwd = .{ .path = client_path }, 1686 .stdin = .ignore, 1687 .stdout = .ignore, 1688 .stderr = .ignore, 1689 }); 1690 const term = try process.wait(io); 1691 if (term != .exited or term.exited != 0) { 1692 return error.GitCommandFailed; 1693 } 1694 } else { 1695 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1696 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1697 1698 try client_repo.push( 1699 io, 1700 allocator, 1701 "origin", 1702 "master", 1703 false, 1704 .{ .wire = .{ .ssh = .{ 1705 .command = ssh_cmd_maybe, 1706 } } }, 1707 ); 1708 } 1709 1710 // make sure push was successful 1711 { 1712 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1713 try std.testing.expectEqualStrings(&commit2, &oid_master); 1714 1715 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1716 defer hello_txt.close(io); 1717 } 1718 } 1719 1720 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1721 var iter = src_dir.iterate(); 1722 while (try iter.next(io)) |entry| { 1723 switch (entry.kind) { 1724 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1725 .directory => { 1726 try dest_dir.createDirPath(io, entry.name); 1727 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1728 defer dest_entry_dir.close(io); 1729 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1730 defer src_entry_dir.close(io); 1731 try copyDir(io, src_entry_dir, dest_entry_dir); 1732 }, 1733 else => {}, 1734 } 1735 } 1736 } 1737 1738 // the dev SSH public key matching the private key written by runServer 1739 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1740 1741 // create the admin event repo with a user holding the dev SSH public key 1742 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1743 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1744 defer allocator.free(cwd_path); 1745 1746 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1747 defer allocator.free(admin_repo_path); 1748 1749 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1750 defer repo.deinit(io, allocator); 1751 1752 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1753 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1754 1755 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1756 const user_id = evt.EventWithId.randomId(prng.random()); 1757 1758 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1759 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1760 1761 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1762 .id = std.fmt.bytesToHex(user_id, .lower), 1763 .author = .{ .name = "admin", .email = "admin@example.test" }, 1764 .event = .{ .user = .{ 1765 .name = "admin", 1766 .email = "admin@example.test", 1767 .password_hash = password_hash, 1768 .ssh_keys = admin_ssh_key, 1769 } }, 1770 }}); 1771 } 1772 1773 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1774 // the event store 1775 fn repoOnDiskPath( 1776 io: std.Io, 1777 allocator: std.mem.Allocator, 1778 data_dir_name: []const u8, 1779 repo_name: []const u8, 1780 create: bool, 1781 ) !?[]u8 { 1782 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1783 defer allocator.free(cwd_path); 1784 1785 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1786 defer allocator.free(admin_repo_path); 1787 1788 const event_id_hex = (try evt.resolveOrCreateRepo( 1789 io, 1790 allocator, 1791 admin_repo_path, 1792 "admin", 1793 repo_name, 1794 if (create) .{ .read_access = .public } else null, 1795 )) orelse return null; 1796 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1797 } 1798 1799 // build the remote URL addressing admin/<repo_name> over the given transport 1800 fn remoteUrl( 1801 comptime transport_def: net.TransportDefinition, 1802 allocator: std.mem.Allocator, 1803 repo_name: []const u8, 1804 port: u16, 1805 ) ![]u8 { 1806 return switch (transport_def) { 1807 .file => unreachable, 1808 .wire => |wire_kind| switch (wire_kind) { 1809 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1810 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1811 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1812 }, 1813 }; 1814 } 1815 1816 // the ssh command the haxy git client should invoke, or null when not using 1817 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1818 // and skip writing to known_hosts. 1819 fn sshCommand( 1820 is_ssh: bool, 1821 allocator: std.mem.Allocator, 1822 cwd_path: []const u8, 1823 temp_dir_name: []const u8, 1824 port: u16, 1825 ) !?[]u8 { 1826 if (!is_ssh) return null; 1827 1828 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1829 defer allocator.free(priv_key_path); 1830 1831 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1832 } 1833