╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1649 .name = "admin", 1650 .email = "admin@example.test", 1651 .password_hash = password_hash, 1652 .ssh_keys = admin_ssh_key, 1653 } }, 1654 }}); 1655 1656 const repos_dir = try std.fs.path.join(allocator, &.{ data_path, "repos" }); 1657 defer allocator.free(repos_dir); 1658 const user_repo_path = try evt.userRepoPath(allocator, repos_dir, &user_id); 1659 defer allocator.free(user_repo_path); 1660 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1661 defer user_repo.deinit(io, allocator); 1662 } 1663 1664 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1665 // the event store 1666 fn repoOnDiskPath( 1667 io: std.Io, 1668 allocator: std.mem.Allocator, 1669 data_path: []const u8, 1670 repo_name: []const u8, 1671 create: bool, 1672 ) !?[]u8 { 1673 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1674 defer allocator.free(admin_repo_path); 1675 1676 const event_id_hex = (try evt.resolveOrCreateRepo( 1677 io, 1678 allocator, 1679 admin_repo_path, 1680 "admin", 1681 repo_name, 1682 if (create) .{ .read_access = .public } else null, 1683 )) orelse return null; 1684 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1685 } 1686 1687 // build the remote URL addressing admin/<repo_name> over the given transport 1688 fn remoteUrl( 1689 comptime transport_def: net.TransportDefinition, 1690 allocator: std.mem.Allocator, 1691 repo_name: []const u8, 1692 port: u16, 1693 ) ![]u8 { 1694 return switch (transport_def) { 1695 .file => unreachable, 1696 .wire => |wire_kind| switch (wire_kind) { 1697 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1698 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1699 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1700 }, 1701 }; 1702 } 1703 1704 // the ssh command the haxy git client should invoke, or null when not using 1705 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1706 // and skip writing to known_hosts. 1707 fn sshCommand( 1708 is_ssh: bool, 1709 allocator: std.mem.Allocator, 1710 temp_path: []const u8, 1711 port: u16, 1712 ) !?[]u8 { 1713 if (!is_ssh) return null; 1714 1715 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1716 defer allocator.free(priv_key_path); 1717 1718 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1719 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1720 return try std.fmt.allocPrint(allocator, "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 }); 1721 } 1722 1723 const ServerFiles = union(enum) { 1724 files: []const struct { path: []const u8, content: []const u8 }, 1725 dirs: []const []const u8, 1726 }; 1727 1728 /// commit files without writing the server's index or worktree. 1729 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 { 1730 const R = @TypeOf(repo.*); 1731 const opts = repo.self_repo_opts; 1732 const Index = xit.index.Index(.xit, opts); 1733 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1734 const Ctx = struct { 1735 core: *R.Core, 1736 io: std.Io, 1737 allocator: std.mem.Allocator, 1738 files: ServerFiles, 1739 metadata: xit.object.CommitMetadata(opts.hash), 1740 result: *[xit.hash.hexLen(opts.hash)]u8, 1741 1742 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1743 var reader = std.Io.Reader.fixed(content); 1744 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1745 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1746 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1747 defer ctx.allocator.free(path_parts); 1748 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1749 } 1750 1751 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1752 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1753 defer dir.close(ctx.io); 1754 var entries = dir.iterate(); 1755 while (try entries.next(ctx.io)) |entry| { 1756 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1757 defer ctx.allocator.free(child); 1758 switch (entry.kind) { 1759 .directory => try ctx.addDir(state, index, child), 1760 .file => { 1761 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1762 defer ctx.allocator.free(content); 1763 try ctx.addFile(state, index, child, content); 1764 }, 1765 else => {}, 1766 } 1767 } 1768 } 1769 1770 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1771 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1772 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1773 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1774 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); 1775 defer index.deinit(); 1776 switch (ctx.files) { 1777 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1778 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1779 } 1780 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1781 defer tree.deinit(); 1782 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1783 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1784 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1785 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) }); 1786 } 1787 }; 1788 try repo.core.db_file.lock(io, .exclusive); 1789 defer repo.core.db_file.unlock(io); 1790 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1791 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1792 .core = &repo.core, 1793 .io = io, 1794 .allocator = allocator, 1795 .files = files, 1796 .metadata = metadata, 1797 .result = &result, 1798 }); 1799 return result; 1800 } 1801