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