╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1592 }; 1593 1594 if (shell_out_to_git) { 1595 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1596 defer allocator.free(priv_key_path); 1597 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 }); 1598 defer allocator.free(ssh_config_arg); 1599 1600 // shell out to git so it will send delta objects 1601 var process = try std.process.spawn(io, .{ 1602 .argv = if (is_ssh) 1603 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1604 else 1605 &.{ "git", "push", "origin", "master" }, 1606 .cwd = .{ .path = client_path }, 1607 .stdin = .ignore, 1608 .stdout = .ignore, 1609 .stderr = .ignore, 1610 }); 1611 const term = try process.wait(io); 1612 if (term != .exited or term.exited != 0) { 1613 return error.GitCommandFailed; 1614 } 1615 } else { 1616 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1617 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1618 1619 try client_repo.push( 1620 io, 1621 allocator, 1622 "origin", 1623 "master", 1624 false, 1625 .{ .wire = .{ .ssh = .{ 1626 .command = ssh_cmd_maybe, 1627 } } }, 1628 ); 1629 } 1630 1631 // make sure push was successful 1632 { 1633 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1634 try std.testing.expectEqualStrings(&commit2, &oid_master); 1635 1636 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1637 } 1638 } 1639 1640 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1641 var iter = src_dir.iterate(); 1642 while (try iter.next(io)) |entry| { 1643 switch (entry.kind) { 1644 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1645 .directory => { 1646 try dest_dir.createDirPath(io, entry.name); 1647 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1648 defer dest_entry_dir.close(io); 1649 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1650 defer src_entry_dir.close(io); 1651 try copyDir(io, src_entry_dir, dest_entry_dir); 1652 }, 1653 else => {}, 1654 } 1655 } 1656 } 1657 1658 // the dev SSH public key matching the private key written by runServer 1659 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1660 1661 // create the admin event repo with a user holding the dev SSH public key 1662 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1663 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1664 defer allocator.free(cwd_path); 1665 1666 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1667 defer allocator.free(admin_repo_path); 1668 1669 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1670 defer repo.deinit(io, allocator); 1671 1672 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1673 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1674 1675 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1676 const user_id = evt.EventWithId.randomId(prng.random()); 1677 1678 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1679 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1680 1681 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1682 .id = std.fmt.bytesToHex(user_id, .lower), 1683 .author = .{ .name = "admin", .email = "admin@example.test" }, 1684 .event = .{ .user = .{ 1685 .name = "admin", 1686 .email = "admin@example.test", 1687 .password_hash = password_hash, 1688 .ssh_keys = admin_ssh_key, 1689 } }, 1690 }}); 1691 } 1692 1693 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1694 // the event store 1695 fn repoOnDiskPath( 1696 io: std.Io, 1697 allocator: std.mem.Allocator, 1698 data_dir_name: []const u8, 1699 repo_name: []const u8, 1700 create: bool, 1701 ) !?[]u8 { 1702 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1703 defer allocator.free(cwd_path); 1704 1705 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1706 defer allocator.free(admin_repo_path); 1707 1708 const event_id_hex = (try evt.resolveOrCreateRepo( 1709 io, 1710 allocator, 1711 admin_repo_path, 1712 "admin", 1713 repo_name, 1714 if (create) .{ .read_access = .public } else null, 1715 )) orelse return null; 1716 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1717 } 1718 1719 // build the remote URL addressing admin/<repo_name> over the given transport 1720 fn remoteUrl( 1721 comptime transport_def: net.TransportDefinition, 1722 allocator: std.mem.Allocator, 1723 repo_name: []const u8, 1724 port: u16, 1725 ) ![]u8 { 1726 return switch (transport_def) { 1727 .file => unreachable, 1728 .wire => |wire_kind| switch (wire_kind) { 1729 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1730 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1731 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1732 }, 1733 }; 1734 } 1735 1736 // the ssh command the haxy git client should invoke, or null when not using 1737 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1738 // and skip writing to known_hosts. 1739 fn sshCommand( 1740 is_ssh: bool, 1741 allocator: std.mem.Allocator, 1742 cwd_path: []const u8, 1743 temp_dir_name: []const u8, 1744 port: u16, 1745 ) !?[]u8 { 1746 if (!is_ssh) return null; 1747 1748 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1749 defer allocator.free(priv_key_path); 1750 1751 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 }); 1752 } 1753 1754 const ServerFiles = union(enum) { 1755 files: []const struct { path: []const u8, content: []const u8 }, 1756 dirs: []const []const u8, 1757 }; 1758 1759 /// commit files without writing the server's index or worktree. 1760 fn commitServer(repo: anytype, io: std.Io, allocator: std.mem.Allocator, files: ServerFiles, metadata: xit.object.CommitMetadata(repo.self_repo_opts.hash)) ![xit.hash.hexLen(repo.self_repo_opts.hash)]u8 { 1761 const R = @TypeOf(repo.*); 1762 const opts = repo.self_repo_opts; 1763 const Index = xit.index.Index(.xit, opts); 1764 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1765 const Ctx = struct { 1766 core: *R.Core, 1767 io: std.Io, 1768 allocator: std.mem.Allocator, 1769 files: ServerFiles, 1770 metadata: xit.object.CommitMetadata(opts.hash), 1771 result: *[xit.hash.hexLen(opts.hash)]u8, 1772 1773 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1774 var reader = std.Io.Reader.fixed(content); 1775 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1776 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1777 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1778 defer ctx.allocator.free(path_parts); 1779 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1780 } 1781 1782 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1783 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1784 defer dir.close(ctx.io); 1785 var entries = dir.iterate(); 1786 while (try entries.next(ctx.io)) |entry| { 1787 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1788 defer ctx.allocator.free(child); 1789 switch (entry.kind) { 1790 .directory => try ctx.addDir(state, index, child), 1791 .file => { 1792 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1793 defer ctx.allocator.free(content); 1794 try ctx.addFile(state, index, child, content); 1795 }, 1796 else => {}, 1797 } 1798 } 1799 } 1800 1801 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1802 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1803 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1804 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1805 var index = if (head_oid) |oid| try Index.initFromCommit(state.readOnly(), ctx.io, ctx.allocator, &oid) else try Index.init(state.readOnly(), ctx.io, ctx.allocator); 1806 defer index.deinit(); 1807 switch (ctx.files) { 1808 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1809 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1810 } 1811 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1812 defer tree.deinit(); 1813 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1814 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1815 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1816 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1817 } 1818 }; 1819 try repo.core.db_file.lock(io, .exclusive); 1820 defer repo.core.db_file.unlock(io); 1821 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1822 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1823 .core = &repo.core, 1824 .io = io, 1825 .allocator = allocator, 1826 .files = files, 1827 .metadata = metadata, 1828 .result = &result, 1829 }); 1830 return result; 1831 } 1832