╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1798 defer allocator.free(admin_repo_path); 1799 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1800 defer allocator.free(users_dir); 1801 1802 const location = (try evt.resolveOrCreateRepo( 1803 io, 1804 allocator, 1805 users_dir, 1806 admin_repo_path, 1807 "admin", 1808 repo_name, 1809 if (create) .{ .read_access = .public } else null, 1810 )) orelse return null; 1811 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1812 } 1813 1814 // build the remote URL addressing admin:<repo_name> over the given transport 1815 fn remoteUrl( 1816 comptime transport_def: net.TransportDefinition, 1817 allocator: std.mem.Allocator, 1818 repo_name: []const u8, 1819 port: u16, 1820 ) ![]u8 { 1821 return switch (transport_def) { 1822 .file => unreachable, 1823 .wire => |wire_kind| switch (wire_kind) { 1824 .http => try allocator.print("http://localhost:{}/admin:{s}", .{ port, repo_name }), 1825 .raw => try allocator.print("git://localhost:{}/admin:{s}", .{ port, repo_name }), 1826 .ssh => try allocator.print("git@localhost:admin:{s}", .{repo_name}), 1827 }, 1828 }; 1829 } 1830 1831 // the ssh command the haxy git client should invoke, or null when not using 1832 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1833 // and skip writing to known_hosts. 1834 fn sshCommand( 1835 is_ssh: bool, 1836 allocator: std.mem.Allocator, 1837 temp_path: []const u8, 1838 port: u16, 1839 ) !?[]u8 { 1840 if (!is_ssh) return null; 1841 1842 const priv_key_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "key" }); 1843 defer allocator.free(priv_key_path); 1844 1845 if (builtin.target.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1846 const null_path = if (builtin.target.os.tag == .windows) "NUL" else "/dev/null"; 1847 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 }); 1848 } 1849 1850 const ServerFiles = union(enum) { 1851 files: []const struct { path: []const u8, content: []const u8 }, 1852 dirs: []const []const u8, 1853 }; 1854 1855 /// commit files without writing the server's index or worktree. 1856 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 { 1857 const R = @TypeOf(repo.*); 1858 const opts = repo.self_repo_opts; 1859 const Index = xit.index.Index(.xit, opts); 1860 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1861 const Ctx = struct { 1862 core: *R.Core, 1863 io: std.Io, 1864 allocator: std.mem.Allocator, 1865 files: ServerFiles, 1866 metadata: xit.object.CommitMetadata(opts.hash), 1867 result: *[xit.hash.hexLen(opts.hash)]u8, 1868 1869 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1870 var reader = std.Io.Reader.fixed(content); 1871 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1872 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1873 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1874 defer ctx.allocator.free(path_parts); 1875 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1876 } 1877 1878 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1879 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1880 defer dir.close(ctx.io); 1881 var entries = dir.iterate(); 1882 while (try entries.next(ctx.io)) |entry| { 1883 const child = try std.Io.Dir.path.join(ctx.allocator, &.{ path, entry.name }); 1884 defer ctx.allocator.free(child); 1885 switch (entry.kind) { 1886 .directory => try ctx.addDir(state, index, child), 1887 .file => { 1888 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1889 defer ctx.allocator.free(content); 1890 try ctx.addFile(state, index, child, content); 1891 }, 1892 else => {}, 1893 } 1894 } 1895 } 1896 1897 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1898 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1899 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1900 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1901 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); 1902 defer index.deinit(); 1903 switch (ctx.files) { 1904 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1905 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1906 } 1907 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1908 defer tree.deinit(); 1909 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1910 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1911 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1912 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) }); 1913 } 1914 }; 1915 try repo.core.db_file.lock(io, .exclusive); 1916 defer repo.core.db_file.unlock(io); 1917 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1918 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1919 .core = &repo.core, 1920 .io = io, 1921 .allocator = allocator, 1922 .files = files, 1923 .metadata = metadata, 1924 .result = &result, 1925 }); 1926 return result; 1927 } 1928