╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1497 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1498 defer src_repo_dir.close(io); 1499 1500 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1501 defer dest_repo_dir.close(io); 1502 1503 try copyDir(io, src_repo_dir, dest_repo_dir); 1504 1505 try client_repo.add(io, allocator, &.{dir_name}); 1506 } 1507 1508 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1509 1510 // change the files so git will send them as delta objects 1511 for (&[_][]const u8{"src"}) |dir_name| { 1512 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1513 defer dest_repo_dir.close(io); 1514 1515 { 1516 var iter = dest_repo_dir.iterate(); 1517 while (try iter.next(io)) |entry| { 1518 switch (entry.kind) { 1519 .file => { 1520 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1521 defer file.close(io); 1522 var writer = file.writer(io, &.{}); 1523 try writer.interface.writeAll("EDIT"); 1524 }, 1525 else => {}, 1526 } 1527 } 1528 } 1529 1530 try client_repo.add(io, allocator, &.{dir_name}); 1531 } 1532 1533 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1534 1535 // add remote 1536 { 1537 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1538 defer allocator.free(remote_url); 1539 1540 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1541 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1542 } 1543 1544 const is_ssh = switch (transport_def) { 1545 .file => false, 1546 .wire => |wire_kind| .ssh == wire_kind, 1547 }; 1548 1549 if (shell_out_to_git) { 1550 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1551 defer allocator.free(ssh_cmd); 1552 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1553 defer allocator.free(ssh_config_arg); 1554 1555 // shell out to git so it will send delta objects 1556 var process = try std.process.spawn(io, .{ 1557 .argv = if (is_ssh) 1558 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1559 else 1560 &.{ "git", "push", "--quiet", "origin", "master" }, 1561 .cwd = .{ .path = client_path }, 1562 .stdin = .ignore, 1563 .stdout = .ignore, 1564 .stderr = .inherit, 1565 }); 1566 defer process.kill(io); 1567 const term = try process.wait(io); 1568 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1569 } else { 1570 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1571 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1572 1573 try client_repo.push( 1574 io, 1575 allocator, 1576 "origin", 1577 "master", 1578 false, 1579 .{ .wire = .{ .ssh = .{ 1580 .command = ssh_cmd_maybe, 1581 } } }, 1582 ); 1583 } 1584 1585 // make sure push was successful 1586 { 1587 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1588 try std.testing.expectEqualStrings(&commit2, &oid_master); 1589 1590 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1591 } 1592 } 1593 1594 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1595 var iter = src_dir.iterate(); 1596 while (try iter.next(io)) |entry| { 1597 switch (entry.kind) { 1598 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1599 .directory => { 1600 try dest_dir.createDirPath(io, entry.name); 1601 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1602 defer dest_entry_dir.close(io); 1603 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1604 defer src_entry_dir.close(io); 1605 try copyDir(io, src_entry_dir, dest_entry_dir); 1606 }, 1607 else => {}, 1608 } 1609 } 1610 } 1611 1612 // the dev SSH public key matching the private key written by runServer 1613 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1614 1615 // create the admin event repo with a user holding the dev SSH public key 1616 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1617 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1618 defer allocator.free(admin_repo_path); 1619 1620 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1621 defer repo.deinit(io, allocator); 1622 1623 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1624 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1625 1626 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1627 const user_id = evt.EventWithId.randomId(prng.random()); 1628 1629 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1630 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1631 1632 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1633 .id = std.fmt.bytesToHex(user_id, .lower), 1634 .author = .{ .name = "admin", .email = "admin@example.test" }, 1635 .event = .{ .user = .{ 1636 .name = "admin", 1637 .email = "admin@example.test", 1638 .password_hash = password_hash, 1639 .ssh_keys = admin_ssh_key, 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