╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1642 { 1643 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1644 defer allocator.free(remote_url); 1645 1646 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1647 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1648 } 1649 1650 const is_ssh = switch (transport_def) { 1651 .file => false, 1652 .wire => |wire_kind| .ssh == wire_kind, 1653 }; 1654 1655 if (shell_out_to_git) { 1656 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1657 defer allocator.free(priv_key_path); 1658 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 }); 1659 defer allocator.free(ssh_config_arg); 1660 1661 // shell out to git so it will send delta objects 1662 var process = try std.process.spawn(io, .{ 1663 .argv = if (is_ssh) 1664 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1665 else 1666 &.{ "git", "push", "origin", "master" }, 1667 .cwd = .{ .path = client_path }, 1668 .stdin = .ignore, 1669 .stdout = .ignore, 1670 .stderr = .ignore, 1671 }); 1672 const term = try process.wait(io); 1673 if (term != .exited or term.exited != 0) { 1674 return error.GitCommandFailed; 1675 } 1676 } else { 1677 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1678 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1679 1680 try client_repo.push( 1681 io, 1682 allocator, 1683 "origin", 1684 "master", 1685 false, 1686 .{ .wire = .{ .ssh = .{ 1687 .command = ssh_cmd_maybe, 1688 } } }, 1689 ); 1690 } 1691 1692 // make sure push was successful 1693 { 1694 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1695 try std.testing.expectEqualStrings(&commit2, &oid_master); 1696 1697 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1698 defer hello_txt.close(io); 1699 } 1700 } 1701 1702 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1703 var iter = src_dir.iterate(); 1704 while (try iter.next(io)) |entry| { 1705 switch (entry.kind) { 1706 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1707 .directory => { 1708 try dest_dir.createDirPath(io, entry.name); 1709 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1710 defer dest_entry_dir.close(io); 1711 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1712 defer src_entry_dir.close(io); 1713 try copyDir(io, src_entry_dir, dest_entry_dir); 1714 }, 1715 else => {}, 1716 } 1717 } 1718 } 1719 1720 // the dev SSH public key matching the private key written by runServer 1721 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1722 1723 // create the admin event repo with a user holding the dev SSH public key 1724 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1725 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1726 defer allocator.free(cwd_path); 1727 1728 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1729 defer allocator.free(admin_repo_path); 1730 1731 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1732 defer repo.deinit(io, allocator); 1733 1734 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1735 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1736 1737 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1738 const user_id = evt.EventWithId.randomId(prng.random()); 1739 1740 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1741 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1742 1743 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1744 .id = std.fmt.bytesToHex(user_id, .lower), 1745 .author = .{ .name = "admin", .email = "admin@example.test" }, 1746 .event = .{ .user = .{ 1747 .name = "admin", 1748 .email = "admin@example.test", 1749 .password_hash = password_hash, 1750 .ssh_keys = admin_ssh_key, 1751 } }, 1752 }}); 1753 } 1754 1755 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1756 // the event store 1757 fn repoOnDiskPath( 1758 io: std.Io, 1759 allocator: std.mem.Allocator, 1760 data_dir_name: []const u8, 1761 repo_name: []const u8, 1762 create: bool, 1763 ) !?[]u8 { 1764 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1765 defer allocator.free(cwd_path); 1766 1767 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1768 defer allocator.free(admin_repo_path); 1769 1770 const event_id_hex = (try evt.resolveOrCreateRepo( 1771 io, 1772 allocator, 1773 admin_repo_path, 1774 "admin", 1775 repo_name, 1776 if (create) .{ .read_access = .public } else null, 1777 )) orelse return null; 1778 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1779 } 1780 1781 // build the remote URL addressing admin/<repo_name> over the given transport 1782 fn remoteUrl( 1783 comptime transport_def: net.TransportDefinition, 1784 allocator: std.mem.Allocator, 1785 repo_name: []const u8, 1786 port: u16, 1787 ) ![]u8 { 1788 return switch (transport_def) { 1789 .file => unreachable, 1790 .wire => |wire_kind| switch (wire_kind) { 1791 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1792 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1793 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1794 }, 1795 }; 1796 } 1797 1798 // the ssh command the haxy git client should invoke, or null when not using 1799 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1800 // and skip writing to known_hosts. 1801 fn sshCommand( 1802 is_ssh: bool, 1803 allocator: std.mem.Allocator, 1804 cwd_path: []const u8, 1805 temp_dir_name: []const u8, 1806 port: u16, 1807 ) !?[]u8 { 1808 if (!is_ssh) return null; 1809 1810 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1811 defer allocator.free(priv_key_path); 1812 1813 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 }); 1814 } 1815