╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1461 { 1462 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1463 try std.testing.expectEqualStrings(&commit1, &oid_master); 1464 } 1465 } 1466 1467 fn testPushLarge( 1468 comptime repo_kind: rp.RepoKind, 1469 comptime transport_def: net.TransportDefinition, 1470 comptime shell_out_to_git: bool, 1471 comptime hash_kind: hash.HashKind, 1472 comptime port: u16, 1473 io: std.Io, 1474 allocator: std.mem.Allocator, 1475 ) !void { 1476 // create the temp dir 1477 var temp = std.testing.tmpDir(.{}); 1478 defer temp.cleanup(); 1479 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1480 defer allocator.free(temp_path); 1481 1482 // init server 1483 var server_process = try runServer(io, allocator, temp_path, port); 1484 defer _ = server_process.kill(io); 1485 1486 // register the repo under admin and locate its on-disk directory 1487 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1488 defer allocator.free(server_path); 1489 1490 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1491 defer server_repo.deinit(io, allocator); 1492 1493 // add config 1494 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1495 1496 // export server repo 1497 { 1498 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1499 defer export_file.close(io); 1500 } 1501 1502 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 1503 defer allocator.free(client_path); 1504 1505 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1506 defer client_repo.deinit(io, allocator); 1507 1508 var client_dir = try temp.dir.openDir(io, "client", .{}); 1509 defer client_dir.close(io); 1510 1511 { 1512 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1513 defer hello_txt.close(io); 1514 try hello_txt.writeStreamingAll(io, "hello, world!"); 1515 try client_repo.add(io, allocator, &.{"hello.txt"}); 1516 } 1517 1518 // copy files from current repo into client dir 1519 const cwd = std.Io.Dir.cwd(); 1520 for (&[_][]const u8{"src"}) |dir_name| { 1521 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1522 defer src_repo_dir.close(io); 1523 1524 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1525 defer dest_repo_dir.close(io); 1526 1527 try copyDir(io, src_repo_dir, dest_repo_dir); 1528 1529 try client_repo.add(io, allocator, &.{dir_name}); 1530 } 1531 1532 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1533 1534 // change the files so git will send them as delta objects 1535 for (&[_][]const u8{"src"}) |dir_name| { 1536 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1537 defer dest_repo_dir.close(io); 1538 1539 { 1540 var iter = dest_repo_dir.iterate(); 1541 while (try iter.next(io)) |entry| { 1542 switch (entry.kind) { 1543 .file => { 1544 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1545 defer file.close(io); 1546 var writer = file.writer(io, &.{}); 1547 try writer.interface.writeAll("EDIT"); 1548 }, 1549 else => {}, 1550 } 1551 } 1552 } 1553 1554 try client_repo.add(io, allocator, &.{dir_name}); 1555 } 1556 1557 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1558 1559 // add remote 1560 { 1561 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1562 defer allocator.free(remote_url); 1563 1564 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1565 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1566 } 1567 1568 const is_ssh = switch (transport_def) { 1569 .file => false, 1570 .wire => |wire_kind| .ssh == wire_kind, 1571 }; 1572 1573 if (shell_out_to_git) { 1574 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1575 defer allocator.free(ssh_cmd); 1576 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1577 defer allocator.free(ssh_config_arg); 1578 1579 // shell out to git so it will send delta objects 1580 var process = try std.process.spawn(io, .{ 1581 .argv = if (is_ssh) 1582 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1583 else 1584 &.{ "git", "push", "--quiet", "origin", "master" }, 1585 .cwd = .{ .path = client_path }, 1586 .stdin = .ignore, 1587 .stdout = .ignore, 1588 .stderr = .inherit, 1589 }); 1590 defer process.kill(io); 1591 const term = try process.wait(io); 1592 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1593 } else { 1594 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1595 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1596 1597 try client_repo.push( 1598 io, 1599 allocator, 1600 "origin", 1601 "master", 1602 false, 1603 .{ .wire = .{ .ssh = .{ 1604 .command = ssh_cmd_maybe, 1605 } } }, 1606 ); 1607 } 1608 1609 // make sure push was successful 1610 { 1611 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1612 try std.testing.expectEqualStrings(&commit2, &oid_master); 1613 1614 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1615 } 1616 } 1617 1618 fn testPushClearUndo( 1619 comptime hash_kind: hash.HashKind, 1620 comptime port: u16, 1621 io: std.Io, 1622 allocator: std.mem.Allocator, 1623 ) !void { 1624 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 1625 1626 // create the temp dir 1627 var temp = std.testing.tmpDir(.{}); 1628 defer temp.cleanup(); 1629 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1630 defer allocator.free(temp_path); 1631 1632 // init server 1633 var server_process = try runServer(io, allocator, temp_path, port); 1634 defer _ = server_process.kill(io); 1635 1636 // register the repo under admin and locate its on-disk directory 1637 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1638 defer allocator.free(server_path); 1639 { 1640 var server_repo = try rp.Repo(.xit, repo_opts).init(io, allocator, .{ .bare = true, .path = server_path }); 1641 defer server_repo.deinit(io, allocator); 1642 } 1643 1644 // a git client with one commit to push 1645 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 1646 defer allocator.free(client_path); 1647 const commit = blk: { 1648 var client_repo = try rp.Repo(.git, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1649 defer client_repo.deinit(io, allocator); 1650 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1651 defer hello_txt.close(io); 1652 try hello_txt.writeStreamingAll(io, "hello, world!"); 1653 try client_repo.add(io, allocator, &.{"hello.txt"}); 1654 const remote_url = try remoteUrl(.{ .wire = .ssh }, allocator, "testrepo", port); 1655 defer allocator.free(remote_url); 1656 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1657 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1658 }; 1659 1660 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1661 defer allocator.free(ssh_cmd); 1662 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1663 defer allocator.free(ssh_config_arg); 1664 1665 { 1666 const result = try gitPush(io, allocator, client_path, ssh_config_arg, &.{"master"}); 1667 defer allocator.free(result.stderr); 1668 try std.testing.expect(result.ok); 1669 } 1670 const pushed_count = try historyCount(repo_opts, io, allocator, server_path); 1671 try std.testing.expect(pushed_count > 1); 1672 1673 // the clear ref can only be deleted 1674 { 1675 const result = try gitPush(io, allocator, client_path, ssh_config_arg, &.{"master:refs/haxy/undo"}); 1676 defer allocator.free(result.stderr); 1677 try std.testing.expect(!result.ok); 1678 try std.testing.expect(std.mem.indexOf(u8, result.stderr, "can only be deleted") != null); 1679 } 1680 1681 // and only alone, refusing the whole push 1682 { 1683 const result = try gitPush(io, allocator, client_path, ssh_config_arg, &.{ "master:refs/heads/other", ":refs/haxy/undo" }); 1684 defer allocator.free(result.stderr); 1685 try std.testing.expect(!result.ok); 1686 try std.testing.expect(std.mem.indexOf(u8, result.stderr, "must be deleted alone") != null); 1687 } 1688 1689 // a refused push leaves no transaction behind 1690 try std.testing.expectEqual(pushed_count, try historyCount(repo_opts, io, allocator, server_path)); 1691 1692 { 1693 const result = try gitPush(io, allocator, client_path, ssh_config_arg, &.{":refs/haxy/undo"}); 1694 defer allocator.free(result.stderr); 1695 try std.testing.expect(result.ok); 1696 try std.testing.expect(std.mem.indexOf(u8, result.stderr, "Compacting database") != null); 1697 } 1698 1699 // one transaction is left, and it still holds the pushed branch 1700 try std.testing.expectEqual(1, try historyCount(repo_opts, io, allocator, server_path)); 1701 { 1702 var server_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = server_path }); 1703 defer server_repo.deinit(io, allocator); 1704 try std.testing.expectEqualStrings(&commit, &(try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?); 1705 try std.testing.expectEqual(null, try server_repo.readRef(io, .{ .kind = .head, .name = "other" })); 1706 } 1707 } 1708 1709 // whether a git push from the client succeeded, and what it printed. progress 1710 // is forced on, since git hides it when stderr isn't a terminal. 1711 fn gitPush(io: std.Io, allocator: std.mem.Allocator, client_path: []const u8, ssh_config_arg: []const u8, refspecs: []const []const u8) !struct { ok: bool, stderr: []u8 } { 1712 var argv: std.ArrayList([]const u8) = .empty; 1713 defer argv.deinit(allocator); 1714 try argv.appendSlice(allocator, &.{ "git", "-c", ssh_config_arg, "push", "--progress", "origin" }); 1715 try argv.appendSlice(allocator, refspecs); 1716 const result = try std.process.run(allocator, io, .{ .argv = argv.items, .cwd = .{ .path = client_path } }); 1717 allocator.free(result.stdout); 1718 return .{ .ok = result.term == .exited and result.term.exited == 0, .stderr = result.stderr }; 1719 } 1720 1721 // the number of transactions in a repo's history. opened afresh each time, 1722 // since gc replaces the database file. 1723 fn historyCount(comptime repo_opts: rp.RepoOpts(.xit), io: std.Io, allocator: std.mem.Allocator, path: []const u8) !u64 { 1724 var repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = path }); 1725 defer repo.deinit(io, allocator); 1726 const history = try rp.Repo(.xit, repo_opts).DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 1727 return try history.count(); 1728 } 1729 1730 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1731 var iter = src_dir.iterate(); 1732 while (try iter.next(io)) |entry| { 1733 switch (entry.kind) { 1734 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1735 .directory => { 1736 try dest_dir.createDirPath(io, entry.name); 1737 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1738 defer dest_entry_dir.close(io); 1739 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1740 defer src_entry_dir.close(io); 1741 try copyDir(io, src_entry_dir, dest_entry_dir); 1742 }, 1743 else => {}, 1744 } 1745 } 1746 } 1747 1748 // the dev SSH public key matching the private key written by runServer 1749 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1750 1751 // create the admin event repo with a user holding the dev SSH public key, plus 1752 // that user's repo 1753 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1754 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1755 defer allocator.free(admin_repo_path); 1756 1757 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1758 defer repo.deinit(io, allocator); 1759 1760 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1761 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1762 1763 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1764 const user_id = evt.EventWithId.randomId(prng.random()); 1765 1766 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1767 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1768 1769 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1770 defer allocator.free(users_dir); 1771 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1772 .id = std.fmt.bytesToHex(user_id, .lower), 1773 .author = .{ .name = "admin", .email = "admin@example.test" }, 1774 .event = .{ .user = .{ 1775 .name = "admin", 1776 .email = "admin@example.test", 1777 .password_hash = password_hash, 1778 .ssh_keys = admin_ssh_key, 1779 } }, 1780 }}); 1781 1782 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 1783 defer allocator.free(user_repo_path); 1784 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1785 defer user_repo.deinit(io, allocator); 1786 } 1787 1788 // resolve admin:<repo_name> to its on-disk directory under <data_dir>/users via 1789 // the event store 1790 fn repoOnDiskPath( 1791 io: std.Io, 1792 allocator: std.mem.Allocator, 1793 data_path: []const u8, 1794 repo_name: []const u8, 1795 create: bool, 1796 ) !?[]u8 { 1797 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1798 defer allocator.free(admin_repo_path); 1799 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1800 defer allocator.free(users_dir); 1801 1802 const location = (try evt.resolveOrCreateRepo( 1803 io, 1804 allocator, 1805 users_dir, 1806 admin_repo_path, 1807 "admin", 1808 repo_name, 1809 if (create) .{ .read_access = .public } else null, 1810 )) orelse return null; 1811 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1812 } 1813 1814 // build the remote URL addressing admin:<repo_name> over the given transport 1815 fn remoteUrl( 1816 comptime transport_def: net.TransportDefinition, 1817 allocator: std.mem.Allocator, 1818 repo_name: []const u8, 1819 port: u16, 1820 ) ![]u8 { 1821 return switch (transport_def) { 1822 .file => unreachable, 1823 .wire => |wire_kind| switch (wire_kind) { 1824 .http => try allocator.print("http://localhost:{}/admin:{s}", .{ port, repo_name }), 1825 .raw => try allocator.print("git://localhost:{}/admin:{s}", .{ port, repo_name }), 1826 .ssh => try allocator.print("git@localhost:admin:{s}", .{repo_name}), 1827 }, 1828 }; 1829 } 1830 1831 // the ssh command the haxy git client should invoke, or null when not using 1832 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1833 // and skip writing to known_hosts. 1834 fn sshCommand( 1835 is_ssh: bool, 1836 allocator: std.mem.Allocator, 1837 temp_path: []const u8, 1838 port: u16, 1839 ) !?[]u8 { 1840 if (!is_ssh) return null; 1841 1842 const priv_key_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "key" }); 1843 defer allocator.free(priv_key_path); 1844 1845 if (builtin.target.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1846 const null_path = if (builtin.target.os.tag == .windows) "NUL" else "/dev/null"; 1847 return try allocator.print("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 }); 1848 } 1849 1850 const ServerFiles = union(enum) { 1851 files: []const struct { path: []const u8, content: []const u8 }, 1852 dirs: []const []const u8, 1853 }; 1854 1855 /// commit files without writing the server's index or worktree. 1856 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 { 1857 const R = @TypeOf(repo.*); 1858 const opts = repo.self_repo_opts; 1859 const Index = xit.index.Index(.xit, opts); 1860 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1861 const Ctx = struct { 1862 core: *R.Core, 1863 io: std.Io, 1864 allocator: std.mem.Allocator, 1865 files: ServerFiles, 1866 metadata: xit.object.CommitMetadata(opts.hash), 1867 result: *[xit.hash.hexLen(opts.hash)]u8, 1868 1869 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1870 var reader = std.Io.Reader.fixed(content); 1871 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1872 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1873 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1874 defer ctx.allocator.free(path_parts); 1875 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1876 } 1877 1878 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1879 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1880 defer dir.close(ctx.io); 1881 var entries = dir.iterate(); 1882 while (try entries.next(ctx.io)) |entry| { 1883 const child = try std.Io.Dir.path.join(ctx.allocator, &.{ path, entry.name }); 1884 defer ctx.allocator.free(child); 1885 switch (entry.kind) { 1886 .directory => try ctx.addDir(state, index, child), 1887 .file => { 1888 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1889 defer ctx.allocator.free(content); 1890 try ctx.addFile(state, index, child, content); 1891 }, 1892 else => {}, 1893 } 1894 } 1895 } 1896 1897 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1898 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1899 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1900 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1901 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); 1902 defer index.deinit(); 1903 switch (ctx.files) { 1904 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1905 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1906 } 1907 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1908 defer tree.deinit(); 1909 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1910 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1911 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1912 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) }); 1913 } 1914 }; 1915 try repo.core.db_file.lock(io, .exclusive); 1916 defer repo.core.db_file.unlock(io); 1917 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1918 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1919 .core = &repo.core, 1920 .io = io, 1921 .allocator = allocator, 1922 .files = files, 1923 .metadata = metadata, 1924 .result = &result, 1925 }); 1926 return result; 1927 } 1928