╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ test.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1472 } 1473 1474 const is_ssh = switch (transport_def) { 1475 .file => false, 1476 .wire => |wire_kind| .ssh == wire_kind, 1477 }; 1478 1479 const upload_pack_command = try uploadPackCommand(server_repo_kind, is_ssh, use_serve_command, allocator, cwd_path, port); 1480 defer allocator.free(upload_pack_command); 1481 1482 if (shell_out_to_git) { 1483 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1484 defer allocator.free(priv_key_path); 1485 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1486 defer allocator.free(ssh_config_arg); 1487 1488 { 1489 var process = try std.process.spawn(io, .{ 1490 .argv = if (is_ssh) 1491 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "origin", "master" } 1492 else 1493 &.{ "git", "pull", "origin", "master" }, 1494 .cwd = .{ .path = client_path }, 1495 .stdin = .ignore, 1496 .stdout = .ignore, 1497 .stderr = .ignore, 1498 }); 1499 const term = try process.wait(io); 1500 if (term != .exited) { 1501 return error.GitCommandFailed; 1502 } 1503 } 1504 1505 // make sure pull was successful 1506 { 1507 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1508 try std.testing.expectEqualStrings(&commit1, &oid_master); 1509 } 1510 1511 // make another commit on the server 1512 const commit2 = blk: { 1513 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1514 defer extra_txt.close(io); 1515 try extra_txt.writeStreamingAll(io, "extra content"); 1516 try server_repo.add(io, allocator, &.{"extra.txt"}); 1517 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1518 }; 1519 1520 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1521 { 1522 var process = try std.process.spawn(io, .{ 1523 .argv = if (is_ssh) 1524 &.{ "git", "-c", ssh_config_arg, "fetch", "--upload-pack", upload_pack_command, "origin", "master" } 1525 else 1526 &.{ "git", "fetch", "origin", "master" }, 1527 .cwd = .{ .path = client_path }, 1528 .stdin = .ignore, 1529 .stdout = .ignore, 1530 .stderr = .ignore, 1531 }); 1532 const term = try process.wait(io); 1533 if (term != .exited) { 1534 return error.GitCommandFailed; 1535 } 1536 } 1537 1538 // make sure fetch with want-ref was successful 1539 { 1540 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1541 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1542 } 1543 } else { 1544 const refspecs = &.{ 1545 "+refs/heads/master:refs/heads/master", 1546 }; 1547 1548 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1549 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1550 defer allocator.free(known_hosts_path); 1551 1552 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1553 defer allocator.free(priv_key_path); 1554 1555 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1556 } else null; 1557 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1558 1559 try client_repo.fetch( 1560 io, 1561 allocator, 1562 "origin", 1563 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1564 .command = ssh_cmd_maybe, 1565 .upload_pack_command = upload_pack_command, 1566 } } }, 1567 ); 1568 1569 // update the working dir 1570 try client_repo.restore(io, allocator, "."); 1571 } 1572 1573 // make sure fetch was successful 1574 { 1575 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1576 try std.testing.expectEqualStrings(&commit1, &oid_master); 1577 } 1578 } 1579 1580 fn testPushLarge( 1581 comptime repo_kind: rp.RepoKind, 1582 comptime server_repo_kind: rp.RepoKind, 1583 comptime transport_def: net.TransportDefinition, 1584 comptime port: u16, 1585 comptime shell_out_to_git: bool, 1586 comptime use_serve_command: bool, 1587 io: std.Io, 1588 allocator: std.mem.Allocator, 1589 ) !void { 1590 const temp_dir_name = "temp-testnet-push-large"; 1591 1592 // create the temp dir 1593 const cwd = std.Io.Dir.cwd(); 1594 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1595 if (temp_dir_or_err) |*temp_dir| { 1596 temp_dir.close(io); 1597 try cwd.deleteTree(io, temp_dir_name); 1598 } else |_| {} 1599 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1600 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1601 defer temp_dir.close(io); 1602 1603 // init server 1604 var server = try Server(server_repo_kind, transport_def, temp_dir_name, port, use_serve_command).init(io, allocator); 1605 try server.start(); 1606 defer server.stop(); 1607 1608 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1609 defer allocator.free(cwd_path); 1610 1611 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 1612 defer allocator.free(server_path); 1613 1614 var server_repo = try rp.Repo(server_repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1615 defer server_repo.deinit(io, allocator); 1616 1617 // add config 1618 switch (transport_def) { 1619 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1620 .wire => { 1621 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1622 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1623 }, 1624 } 1625 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1626 1627 // export server repo 1628 { 1629 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1630 defer export_file.close(io); 1631 } 1632 1633 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1634 defer allocator.free(client_path); 1635 1636 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1637 defer client_repo.deinit(io, allocator); 1638 1639 var client_dir = try cwd.openDir(io, client_path, .{}); 1640 defer client_dir.close(io); 1641 1642 { 1643 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1644 defer hello_txt.close(io); 1645 try hello_txt.writeStreamingAll(io, "hello, world!"); 1646 try client_repo.add(io, allocator, &.{"hello.txt"}); 1647 } 1648 1649 // copy files from current repo into client dir 1650 for (&[_][]const u8{"src"}) |dir_name| { 1651 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1652 defer src_repo_dir.close(io); 1653 1654 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1655 defer dest_repo_dir.close(io); 1656 1657 try copyDir(io, src_repo_dir, dest_repo_dir); 1658 1659 try client_repo.add(io, allocator, &.{dir_name}); 1660 } 1661 1662 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1663 1664 // change the files so git will send them as delta objects 1665 for (&[_][]const u8{"src"}) |dir_name| { 1666 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1667 defer dest_repo_dir.close(io); 1668 1669 { 1670 var iter = dest_repo_dir.iterate(); 1671 while (try iter.next(io)) |entry| { 1672 switch (entry.kind) { 1673 .file => { 1674 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1675 defer file.close(io); 1676 var writer = file.writer(io, &.{}); 1677 try writer.interface.writeAll("EDIT"); 1678 }, 1679 else => {}, 1680 } 1681 } 1682 } 1683 1684 try client_repo.add(io, allocator, &.{dir_name}); 1685 } 1686 1687 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1688 1689 // add remote 1690 { 1691 if (.windows == builtin.os.tag) { 1692 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1693 } 1694 const separator = if (server_path[0] == '/') "" else "/"; 1695 1696 const remote_url = switch (transport_def) { 1697 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1698 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1699 .wire => |wire_kind| switch (wire_kind) { 1700 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1701 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1702 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1703 }, 1704 }; 1705 defer allocator.free(remote_url); 1706 1707 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1708 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1709 } 1710 1711 const is_ssh = switch (transport_def) { 1712 .file => false, 1713 .wire => |wire_kind| .ssh == wire_kind, 1714 }; 1715 1716 const receive_pack_command = try receivePackCommand(server_repo_kind, is_ssh, use_serve_command, allocator, cwd_path, port); 1717 defer allocator.free(receive_pack_command); 1718 1719 if (shell_out_to_git) { 1720 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1721 defer allocator.free(priv_key_path); 1722 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1723 defer allocator.free(ssh_config_arg); 1724 1725 // shell out to git so it will send delta objects 1726 var process = try std.process.spawn(io, .{ 1727 .argv = if (is_ssh) 1728 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" } 1729 else 1730 &.{ "git", "push", "origin", "master" }, 1731 .cwd = .{ .path = client_path }, 1732 .stdin = .ignore, 1733 .stdout = .ignore, 1734 .stderr = .ignore, 1735 }); 1736 const term = try process.wait(io); 1737 if (term != .exited) { 1738 return error.GitCommandFailed; 1739 } 1740 } else { 1741 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1742 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1743 defer allocator.free(known_hosts_path); 1744 1745 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1746 defer allocator.free(priv_key_path); 1747 1748 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1749 } else null; 1750 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1751 1752 try client_repo.push( 1753 io, 1754 allocator, 1755 "origin", 1756 "master", 1757 false, 1758 .{ .wire = .{ .ssh = .{ 1759 .command = ssh_cmd_maybe, 1760 .receive_pack_command = receive_pack_command, 1761 } } }, 1762 ); 1763 1764 if (transport_def == .file) { 1765 // update the working dir 1766 try server_repo.restore(io, allocator, "."); 1767 } 1768 } 1769 1770 // make sure push was successful 1771 { 1772 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1773 try std.testing.expectEqualStrings(&commit2, &oid_master); 1774 1775 const hello_txt = try temp_dir.openFile(io, "server/hello.txt", .{}); 1776 defer hello_txt.close(io); 1777 } 1778 } 1779 1780 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1781 var iter = src_dir.iterate(); 1782 while (try iter.next(io)) |entry| { 1783 switch (entry.kind) { 1784 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1785 .directory => { 1786 try dest_dir.createDirPath(io, entry.name); 1787 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1788 defer dest_entry_dir.close(io); 1789 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1790 defer src_entry_dir.close(io); 1791 try copyDir(io, src_entry_dir, dest_entry_dir); 1792 }, 1793 else => {}, 1794 } 1795 } 1796 } 1797