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