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