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