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