╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1205 { 1206 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1207 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1208 } 1209 } else { 1210 const refspecs = &.{ 1211 "+refs/heads/master:refs/heads/master", 1212 }; 1213 1214 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1215 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1216 defer allocator.free(known_hosts_path); 1217 1218 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1219 defer allocator.free(priv_key_path); 1220 1221 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1222 } else null; 1223 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1224 1225 try client_repo.fetch( 1226 io, 1227 allocator, 1228 "origin", 1229 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1230 .command = ssh_cmd_maybe, 1231 } } }, 1232 ); 1233 1234 // update the working dir 1235 try client_repo.restore(io, allocator, "."); 1236 } 1237 1238 // make sure fetch was successful 1239 { 1240 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1241 try std.testing.expectEqualStrings(&commit1, &oid_master); 1242 } 1243 } 1244 1245 fn testPushLarge( 1246 comptime repo_kind: rp.RepoKind, 1247 comptime transport_def: net.TransportDefinition, 1248 comptime port: u16, 1249 comptime shell_out_to_git: bool, 1250 io: std.Io, 1251 allocator: std.mem.Allocator, 1252 ) !void { 1253 const temp_dir_name = "temp-testnet-push-large"; 1254 1255 // create the temp dir 1256 const cwd = std.Io.Dir.cwd(); 1257 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1258 if (temp_dir_or_err) |*temp_dir| { 1259 temp_dir.close(io); 1260 try cwd.deleteTree(io, temp_dir_name); 1261 } else |_| {} 1262 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1263 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1264 defer temp_dir.close(io); 1265 1266 // init server 1267 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1268 try server.start(); 1269 defer server.stop(); 1270 1271 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1272 defer allocator.free(cwd_path); 1273 1274 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1275 defer allocator.free(server_path); 1276 1277 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1278 defer server_repo.deinit(io, allocator); 1279 1280 // add config 1281 switch (transport_def) { 1282 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1283 .wire => { 1284 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1285 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1286 }, 1287 } 1288 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1289 1290 // export server repo 1291 { 1292 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1293 defer export_file.close(io); 1294 } 1295 1296 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1297 defer allocator.free(client_path); 1298 1299 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1300 defer client_repo.deinit(io, allocator); 1301 1302 var client_dir = try cwd.openDir(io, client_path, .{}); 1303 defer client_dir.close(io); 1304 1305 { 1306 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1307 defer hello_txt.close(io); 1308 try hello_txt.writeStreamingAll(io, "hello, world!"); 1309 try client_repo.add(io, allocator, &.{"hello.txt"}); 1310 } 1311 1312 // copy files from current repo into client dir 1313 for (&[_][]const u8{"src"}) |dir_name| { 1314 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1315 defer src_repo_dir.close(io); 1316 1317 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1318 defer dest_repo_dir.close(io); 1319 1320 try copyDir(io, src_repo_dir, dest_repo_dir); 1321 1322 try client_repo.add(io, allocator, &.{dir_name}); 1323 } 1324 1325 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1326 1327 // change the files so git will send them as delta objects 1328 for (&[_][]const u8{"src"}) |dir_name| { 1329 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1330 defer dest_repo_dir.close(io); 1331 1332 { 1333 var iter = dest_repo_dir.iterate(); 1334 while (try iter.next(io)) |entry| { 1335 switch (entry.kind) { 1336 .file => { 1337 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1338 defer file.close(io); 1339 var writer = file.writer(io, &.{}); 1340 try writer.interface.writeAll("EDIT"); 1341 }, 1342 else => {}, 1343 } 1344 } 1345 } 1346 1347 try client_repo.add(io, allocator, &.{dir_name}); 1348 } 1349 1350 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1351 1352 // add remote 1353 { 1354 if (.windows == builtin.os.tag) { 1355 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1356 } 1357 const separator = if (server_path[0] == '/') "" else "/"; 1358 1359 const remote_url = switch (transport_def) { 1360 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1361 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1362 .wire => |wire_kind| switch (wire_kind) { 1363 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1364 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1365 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1366 }, 1367 }; 1368 defer allocator.free(remote_url); 1369 1370 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1371 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1372 } 1373 1374 const is_ssh = switch (transport_def) { 1375 .file => false, 1376 .wire => |wire_kind| .ssh == wire_kind, 1377 }; 1378 1379 1380 if (shell_out_to_git) { 1381 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1382 defer allocator.free(priv_key_path); 1383 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 1384 defer allocator.free(ssh_config_arg); 1385 1386 // shell out to git so it will send delta objects 1387 var process = try std.process.spawn(io, .{ 1388 .argv = if (is_ssh) 1389 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1390 else 1391 &.{ "git", "push", "origin", "master" }, 1392 .cwd = .{ .path = client_path }, 1393 .stdin = .ignore, 1394 .stdout = .ignore, 1395 .stderr = .ignore, 1396 }); 1397 const term = try process.wait(io); 1398 if (term != .exited or term.exited != 0) { 1399 return error.GitCommandFailed; 1400 } 1401 } else { 1402 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1403 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1404 defer allocator.free(known_hosts_path); 1405 1406 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1407 defer allocator.free(priv_key_path); 1408 1409 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1410 } else null; 1411 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1412 1413 try client_repo.push( 1414 io, 1415 allocator, 1416 "origin", 1417 "master", 1418 false, 1419 .{ .wire = .{ .ssh = .{ 1420 .command = ssh_cmd_maybe, 1421 } } }, 1422 ); 1423 1424 if (transport_def == .file) { 1425 // update the working dir 1426 try server_repo.restore(io, allocator, "."); 1427 } 1428 } 1429 1430 // make sure push was successful 1431 { 1432 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1433 try std.testing.expectEqualStrings(&commit2, &oid_master); 1434 1435 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1436 defer hello_txt.close(io); 1437 } 1438 } 1439 1440 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1441 var iter = src_dir.iterate(); 1442 while (try iter.next(io)) |entry| { 1443 switch (entry.kind) { 1444 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1445 .directory => { 1446 try dest_dir.createDirPath(io, entry.name); 1447 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1448 defer dest_entry_dir.close(io); 1449 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1450 defer src_entry_dir.close(io); 1451 try copyDir(io, src_entry_dir, dest_entry_dir); 1452 }, 1453 else => {}, 1454 } 1455 } 1456 } 1457