╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1093 // get remote url 1094 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1095 defer allocator.free(remote_url); 1096 1097 const is_ssh = switch (transport_def) { 1098 .file => false, 1099 .wire => |wire_kind| .ssh == wire_kind, 1100 }; 1101 1102 if (shell_out_to_git) { 1103 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1104 defer allocator.free(priv_key_path); 1105 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}", .{ port + 1, priv_key_path }); 1106 defer allocator.free(ssh_config_arg); 1107 1108 { 1109 var process = try std.process.spawn(io, .{ 1110 .argv = if (is_ssh) 1111 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 1112 else 1113 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1114 .cwd = .{ .path = temp_path }, 1115 .stdin = .ignore, 1116 .stdout = .ignore, 1117 .stderr = .ignore, 1118 }); 1119 const term = try process.wait(io); 1120 if (term != .exited or term.exited != 0) { 1121 return error.GitCommandFailed; 1122 } 1123 } 1124 1125 // make sure shallow clone was successful 1126 { 1127 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1128 hello_txt.close(io); 1129 } 1130 1131 // make a third commit on the server 1132 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1133 1134 // pull --unshallow to deepen the clone and get the new commit 1135 { 1136 var process = try std.process.spawn(io, .{ 1137 .argv = if (is_ssh) 1138 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 1139 else 1140 &.{ "git", "pull", "--unshallow" }, 1141 .cwd = .{ .path = client_path }, 1142 .stdin = .ignore, 1143 .stdout = .ignore, 1144 .stderr = .ignore, 1145 }); 1146 const term = try process.wait(io); 1147 if (term != .exited or term.exited != 0) { 1148 return error.GitCommandFailed; 1149 } 1150 } 1151 1152 // make sure unshallow pull was successful 1153 { 1154 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1155 extra_txt.close(io); 1156 } 1157 1158 // delete client and clone again with --shallow-since 1159 try temp.dir.deleteTree(io, "client"); 1160 1161 { 1162 var process = try std.process.spawn(io, .{ 1163 .argv = if (is_ssh) 1164 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 1165 else 1166 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1167 .cwd = .{ .path = temp_path }, 1168 .stdin = .ignore, 1169 .stdout = .ignore, 1170 .stderr = .ignore, 1171 }); 1172 const term = try process.wait(io); 1173 if (term != .exited or term.exited != 0) { 1174 return error.GitCommandFailed; 1175 } 1176 } 1177 1178 // make sure shallow clone was successful 1179 { 1180 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1181 hello_txt.close(io); 1182 } 1183 1184 // delete client and clone again with --shallow-exclude 1185 try temp.dir.deleteTree(io, "client"); 1186 1187 { 1188 var process = try std.process.spawn(io, .{ 1189 .argv = if (is_ssh) 1190 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 1191 else 1192 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1193 .cwd = .{ .path = temp_path }, 1194 .stdin = .ignore, 1195 .stdout = .ignore, 1196 .stderr = .ignore, 1197 }); 1198 const term = try process.wait(io); 1199 if (term != .exited or term.exited != 0) { 1200 return error.GitCommandFailed; 1201 } 1202 } 1203 1204 // make sure shallow clone was successful 1205 { 1206 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1207 hello_txt.close(io); 1208 } 1209 1210 // delete client and clone again with --filter=blob:none 1211 try temp.dir.deleteTree(io, "client"); 1212 1213 { 1214 var process = try std.process.spawn(io, .{ 1215 .argv = if (is_ssh) 1216 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 1217 else 1218 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1219 .cwd = .{ .path = temp_path }, 1220 .stdin = .ignore, 1221 .stdout = .ignore, 1222 .stderr = .ignore, 1223 }); 1224 const term = try process.wait(io); 1225 if (term != .exited or term.exited != 0) { 1226 return error.GitCommandFailed; 1227 } 1228 } 1229 1230 // make sure partial clone was successful 1231 { 1232 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1233 hello_txt.close(io); 1234 } 1235 1236 // delete client and clone again with --filter=tree:0 1237 try temp.dir.deleteTree(io, "client"); 1238 1239 { 1240 var process = try std.process.spawn(io, .{ 1241 .argv = if (is_ssh) 1242 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 1243 else 1244 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1245 .cwd = .{ .path = temp_path }, 1246 .stdin = .ignore, 1247 .stdout = .ignore, 1248 .stderr = .ignore, 1249 }); 1250 const term = try process.wait(io); 1251 if (term != .exited or term.exited != 0) { 1252 return error.GitCommandFailed; 1253 } 1254 } 1255 1256 // make sure treeless clone was successful 1257 { 1258 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1259 goodbye_txt.close(io); 1260 } 1261 } else { 1262 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1263 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1264 1265 // clone repo 1266 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1267 io, 1268 allocator, 1269 remote_url, 1270 temp_path, 1271 client_path, 1272 null, 1273 .{ .transport = .{ .wire = .{ .ssh = .{ 1274 .command = ssh_cmd_maybe, 1275 } } } }, 1276 ); 1277 defer clone_result.deinit(io, allocator); 1278 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1279 1280 // make sure HEAD points to the right default branch 1281 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1282 const head = try client_repo.head(io, &current_branch_buffer); 1283 try std.testing.expectEqualStrings("main", head.ref.name); 1284 1285 // make sure clone was successful 1286 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1287 defer hello_txt.close(io); 1288 } 1289 } 1290 1291 fn testFetchLarge( 1292 comptime repo_kind: rp.RepoKind, 1293 comptime transport_def: net.TransportDefinition, 1294 comptime shell_out_to_git: bool, 1295 comptime hash_kind: hash.HashKind, 1296 comptime port: u16, 1297 io: std.Io, 1298 allocator: std.mem.Allocator, 1299 ) !void { 1300 // create the temp dir 1301 var temp = std.testing.tmpDir(.{}); 1302 defer temp.cleanup(); 1303 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1304 defer allocator.free(temp_path); 1305 1306 // init server 1307 var server_process = try runServer(io, allocator, temp_path, port); 1308 defer _ = server_process.kill(io); 1309 1310 // register the repo under admin and locate its on-disk directory 1311 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1312 defer allocator.free(server_path); 1313 1314 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1315 defer server_repo.deinit(io, allocator); 1316 1317 // make a commit with files from the current repo 1318 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1319 1320 // export server repo 1321 { 1322 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1323 defer export_file.close(io); 1324 } 1325 1326 if (shell_out_to_git) { 1327 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1328 } 1329 1330 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1331 defer allocator.free(client_path); 1332 1333 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1334 defer client_repo.deinit(io, allocator); 1335 1336 // add remote 1337 { 1338 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1339 defer allocator.free(remote_url); 1340 1341 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1342 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1343 } 1344 1345 const is_ssh = switch (transport_def) { 1346 .file => false, 1347 .wire => |wire_kind| .ssh == wire_kind, 1348 }; 1349 1350 if (shell_out_to_git) { 1351 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1352 defer allocator.free(priv_key_path); 1353 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}", .{ port + 1, priv_key_path }); 1354 defer allocator.free(ssh_config_arg); 1355 1356 { 1357 var process = try std.process.spawn(io, .{ 1358 .argv = if (is_ssh) 1359 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1360 else 1361 &.{ "git", "pull", "origin", "master" }, 1362 .cwd = .{ .path = client_path }, 1363 .stdin = .ignore, 1364 .stdout = .ignore, 1365 .stderr = .ignore, 1366 }); 1367 const term = try process.wait(io); 1368 if (term != .exited or term.exited != 0) { 1369 return error.GitCommandFailed; 1370 } 1371 } 1372 1373 // make sure pull was successful 1374 { 1375 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1376 try std.testing.expectEqualStrings(&commit1, &oid_master); 1377 } 1378 1379 // make another commit on the server 1380 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1381 1382 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1383 { 1384 var process = try std.process.spawn(io, .{ 1385 .argv = if (is_ssh) 1386 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1387 else 1388 &.{ "git", "fetch", "origin", "master" }, 1389 .cwd = .{ .path = client_path }, 1390 .stdin = .ignore, 1391 .stdout = .ignore, 1392 .stderr = .ignore, 1393 }); 1394 const term = try process.wait(io); 1395 if (term != .exited or term.exited != 0) { 1396 return error.GitCommandFailed; 1397 } 1398 } 1399 1400 // make sure fetch with want-ref was successful 1401 { 1402 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1403 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1404 } 1405 } else { 1406 const refspecs = &.{ 1407 "+refs/heads/master:refs/heads/master", 1408 }; 1409 1410 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1411 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1412 1413 try client_repo.fetch( 1414 io, 1415 allocator, 1416 "origin", 1417 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1418 .command = ssh_cmd_maybe, 1419 } } }, 1420 ); 1421 1422 // update the working dir 1423 try client_repo.restore(io, allocator, "."); 1424 } 1425 1426 // make sure fetch was successful 1427 { 1428 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1429 try std.testing.expectEqualStrings(&commit1, &oid_master); 1430 } 1431 } 1432 1433 fn testPushLarge( 1434 comptime repo_kind: rp.RepoKind, 1435 comptime transport_def: net.TransportDefinition, 1436 comptime shell_out_to_git: bool, 1437 comptime hash_kind: hash.HashKind, 1438 comptime port: u16, 1439 io: std.Io, 1440 allocator: std.mem.Allocator, 1441 ) !void { 1442 // create the temp dir 1443 var temp = std.testing.tmpDir(.{}); 1444 defer temp.cleanup(); 1445 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1446 defer allocator.free(temp_path); 1447 1448 // init server 1449 var server_process = try runServer(io, allocator, temp_path, port); 1450 defer _ = server_process.kill(io); 1451 1452 // register the repo under admin and locate its on-disk directory 1453 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1454 defer allocator.free(server_path); 1455 1456 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1457 defer server_repo.deinit(io, allocator); 1458 1459 // add config 1460 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1461 1462 // export server repo 1463 { 1464 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1465 defer export_file.close(io); 1466 } 1467 1468 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1469 defer allocator.free(client_path); 1470 1471 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1472 defer client_repo.deinit(io, allocator); 1473 1474 var client_dir = try temp.dir.openDir(io, "client", .{}); 1475 defer client_dir.close(io); 1476 1477 { 1478 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1479 defer hello_txt.close(io); 1480 try hello_txt.writeStreamingAll(io, "hello, world!"); 1481 try client_repo.add(io, allocator, &.{"hello.txt"}); 1482 } 1483 1484 // copy files from current repo into client dir 1485 const cwd = std.Io.Dir.cwd(); 1486 for (&[_][]const u8{"src"}) |dir_name| { 1487 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1488 defer src_repo_dir.close(io); 1489 1490 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1491 defer dest_repo_dir.close(io); 1492 1493 try copyDir(io, src_repo_dir, dest_repo_dir); 1494 1495 try client_repo.add(io, allocator, &.{dir_name}); 1496 } 1497 1498 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1499 1500 // change the files so git will send them as delta objects 1501 for (&[_][]const u8{"src"}) |dir_name| { 1502 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1503 defer dest_repo_dir.close(io); 1504 1505 { 1506 var iter = dest_repo_dir.iterate(); 1507 while (try iter.next(io)) |entry| { 1508 switch (entry.kind) { 1509 .file => { 1510 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1511 defer file.close(io); 1512 var writer = file.writer(io, &.{}); 1513 try writer.interface.writeAll("EDIT"); 1514 }, 1515 else => {}, 1516 } 1517 } 1518 } 1519 1520 try client_repo.add(io, allocator, &.{dir_name}); 1521 } 1522 1523 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1524 1525 // add remote 1526 { 1527 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1528 defer allocator.free(remote_url); 1529 1530 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1531 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1532 } 1533 1534 const is_ssh = switch (transport_def) { 1535 .file => false, 1536 .wire => |wire_kind| .ssh == wire_kind, 1537 }; 1538 1539 if (shell_out_to_git) { 1540 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1541 defer allocator.free(priv_key_path); 1542 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}", .{ port + 1, priv_key_path }); 1543 defer allocator.free(ssh_config_arg); 1544 1545 // shell out to git so it will send delta objects 1546 var process = try std.process.spawn(io, .{ 1547 .argv = if (is_ssh) 1548 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1549 else 1550 &.{ "git", "push", "origin", "master" }, 1551 .cwd = .{ .path = client_path }, 1552 .stdin = .ignore, 1553 .stdout = .ignore, 1554 .stderr = .ignore, 1555 }); 1556 const term = try process.wait(io); 1557 if (term != .exited or term.exited != 0) { 1558 return error.GitCommandFailed; 1559 } 1560 } else { 1561 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1562 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1563 1564 try client_repo.push( 1565 io, 1566 allocator, 1567 "origin", 1568 "master", 1569 false, 1570 .{ .wire = .{ .ssh = .{ 1571 .command = ssh_cmd_maybe, 1572 } } }, 1573 ); 1574 } 1575 1576 // make sure push was successful 1577 { 1578 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1579 try std.testing.expectEqualStrings(&commit2, &oid_master); 1580 1581 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1582 } 1583 } 1584 1585 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1586 var iter = src_dir.iterate(); 1587 while (try iter.next(io)) |entry| { 1588 switch (entry.kind) { 1589 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1590 .directory => { 1591 try dest_dir.createDirPath(io, entry.name); 1592 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1593 defer dest_entry_dir.close(io); 1594 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1595 defer src_entry_dir.close(io); 1596 try copyDir(io, src_entry_dir, dest_entry_dir); 1597 }, 1598 else => {}, 1599 } 1600 } 1601 } 1602 1603 // the dev SSH public key matching the private key written by runServer 1604 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1605 1606 // create the admin event repo with a user holding the dev SSH public key 1607 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1608 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1609 defer allocator.free(admin_repo_path); 1610 1611 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1612 defer repo.deinit(io, allocator); 1613 1614 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1615 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1616 1617 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1618 const user_id = evt.EventWithId.randomId(prng.random()); 1619 1620 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1621 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1622 1623 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1624 .id = std.fmt.bytesToHex(user_id, .lower), 1625 .author = .{ .name = "admin", .email = "admin@example.test" }, 1626 .event = .{ .user = .{ 1627 .name = "admin", 1628 .email = "admin@example.test", 1629 .password_hash = password_hash, 1630 .ssh_keys = admin_ssh_key, 1631 } }, 1632 }}); 1633 } 1634 1635 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1636 // the event store 1637 fn repoOnDiskPath( 1638 io: std.Io, 1639 allocator: std.mem.Allocator, 1640 data_path: []const u8, 1641 repo_name: []const u8, 1642 create: bool, 1643 ) !?[]u8 { 1644 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1645 defer allocator.free(admin_repo_path); 1646 1647 const event_id_hex = (try evt.resolveOrCreateRepo( 1648 io, 1649 allocator, 1650 admin_repo_path, 1651 "admin", 1652 repo_name, 1653 if (create) .{ .read_access = .public } else null, 1654 )) orelse return null; 1655 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1656 } 1657 1658 // build the remote URL addressing admin/<repo_name> over the given transport 1659 fn remoteUrl( 1660 comptime transport_def: net.TransportDefinition, 1661 allocator: std.mem.Allocator, 1662 repo_name: []const u8, 1663 port: u16, 1664 ) ![]u8 { 1665 return switch (transport_def) { 1666 .file => unreachable, 1667 .wire => |wire_kind| switch (wire_kind) { 1668 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1669 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1670 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1671 }, 1672 }; 1673 } 1674 1675 // the ssh command the haxy git client should invoke, or null when not using 1676 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1677 // and skip writing to known_hosts. 1678 fn sshCommand( 1679 is_ssh: bool, 1680 allocator: std.mem.Allocator, 1681 temp_path: []const u8, 1682 port: u16, 1683 ) !?[]u8 { 1684 if (!is_ssh) return null; 1685 1686 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1687 defer allocator.free(priv_key_path); 1688 1689 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}\"", .{ port + 1, priv_key_path }); 1690 } 1691 1692 const ServerFiles = union(enum) { 1693 files: []const struct { path: []const u8, content: []const u8 }, 1694 dirs: []const []const u8, 1695 }; 1696 1697 /// commit files without writing the server's index or worktree. 1698 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 { 1699 const R = @TypeOf(repo.*); 1700 const opts = repo.self_repo_opts; 1701 const Index = xit.index.Index(.xit, opts); 1702 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1703 const Ctx = struct { 1704 core: *R.Core, 1705 io: std.Io, 1706 allocator: std.mem.Allocator, 1707 files: ServerFiles, 1708 metadata: xit.object.CommitMetadata(opts.hash), 1709 result: *[xit.hash.hexLen(opts.hash)]u8, 1710 1711 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1712 var reader = std.Io.Reader.fixed(content); 1713 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1714 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1715 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1716 defer ctx.allocator.free(path_parts); 1717 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1718 } 1719 1720 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1721 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1722 defer dir.close(ctx.io); 1723 var entries = dir.iterate(); 1724 while (try entries.next(ctx.io)) |entry| { 1725 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1726 defer ctx.allocator.free(child); 1727 switch (entry.kind) { 1728 .directory => try ctx.addDir(state, index, child), 1729 .file => { 1730 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1731 defer ctx.allocator.free(content); 1732 try ctx.addFile(state, index, child, content); 1733 }, 1734 else => {}, 1735 } 1736 } 1737 } 1738 1739 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1740 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1741 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1742 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1743 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); 1744 defer index.deinit(); 1745 switch (ctx.files) { 1746 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1747 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1748 } 1749 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1750 defer tree.deinit(); 1751 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1752 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1753 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1754 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1755 } 1756 }; 1757 try repo.core.db_file.lock(io, .exclusive); 1758 defer repo.core.db_file.unlock(io); 1759 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1760 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1761 .core = &repo.core, 1762 .io = io, 1763 .allocator = allocator, 1764 .files = files, 1765 .metadata = metadata, 1766 .result = &result, 1767 }); 1768 return result; 1769 } 1770