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