╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1386 1387 try server_repo.add(io, allocator, &.{dir_name}); 1388 } 1389 1390 // make a commit 1391 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1392 1393 // export server repo 1394 { 1395 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1396 defer export_file.close(io); 1397 } 1398 1399 if (shell_out_to_git) { 1400 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1401 } 1402 1403 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1404 defer allocator.free(client_path); 1405 1406 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1407 defer client_repo.deinit(io, allocator); 1408 1409 // add remote 1410 { 1411 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1412 defer allocator.free(remote_url); 1413 1414 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1415 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1416 } 1417 1418 const is_ssh = switch (transport_def) { 1419 .file => false, 1420 .wire => |wire_kind| .ssh == wire_kind, 1421 }; 1422 1423 if (shell_out_to_git) { 1424 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1425 defer allocator.free(priv_key_path); 1426 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 }); 1427 defer allocator.free(ssh_config_arg); 1428 1429 { 1430 var process = try std.process.spawn(io, .{ 1431 .argv = if (is_ssh) 1432 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1433 else 1434 &.{ "git", "pull", "origin", "master" }, 1435 .cwd = .{ .path = client_path }, 1436 .stdin = .ignore, 1437 .stdout = .ignore, 1438 .stderr = .ignore, 1439 }); 1440 const term = try process.wait(io); 1441 if (term != .exited or term.exited != 0) { 1442 return error.GitCommandFailed; 1443 } 1444 } 1445 1446 // make sure pull was successful 1447 { 1448 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1449 try std.testing.expectEqualStrings(&commit1, &oid_master); 1450 } 1451 1452 // make another commit on the server 1453 const commit2 = blk: { 1454 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1455 defer extra_txt.close(io); 1456 try extra_txt.writeStreamingAll(io, "extra content"); 1457 try server_repo.add(io, allocator, &.{"extra.txt"}); 1458 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1459 }; 1460 1461 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1462 { 1463 var process = try std.process.spawn(io, .{ 1464 .argv = if (is_ssh) 1465 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1466 else 1467 &.{ "git", "fetch", "origin", "master" }, 1468 .cwd = .{ .path = client_path }, 1469 .stdin = .ignore, 1470 .stdout = .ignore, 1471 .stderr = .ignore, 1472 }); 1473 const term = try process.wait(io); 1474 if (term != .exited or term.exited != 0) { 1475 return error.GitCommandFailed; 1476 } 1477 } 1478 1479 // make sure fetch with want-ref was successful 1480 { 1481 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1482 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1483 } 1484 } else { 1485 const refspecs = &.{ 1486 "+refs/heads/master:refs/heads/master", 1487 }; 1488 1489 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1490 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1491 1492 try client_repo.fetch( 1493 io, 1494 allocator, 1495 "origin", 1496 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1497 .command = ssh_cmd_maybe, 1498 } } }, 1499 ); 1500 1501 // update the working dir 1502 try client_repo.restore(io, allocator, "."); 1503 } 1504 1505 // make sure fetch was successful 1506 { 1507 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1508 try std.testing.expectEqualStrings(&commit1, &oid_master); 1509 } 1510 } 1511 1512 fn testPushLarge( 1513 comptime repo_kind: rp.RepoKind, 1514 comptime transport_def: net.TransportDefinition, 1515 comptime shell_out_to_git: bool, 1516 comptime port: u16, 1517 io: std.Io, 1518 allocator: std.mem.Allocator, 1519 ) !void { 1520 const temp_dir_name = "temp-testnet-push-large"; 1521 1522 // create the temp dir 1523 const cwd = std.Io.Dir.cwd(); 1524 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1525 if (temp_dir_or_err) |*temp_dir| { 1526 temp_dir.close(io); 1527 try cwd.deleteTree(io, temp_dir_name); 1528 } else |_| {} 1529 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1530 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1531 defer temp_dir.close(io); 1532 1533 // init server 1534 var server_process = try runServer(io, allocator, temp_dir_name, port); 1535 defer _ = server_process.kill(io); 1536 1537 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1538 defer allocator.free(cwd_path); 1539 1540 // register the repo under admin and locate its on-disk directory 1541 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1542 defer allocator.free(server_path); 1543 1544 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1545 defer server_repo.deinit(io, allocator); 1546 1547 // add config 1548 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1549 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1550 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1551 1552 // export server repo 1553 { 1554 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1555 defer export_file.close(io); 1556 } 1557 1558 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1559 defer allocator.free(client_path); 1560 1561 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1562 defer client_repo.deinit(io, allocator); 1563 1564 var client_dir = try cwd.openDir(io, client_path, .{}); 1565 defer client_dir.close(io); 1566 1567 { 1568 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1569 defer hello_txt.close(io); 1570 try hello_txt.writeStreamingAll(io, "hello, world!"); 1571 try client_repo.add(io, allocator, &.{"hello.txt"}); 1572 } 1573 1574 // copy files from current repo into client dir 1575 for (&[_][]const u8{"src"}) |dir_name| { 1576 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1577 defer src_repo_dir.close(io); 1578 1579 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1580 defer dest_repo_dir.close(io); 1581 1582 try copyDir(io, src_repo_dir, dest_repo_dir); 1583 1584 try client_repo.add(io, allocator, &.{dir_name}); 1585 } 1586 1587 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1588 1589 // change the files so git will send them as delta objects 1590 for (&[_][]const u8{"src"}) |dir_name| { 1591 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1592 defer dest_repo_dir.close(io); 1593 1594 { 1595 var iter = dest_repo_dir.iterate(); 1596 while (try iter.next(io)) |entry| { 1597 switch (entry.kind) { 1598 .file => { 1599 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1600 defer file.close(io); 1601 var writer = file.writer(io, &.{}); 1602 try writer.interface.writeAll("EDIT"); 1603 }, 1604 else => {}, 1605 } 1606 } 1607 } 1608 1609 try client_repo.add(io, allocator, &.{dir_name}); 1610 } 1611 1612 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1613 1614 // add remote 1615 { 1616 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1617 defer allocator.free(remote_url); 1618 1619 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1620 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1621 } 1622 1623 const is_ssh = switch (transport_def) { 1624 .file => false, 1625 .wire => |wire_kind| .ssh == wire_kind, 1626 }; 1627 1628 if (shell_out_to_git) { 1629 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1630 defer allocator.free(priv_key_path); 1631 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 }); 1632 defer allocator.free(ssh_config_arg); 1633 1634 // shell out to git so it will send delta objects 1635 var process = try std.process.spawn(io, .{ 1636 .argv = if (is_ssh) 1637 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1638 else 1639 &.{ "git", "push", "origin", "master" }, 1640 .cwd = .{ .path = client_path }, 1641 .stdin = .ignore, 1642 .stdout = .ignore, 1643 .stderr = .ignore, 1644 }); 1645 const term = try process.wait(io); 1646 if (term != .exited or term.exited != 0) { 1647 return error.GitCommandFailed; 1648 } 1649 } else { 1650 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1651 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1652 1653 try client_repo.push( 1654 io, 1655 allocator, 1656 "origin", 1657 "master", 1658 false, 1659 .{ .wire = .{ .ssh = .{ 1660 .command = ssh_cmd_maybe, 1661 } } }, 1662 ); 1663 } 1664 1665 // make sure push was successful 1666 { 1667 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1668 try std.testing.expectEqualStrings(&commit2, &oid_master); 1669 1670 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1671 defer hello_txt.close(io); 1672 } 1673 } 1674 1675 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1676 var iter = src_dir.iterate(); 1677 while (try iter.next(io)) |entry| { 1678 switch (entry.kind) { 1679 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1680 .directory => { 1681 try dest_dir.createDirPath(io, entry.name); 1682 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1683 defer dest_entry_dir.close(io); 1684 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1685 defer src_entry_dir.close(io); 1686 try copyDir(io, src_entry_dir, dest_entry_dir); 1687 }, 1688 else => {}, 1689 } 1690 } 1691 } 1692 1693 // the dev SSH public key matching the private key written by runServer 1694 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1695 1696 // create the admin event repo with a user holding the dev SSH public key 1697 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1698 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1699 defer allocator.free(cwd_path); 1700 1701 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1702 defer allocator.free(admin_repo_path); 1703 1704 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1705 defer repo.deinit(io, allocator); 1706 1707 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1708 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1709 1710 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1711 const user_id = evt.EventWithId.randomId(prng.random()); 1712 1713 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1714 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1715 1716 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1717 .id = std.fmt.bytesToHex(user_id, .lower), 1718 .author = .{ .name = "admin", .email = "admin@example.test" }, 1719 .event = .{ .user = .{ 1720 .name = "admin", 1721 .email = "admin@example.test", 1722 .password_hash = password_hash, 1723 .ssh_keys = admin_ssh_key, 1724 } }, 1725 }}); 1726 } 1727 1728 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1729 // the event store 1730 fn repoOnDiskPath( 1731 io: std.Io, 1732 allocator: std.mem.Allocator, 1733 data_dir_name: []const u8, 1734 repo_name: []const u8, 1735 create: bool, 1736 ) !?[]u8 { 1737 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1738 defer allocator.free(cwd_path); 1739 1740 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1741 defer allocator.free(admin_repo_path); 1742 1743 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1744 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1745 } 1746 1747 // build the remote URL addressing admin/<repo_name> over the given transport 1748 fn remoteUrl( 1749 comptime transport_def: net.TransportDefinition, 1750 allocator: std.mem.Allocator, 1751 repo_name: []const u8, 1752 port: u16, 1753 ) ![]u8 { 1754 return switch (transport_def) { 1755 .file => unreachable, 1756 .wire => |wire_kind| switch (wire_kind) { 1757 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1758 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1759 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1760 }, 1761 }; 1762 } 1763 1764 // the ssh command the haxy git client should invoke, or null when not using 1765 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1766 // and skip writing to known_hosts. 1767 fn sshCommand( 1768 is_ssh: bool, 1769 allocator: std.mem.Allocator, 1770 cwd_path: []const u8, 1771 temp_dir_name: []const u8, 1772 port: u16, 1773 ) !?[]u8 { 1774 if (!is_ssh) return null; 1775 1776 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1777 defer allocator.free(priv_key_path); 1778 1779 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 }); 1780 } 1781