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