1144 return error.GitCommandFailed;
1145 }
1146 }
1147
1148 // make sure partial clone was successful
1149 {
1150 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{});
1151 hello_txt.close(io);
1152 }
1153
1154 // delete client and clone again with --filter=tree:0
1155 try cwd.deleteTree(io, temp_dir_name ++ "/client");
1156
1157 {
1158 var process = try std.process.spawn(io, .{
1159 .argv = if (is_ssh)
1160 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--filter=tree:0", remote_url, "client" }
1161 else
1162 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" },
1163 .cwd = .{ .path = temp_path },
1164 .stdin = .ignore,
1165 .stdout = .ignore,
1166 .stderr = .ignore,
1167 });
1168 const term = try process.wait(io);
1169 if (term != .exited) {
1170 return error.GitCommandFailed;
1171 }
1172 }
1173
1174 // make sure treeless clone was successful
1175 {
1176 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{});
1177 goodbye_txt.close(io);
1178 }
1179 } else {
1180 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: {
1181 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" });
1182 defer allocator.free(known_hosts_path);
1183
1184 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" });
1185 defer allocator.free(priv_key_path);
1186
1187 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path });
1188 } else null;
1189 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd);
1190
1191 // clone repo
1192 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone(
1193 io,
1194 allocator,
1195 remote_url,
1196 temp_path,
1197 client_path,
1198 .{ .wire = .{ .ssh = .{
1199 .command = ssh_cmd_maybe,
1200 .upload_pack_command = upload_pack_command,
1201 } } },
1202 );
1203 defer client_repo.deinit(io, allocator);
1204
1205 // make sure HEAD points to the right default branch
1206 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE;
1207 const head = try client_repo.head(io, ¤t_branch_buffer);
1208 try std.testing.expectEqualStrings("main", head.ref.name);
1209
1210 // make sure clone was successful
1211 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{});
1212 defer hello_txt.close(io);
1213 }
1214 }
1215
1216 fn testFetchLarge(
1217 comptime repo_kind: rp.RepoKind,
1218 comptime transport_def: net.TransportDefinition,
1219 comptime port: u16,
1220 comptime shell_out_to_git: bool,
1221 io: std.Io,
1222 allocator: std.mem.Allocator,
1223 ) !void {
1224 const temp_dir_name = "temp-testnet-fetch-large";
1225
1226 // create the temp dir
1227 const cwd = std.Io.Dir.cwd();
1228 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{});
1229 if (temp_dir_or_err) |*temp_dir| {
1230 temp_dir.close(io);
1231 try cwd.deleteTree(io, temp_dir_name);
1232 } else |_| {}
1233 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{});
1234 defer cwd.deleteTree(io, temp_dir_name) catch {};
1235 defer temp_dir.close(io);
1236
1237 // init server
1238 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator);
1239 try server.start();
1240 defer server.stop();
1241
1242 const cwd_path = try std.process.currentPathAlloc(io, allocator);
1243 defer allocator.free(cwd_path);
1244
1245 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" });
1246 defer allocator.free(server_path);
1247
1248 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path });
1249 defer server_repo.deinit(io, allocator);
1250
1251 var server_dir = try cwd.openDir(io, server_path, .{});
1252 defer server_dir.close(io);
1253
1254 // copy files from current repo into server dir
1255 for (&[_][]const u8{"src"}) |dir_name| {
1256 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true });
1257 defer src_repo_dir.close(io);
1258
1259 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{});
1260 defer dest_repo_dir.close(io);
1261
1262 try copyDir(io, src_repo_dir, dest_repo_dir);
1263
1264 try server_repo.add(io, allocator, &.{dir_name});
1265 }
1266
1267 // make a commit
1268 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" });
1269
1270 // export server repo
1271 {
1272 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{});
1273 defer export_file.close(io);
1274 }
1275
1276 if (shell_out_to_git) {
1277 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" });
1278 }
1279
1280 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" });
1281 defer allocator.free(client_path);
1282
1283 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path });
1284 defer client_repo.deinit(io, allocator);
1285
1286 // add remote
1287 {
1288 if (.windows == builtin.os.tag) {
1289 std.mem.replaceScalar(u8, server_path, '\\', '/');
1290 }
1291 const separator = if (server_path[0] == '/') "" else "/";
1292
1293 const remote_url = switch (transport_def) {
1294 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }),
1295 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too
1296 .wire => |wire_kind| switch (wire_kind) {
1297 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}),
1298 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}),
1299 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }),
1300 },
1301 };
1302 defer allocator.free(remote_url);
1303
1304 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url });
1305 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" });
1306 }
1307
1308 const is_ssh = switch (transport_def) {
1309 .file => false,
1310 .wire => |wire_kind| .ssh == wire_kind,
1311 };
1312
1313 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port);
1314 defer allocator.free(upload_pack_command);
1315
1316 if (shell_out_to_git) {
1317 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" });
1318 defer allocator.free(priv_key_path);
1319 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path});
1320 defer allocator.free(ssh_config_arg);
1321
1322 {
1323 var process = try std.process.spawn(io, .{
1324 .argv = if (is_ssh)
1325 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "origin", "master" }
1326 else
1327 &.{ "git", "pull", "origin", "master" },
1328 .cwd = .{ .path = client_path },
1329 .stdin = .ignore,
1330 .stdout = .ignore,
1331 .stderr = .ignore,
1332 });
1333 const term = try process.wait(io);
1334 if (term != .exited) {
1335 return error.GitCommandFailed;
1336 }
1337 }
1338
1339 // make sure pull was successful
1340 {
1341 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?;
1342 try std.testing.expectEqualStrings(&commit1, &oid_master);
1343 }
1344
1345 // make another commit on the server
1346 const commit2 = blk: {
1347 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true });
1348 defer extra_txt.close(io);
1349 try extra_txt.writeStreamingAll(io, "extra content");
1350 try server_repo.add(io, allocator, &.{"extra.txt"});
1351 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" });
1352 };
1353
1354 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs)
1355 {
1356 var process = try std.process.spawn(io, .{
1357 .argv = if (is_ssh)
1358 &.{ "git", "-c", ssh_config_arg, "fetch", "--upload-pack", upload_pack_command, "origin", "master" }
1359 else
1360 &.{ "git", "fetch", "origin", "master" },
1361 .cwd = .{ .path = client_path },
1362 .stdin = .ignore,
1363 .stdout = .ignore,
1364 .stderr = .ignore,
1365 });
1366 const term = try process.wait(io);
1367 if (term != .exited) {
1368 return error.GitCommandFailed;
1369 }
1370 }
1371
1372 // make sure fetch with want-ref was successful
1373 {
1374 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?;
1375 try std.testing.expectEqualStrings(&commit2, &oid_remote_master);
1376 }
1377 } else {
1378 const refspecs = &.{
1379 "+refs/heads/master:refs/heads/master",
1380 };
1381
1382 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: {
1383 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" });
1384 defer allocator.free(known_hosts_path);
1385
1386 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" });
1387 defer allocator.free(priv_key_path);
1388
1389 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path });
1390 } else null;
1391 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd);
1392
1393 try client_repo.fetch(
1394 io,
1395 allocator,
1396 "origin",
1397 .{ .refspecs = refspecs, .wire = .{ .ssh = .{
1398 .command = ssh_cmd_maybe,
1399 .upload_pack_command = upload_pack_command,
1400 } } },
1401 );
1402
1403 // update the working dir
1404 try client_repo.restore(io, allocator, ".");
1405 }
1406
1407 // make sure fetch was successful
1408 {
1409 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?;
1410 try std.testing.expectEqualStrings(&commit1, &oid_master);
1411 }
1412 }
1413
1414 fn testPushLarge(
1415 comptime repo_kind: rp.RepoKind,
1416 comptime transport_def: net.TransportDefinition,
1417 comptime port: u16,
1418 comptime shell_out_to_git: bool,
1419 io: std.Io,
1420 allocator: std.mem.Allocator,
1421 ) !void {
1422 const temp_dir_name = "temp-testnet-push-large";
1423
1424 // create the temp dir
1425 const cwd = std.Io.Dir.cwd();
1426 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{});
1427 if (temp_dir_or_err) |*temp_dir| {
1428 temp_dir.close(io);
1429 try cwd.deleteTree(io, temp_dir_name);
1430 } else |_| {}
1431 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{});
1432 defer cwd.deleteTree(io, temp_dir_name) catch {};
1433 defer temp_dir.close(io);
1434
1435 // init server
1436 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator);
1437 try server.start();
1438 defer server.stop();
1439
1440 const cwd_path = try std.process.currentPathAlloc(io, allocator);
1441 defer allocator.free(cwd_path);
1442
1443 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" });
1444 defer allocator.free(server_path);
1445
1446 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path });
1447 defer server_repo.deinit(io, allocator);
1448
1449 // add config
1450 switch (transport_def) {
1451 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }),
1452 .wire => {
1453 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" });
1454 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" });
1455 },
1456 }
1457 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" });
1458
1459 // export server repo
1460 {
1461 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{});
1462 defer export_file.close(io);
1463 }
1464
1465 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" });
1466 defer allocator.free(client_path);
1467
1468 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path });
1469 defer client_repo.deinit(io, allocator);
1470
1471 var client_dir = try cwd.openDir(io, client_path, .{});
1472 defer client_dir.close(io);
1473
1474 {
1475 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true });
1476 defer hello_txt.close(io);
1477 try hello_txt.writeStreamingAll(io, "hello, world!");
1478 try client_repo.add(io, allocator, &.{"hello.txt"});
1479 }
1480
1481 // copy files from current repo into client dir
1482 for (&[_][]const u8{"src"}) |dir_name| {
1483 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true });
1484 defer src_repo_dir.close(io);
1485
1486 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{});
1487 defer dest_repo_dir.close(io);
1488
1489 try copyDir(io, src_repo_dir, dest_repo_dir);
1490
1491 try client_repo.add(io, allocator, &.{dir_name});
1492 }
1493
1494 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" });
1495
1496 // change the files so git will send them as delta objects
1497 for (&[_][]const u8{"src"}) |dir_name| {
1498 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } });
1499 defer dest_repo_dir.close(io);
1500
1501 {
1502 var iter = dest_repo_dir.iterate();
1503 while (try iter.next(io)) |entry| {
1504 switch (entry.kind) {
1505 .file => {
1506 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write });
1507 defer file.close(io);
1508 var writer = file.writer(io, &.{});
1509 try writer.interface.writeAll("EDIT");
1510 },
1511 else => {},
1512 }
1513 }
1514 }
1515
1516 try client_repo.add(io, allocator, &.{dir_name});
1517 }
1518
1519 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" });
1520
1521 // add remote
1522 {
1523 if (.windows == builtin.os.tag) {
1524 std.mem.replaceScalar(u8, server_path, '\\', '/');
1525 }
1526 const separator = if (server_path[0] == '/') "" else "/";
1527
1528 const remote_url = switch (transport_def) {
1529 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }),
1530 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too
1531 .wire => |wire_kind| switch (wire_kind) {
1532 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}),
1533 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}),
1534 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }),
1535 },
1536 };
1537 defer allocator.free(remote_url);
1538
1539 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url });
1540 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" });
1541 }
1542
1543 const is_ssh = switch (transport_def) {
1544 .file => false,
1545 .wire => |wire_kind| .ssh == wire_kind,
1546 };
1547
1548 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port);
1549 defer allocator.free(receive_pack_command);
1550
1551 if (shell_out_to_git) {
1552 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" });
1553 defer allocator.free(priv_key_path);
1554 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path});
1555 defer allocator.free(ssh_config_arg);
1556
1557 // shell out to git so it will send delta objects
1558 var process = try std.process.spawn(io, .{
1559 .argv = if (is_ssh)
1560 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" }
1561 else
1562 &.{ "git", "push", "origin", "master" },
1563 .cwd = .{ .path = client_path },
1564 .stdin = .ignore,
1565 .stdout = .ignore,
1566 .stderr = .ignore,
1567 });
1568 const term = try process.wait(io);
1569 if (term != .exited) {
1570 return error.GitCommandFailed;
1571 }
1572 } else {
1573 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: {
1574 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" });
1575 defer allocator.free(known_hosts_path);
1576
1577 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" });
1578 defer allocator.free(priv_key_path);
1579
1580 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path });
1581 } else null;
1582 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd);
1583
1584 try client_repo.push(
1585 io,
1586 allocator,
1587 "origin",
1588 "master",
1589 false,
1590 .{ .wire = .{ .ssh = .{
1591 .command = ssh_cmd_maybe,
1592 .receive_pack_command = receive_pack_command,
1593 } } },
1594 );
1595
1596 if (transport_def == .file) {
1597 // update the working dir
1598 try server_repo.restore(io, allocator, ".");
1599 }
1600 }
1601
1602 // make sure push was successful
1603 {
1604 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?;
1605 try std.testing.expectEqualStrings(&commit2, &oid_master);
1606
1607 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{});
1608 defer hello_txt.close(io);
1609 }
1610 }
1611
1612 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void {
1613 var iter = src_dir.iterate();
1614 while (try iter.next(io)) |entry| {
1615 switch (entry.kind) {
1616 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}),
1617 .directory => {
1618 try dest_dir.createDirPath(io, entry.name);
1619 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false });
1620 defer dest_entry_dir.close(io);
1621 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false });
1622 defer src_entry_dir.close(io);
1623 try copyDir(io, src_entry_dir, dest_entry_dir);
1624 },
1625 else => {},
1626 }
1627 }
1628 }
1629