╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1282 // change the files so git will send them as delta objects 1283 for (&[_][]const u8{"src"}) |dir_name| { 1284 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1285 defer dest_repo_dir.close(io); 1286 1287 { 1288 var iter = dest_repo_dir.iterate(); 1289 while (try iter.next(io)) |entry| { 1290 switch (entry.kind) { 1291 .file => { 1292 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1293 defer file.close(io); 1294 var writer = file.writer(io, &.{}); 1295 try writer.interface.writeAll("EDIT"); 1296 }, 1297 else => {}, 1298 } 1299 } 1300 } 1301 1302 try client_repo.add(io, allocator, &.{dir_name}); 1303 } 1304 1305 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1306 1307 // add remote 1308 { 1309 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1310 defer allocator.free(remote_url); 1311 1312 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1313 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1314 } 1315 1316 const is_ssh = switch (transport_def) { 1317 .file => false, 1318 .wire => |wire_kind| .ssh == wire_kind, 1319 }; 1320 1321 if (shell_out_to_git) { 1322 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1323 defer allocator.free(priv_key_path); 1324 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 }); 1325 defer allocator.free(ssh_config_arg); 1326 1327 // shell out to git so it will send delta objects 1328 var process = try std.process.spawn(io, .{ 1329 .argv = if (is_ssh) 1330 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1331 else 1332 &.{ "git", "push", "origin", "master" }, 1333 .cwd = .{ .path = client_path }, 1334 .stdin = .ignore, 1335 .stdout = .ignore, 1336 .stderr = .ignore, 1337 }); 1338 const term = try process.wait(io); 1339 if (term != .exited or term.exited != 0) { 1340 return error.GitCommandFailed; 1341 } 1342 } else { 1343 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1344 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1345 1346 try client_repo.push( 1347 io, 1348 allocator, 1349 "origin", 1350 "master", 1351 false, 1352 .{ .wire = .{ .ssh = .{ 1353 .command = ssh_cmd_maybe, 1354 } } }, 1355 ); 1356 } 1357 1358 // make sure push was successful 1359 { 1360 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1361 try std.testing.expectEqualStrings(&commit2, &oid_master); 1362 1363 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1364 defer hello_txt.close(io); 1365 } 1366 } 1367 1368 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1369 var iter = src_dir.iterate(); 1370 while (try iter.next(io)) |entry| { 1371 switch (entry.kind) { 1372 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1373 .directory => { 1374 try dest_dir.createDirPath(io, entry.name); 1375 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1376 defer dest_entry_dir.close(io); 1377 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1378 defer src_entry_dir.close(io); 1379 try copyDir(io, src_entry_dir, dest_entry_dir); 1380 }, 1381 else => {}, 1382 } 1383 } 1384 } 1385 1386 // the dev SSH public key matching the private key written by runServer 1387 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1388 1389 // create the admin event repo with a user holding the dev SSH public key 1390 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1391 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1392 defer allocator.free(cwd_path); 1393 1394 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1395 defer allocator.free(admin_repo_path); 1396 1397 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1398 defer repo.deinit(io, allocator); 1399 1400 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1401 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1402 1403 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1404 const user_id = evt.EventWithId.randomId(prng.random()); 1405 1406 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1407 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1408 1409 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1410 .id = std.fmt.bytesToHex(user_id, .lower), 1411 .author_email = "admin@example.test", 1412 .event = .{ .user = .{ 1413 .name = "admin", 1414 .display_name = "Admin", 1415 .email = "admin@example.test", 1416 .password_hash = password_hash, 1417 .ssh_keys = admin_ssh_key, 1418 } }, 1419 }}); 1420 } 1421 1422 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1423 // the event store 1424 fn repoOnDiskPath( 1425 io: std.Io, 1426 allocator: std.mem.Allocator, 1427 data_dir_name: []const u8, 1428 repo_name: []const u8, 1429 create: bool, 1430 ) !?[]u8 { 1431 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1432 defer allocator.free(cwd_path); 1433 1434 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1435 defer allocator.free(admin_repo_path); 1436 1437 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1438 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1439 } 1440 1441 // build the remote URL addressing admin/<repo_name> over the given transport 1442 fn remoteUrl( 1443 comptime transport_def: net.TransportDefinition, 1444 allocator: std.mem.Allocator, 1445 repo_name: []const u8, 1446 ) ![]u8 { 1447 return switch (transport_def) { 1448 .file => unreachable, 1449 .wire => |wire_kind| switch (wire_kind) { 1450 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1451 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1452 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1453 }, 1454 }; 1455 } 1456 1457 // the ssh command the haxy git client should invoke, or null when not using 1458 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1459 // and skip writing to known_hosts. 1460 fn sshCommand( 1461 is_ssh: bool, 1462 allocator: std.mem.Allocator, 1463 cwd_path: []const u8, 1464 temp_dir_name: []const u8, 1465 ) !?[]u8 { 1466 if (!is_ssh) return null; 1467 1468 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1469 defer allocator.free(priv_key_path); 1470 1471 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 }); 1472 } 1473