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