╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1521 try client_repo.add(io, allocator, &.{dir_name}); 1522 } 1523 1524 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1525 1526 // change the files so git will send them as delta objects 1527 for (&[_][]const u8{"src"}) |dir_name| { 1528 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1529 defer dest_repo_dir.close(io); 1530 1531 { 1532 var iter = dest_repo_dir.iterate(); 1533 while (try iter.next(io)) |entry| { 1534 switch (entry.kind) { 1535 .file => { 1536 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1537 defer file.close(io); 1538 var writer = file.writer(io, &.{}); 1539 try writer.interface.writeAll("EDIT"); 1540 }, 1541 else => {}, 1542 } 1543 } 1544 } 1545 1546 try client_repo.add(io, allocator, &.{dir_name}); 1547 } 1548 1549 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1550 1551 // add remote 1552 { 1553 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1554 defer allocator.free(remote_url); 1555 1556 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1557 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1558 } 1559 1560 const is_ssh = switch (transport_def) { 1561 .file => false, 1562 .wire => |wire_kind| .ssh == wire_kind, 1563 }; 1564 1565 if (shell_out_to_git) { 1566 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1567 defer allocator.free(ssh_cmd); 1568 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1569 defer allocator.free(ssh_config_arg); 1570 1571 // shell out to git so it will send delta objects 1572 var process = try std.process.spawn(io, .{ 1573 .argv = if (is_ssh) 1574 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1575 else 1576 &.{ "git", "push", "--quiet", "origin", "master" }, 1577 .cwd = .{ .path = client_path }, 1578 .stdin = .ignore, 1579 .stdout = .ignore, 1580 .stderr = .inherit, 1581 }); 1582 defer process.kill(io); 1583 const term = try process.wait(io); 1584 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1585 } else { 1586 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1587 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1588 1589 try client_repo.push( 1590 io, 1591 allocator, 1592 "origin", 1593 "master", 1594 false, 1595 .{ .wire = .{ .ssh = .{ 1596 .command = ssh_cmd_maybe, 1597 } } }, 1598 ); 1599 } 1600 1601 // make sure push was successful 1602 { 1603 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1604 try std.testing.expectEqualStrings(&commit2, &oid_master); 1605 1606 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1607 } 1608 } 1609 1610 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1611 var iter = src_dir.iterate(); 1612 while (try iter.next(io)) |entry| { 1613 switch (entry.kind) { 1614 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1615 .directory => { 1616 try dest_dir.createDirPath(io, entry.name); 1617 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1618 defer dest_entry_dir.close(io); 1619 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1620 defer src_entry_dir.close(io); 1621 try copyDir(io, src_entry_dir, dest_entry_dir); 1622 }, 1623 else => {}, 1624 } 1625 } 1626 } 1627 1628 // the dev SSH public key matching the private key written by runServer 1629 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1630 1631 // create the admin event repo with a user holding the dev SSH public key, plus 1632 // that user's repo 1633 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1634 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1635 defer allocator.free(admin_repo_path); 1636 1637 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1638 defer repo.deinit(io, allocator); 1639 1640 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1641 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1642 1643 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1644 const user_id = evt.EventWithId.randomId(prng.random()); 1645 1646 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1647 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1648 1649 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1650 defer allocator.free(users_dir); 1651 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1652 .id = std.fmt.bytesToHex(user_id, .lower), 1653 .author = .{ .name = "admin", .email = "admin@example.test" }, 1654 .event = .{ .user = .{ 1655 .name = "admin", 1656 .email = "admin@example.test", 1657 .password_hash = password_hash, 1658 .ssh_keys = admin_ssh_key, 1659 } }, 1660 }}); 1661 1662 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 1663 defer allocator.free(user_repo_path); 1664 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1665 defer user_repo.deinit(io, allocator); 1666 } 1667 1668 // resolve admin:<repo_name> to its on-disk directory under <data_dir>/users via 1669 // the event store 1670 fn repoOnDiskPath( 1671 io: std.Io, 1672 allocator: std.mem.Allocator, 1673 data_path: []const u8, 1674 repo_name: []const u8, 1675 create: bool, 1676 ) !?[]u8 { 1677 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1678 defer allocator.free(admin_repo_path); 1679 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1680 defer allocator.free(users_dir); 1681 1682 const location = (try evt.resolveOrCreateRepo( 1683 io, 1684 allocator, 1685 users_dir, 1686 admin_repo_path, 1687 "admin", 1688 repo_name, 1689 if (create) .{ .read_access = .public } else null, 1690 )) orelse return null; 1691 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1692 } 1693 1694 // build the remote URL addressing admin:<repo_name> over the given transport 1695 fn remoteUrl( 1696 comptime transport_def: net.TransportDefinition, 1697 allocator: std.mem.Allocator, 1698 repo_name: []const u8, 1699 port: u16, 1700 ) ![]u8 { 1701 return switch (transport_def) { 1702 .file => unreachable, 1703 .wire => |wire_kind| switch (wire_kind) { 1704 .http => try allocator.print("http://localhost:{}/admin:{s}", .{ port, repo_name }), 1705 .raw => try allocator.print("git://localhost:{}/admin:{s}", .{ port, repo_name }), 1706 .ssh => try allocator.print("git@localhost:admin:{s}", .{repo_name}), 1707 }, 1708 }; 1709 } 1710 1711 // the ssh command the haxy git client should invoke, or null when not using 1712 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1713 // and skip writing to known_hosts. 1714 fn sshCommand( 1715 is_ssh: bool, 1716 allocator: std.mem.Allocator, 1717 temp_path: []const u8, 1718 port: u16, 1719 ) !?[]u8 { 1720 if (!is_ssh) return null; 1721 1722 const priv_key_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "key" }); 1723 defer allocator.free(priv_key_path); 1724 1725 if (builtin.target.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1726 const null_path = if (builtin.target.os.tag == .windows) "NUL" else "/dev/null"; 1727 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 }); 1728 } 1729 1730 const ServerFiles = union(enum) { 1731 files: []const struct { path: []const u8, content: []const u8 }, 1732 dirs: []const []const u8, 1733 }; 1734 1735 /// commit files without writing the server's index or worktree. 1736 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 { 1737 const R = @TypeOf(repo.*); 1738 const opts = repo.self_repo_opts; 1739 const Index = xit.index.Index(.xit, opts); 1740 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1741 const Ctx = struct { 1742 core: *R.Core, 1743 io: std.Io, 1744 allocator: std.mem.Allocator, 1745 files: ServerFiles, 1746 metadata: xit.object.CommitMetadata(opts.hash), 1747 result: *[xit.hash.hexLen(opts.hash)]u8, 1748 1749 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1750 var reader = std.Io.Reader.fixed(content); 1751 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1752 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1753 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1754 defer ctx.allocator.free(path_parts); 1755 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1756 } 1757 1758 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1759 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1760 defer dir.close(ctx.io); 1761 var entries = dir.iterate(); 1762 while (try entries.next(ctx.io)) |entry| { 1763 const child = try std.Io.Dir.path.join(ctx.allocator, &.{ path, entry.name }); 1764 defer ctx.allocator.free(child); 1765 switch (entry.kind) { 1766 .directory => try ctx.addDir(state, index, child), 1767 .file => { 1768 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1769 defer ctx.allocator.free(content); 1770 try ctx.addFile(state, index, child, content); 1771 }, 1772 else => {}, 1773 } 1774 } 1775 } 1776 1777 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1778 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1779 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1780 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1781 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); 1782 defer index.deinit(); 1783 switch (ctx.files) { 1784 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1785 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1786 } 1787 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1788 defer tree.deinit(); 1789 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1790 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1791 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1792 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) }); 1793 } 1794 }; 1795 try repo.core.db_file.lock(io, .exclusive); 1796 defer repo.core.db_file.unlock(io); 1797 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1798 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1799 .core = &repo.core, 1800 .io = io, 1801 .allocator = allocator, 1802 .files = files, 1803 .metadata = metadata, 1804 .result = &result, 1805 }); 1806 return result; 1807 } 1808