╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1059 const term = try process.wait(io); 1060 if (term != .exited or term.exited != 0) { 1061 return error.GitCommandFailed; 1062 } 1063 } 1064 1065 // make sure pull was successful 1066 { 1067 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1068 try std.testing.expectEqualStrings(&commit1, &oid_master); 1069 } 1070 1071 // make another commit on the server 1072 const commit2 = blk: { 1073 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1074 defer extra_txt.close(io); 1075 try extra_txt.writeStreamingAll(io, "extra content"); 1076 try server_repo.add(io, allocator, &.{"extra.txt"}); 1077 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1078 }; 1079 1080 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1081 { 1082 var process = try std.process.spawn(io, .{ 1083 .argv = if (is_ssh) 1084 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1085 else 1086 &.{ "git", "fetch", "origin", "master" }, 1087 .cwd = .{ .path = client_path }, 1088 .stdin = .ignore, 1089 .stdout = .ignore, 1090 .stderr = .ignore, 1091 }); 1092 const term = try process.wait(io); 1093 if (term != .exited or term.exited != 0) { 1094 return error.GitCommandFailed; 1095 } 1096 } 1097 1098 // make sure fetch with want-ref was successful 1099 { 1100 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1101 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1102 } 1103 } else { 1104 const refspecs = &.{ 1105 "+refs/heads/master:refs/heads/master", 1106 }; 1107 1108 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1109 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1110 defer allocator.free(known_hosts_path); 1111 1112 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1113 defer allocator.free(priv_key_path); 1114 1115 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1116 } else null; 1117 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1118 1119 try client_repo.fetch( 1120 io, 1121 allocator, 1122 "origin", 1123 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1124 .command = ssh_cmd_maybe, 1125 } } }, 1126 ); 1127 1128 // update the working dir 1129 try client_repo.restore(io, allocator, "."); 1130 } 1131 1132 // make sure fetch was successful 1133 { 1134 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1135 try std.testing.expectEqualStrings(&commit1, &oid_master); 1136 } 1137 } 1138 1139 fn testPushLarge( 1140 comptime repo_kind: rp.RepoKind, 1141 comptime transport_def: net.TransportDefinition, 1142 comptime shell_out_to_git: bool, 1143 io: std.Io, 1144 allocator: std.mem.Allocator, 1145 ) !void { 1146 const temp_dir_name = "temp-testnet-push-large"; 1147 1148 // create the temp dir 1149 const cwd = std.Io.Dir.cwd(); 1150 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1151 if (temp_dir_or_err) |*temp_dir| { 1152 temp_dir.close(io); 1153 try cwd.deleteTree(io, temp_dir_name); 1154 } else |_| {} 1155 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1156 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1157 defer temp_dir.close(io); 1158 1159 // init server 1160 var server_process = try runServer(io, allocator, temp_dir_name); 1161 defer _ = server_process.kill(io); 1162 1163 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1164 defer allocator.free(cwd_path); 1165 1166 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1167 defer allocator.free(server_path); 1168 1169 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1170 defer server_repo.deinit(io, allocator); 1171 1172 // add config 1173 switch (transport_def) { 1174 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1175 .wire => { 1176 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1177 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1178 }, 1179 } 1180 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1181 1182 // export server repo 1183 { 1184 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1185 defer export_file.close(io); 1186 } 1187 1188 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1189 defer allocator.free(client_path); 1190 1191 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1192 defer client_repo.deinit(io, allocator); 1193 1194 var client_dir = try cwd.openDir(io, client_path, .{}); 1195 defer client_dir.close(io); 1196 1197 { 1198 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1199 defer hello_txt.close(io); 1200 try hello_txt.writeStreamingAll(io, "hello, world!"); 1201 try client_repo.add(io, allocator, &.{"hello.txt"}); 1202 } 1203 1204 // copy files from current repo into client dir 1205 for (&[_][]const u8{"src"}) |dir_name| { 1206 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1207 defer src_repo_dir.close(io); 1208 1209 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1210 defer dest_repo_dir.close(io); 1211 1212 try copyDir(io, src_repo_dir, dest_repo_dir); 1213 1214 try client_repo.add(io, allocator, &.{dir_name}); 1215 } 1216 1217 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1218 1219 // change the files so git will send them as delta objects 1220 for (&[_][]const u8{"src"}) |dir_name| { 1221 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1222 defer dest_repo_dir.close(io); 1223 1224 { 1225 var iter = dest_repo_dir.iterate(); 1226 while (try iter.next(io)) |entry| { 1227 switch (entry.kind) { 1228 .file => { 1229 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1230 defer file.close(io); 1231 var writer = file.writer(io, &.{}); 1232 try writer.interface.writeAll("EDIT"); 1233 }, 1234 else => {}, 1235 } 1236 } 1237 } 1238 1239 try client_repo.add(io, allocator, &.{dir_name}); 1240 } 1241 1242 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1243 1244 // add remote 1245 { 1246 if (.windows == builtin.os.tag) { 1247 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1248 } 1249 const separator = if (server_path[0] == '/') "" else "/"; 1250 1251 const remote_url = switch (transport_def) { 1252 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1253 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1254 .wire => |wire_kind| switch (wire_kind) { 1255 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{http_port}), 1256 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{http_port}), 1257 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ ssh_port, separator, server_path }), 1258 }, 1259 }; 1260 defer allocator.free(remote_url); 1261 1262 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1263 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1264 } 1265 1266 const is_ssh = switch (transport_def) { 1267 .file => false, 1268 .wire => |wire_kind| .ssh == wire_kind, 1269 }; 1270 1271 if (shell_out_to_git) { 1272 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1273 defer allocator.free(priv_key_path); 1274 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 1275 defer allocator.free(ssh_config_arg); 1276 1277 // shell out to git so it will send delta objects 1278 var process = try std.process.spawn(io, .{ 1279 .argv = if (is_ssh) 1280 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1281 else 1282 &.{ "git", "push", "origin", "master" }, 1283 .cwd = .{ .path = client_path }, 1284 .stdin = .ignore, 1285 .stdout = .ignore, 1286 .stderr = .ignore, 1287 }); 1288 const term = try process.wait(io); 1289 if (term != .exited or term.exited != 0) { 1290 return error.GitCommandFailed; 1291 } 1292 } else { 1293 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1294 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1295 defer allocator.free(known_hosts_path); 1296 1297 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1298 defer allocator.free(priv_key_path); 1299 1300 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1301 } else null; 1302 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1303 1304 try client_repo.push( 1305 io, 1306 allocator, 1307 "origin", 1308 "master", 1309 false, 1310 .{ .wire = .{ .ssh = .{ 1311 .command = ssh_cmd_maybe, 1312 } } }, 1313 ); 1314 1315 if (transport_def == .file) { 1316 // update the working dir 1317 try server_repo.restore(io, allocator, "."); 1318 } 1319 } 1320 1321 // make sure push was successful 1322 { 1323 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1324 try std.testing.expectEqualStrings(&commit2, &oid_master); 1325 1326 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1327 defer hello_txt.close(io); 1328 } 1329 } 1330 1331 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1332 var iter = src_dir.iterate(); 1333 while (try iter.next(io)) |entry| { 1334 switch (entry.kind) { 1335 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1336 .directory => { 1337 try dest_dir.createDirPath(io, entry.name); 1338 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1339 defer dest_entry_dir.close(io); 1340 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1341 defer src_entry_dir.close(io); 1342 try copyDir(io, src_entry_dir, dest_entry_dir); 1343 }, 1344 else => {}, 1345 } 1346 } 1347 } 1348