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