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