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