╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
726 break :blk try std.fmt.allocPrint(allocator, "ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{priv_key_path}); 727 } else null; 728 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 729 730 731 try client_repo.push( 732 io, 733 allocator, 734 "origin", 735 "master", 736 false, 737 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 738 .command = ssh_cmd_maybe, 739 } } }, 740 ); 741 742 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 743 defer server_repo.deinit(io, allocator); 744 745 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 746 747 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 748 try std.testing.expectEqualStrings(&commit1, &oid_master); 749 } 750 751 fn testClone( 752 comptime repo_kind: rp.RepoKind, 753 comptime transport_def: net.TransportDefinition, 754 comptime port: u16, 755 comptime shell_out_to_git: bool, 756 io: std.Io, 757 allocator: std.mem.Allocator, 758 ) !void { 759 const temp_dir_name = "temp-testnet-clone"; 760 761 // create the temp dir 762 const cwd = std.Io.Dir.cwd(); 763 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 764 if (temp_dir_or_err) |*temp_dir| { 765 temp_dir.close(io); 766 try cwd.deleteTree(io, temp_dir_name); 767 } else |_| {} 768 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 769 defer cwd.deleteTree(io, temp_dir_name) catch {}; 770 defer temp_dir.close(io); 771 772 // init server 773 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 774 try server.start(); 775 defer server.stop(); 776 777 const cwd_path = try std.process.currentPathAlloc(io, allocator); 778 defer allocator.free(cwd_path); 779 780 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 781 defer allocator.free(temp_path); 782 783 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 784 defer allocator.free(server_path); 785 786 // init server repo with default branch name as main 787 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 788 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 789 defer server_repo.deinit(io, allocator); 790 791 if (shell_out_to_git) { 792 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 793 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 794 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 795 } 796 797 // make a commit 798 { 799 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 800 defer hello_txt.close(io); 801 try hello_txt.writeStreamingAll(io, "hello, world!"); 802 try server_repo.add(io, allocator, &.{"hello.txt"}); 803 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 804 } 805 806 // tag first commit 807 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 808 809 // make a commit 810 { 811 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 812 defer goodbye_txt.close(io); 813 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 814 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 815 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 816 } 817 818 // export server repo 819 { 820 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 821 defer export_file.close(io); 822 } 823 824 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 825 defer allocator.free(client_path); 826 827 // get remote url 828 const remote_url = blk: { 829 if (.windows == builtin.os.tag) { 830 std.mem.replaceScalar(u8, server_path, '\\', '/'); 831 } 832 const separator = if (server_path[0] == '/') "" else "/"; 833 834 break :blk switch (transport_def) { 835 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 836 .file => try std.fmt.allocPrint(allocator, "server", .{}), // relative file paths work too 837 .wire => |wire_kind| switch (wire_kind) { 838 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 839 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 840 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 841 }, 842 }; 843 }; 844 defer allocator.free(remote_url); 845 846 const is_ssh = switch (transport_def) { 847 .file => false, 848 .wire => |wire_kind| .ssh == wire_kind, 849 }; 850 851 852 if (shell_out_to_git) { 853 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 854 defer allocator.free(priv_key_path); 855 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 856 defer allocator.free(ssh_config_arg); 857 858 { 859 var process = try std.process.spawn(io, .{ 860 .argv = if (is_ssh) 861 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 862 else 863 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 864 .cwd = .{ .path = temp_path }, 865 .stdin = .ignore, 866 .stdout = .ignore, 867 .stderr = .ignore, 868 }); 869 const term = try process.wait(io); 870 if (term != .exited or term.exited != 0) { 871 return error.GitCommandFailed; 872 } 873 } 874 875 // make sure shallow clone was successful 876 { 877 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 878 hello_txt.close(io); 879 } 880 881 // make a third commit on the server 882 { 883 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 884 defer extra_txt.close(io); 885 try extra_txt.writeStreamingAll(io, "extra content"); 886 try server_repo.add(io, allocator, &.{"extra.txt"}); 887 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 888 } 889 890 // pull --unshallow to deepen the clone and get the new commit 891 { 892 var process = try std.process.spawn(io, .{ 893 .argv = if (is_ssh) 894 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 895 else 896 &.{ "git", "pull", "--unshallow" }, 897 .cwd = .{ .path = client_path }, 898 .stdin = .ignore, 899 .stdout = .ignore, 900 .stderr = .ignore, 901 }); 902 const term = try process.wait(io); 903 if (term != .exited or term.exited != 0) { 904 return error.GitCommandFailed; 905 } 906 } 907 908 // make sure unshallow pull was successful 909 { 910 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 911 extra_txt.close(io); 912 } 913 914 // delete client and clone again with --shallow-since 915 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 916 917 { 918 var process = try std.process.spawn(io, .{ 919 .argv = if (is_ssh) 920 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 921 else 922 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 923 .cwd = .{ .path = temp_path }, 924 .stdin = .ignore, 925 .stdout = .ignore, 926 .stderr = .ignore, 927 }); 928 const term = try process.wait(io); 929 if (term != .exited or term.exited != 0) { 930 return error.GitCommandFailed; 931 } 932 } 933 934 // make sure shallow clone was successful 935 { 936 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 937 hello_txt.close(io); 938 } 939 940 // delete client and clone again with --shallow-exclude 941 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 942 943 { 944 var process = try std.process.spawn(io, .{ 945 .argv = if (is_ssh) 946 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 947 else 948 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 949 .cwd = .{ .path = temp_path }, 950 .stdin = .ignore, 951 .stdout = .ignore, 952 .stderr = .ignore, 953 }); 954 const term = try process.wait(io); 955 if (term != .exited or term.exited != 0) { 956 return error.GitCommandFailed; 957 } 958 } 959 960 // make sure shallow clone was successful 961 { 962 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 963 hello_txt.close(io); 964 } 965 966 // delete client and clone again with --filter=blob:none 967 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 968 969 { 970 var process = try std.process.spawn(io, .{ 971 .argv = if (is_ssh) 972 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 973 else 974 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 975 .cwd = .{ .path = temp_path }, 976 .stdin = .ignore, 977 .stdout = .ignore, 978 .stderr = .ignore, 979 }); 980 const term = try process.wait(io); 981 if (term != .exited or term.exited != 0) { 982 return error.GitCommandFailed; 983 } 984 } 985 986 // make sure partial clone was successful 987 { 988 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 989 hello_txt.close(io); 990 } 991 992 // delete client and clone again with --filter=tree:0 993 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 994 995 { 996 var process = try std.process.spawn(io, .{ 997 .argv = if (is_ssh) 998 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 999 else 1000 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1001 .cwd = .{ .path = temp_path }, 1002 .stdin = .ignore, 1003 .stdout = .ignore, 1004 .stderr = .ignore, 1005 }); 1006 const term = try process.wait(io); 1007 if (term != .exited or term.exited != 0) { 1008 return error.GitCommandFailed; 1009 } 1010 } 1011 1012 // make sure treeless clone was successful 1013 { 1014 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1015 goodbye_txt.close(io); 1016 } 1017 } else { 1018 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1019 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1020 defer allocator.free(priv_key_path); 1021 1022 break :blk try std.fmt.allocPrint(allocator, "ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{priv_key_path}); 1023 } else null; 1024 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1025 1026 // clone repo 1027 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1028 io, 1029 allocator, 1030 remote_url, 1031 temp_path, 1032 client_path, 1033 .{ .wire = .{ .ssh = .{ 1034 .command = ssh_cmd_maybe, 1035 } } }, 1036 ); 1037 defer client_repo.deinit(io, allocator); 1038 1039 // make sure HEAD points to the right default branch 1040 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1041 const head = try client_repo.head(io, &current_branch_buffer); 1042 try std.testing.expectEqualStrings("main", head.ref.name); 1043 1044 // make sure clone was successful 1045 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1046 defer hello_txt.close(io); 1047 } 1048 } 1049 1050 fn testFetchLarge( 1051 comptime repo_kind: rp.RepoKind, 1052 comptime transport_def: net.TransportDefinition, 1053 comptime port: u16, 1054 comptime shell_out_to_git: bool, 1055 io: std.Io, 1056 allocator: std.mem.Allocator, 1057 ) !void { 1058 const temp_dir_name = "temp-testnet-fetch-large"; 1059 1060 // create the temp dir 1061 const cwd = std.Io.Dir.cwd(); 1062 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1063 if (temp_dir_or_err) |*temp_dir| { 1064 temp_dir.close(io); 1065 try cwd.deleteTree(io, temp_dir_name); 1066 } else |_| {} 1067 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1068 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1069 defer temp_dir.close(io); 1070 1071 // init server 1072 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1073 try server.start(); 1074 defer server.stop(); 1075 1076 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1077 defer allocator.free(cwd_path); 1078 1079 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1080 defer allocator.free(server_path); 1081 1082 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1083 defer server_repo.deinit(io, allocator); 1084 1085 var server_dir = try cwd.openDir(io, server_path, .{}); 1086 defer server_dir.close(io); 1087 1088 // copy files from current repo into server dir 1089 for (&[_][]const u8{"src"}) |dir_name| { 1090 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1091 defer src_repo_dir.close(io); 1092 1093 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1094 defer dest_repo_dir.close(io); 1095 1096 try copyDir(io, src_repo_dir, dest_repo_dir); 1097 1098 try server_repo.add(io, allocator, &.{dir_name}); 1099 } 1100 1101 // make a commit 1102 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1103 1104 // export server repo 1105 { 1106 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1107 defer export_file.close(io); 1108 } 1109 1110 if (shell_out_to_git) { 1111 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1112 } 1113 1114 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1115 defer allocator.free(client_path); 1116 1117 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1118 defer client_repo.deinit(io, allocator); 1119 1120 // add remote 1121 { 1122 if (.windows == builtin.os.tag) { 1123 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1124 } 1125 const separator = if (server_path[0] == '/') "" else "/"; 1126 1127 const remote_url = switch (transport_def) { 1128 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1129 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1130 .wire => |wire_kind| switch (wire_kind) { 1131 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1132 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1133 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1134 }, 1135 }; 1136 defer allocator.free(remote_url); 1137 1138 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1139 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1140 } 1141 1142 const is_ssh = switch (transport_def) { 1143 .file => false, 1144 .wire => |wire_kind| .ssh == wire_kind, 1145 }; 1146 1147 1148 if (shell_out_to_git) { 1149 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1150 defer allocator.free(priv_key_path); 1151 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 1152 defer allocator.free(ssh_config_arg); 1153 1154 { 1155 var process = try std.process.spawn(io, .{ 1156 .argv = if (is_ssh) 1157 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1158 else 1159 &.{ "git", "pull", "origin", "master" }, 1160 .cwd = .{ .path = client_path }, 1161 .stdin = .ignore, 1162 .stdout = .ignore, 1163 .stderr = .ignore, 1164 }); 1165 const term = try process.wait(io); 1166 if (term != .exited or term.exited != 0) { 1167 return error.GitCommandFailed; 1168 } 1169 } 1170 1171 // make sure pull was successful 1172 { 1173 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1174 try std.testing.expectEqualStrings(&commit1, &oid_master); 1175 } 1176 1177 // make another commit on the server 1178 const commit2 = blk: { 1179 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1180 defer extra_txt.close(io); 1181 try extra_txt.writeStreamingAll(io, "extra content"); 1182 try server_repo.add(io, allocator, &.{"extra.txt"}); 1183 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1184 }; 1185 1186 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1187 { 1188 var process = try std.process.spawn(io, .{ 1189 .argv = if (is_ssh) 1190 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1191 else 1192 &.{ "git", "fetch", "origin", "master" }, 1193 .cwd = .{ .path = client_path }, 1194 .stdin = .ignore, 1195 .stdout = .ignore, 1196 .stderr = .ignore, 1197 }); 1198 const term = try process.wait(io); 1199 if (term != .exited or term.exited != 0) { 1200 return error.GitCommandFailed; 1201 } 1202 } 1203 1204 // make sure fetch with want-ref was successful 1205 { 1206 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1207 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1208 } 1209 } else { 1210 const refspecs = &.{ 1211 "+refs/heads/master:refs/heads/master", 1212 }; 1213 1214 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1215 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1216 defer allocator.free(known_hosts_path); 1217 1218 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1219 defer allocator.free(priv_key_path); 1220 1221 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1222 } else null; 1223 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1224 1225 try client_repo.fetch( 1226 io, 1227 allocator, 1228 "origin", 1229 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1230 .command = ssh_cmd_maybe, 1231 } } }, 1232 ); 1233 1234 // update the working dir 1235 try client_repo.restore(io, allocator, "."); 1236 } 1237 1238 // make sure fetch was successful 1239 { 1240 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1241 try std.testing.expectEqualStrings(&commit1, &oid_master); 1242 } 1243 } 1244 1245 fn testPushLarge( 1246 comptime repo_kind: rp.RepoKind, 1247 comptime transport_def: net.TransportDefinition, 1248 comptime port: u16, 1249 comptime shell_out_to_git: bool, 1250 io: std.Io, 1251 allocator: std.mem.Allocator, 1252 ) !void { 1253 const temp_dir_name = "temp-testnet-push-large"; 1254 1255 // create the temp dir 1256 const cwd = std.Io.Dir.cwd(); 1257 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1258 if (temp_dir_or_err) |*temp_dir| { 1259 temp_dir.close(io); 1260 try cwd.deleteTree(io, temp_dir_name); 1261 } else |_| {} 1262 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1263 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1264 defer temp_dir.close(io); 1265 1266 // init server 1267 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1268 try server.start(); 1269 defer server.stop(); 1270 1271 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1272 defer allocator.free(cwd_path); 1273 1274 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1275 defer allocator.free(server_path); 1276 1277 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1278 defer server_repo.deinit(io, allocator); 1279 1280 // add config 1281 switch (transport_def) { 1282 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1283 .wire => { 1284 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1285 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1286 }, 1287 } 1288 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1289 1290 // export server repo 1291 { 1292 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1293 defer export_file.close(io); 1294 } 1295 1296 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1297 defer allocator.free(client_path); 1298 1299 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1300 defer client_repo.deinit(io, allocator); 1301 1302 var client_dir = try cwd.openDir(io, client_path, .{}); 1303 defer client_dir.close(io); 1304 1305 { 1306 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1307 defer hello_txt.close(io); 1308 try hello_txt.writeStreamingAll(io, "hello, world!"); 1309 try client_repo.add(io, allocator, &.{"hello.txt"}); 1310 } 1311 1312 // copy files from current repo into client dir 1313 for (&[_][]const u8{"src"}) |dir_name| { 1314 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1315 defer src_repo_dir.close(io); 1316 1317 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1318 defer dest_repo_dir.close(io); 1319 1320 try copyDir(io, src_repo_dir, dest_repo_dir); 1321 1322 try client_repo.add(io, allocator, &.{dir_name}); 1323 } 1324 1325 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1326 1327 // change the files so git will send them as delta objects 1328 for (&[_][]const u8{"src"}) |dir_name| { 1329 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1330 defer dest_repo_dir.close(io); 1331 1332 { 1333 var iter = dest_repo_dir.iterate(); 1334 while (try iter.next(io)) |entry| { 1335 switch (entry.kind) { 1336 .file => { 1337 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1338 defer file.close(io); 1339 var writer = file.writer(io, &.{}); 1340 try writer.interface.writeAll("EDIT"); 1341 }, 1342 else => {}, 1343 } 1344 } 1345 } 1346 1347 try client_repo.add(io, allocator, &.{dir_name}); 1348 } 1349 1350 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1351 1352 // add remote 1353 { 1354 if (.windows == builtin.os.tag) { 1355 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1356 } 1357 const separator = if (server_path[0] == '/') "" else "/"; 1358 1359 const remote_url = switch (transport_def) { 1360 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1361 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1362 .wire => |wire_kind| switch (wire_kind) { 1363 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1364 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1365 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1366 }, 1367 }; 1368 defer allocator.free(remote_url); 1369 1370 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1371 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1372 } 1373 1374 const is_ssh = switch (transport_def) { 1375 .file => false, 1376 .wire => |wire_kind| .ssh == wire_kind, 1377 }; 1378 1379 1380 if (shell_out_to_git) { 1381 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1382 defer allocator.free(priv_key_path); 1383 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 1384 defer allocator.free(ssh_config_arg); 1385 1386 // shell out to git so it will send delta objects 1387 var process = try std.process.spawn(io, .{ 1388 .argv = if (is_ssh) 1389 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1390 else 1391 &.{ "git", "push", "origin", "master" }, 1392 .cwd = .{ .path = client_path }, 1393 .stdin = .ignore, 1394 .stdout = .ignore, 1395 .stderr = .ignore, 1396 }); 1397 const term = try process.wait(io); 1398 if (term != .exited or term.exited != 0) { 1399 return error.GitCommandFailed; 1400 } 1401 } else { 1402 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1403 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1404 defer allocator.free(known_hosts_path); 1405 1406 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1407 defer allocator.free(priv_key_path); 1408 1409 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1410 } else null; 1411 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1412 1413 try client_repo.push( 1414 io, 1415 allocator, 1416 "origin", 1417 "master", 1418 false, 1419 .{ .wire = .{ .ssh = .{ 1420 .command = ssh_cmd_maybe, 1421 } } }, 1422 ); 1423 1424 if (transport_def == .file) { 1425 // update the working dir 1426 try server_repo.restore(io, allocator, "."); 1427 } 1428 } 1429 1430 // make sure push was successful 1431 { 1432 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1433 try std.testing.expectEqualStrings(&commit2, &oid_master); 1434 1435 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1436 defer hello_txt.close(io); 1437 } 1438 } 1439 1440 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1441 var iter = src_dir.iterate(); 1442 while (try iter.next(io)) |entry| { 1443 switch (entry.kind) { 1444 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1445 .directory => { 1446 try dest_dir.createDirPath(io, entry.name); 1447 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1448 defer dest_entry_dir.close(io); 1449 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1450 defer src_entry_dir.close(io); 1451 try copyDir(io, src_entry_dir, dest_entry_dir); 1452 }, 1453 else => {}, 1454 } 1455 } 1456 } 1457