╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
901 902 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 903 904 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 905 try std.testing.expectEqualStrings(&commit1, &oid_master); 906 } 907 908 fn testPushEvents( 909 comptime repo_kind: rp.RepoKind, 910 comptime transport_def: net.TransportDefinition, 911 comptime hash_kind: hash.HashKind, 912 comptime port: u16, 913 io: std.Io, 914 allocator: std.mem.Allocator, 915 ) !void { 916 // create the temp dir 917 var temp = std.testing.tmpDir(.{}); 918 defer temp.cleanup(); 919 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 920 defer allocator.free(temp_path); 921 922 // init server 923 var server_process = try runServer(io, allocator, temp_path, port); 924 defer _ = server_process.kill(io); 925 926 // register the repo under admin and locate its on-disk directory 927 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 928 defer allocator.free(server_path); 929 930 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 931 const ServerRepo = rp.Repo(.xit, repo_opts); 932 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 933 defer server_repo.deinit(io, allocator); 934 935 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 936 defer allocator.free(client_path); 937 938 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 939 defer client_repo.deinit(io, allocator); 940 941 // add remote 942 { 943 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 944 defer allocator.free(remote_url); 945 946 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 947 } 948 949 const is_ssh = switch (transport_def) { 950 .file => false, 951 .wire => |wire_kind| .ssh == wire_kind, 952 }; 953 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 954 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 955 956 var arena = std.heap.ArenaAllocator.init(allocator); 957 defer arena.deinit(); 958 959 // 960 // define test events 961 // 962 963 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 964 const issue_event_id = evt.EventWithId.randomId(prng.random()); 965 966 const events_to_push = [_]evt.EventWithId{ 967 .{ 968 .id = std.fmt.bytesToHex(issue_event_id, .lower), 969 .author = .{ .name = "haxy", .email = "user@haxy" }, 970 .event = .{ 971 .issue = .{ 972 .title = "Login form clears password on validation error", 973 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 974 .tags = "bug priority-high ui", 975 }, 976 }, 977 }, 978 }; 979 980 // commit the issue on the client and push it to the server 981 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 982 try client_repo.push( 983 io, 984 allocator, 985 "origin", 986 "haxy/events", 987 false, 988 .{ .wire = .{ .ssh = .{ 989 .command = ssh_cmd_maybe, 990 } } }, 991 ); 992 993 // the push consumed the issue into the server repo's db 994 { 995 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 996 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 997 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 998 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 999 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1000 1001 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1002 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.event.tags); 1003 } 1004 1005 // 1006 // edit the issue 1007 // 1008 1009 // this event edits the previous one because it has the same id 1010 const events_to_push2 = [_]evt.EventWithId{ 1011 .{ 1012 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1013 .author = .{ .name = "haxy", .email = "user@haxy" }, 1014 .event = .{ 1015 .issue = .{ 1016 .title = "Login form clears password on validation error", 1017 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1018 .tags = "bug priority-low ui", 1019 }, 1020 }, 1021 }, 1022 }; 1023 1024 // commit the edit on the client and push it to the server 1025 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1026 try client_repo.push( 1027 io, 1028 allocator, 1029 "origin", 1030 "haxy/events", 1031 false, 1032 .{ .wire = .{ .ssh = .{ 1033 .command = ssh_cmd_maybe, 1034 } } }, 1035 ); 1036 1037 // the push consumed the edit into the server repo's db 1038 { 1039 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1040 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1041 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1042 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1043 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1044 1045 // the description and tags were correctly edited 1046 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1047 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.event.tags); 1048 } 1049 } 1050 1051 fn testClone( 1052 comptime repo_kind: rp.RepoKind, 1053 comptime transport_def: net.TransportDefinition, 1054 comptime shell_out_to_git: bool, 1055 comptime hash_kind: hash.HashKind, 1056 comptime port: u16, 1057 io: std.Io, 1058 allocator: std.mem.Allocator, 1059 ) !void { 1060 // create the temp dir 1061 var temp = std.testing.tmpDir(.{}); 1062 defer temp.cleanup(); 1063 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1064 defer allocator.free(temp_path); 1065 1066 // init server 1067 var server_process = try runServer(io, allocator, temp_path, port); 1068 defer _ = server_process.kill(io); 1069 1070 // register the repo under admin and locate its on-disk directory 1071 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1072 defer allocator.free(server_path); 1073 1074 // init server repo with default branch name as main 1075 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1076 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = !shell_out_to_git }).init(io, allocator, .{ .bare = true, .path = server_path, .create_default_branch = "main" }); 1077 defer server_repo.deinit(io, allocator); 1078 1079 if (shell_out_to_git) { 1080 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1081 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1082 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1083 } 1084 1085 // make a commit 1086 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1087 1088 // tag first commit 1089 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1090 1091 // make a commit 1092 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1093 1094 // export server repo 1095 { 1096 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1097 defer export_file.close(io); 1098 } 1099 1100 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1101 defer allocator.free(client_path); 1102 1103 // get remote url 1104 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1105 defer allocator.free(remote_url); 1106 1107 const is_ssh = switch (transport_def) { 1108 .file => false, 1109 .wire => |wire_kind| .ssh == wire_kind, 1110 }; 1111 1112 if (shell_out_to_git) { 1113 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1114 defer allocator.free(ssh_cmd); 1115 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1116 defer allocator.free(ssh_config_arg); 1117 1118 { 1119 var process = try std.process.spawn(io, .{ 1120 .argv = if (is_ssh) 1121 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--depth", "1", remote_url, "client" } 1122 else 1123 &.{ "git", "clone", "--quiet", "--depth", "1", remote_url, "client" }, 1124 .cwd = .{ .path = temp_path }, 1125 .stdin = .ignore, 1126 .stdout = .ignore, 1127 .stderr = .inherit, 1128 }); 1129 defer process.kill(io); 1130 const term = try process.wait(io); 1131 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1132 } 1133 1134 // make sure shallow clone was successful 1135 { 1136 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1137 hello_txt.close(io); 1138 } 1139 1140 // make a third commit on the server 1141 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1142 1143 // pull --unshallow to deepen the clone and get the new commit 1144 { 1145 var process = try std.process.spawn(io, .{ 1146 .argv = if (is_ssh) 1147 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "--unshallow" } 1148 else 1149 &.{ "git", "pull", "--quiet", "--unshallow" }, 1150 .cwd = .{ .path = client_path }, 1151 .stdin = .ignore, 1152 .stdout = .ignore, 1153 .stderr = .inherit, 1154 }); 1155 defer process.kill(io); 1156 const term = try process.wait(io); 1157 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1158 } 1159 1160 // make sure unshallow pull was successful 1161 { 1162 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1163 extra_txt.close(io); 1164 } 1165 1166 // delete client and clone again with --shallow-since 1167 try temp.dir.deleteTree(io, "client"); 1168 1169 { 1170 var process = try std.process.spawn(io, .{ 1171 .argv = if (is_ssh) 1172 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" } 1173 else 1174 &.{ "git", "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" }, 1175 .cwd = .{ .path = temp_path }, 1176 .stdin = .ignore, 1177 .stdout = .ignore, 1178 .stderr = .inherit, 1179 }); 1180 defer process.kill(io); 1181 const term = try process.wait(io); 1182 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1183 } 1184 1185 // make sure shallow clone was successful 1186 { 1187 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1188 hello_txt.close(io); 1189 } 1190 1191 // delete client and clone again with --shallow-exclude 1192 try temp.dir.deleteTree(io, "client"); 1193 1194 { 1195 var process = try std.process.spawn(io, .{ 1196 .argv = if (is_ssh) 1197 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1198 else 1199 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1200 .cwd = .{ .path = temp_path }, 1201 .stdin = .ignore, 1202 .stdout = .ignore, 1203 .stderr = .inherit, 1204 }); 1205 defer process.kill(io); 1206 const term = try process.wait(io); 1207 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1208 } 1209 1210 // make sure shallow clone was successful 1211 { 1212 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1213 hello_txt.close(io); 1214 } 1215 1216 // delete client and clone again with --filter=blob:none 1217 try temp.dir.deleteTree(io, "client"); 1218 1219 { 1220 var process = try std.process.spawn(io, .{ 1221 .argv = if (is_ssh) 1222 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1223 else 1224 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1225 .cwd = .{ .path = temp_path }, 1226 .stdin = .ignore, 1227 .stdout = .ignore, 1228 .stderr = .inherit, 1229 }); 1230 defer process.kill(io); 1231 const term = try process.wait(io); 1232 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1233 } 1234 1235 // make sure partial clone was successful 1236 { 1237 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1238 hello_txt.close(io); 1239 } 1240 1241 // delete client and clone again with --filter=tree:0 1242 try temp.dir.deleteTree(io, "client"); 1243 1244 { 1245 var process = try std.process.spawn(io, .{ 1246 .argv = if (is_ssh) 1247 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1248 else 1249 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1250 .cwd = .{ .path = temp_path }, 1251 .stdin = .ignore, 1252 .stdout = .ignore, 1253 .stderr = .inherit, 1254 }); 1255 defer process.kill(io); 1256 const term = try process.wait(io); 1257 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1258 } 1259 1260 // make sure treeless clone was successful 1261 { 1262 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1263 goodbye_txt.close(io); 1264 } 1265 } else { 1266 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1267 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1268 1269 // clone repo 1270 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1271 io, 1272 allocator, 1273 remote_url, 1274 temp_path, 1275 client_path, 1276 null, 1277 .{ .transport = .{ .wire = .{ .ssh = .{ 1278 .command = ssh_cmd_maybe, 1279 } } } }, 1280 ); 1281 defer clone_result.deinit(io, allocator); 1282 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1283 1284 // make sure HEAD points to the right default branch 1285 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1286 const head = try client_repo.head(io, &current_branch_buffer); 1287 try std.testing.expectEqualStrings("main", head.ref.name); 1288 1289 // make sure clone was successful 1290 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1291 defer hello_txt.close(io); 1292 } 1293 } 1294 1295 fn testFetchLarge( 1296 comptime repo_kind: rp.RepoKind, 1297 comptime transport_def: net.TransportDefinition, 1298 comptime shell_out_to_git: bool, 1299 comptime hash_kind: hash.HashKind, 1300 comptime port: u16, 1301 io: std.Io, 1302 allocator: std.mem.Allocator, 1303 ) !void { 1304 // create the temp dir 1305 var temp = std.testing.tmpDir(.{}); 1306 defer temp.cleanup(); 1307 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1308 defer allocator.free(temp_path); 1309 1310 // init server 1311 var server_process = try runServer(io, allocator, temp_path, port); 1312 defer _ = server_process.kill(io); 1313 1314 // register the repo under admin and locate its on-disk directory 1315 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1316 defer allocator.free(server_path); 1317 1318 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1319 defer server_repo.deinit(io, allocator); 1320 1321 // make a commit with files from the current repo 1322 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1323 1324 // export server repo 1325 { 1326 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1327 defer export_file.close(io); 1328 } 1329 1330 if (shell_out_to_git) { 1331 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1332 } 1333 1334 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1335 defer allocator.free(client_path); 1336 1337 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1338 defer client_repo.deinit(io, allocator); 1339 1340 // add remote 1341 { 1342 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1343 defer allocator.free(remote_url); 1344 1345 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1346 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1347 } 1348 1349 const is_ssh = switch (transport_def) { 1350 .file => false, 1351 .wire => |wire_kind| .ssh == wire_kind, 1352 }; 1353 1354 if (shell_out_to_git) { 1355 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1356 defer allocator.free(ssh_cmd); 1357 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1358 defer allocator.free(ssh_config_arg); 1359 1360 { 1361 var process = try std.process.spawn(io, .{ 1362 .argv = if (is_ssh) 1363 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1364 else 1365 &.{ "git", "pull", "--quiet", "origin", "master" }, 1366 .cwd = .{ .path = client_path }, 1367 .stdin = .ignore, 1368 .stdout = .ignore, 1369 .stderr = .inherit, 1370 }); 1371 defer process.kill(io); 1372 const term = try process.wait(io); 1373 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1374 } 1375 1376 // make sure pull was successful 1377 { 1378 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1379 try std.testing.expectEqualStrings(&commit1, &oid_master); 1380 } 1381 1382 // make another commit on the server 1383 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1384 1385 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1386 { 1387 var process = try std.process.spawn(io, .{ 1388 .argv = if (is_ssh) 1389 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1390 else 1391 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1392 .cwd = .{ .path = client_path }, 1393 .stdin = .ignore, 1394 .stdout = .ignore, 1395 .stderr = .inherit, 1396 }); 1397 defer process.kill(io); 1398 const term = try process.wait(io); 1399 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1400 } 1401 1402 // make sure fetch with want-ref was successful 1403 { 1404 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1405 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1406 } 1407 } else { 1408 const refspecs = &.{ 1409 "+refs/heads/master:refs/heads/master", 1410 }; 1411 1412 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1413 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1414 1415 try client_repo.fetch( 1416 io, 1417 allocator, 1418 "origin", 1419 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1420 .command = ssh_cmd_maybe, 1421 } } }, 1422 ); 1423 1424 // update the working dir 1425 try client_repo.restore(io, allocator, "."); 1426 } 1427 1428 // make sure fetch was successful 1429 { 1430 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1431 try std.testing.expectEqualStrings(&commit1, &oid_master); 1432 } 1433 } 1434 1435 fn testPushLarge( 1436 comptime repo_kind: rp.RepoKind, 1437 comptime transport_def: net.TransportDefinition, 1438 comptime shell_out_to_git: bool, 1439 comptime hash_kind: hash.HashKind, 1440 comptime port: u16, 1441 io: std.Io, 1442 allocator: std.mem.Allocator, 1443 ) !void { 1444 // create the temp dir 1445 var temp = std.testing.tmpDir(.{}); 1446 defer temp.cleanup(); 1447 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1448 defer allocator.free(temp_path); 1449 1450 // init server 1451 var server_process = try runServer(io, allocator, temp_path, port); 1452 defer _ = server_process.kill(io); 1453 1454 // register the repo under admin and locate its on-disk directory 1455 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1456 defer allocator.free(server_path); 1457 1458 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1459 defer server_repo.deinit(io, allocator); 1460 1461 // add config 1462 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1463 1464 // export server repo 1465 { 1466 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1467 defer export_file.close(io); 1468 } 1469 1470 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1471 defer allocator.free(client_path); 1472 1473 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1474 defer client_repo.deinit(io, allocator); 1475 1476 var client_dir = try temp.dir.openDir(io, "client", .{}); 1477 defer client_dir.close(io); 1478 1479 { 1480 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1481 defer hello_txt.close(io); 1482 try hello_txt.writeStreamingAll(io, "hello, world!"); 1483 try client_repo.add(io, allocator, &.{"hello.txt"}); 1484 } 1485 1486 // copy files from current repo into client dir 1487 const cwd = std.Io.Dir.cwd(); 1488 for (&[_][]const u8{"src"}) |dir_name| { 1489 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1490 defer src_repo_dir.close(io); 1491 1492 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1493 defer dest_repo_dir.close(io); 1494 1495 try copyDir(io, src_repo_dir, dest_repo_dir); 1496 1497 try client_repo.add(io, allocator, &.{dir_name}); 1498 } 1499 1500 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1501 1502 // change the files so git will send them as delta objects 1503 for (&[_][]const u8{"src"}) |dir_name| { 1504 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1505 defer dest_repo_dir.close(io); 1506 1507 { 1508 var iter = dest_repo_dir.iterate(); 1509 while (try iter.next(io)) |entry| { 1510 switch (entry.kind) { 1511 .file => { 1512 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1513 defer file.close(io); 1514 var writer = file.writer(io, &.{}); 1515 try writer.interface.writeAll("EDIT"); 1516 }, 1517 else => {}, 1518 } 1519 } 1520 } 1521 1522 try client_repo.add(io, allocator, &.{dir_name}); 1523 } 1524 1525 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1526 1527 // add remote 1528 { 1529 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1530 defer allocator.free(remote_url); 1531 1532 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1533 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1534 } 1535 1536 const is_ssh = switch (transport_def) { 1537 .file => false, 1538 .wire => |wire_kind| .ssh == wire_kind, 1539 }; 1540 1541 if (shell_out_to_git) { 1542 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1543 defer allocator.free(ssh_cmd); 1544 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1545 defer allocator.free(ssh_config_arg); 1546 1547 // shell out to git so it will send delta objects 1548 var process = try std.process.spawn(io, .{ 1549 .argv = if (is_ssh) 1550 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1551 else 1552 &.{ "git", "push", "--quiet", "origin", "master" }, 1553 .cwd = .{ .path = client_path }, 1554 .stdin = .ignore, 1555 .stdout = .ignore, 1556 .stderr = .inherit, 1557 }); 1558 defer process.kill(io); 1559 const term = try process.wait(io); 1560 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1561 } else { 1562 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1563 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1564 1565 try client_repo.push( 1566 io, 1567 allocator, 1568 "origin", 1569 "master", 1570 false, 1571 .{ .wire = .{ .ssh = .{ 1572 .command = ssh_cmd_maybe, 1573 } } }, 1574 ); 1575 } 1576 1577 // make sure push was successful 1578 { 1579 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1580 try std.testing.expectEqualStrings(&commit2, &oid_master); 1581 1582 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1583 } 1584 } 1585 1586 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1587 var iter = src_dir.iterate(); 1588 while (try iter.next(io)) |entry| { 1589 switch (entry.kind) { 1590 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1591 .directory => { 1592 try dest_dir.createDirPath(io, entry.name); 1593 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1594 defer dest_entry_dir.close(io); 1595 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1596 defer src_entry_dir.close(io); 1597 try copyDir(io, src_entry_dir, dest_entry_dir); 1598 }, 1599 else => {}, 1600 } 1601 } 1602 } 1603 1604 // the dev SSH public key matching the private key written by runServer 1605 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1606 1607 // create the admin event repo with a user holding the dev SSH public key 1608 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1609 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1610 defer allocator.free(admin_repo_path); 1611 1612 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1613 defer repo.deinit(io, allocator); 1614 1615 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1616 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1617 1618 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1619 const user_id = evt.EventWithId.randomId(prng.random()); 1620 1621 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1622 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1623 1624 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1625 .id = std.fmt.bytesToHex(user_id, .lower), 1626 .author = .{ .name = "admin", .email = "admin@example.test" }, 1627 .event = .{ .user = .{ 1628 .name = "admin", 1629 .email = "admin@example.test", 1630 .password_hash = password_hash, 1631 .ssh_keys = admin_ssh_key, 1632 } }, 1633 }}); 1634 } 1635 1636 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1637 // the event store 1638 fn repoOnDiskPath( 1639 io: std.Io, 1640 allocator: std.mem.Allocator, 1641 data_path: []const u8, 1642 repo_name: []const u8, 1643 create: bool, 1644 ) !?[]u8 { 1645 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1646 defer allocator.free(admin_repo_path); 1647 1648 const event_id_hex = (try evt.resolveOrCreateRepo( 1649 io, 1650 allocator, 1651 admin_repo_path, 1652 "admin", 1653 repo_name, 1654 if (create) .{ .read_access = .public } else null, 1655 )) orelse return null; 1656 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1657 } 1658 1659 // build the remote URL addressing admin/<repo_name> over the given transport 1660 fn remoteUrl( 1661 comptime transport_def: net.TransportDefinition, 1662 allocator: std.mem.Allocator, 1663 repo_name: []const u8, 1664 port: u16, 1665 ) ![]u8 { 1666 return switch (transport_def) { 1667 .file => unreachable, 1668 .wire => |wire_kind| switch (wire_kind) { 1669 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1670 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1671 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1672 }, 1673 }; 1674 } 1675 1676 // the ssh command the haxy git client should invoke, or null when not using 1677 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1678 // and skip writing to known_hosts. 1679 fn sshCommand( 1680 is_ssh: bool, 1681 allocator: std.mem.Allocator, 1682 temp_path: []const u8, 1683 port: u16, 1684 ) !?[]u8 { 1685 if (!is_ssh) return null; 1686 1687 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1688 defer allocator.free(priv_key_path); 1689 1690 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1691 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1692 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile={s} -o LogLevel=ERROR -o IdentitiesOnly=yes -i \"{s}\"", .{ port + 1, null_path, priv_key_path }); 1693 } 1694 1695 const ServerFiles = union(enum) { 1696 files: []const struct { path: []const u8, content: []const u8 }, 1697 dirs: []const []const u8, 1698 }; 1699 1700 /// commit files without writing the server's index or worktree. 1701 fn commitServer(repo: anytype, io: std.Io, allocator: std.mem.Allocator, files: ServerFiles, metadata: xit.object.CommitMetadata(repo.self_repo_opts.hash)) ![xit.hash.hexLen(repo.self_repo_opts.hash)]u8 { 1702 const R = @TypeOf(repo.*); 1703 const opts = repo.self_repo_opts; 1704 const Index = xit.index.Index(.xit, opts); 1705 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1706 const Ctx = struct { 1707 core: *R.Core, 1708 io: std.Io, 1709 allocator: std.mem.Allocator, 1710 files: ServerFiles, 1711 metadata: xit.object.CommitMetadata(opts.hash), 1712 result: *[xit.hash.hexLen(opts.hash)]u8, 1713 1714 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1715 var reader = std.Io.Reader.fixed(content); 1716 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1717 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1718 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1719 defer ctx.allocator.free(path_parts); 1720 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1721 } 1722 1723 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1724 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1725 defer dir.close(ctx.io); 1726 var entries = dir.iterate(); 1727 while (try entries.next(ctx.io)) |entry| { 1728 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1729 defer ctx.allocator.free(child); 1730 switch (entry.kind) { 1731 .directory => try ctx.addDir(state, index, child), 1732 .file => { 1733 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1734 defer ctx.allocator.free(content); 1735 try ctx.addFile(state, index, child, content); 1736 }, 1737 else => {}, 1738 } 1739 } 1740 } 1741 1742 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1743 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1744 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1745 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1746 var index = if (head_oid) |oid| try Index.initFromCommit(state.readOnly(), ctx.io, ctx.allocator, &oid) else try Index.init(state.readOnly(), ctx.io, ctx.allocator); 1747 defer index.deinit(); 1748 switch (ctx.files) { 1749 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1750 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1751 } 1752 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1753 defer tree.deinit(); 1754 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1755 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1756 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1757 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1758 } 1759 }; 1760 try repo.core.db_file.lock(io, .exclusive); 1761 defer repo.core.db_file.unlock(io); 1762 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1763 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1764 .core = &repo.core, 1765 .io = io, 1766 .allocator = allocator, 1767 .files = files, 1768 .metadata = metadata, 1769 .result = &result, 1770 }); 1771 return result; 1772 } 1773