╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
656 // define test events 657 // 658 659 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 660 const issue_event_id = evt.EventWithId.randomId(prng.random()); 661 662 const events_to_push = [_]evt.EventWithId{ 663 .{ 664 .id = std.fmt.bytesToHex(issue_event_id, .lower), 665 .author = .{ .name = "haxy", .email = "user@haxy" }, 666 .event = .{ 667 .issue = .{ 668 .title = "Login form clears password on validation error", 669 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 670 .tags = "bug priority-high ui", 671 }, 672 }, 673 }, 674 }; 675 676 // commit the issue on the client and push it to the server 677 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 678 try client_repo.push( 679 io, 680 allocator, 681 "origin", 682 "haxy/events", 683 false, 684 .{ .wire = .{ .ssh = .{ 685 .command = ssh_cmd_maybe, 686 } } }, 687 ); 688 689 // the push consumed the issue into the server repo's db 690 { 691 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 692 693 // get the map of issues 694 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 695 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 696 697 // get the issue out of the map 698 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 699 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 700 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 701 702 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.description); 703 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.tags); 704 } 705 706 // 707 // edit the issue 708 // 709 710 // this event edits the previous one because it has the same id 711 const events_to_push2 = [_]evt.EventWithId{ 712 .{ 713 .id = std.fmt.bytesToHex(issue_event_id, .lower), 714 .author = .{ .name = "haxy", .email = "user@haxy" }, 715 .event = .{ 716 .issue = .{ 717 .title = "Login form clears password on validation error", 718 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 719 .tags = "bug priority-low ui", 720 }, 721 }, 722 }, 723 }; 724 725 // commit the edit on the client and push it to the server 726 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 727 try client_repo.push( 728 io, 729 allocator, 730 "origin", 731 "haxy/events", 732 false, 733 .{ .wire = .{ .ssh = .{ 734 .command = ssh_cmd_maybe, 735 } } }, 736 ); 737 738 // the push consumed the edit into the server repo's db 739 { 740 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 741 742 // get the map of issues 743 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 744 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 745 746 // get the issue out of the map that was edited 747 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 748 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 749 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 750 751 // the description and tags were correctly edited 752 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.description); 753 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.tags); 754 } 755 } 756 757 fn testClone( 758 comptime repo_kind: rp.RepoKind, 759 comptime transport_def: net.TransportDefinition, 760 comptime shell_out_to_git: bool, 761 io: std.Io, 762 allocator: std.mem.Allocator, 763 ) !void { 764 const temp_dir_name = "temp-testnet-clone"; 765 766 // create the temp dir 767 const cwd = std.Io.Dir.cwd(); 768 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 769 if (temp_dir_or_err) |*temp_dir| { 770 temp_dir.close(io); 771 try cwd.deleteTree(io, temp_dir_name); 772 } else |_| {} 773 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 774 defer cwd.deleteTree(io, temp_dir_name) catch {}; 775 defer temp_dir.close(io); 776 777 // init server 778 var server_process = try runServer(io, allocator, temp_dir_name); 779 defer _ = server_process.kill(io); 780 781 const cwd_path = try std.process.currentPathAlloc(io, allocator); 782 defer allocator.free(cwd_path); 783 784 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 785 defer allocator.free(temp_path); 786 787 // register the repo under admin and locate its on-disk directory 788 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 789 defer allocator.free(server_path); 790 791 // init server repo with default branch name as main 792 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 793 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 794 defer server_repo.deinit(io, allocator); 795 796 if (shell_out_to_git) { 797 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 798 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 799 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 800 } 801 802 // make a commit 803 { 804 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 805 defer hello_txt.close(io); 806 try hello_txt.writeStreamingAll(io, "hello, world!"); 807 try server_repo.add(io, allocator, &.{"hello.txt"}); 808 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 809 } 810 811 // tag first commit 812 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 813 814 // make a commit 815 { 816 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 817 defer goodbye_txt.close(io); 818 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 819 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 820 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 821 } 822 823 // export server repo 824 { 825 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 826 defer export_file.close(io); 827 } 828 829 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 830 defer allocator.free(client_path); 831 832 // get remote url 833 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 834 defer allocator.free(remote_url); 835 836 const is_ssh = switch (transport_def) { 837 .file => false, 838 .wire => |wire_kind| .ssh == wire_kind, 839 }; 840 841 if (shell_out_to_git) { 842 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 843 defer allocator.free(priv_key_path); 844 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}", .{ ssh_port, priv_key_path }); 845 defer allocator.free(ssh_config_arg); 846 847 { 848 var process = try std.process.spawn(io, .{ 849 .argv = if (is_ssh) 850 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 851 else 852 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 853 .cwd = .{ .path = temp_path }, 854 .stdin = .ignore, 855 .stdout = .ignore, 856 .stderr = .ignore, 857 }); 858 const term = try process.wait(io); 859 if (term != .exited or term.exited != 0) { 860 return error.GitCommandFailed; 861 } 862 } 863 864 // make sure shallow clone was successful 865 { 866 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 867 hello_txt.close(io); 868 } 869 870 // make a third commit on the server 871 { 872 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 873 defer extra_txt.close(io); 874 try extra_txt.writeStreamingAll(io, "extra content"); 875 try server_repo.add(io, allocator, &.{"extra.txt"}); 876 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 877 } 878 879 // pull --unshallow to deepen the clone and get the new commit 880 { 881 var process = try std.process.spawn(io, .{ 882 .argv = if (is_ssh) 883 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 884 else 885 &.{ "git", "pull", "--unshallow" }, 886 .cwd = .{ .path = client_path }, 887 .stdin = .ignore, 888 .stdout = .ignore, 889 .stderr = .ignore, 890 }); 891 const term = try process.wait(io); 892 if (term != .exited or term.exited != 0) { 893 return error.GitCommandFailed; 894 } 895 } 896 897 // make sure unshallow pull was successful 898 { 899 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 900 extra_txt.close(io); 901 } 902 903 // delete client and clone again with --shallow-since 904 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 905 906 { 907 var process = try std.process.spawn(io, .{ 908 .argv = if (is_ssh) 909 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 910 else 911 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 912 .cwd = .{ .path = temp_path }, 913 .stdin = .ignore, 914 .stdout = .ignore, 915 .stderr = .ignore, 916 }); 917 const term = try process.wait(io); 918 if (term != .exited or term.exited != 0) { 919 return error.GitCommandFailed; 920 } 921 } 922 923 // make sure shallow clone was successful 924 { 925 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 926 hello_txt.close(io); 927 } 928 929 // delete client and clone again with --shallow-exclude 930 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 931 932 { 933 var process = try std.process.spawn(io, .{ 934 .argv = if (is_ssh) 935 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 936 else 937 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 938 .cwd = .{ .path = temp_path }, 939 .stdin = .ignore, 940 .stdout = .ignore, 941 .stderr = .ignore, 942 }); 943 const term = try process.wait(io); 944 if (term != .exited or term.exited != 0) { 945 return error.GitCommandFailed; 946 } 947 } 948 949 // make sure shallow clone was successful 950 { 951 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 952 hello_txt.close(io); 953 } 954 955 // delete client and clone again with --filter=blob:none 956 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 957 958 { 959 var process = try std.process.spawn(io, .{ 960 .argv = if (is_ssh) 961 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 962 else 963 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 964 .cwd = .{ .path = temp_path }, 965 .stdin = .ignore, 966 .stdout = .ignore, 967 .stderr = .ignore, 968 }); 969 const term = try process.wait(io); 970 if (term != .exited or term.exited != 0) { 971 return error.GitCommandFailed; 972 } 973 } 974 975 // make sure partial clone was successful 976 { 977 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 978 hello_txt.close(io); 979 } 980 981 // delete client and clone again with --filter=tree:0 982 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 983 984 { 985 var process = try std.process.spawn(io, .{ 986 .argv = if (is_ssh) 987 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 988 else 989 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 990 .cwd = .{ .path = temp_path }, 991 .stdin = .ignore, 992 .stdout = .ignore, 993 .stderr = .ignore, 994 }); 995 const term = try process.wait(io); 996 if (term != .exited or term.exited != 0) { 997 return error.GitCommandFailed; 998 } 999 } 1000 1001 // make sure treeless clone was successful 1002 { 1003 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1004 goodbye_txt.close(io); 1005 } 1006 } else { 1007 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1008 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1009 1010 // clone repo 1011 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1012 io, 1013 allocator, 1014 remote_url, 1015 temp_path, 1016 client_path, 1017 null, 1018 .{ .wire = .{ .ssh = .{ 1019 .command = ssh_cmd_maybe, 1020 } } }, 1021 ); 1022 defer client_repo.deinit(io, allocator); 1023 1024 // make sure HEAD points to the right default branch 1025 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1026 const head = try client_repo.head(io, &current_branch_buffer); 1027 try std.testing.expectEqualStrings("main", head.ref.name); 1028 1029 // make sure clone was successful 1030 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1031 defer hello_txt.close(io); 1032 } 1033 } 1034 1035 fn testFetchLarge( 1036 comptime repo_kind: rp.RepoKind, 1037 comptime transport_def: net.TransportDefinition, 1038 comptime shell_out_to_git: bool, 1039 io: std.Io, 1040 allocator: std.mem.Allocator, 1041 ) !void { 1042 const temp_dir_name = "temp-testnet-fetch-large"; 1043 1044 // create the temp dir 1045 const cwd = std.Io.Dir.cwd(); 1046 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1047 if (temp_dir_or_err) |*temp_dir| { 1048 temp_dir.close(io); 1049 try cwd.deleteTree(io, temp_dir_name); 1050 } else |_| {} 1051 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1052 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1053 defer temp_dir.close(io); 1054 1055 // init server 1056 var server_process = try runServer(io, allocator, temp_dir_name); 1057 defer _ = server_process.kill(io); 1058 1059 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1060 defer allocator.free(cwd_path); 1061 1062 // register the repo under admin and locate its on-disk directory 1063 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1064 defer allocator.free(server_path); 1065 1066 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1067 defer server_repo.deinit(io, allocator); 1068 1069 var server_dir = try cwd.openDir(io, server_path, .{}); 1070 defer server_dir.close(io); 1071 1072 // copy files from current repo into server dir 1073 for (&[_][]const u8{"src"}) |dir_name| { 1074 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1075 defer src_repo_dir.close(io); 1076 1077 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1078 defer dest_repo_dir.close(io); 1079 1080 try copyDir(io, src_repo_dir, dest_repo_dir); 1081 1082 try server_repo.add(io, allocator, &.{dir_name}); 1083 } 1084 1085 // make a commit 1086 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1087 1088 // export server repo 1089 { 1090 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1091 defer export_file.close(io); 1092 } 1093 1094 if (shell_out_to_git) { 1095 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1096 } 1097 1098 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1099 defer allocator.free(client_path); 1100 1101 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1102 defer client_repo.deinit(io, allocator); 1103 1104 // add remote 1105 { 1106 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1107 defer allocator.free(remote_url); 1108 1109 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1110 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1111 } 1112 1113 const is_ssh = switch (transport_def) { 1114 .file => false, 1115 .wire => |wire_kind| .ssh == wire_kind, 1116 }; 1117 1118 if (shell_out_to_git) { 1119 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1120 defer allocator.free(priv_key_path); 1121 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}", .{ ssh_port, priv_key_path }); 1122 defer allocator.free(ssh_config_arg); 1123 1124 { 1125 var process = try std.process.spawn(io, .{ 1126 .argv = if (is_ssh) 1127 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1128 else 1129 &.{ "git", "pull", "origin", "master" }, 1130 .cwd = .{ .path = client_path }, 1131 .stdin = .ignore, 1132 .stdout = .ignore, 1133 .stderr = .ignore, 1134 }); 1135 const term = try process.wait(io); 1136 if (term != .exited or term.exited != 0) { 1137 return error.GitCommandFailed; 1138 } 1139 } 1140 1141 // make sure pull was successful 1142 { 1143 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1144 try std.testing.expectEqualStrings(&commit1, &oid_master); 1145 } 1146 1147 // make another commit on the server 1148 const commit2 = blk: { 1149 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1150 defer extra_txt.close(io); 1151 try extra_txt.writeStreamingAll(io, "extra content"); 1152 try server_repo.add(io, allocator, &.{"extra.txt"}); 1153 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1154 }; 1155 1156 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1157 { 1158 var process = try std.process.spawn(io, .{ 1159 .argv = if (is_ssh) 1160 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1161 else 1162 &.{ "git", "fetch", "origin", "master" }, 1163 .cwd = .{ .path = client_path }, 1164 .stdin = .ignore, 1165 .stdout = .ignore, 1166 .stderr = .ignore, 1167 }); 1168 const term = try process.wait(io); 1169 if (term != .exited or term.exited != 0) { 1170 return error.GitCommandFailed; 1171 } 1172 } 1173 1174 // make sure fetch with want-ref was successful 1175 { 1176 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1177 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1178 } 1179 } else { 1180 const refspecs = &.{ 1181 "+refs/heads/master:refs/heads/master", 1182 }; 1183 1184 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1185 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1186 1187 try client_repo.fetch( 1188 io, 1189 allocator, 1190 "origin", 1191 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1192 .command = ssh_cmd_maybe, 1193 } } }, 1194 ); 1195 1196 // update the working dir 1197 try client_repo.restore(io, allocator, "."); 1198 } 1199 1200 // make sure fetch was successful 1201 { 1202 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1203 try std.testing.expectEqualStrings(&commit1, &oid_master); 1204 } 1205 } 1206 1207 fn testPushLarge( 1208 comptime repo_kind: rp.RepoKind, 1209 comptime transport_def: net.TransportDefinition, 1210 comptime shell_out_to_git: bool, 1211 io: std.Io, 1212 allocator: std.mem.Allocator, 1213 ) !void { 1214 const temp_dir_name = "temp-testnet-push-large"; 1215 1216 // create the temp dir 1217 const cwd = std.Io.Dir.cwd(); 1218 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1219 if (temp_dir_or_err) |*temp_dir| { 1220 temp_dir.close(io); 1221 try cwd.deleteTree(io, temp_dir_name); 1222 } else |_| {} 1223 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1224 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1225 defer temp_dir.close(io); 1226 1227 // init server 1228 var server_process = try runServer(io, allocator, temp_dir_name); 1229 defer _ = server_process.kill(io); 1230 1231 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1232 defer allocator.free(cwd_path); 1233 1234 // register the repo under admin and locate its on-disk directory 1235 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1236 defer allocator.free(server_path); 1237 1238 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1239 defer server_repo.deinit(io, allocator); 1240 1241 // add config 1242 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1243 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1244 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1245 1246 // export server repo 1247 { 1248 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1249 defer export_file.close(io); 1250 } 1251 1252 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1253 defer allocator.free(client_path); 1254 1255 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1256 defer client_repo.deinit(io, allocator); 1257 1258 var client_dir = try cwd.openDir(io, client_path, .{}); 1259 defer client_dir.close(io); 1260 1261 { 1262 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1263 defer hello_txt.close(io); 1264 try hello_txt.writeStreamingAll(io, "hello, world!"); 1265 try client_repo.add(io, allocator, &.{"hello.txt"}); 1266 } 1267 1268 // copy files from current repo into client dir 1269 for (&[_][]const u8{"src"}) |dir_name| { 1270 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1271 defer src_repo_dir.close(io); 1272 1273 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1274 defer dest_repo_dir.close(io); 1275 1276 try copyDir(io, src_repo_dir, dest_repo_dir); 1277 1278 try client_repo.add(io, allocator, &.{dir_name}); 1279 } 1280 1281 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1282 1283 // change the files so git will send them as delta objects 1284 for (&[_][]const u8{"src"}) |dir_name| { 1285 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1286 defer dest_repo_dir.close(io); 1287 1288 { 1289 var iter = dest_repo_dir.iterate(); 1290 while (try iter.next(io)) |entry| { 1291 switch (entry.kind) { 1292 .file => { 1293 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1294 defer file.close(io); 1295 var writer = file.writer(io, &.{}); 1296 try writer.interface.writeAll("EDIT"); 1297 }, 1298 else => {}, 1299 } 1300 } 1301 } 1302 1303 try client_repo.add(io, allocator, &.{dir_name}); 1304 } 1305 1306 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1307 1308 // add remote 1309 { 1310 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1311 defer allocator.free(remote_url); 1312 1313 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1314 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1315 } 1316 1317 const is_ssh = switch (transport_def) { 1318 .file => false, 1319 .wire => |wire_kind| .ssh == wire_kind, 1320 }; 1321 1322 if (shell_out_to_git) { 1323 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1324 defer allocator.free(priv_key_path); 1325 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}", .{ ssh_port, priv_key_path }); 1326 defer allocator.free(ssh_config_arg); 1327 1328 // shell out to git so it will send delta objects 1329 var process = try std.process.spawn(io, .{ 1330 .argv = if (is_ssh) 1331 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1332 else 1333 &.{ "git", "push", "origin", "master" }, 1334 .cwd = .{ .path = client_path }, 1335 .stdin = .ignore, 1336 .stdout = .ignore, 1337 .stderr = .ignore, 1338 }); 1339 const term = try process.wait(io); 1340 if (term != .exited or term.exited != 0) { 1341 return error.GitCommandFailed; 1342 } 1343 } else { 1344 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1345 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1346 1347 try client_repo.push( 1348 io, 1349 allocator, 1350 "origin", 1351 "master", 1352 false, 1353 .{ .wire = .{ .ssh = .{ 1354 .command = ssh_cmd_maybe, 1355 } } }, 1356 ); 1357 } 1358 1359 // make sure push was successful 1360 { 1361 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1362 try std.testing.expectEqualStrings(&commit2, &oid_master); 1363 1364 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1365 defer hello_txt.close(io); 1366 } 1367 } 1368 1369 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1370 var iter = src_dir.iterate(); 1371 while (try iter.next(io)) |entry| { 1372 switch (entry.kind) { 1373 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1374 .directory => { 1375 try dest_dir.createDirPath(io, entry.name); 1376 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1377 defer dest_entry_dir.close(io); 1378 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1379 defer src_entry_dir.close(io); 1380 try copyDir(io, src_entry_dir, dest_entry_dir); 1381 }, 1382 else => {}, 1383 } 1384 } 1385 } 1386 1387 // the dev SSH public key matching the private key written by runServer 1388 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1389 1390 // create the admin event repo with a user holding the dev SSH public key 1391 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1392 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1393 defer allocator.free(cwd_path); 1394 1395 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1396 defer allocator.free(admin_repo_path); 1397 1398 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1399 defer repo.deinit(io, allocator); 1400 1401 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1402 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1403 1404 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1405 const user_id = evt.EventWithId.randomId(prng.random()); 1406 1407 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1408 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1409 1410 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1411 .id = std.fmt.bytesToHex(user_id, .lower), 1412 .author = .{ .name = "admin", .email = "admin@example.test" }, 1413 .event = .{ .user = .{ 1414 .name = "admin", 1415 .email = "admin@example.test", 1416 .password_hash = password_hash, 1417 .ssh_keys = admin_ssh_key, 1418 } }, 1419 }}); 1420 } 1421 1422 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1423 // the event store 1424 fn repoOnDiskPath( 1425 io: std.Io, 1426 allocator: std.mem.Allocator, 1427 data_dir_name: []const u8, 1428 repo_name: []const u8, 1429 create: bool, 1430 ) !?[]u8 { 1431 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1432 defer allocator.free(cwd_path); 1433 1434 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1435 defer allocator.free(admin_repo_path); 1436 1437 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1438 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1439 } 1440 1441 // build the remote URL addressing admin/<repo_name> over the given transport 1442 fn remoteUrl( 1443 comptime transport_def: net.TransportDefinition, 1444 allocator: std.mem.Allocator, 1445 repo_name: []const u8, 1446 ) ![]u8 { 1447 return switch (transport_def) { 1448 .file => unreachable, 1449 .wire => |wire_kind| switch (wire_kind) { 1450 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1451 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1452 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1453 }, 1454 }; 1455 } 1456 1457 // the ssh command the haxy git client should invoke, or null when not using 1458 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1459 // and skip writing to known_hosts. 1460 fn sshCommand( 1461 is_ssh: bool, 1462 allocator: std.mem.Allocator, 1463 cwd_path: []const u8, 1464 temp_dir_name: []const u8, 1465 ) !?[]u8 { 1466 if (!is_ssh) return null; 1467 1468 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1469 defer allocator.free(priv_key_path); 1470 1471 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}\"", .{ ssh_port, priv_key_path }); 1472 } 1473