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