╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1723 defer argv.deinit(allocator); 1724 try argv.appendSlice(allocator, &.{ "git", "-c", ssh_config_arg, "push", "--progress", "origin" }); 1725 try argv.appendSlice(allocator, refspecs); 1726 const result = try std.process.run(allocator, io, .{ .argv = argv.items, .cwd = .{ .path = client_path } }); 1727 allocator.free(result.stdout); 1728 return .{ .ok = result.term == .exited and result.term.exited == 0, .stderr = result.stderr }; 1729 } 1730 1731 // the number of transactions in a repo's history. opened afresh each time, 1732 // since gc replaces the database file. 1733 fn historyCount(comptime repo_opts: rp.RepoOpts(.xit), io: std.Io, allocator: std.mem.Allocator, path: []const u8) !u64 { 1734 var repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = path }); 1735 defer repo.deinit(io, allocator); 1736 const history = try rp.Repo(.xit, repo_opts).DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 1737 return try history.count(); 1738 } 1739 1740 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1741 var iter = src_dir.iterate(); 1742 while (try iter.next(io)) |entry| { 1743 switch (entry.kind) { 1744 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1745 .directory => { 1746 try dest_dir.createDirPath(io, entry.name); 1747 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1748 defer dest_entry_dir.close(io); 1749 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1750 defer src_entry_dir.close(io); 1751 try copyDir(io, src_entry_dir, dest_entry_dir); 1752 }, 1753 else => {}, 1754 } 1755 } 1756 } 1757 1758 // the dev SSH public key matching the private key written by runServer 1759 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1760 1761 // create the admin event repo with a user holding the dev SSH public key, plus 1762 // that user's repo 1763 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1764 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1765 defer allocator.free(admin_repo_path); 1766 1767 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1768 defer repo.deinit(io, allocator); 1769 1770 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1771 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1772 1773 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1774 const user_id = evt.EventWithId.randomId(prng.random()); 1775 1776 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1777 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1778 1779 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1780 defer allocator.free(users_dir); 1781 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1782 .id = std.fmt.bytesToHex(user_id, .lower), 1783 .author = .{ .name = "admin", .email = "admin@example.test" }, 1784 .event = .{ .user = .{ 1785 .name = "admin", 1786 .email = "admin@example.test", 1787 .password_hash = password_hash, 1788 .ssh_keys = admin_ssh_key, 1789 } }, 1790 }}); 1791 1792 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 1793 defer allocator.free(user_repo_path); 1794 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1795 defer user_repo.deinit(io, allocator); 1796 } 1797 1798 // resolve admin:<repo_name> to its on-disk directory under <data_dir>/users via 1799 // the event store 1800 fn repoOnDiskPath( 1801 io: std.Io, 1802 allocator: std.mem.Allocator, 1803 data_path: []const u8, 1804 repo_name: []const u8, 1805 create: bool, 1806 ) !?[]u8 { 1807 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1808 defer allocator.free(admin_repo_path); 1809 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1810 defer allocator.free(users_dir); 1811 1812 const location = (try evt.resolveOrCreateRepo( 1813 io, 1814 allocator, 1815 users_dir, 1816 admin_repo_path, 1817 "admin", 1818 repo_name, 1819 if (create) .{ .read_access = .public } else null, 1820 )) orelse return null; 1821 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1822 } 1823 1824 // build the remote URL addressing admin:<repo_name> over the given transport 1825 fn remoteUrl( 1826 comptime transport_def: net.TransportDefinition, 1827 allocator: std.mem.Allocator, 1828 repo_name: []const u8, 1829 port: u16, 1830 ) ![]u8 { 1831 return switch (transport_def) { 1832 .file => unreachable, 1833 .wire => |wire_kind| switch (wire_kind) { 1834 .http => try allocator.print("http://localhost:{}/admin:{s}", .{ port, repo_name }), 1835 .raw => try allocator.print("git://localhost:{}/admin:{s}", .{ port, repo_name }), 1836 .ssh => try allocator.print("git@localhost:admin:{s}", .{repo_name}), 1837 }, 1838 }; 1839 } 1840 1841 // the ssh command the haxy git client should invoke, or null when not using 1842 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1843 // and skip writing to known_hosts. 1844 fn sshCommand( 1845 is_ssh: bool, 1846 allocator: std.mem.Allocator, 1847 temp_path: []const u8, 1848 port: u16, 1849 ) !?[]u8 { 1850 if (!is_ssh) return null; 1851 1852 const priv_key_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "key" }); 1853 defer allocator.free(priv_key_path); 1854 1855 if (builtin.target.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1856 const null_path = if (builtin.target.os.tag == .windows) "NUL" else "/dev/null"; 1857 return try allocator.print("ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile={s} -o LogLevel=ERROR -o IdentitiesOnly=yes -i \"{s}\"", .{ port + 1, null_path, priv_key_path }); 1858 } 1859 1860 const ServerFiles = union(enum) { 1861 files: []const struct { path: []const u8, content: []const u8 }, 1862 dirs: []const []const u8, 1863 }; 1864 1865 /// commit files without writing the server's index or worktree. 1866 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 { 1867 const R = @TypeOf(repo.*); 1868 const opts = repo.self_repo_opts; 1869 const Index = xit.index.Index(.xit, opts); 1870 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1871 const Ctx = struct { 1872 core: *R.Core, 1873 io: std.Io, 1874 allocator: std.mem.Allocator, 1875 files: ServerFiles, 1876 metadata: xit.object.CommitMetadata(opts.hash), 1877 result: *[xit.hash.hexLen(opts.hash)]u8, 1878 1879 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1880 var reader = std.Io.Reader.fixed(content); 1881 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1882 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1883 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1884 defer ctx.allocator.free(path_parts); 1885 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1886 } 1887 1888 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1889 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1890 defer dir.close(ctx.io); 1891 var entries = dir.iterate(); 1892 while (try entries.next(ctx.io)) |entry| { 1893 const child = try std.Io.Dir.path.join(ctx.allocator, &.{ path, entry.name }); 1894 defer ctx.allocator.free(child); 1895 switch (entry.kind) { 1896 .directory => try ctx.addDir(state, index, child), 1897 .file => { 1898 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1899 defer ctx.allocator.free(content); 1900 try ctx.addFile(state, index, child, content); 1901 }, 1902 else => {}, 1903 } 1904 } 1905 } 1906 1907 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1908 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1909 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1910 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1911 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); 1912 defer index.deinit(); 1913 switch (ctx.files) { 1914 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1915 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1916 } 1917 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1918 defer tree.deinit(); 1919 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1920 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1921 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1922 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .commit = try .init(ctx.metadata.message, ctx.result.*, opts.max_read_size) }); 1923 } 1924 }; 1925 try repo.core.db_file.lock(io, .exclusive); 1926 defer repo.core.db_file.unlock(io); 1927 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1928 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1929 .core = &repo.core, 1930 .io = io, 1931 .allocator = allocator, 1932 .files = files, 1933 .metadata = metadata, 1934 .result = &result, 1935 }); 1936 return result; 1937 } 1938