╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ test.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1531 var server_dir = try cwd.openDir(io, server_path, .{}); 1532 defer server_dir.close(io); 1533 1534 // copy files from current repo into server dir 1535 for (&[_][]const u8{"src"}) |dir_name| { 1536 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1537 defer src_repo_dir.close(io); 1538 1539 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1540 defer dest_repo_dir.close(io); 1541 1542 try copyDir(io, src_repo_dir, dest_repo_dir); 1543 1544 try server_repo.add(io, allocator, &.{dir_name}); 1545 } 1546 1547 // make a commit 1548 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1549 1550 // export server repo 1551 { 1552 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1553 defer export_file.close(io); 1554 } 1555 1556 if (shell_out_to_git) { 1557 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1558 } 1559 1560 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1561 defer allocator.free(client_path); 1562 1563 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1564 defer client_repo.deinit(io, allocator); 1565 1566 // add remote 1567 { 1568 if (.windows == builtin.os.tag) { 1569 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1570 } 1571 const separator = if (server_path[0] == '/') "" else "/"; 1572 1573 const remote_url = switch (transport_def) { 1574 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1575 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1576 .wire => |wire_kind| switch (wire_kind) { 1577 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1578 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1579 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1580 }, 1581 }; 1582 defer allocator.free(remote_url); 1583 1584 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1585 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1586 } 1587 1588 const is_ssh = switch (transport_def) { 1589 .file => false, 1590 .wire => |wire_kind| .ssh == wire_kind, 1591 }; 1592 1593 const upload_pack_command = try uploadPackCommand(server_repo_kind, is_ssh, use_serve_command, allocator, cwd_path, port); 1594 defer allocator.free(upload_pack_command); 1595 1596 if (shell_out_to_git) { 1597 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1598 defer allocator.free(priv_key_path); 1599 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1600 defer allocator.free(ssh_config_arg); 1601 1602 { 1603 var process = try std.process.spawn(io, .{ 1604 .argv = if (is_ssh) 1605 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "origin", "master" } 1606 else 1607 &.{ "git", "pull", "origin", "master" }, 1608 .cwd = .{ .path = client_path }, 1609 .stdin = .ignore, 1610 .stdout = .ignore, 1611 .stderr = .ignore, 1612 }); 1613 const term = try process.wait(io); 1614 if (term != .exited) { 1615 return error.GitCommandFailed; 1616 } 1617 } 1618 1619 // make sure pull was successful 1620 { 1621 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1622 try std.testing.expectEqualStrings(&commit1, &oid_master); 1623 } 1624 1625 // make another commit on the server 1626 const commit2 = blk: { 1627 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1628 defer extra_txt.close(io); 1629 try extra_txt.writeStreamingAll(io, "extra content"); 1630 try server_repo.add(io, allocator, &.{"extra.txt"}); 1631 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1632 }; 1633 1634 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1635 { 1636 var process = try std.process.spawn(io, .{ 1637 .argv = if (is_ssh) 1638 &.{ "git", "-c", ssh_config_arg, "fetch", "--upload-pack", upload_pack_command, "origin", "master" } 1639 else 1640 &.{ "git", "fetch", "origin", "master" }, 1641 .cwd = .{ .path = client_path }, 1642 .stdin = .ignore, 1643 .stdout = .ignore, 1644 .stderr = .ignore, 1645 }); 1646 const term = try process.wait(io); 1647 if (term != .exited) { 1648 return error.GitCommandFailed; 1649 } 1650 } 1651 1652 // make sure fetch with want-ref was successful 1653 { 1654 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1655 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1656 } 1657 } else { 1658 const refspecs = &.{ 1659 "+refs/heads/master:refs/heads/master", 1660 }; 1661 1662 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1663 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1664 defer allocator.free(known_hosts_path); 1665 1666 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1667 defer allocator.free(priv_key_path); 1668 1669 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1670 } else null; 1671 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1672 1673 try client_repo.fetch( 1674 io, 1675 allocator, 1676 "origin", 1677 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1678 .command = ssh_cmd_maybe, 1679 .upload_pack_command = upload_pack_command, 1680 } } }, 1681 ); 1682 1683 // update the working dir 1684 try client_repo.restore(io, allocator, "."); 1685 } 1686 1687 // make sure fetch was successful 1688 { 1689 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1690 try std.testing.expectEqualStrings(&commit1, &oid_master); 1691 } 1692 } 1693 1694 fn testPushLarge( 1695 comptime repo_kind: rp.RepoKind, 1696 comptime server_repo_kind: rp.RepoKind, 1697 comptime transport_def: net.TransportDefinition, 1698 comptime port: u16, 1699 comptime shell_out_to_git: bool, 1700 comptime use_serve_command: bool, 1701 io: std.Io, 1702 allocator: std.mem.Allocator, 1703 ) !void { 1704 const temp_dir_name = "temp-testnet-push-large"; 1705 1706 // create the temp dir 1707 const cwd = std.Io.Dir.cwd(); 1708 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1709 if (temp_dir_or_err) |*temp_dir| { 1710 temp_dir.close(io); 1711 try cwd.deleteTree(io, temp_dir_name); 1712 } else |_| {} 1713 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1714 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1715 defer temp_dir.close(io); 1716 1717 // init server 1718 var server = try Server(server_repo_kind, transport_def, temp_dir_name, port, use_serve_command).init(io, allocator); 1719 try server.start(); 1720 defer server.stop(); 1721 1722 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1723 defer allocator.free(cwd_path); 1724 1725 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 1726 defer allocator.free(server_path); 1727 1728 var server_repo = try rp.Repo(server_repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1729 defer server_repo.deinit(io, allocator); 1730 1731 // add config 1732 switch (transport_def) { 1733 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1734 .wire => { 1735 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1736 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1737 }, 1738 } 1739 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1740 1741 // export server repo 1742 { 1743 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1744 defer export_file.close(io); 1745 } 1746 1747 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1748 defer allocator.free(client_path); 1749 1750 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1751 defer client_repo.deinit(io, allocator); 1752 1753 var client_dir = try cwd.openDir(io, client_path, .{}); 1754 defer client_dir.close(io); 1755 1756 { 1757 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1758 defer hello_txt.close(io); 1759 try hello_txt.writeStreamingAll(io, "hello, world!"); 1760 try client_repo.add(io, allocator, &.{"hello.txt"}); 1761 } 1762 1763 // copy files from current repo into client dir 1764 for (&[_][]const u8{"src"}) |dir_name| { 1765 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1766 defer src_repo_dir.close(io); 1767 1768 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1769 defer dest_repo_dir.close(io); 1770 1771 try copyDir(io, src_repo_dir, dest_repo_dir); 1772 1773 try client_repo.add(io, allocator, &.{dir_name}); 1774 } 1775 1776 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1777 1778 // change the files so git will send them as delta objects 1779 for (&[_][]const u8{"src"}) |dir_name| { 1780 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1781 defer dest_repo_dir.close(io); 1782 1783 { 1784 var iter = dest_repo_dir.iterate(); 1785 while (try iter.next(io)) |entry| { 1786 switch (entry.kind) { 1787 .file => { 1788 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1789 defer file.close(io); 1790 var writer = file.writer(io, &.{}); 1791 try writer.interface.writeAll("EDIT"); 1792 }, 1793 else => {}, 1794 } 1795 } 1796 } 1797 1798 try client_repo.add(io, allocator, &.{dir_name}); 1799 } 1800 1801 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1802 1803 // add remote 1804 { 1805 if (.windows == builtin.os.tag) { 1806 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1807 } 1808 const separator = if (server_path[0] == '/') "" else "/"; 1809 1810 const remote_url = switch (transport_def) { 1811 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1812 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1813 .wire => |wire_kind| switch (wire_kind) { 1814 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1815 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1816 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1817 }, 1818 }; 1819 defer allocator.free(remote_url); 1820 1821 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1822 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1823 } 1824 1825 const is_ssh = switch (transport_def) { 1826 .file => false, 1827 .wire => |wire_kind| .ssh == wire_kind, 1828 }; 1829 1830 const receive_pack_command = try receivePackCommand(server_repo_kind, is_ssh, use_serve_command, allocator, cwd_path, port); 1831 defer allocator.free(receive_pack_command); 1832 1833 if (shell_out_to_git) { 1834 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1835 defer allocator.free(priv_key_path); 1836 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1837 defer allocator.free(ssh_config_arg); 1838 1839 // shell out to git so it will send delta objects 1840 var process = try std.process.spawn(io, .{ 1841 .argv = if (is_ssh) 1842 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" } 1843 else 1844 &.{ "git", "push", "origin", "master" }, 1845 .cwd = .{ .path = client_path }, 1846 .stdin = .ignore, 1847 .stdout = .ignore, 1848 .stderr = .ignore, 1849 }); 1850 const term = try process.wait(io); 1851 if (term != .exited) { 1852 return error.GitCommandFailed; 1853 } 1854 } else { 1855 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1856 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1857 defer allocator.free(known_hosts_path); 1858 1859 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1860 defer allocator.free(priv_key_path); 1861 1862 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1863 } else null; 1864 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1865 1866 try client_repo.push( 1867 io, 1868 allocator, 1869 "origin", 1870 "master", 1871 false, 1872 .{ .wire = .{ .ssh = .{ 1873 .command = ssh_cmd_maybe, 1874 .receive_pack_command = receive_pack_command, 1875 } } }, 1876 ); 1877 1878 if (transport_def == .file) { 1879 // update the working dir 1880 try server_repo.restore(io, allocator, "."); 1881 } 1882 } 1883 1884 // make sure push was successful 1885 { 1886 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1887 try std.testing.expectEqualStrings(&commit2, &oid_master); 1888 1889 const hello_txt = try temp_dir.openFile(io, "server/hello.txt", .{}); 1890 defer hello_txt.close(io); 1891 } 1892 } 1893 1894 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1895 var iter = src_dir.iterate(); 1896 while (try iter.next(io)) |entry| { 1897 switch (entry.kind) { 1898 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1899 .directory => { 1900 try dest_dir.createDirPath(io, entry.name); 1901 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1902 defer dest_entry_dir.close(io); 1903 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1904 defer src_entry_dir.close(io); 1905 try copyDir(io, src_entry_dir, dest_entry_dir); 1906 }, 1907 else => {}, 1908 } 1909 } 1910 } 1911