╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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