╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
457 "master", 458 false, 459 .{ .wire = .{ .ssh = .{ 460 .command = ssh_cmd_maybe, 461 } } }, 462 )); 463 464 // retrieve the commit object 465 try client_repo.fetch( 466 io, 467 allocator, 468 "origin", 469 .{ .wire = .{ .ssh = .{ 470 .command = ssh_cmd_maybe, 471 } } }, 472 ); 473 474 // can't push because server's history is incompatible 475 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 476 io, 477 allocator, 478 "origin", 479 "master", 480 false, 481 .{ .wire = .{ .ssh = .{ 482 .command = ssh_cmd_maybe, 483 } } }, 484 )); 485 486 // test denyNonFastForwards (only for wire transports, file transport bypasses receive-pack) 487 switch (transport_def) { 488 .file => {}, 489 .wire => { 490 // set denyNonFastForwards on server 491 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 492 493 // save the server's current master ref 494 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 495 496 // force push should be rejected by server due to denyNonFastForwards 497 try client_repo.push( 498 io, 499 allocator, 500 "origin", 501 "master", 502 true, 503 .{ .wire = .{ .ssh = .{ 504 .command = ssh_cmd_maybe, 505 } } }, 506 ); 507 508 // verify the server ref was not updated (push was denied) 509 { 510 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 511 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 512 } 513 514 // remove denyNonFastForwards from server 515 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 516 }, 517 } 518 519 // force push 520 try client_repo.push( 521 io, 522 allocator, 523 "origin", 524 "master", 525 true, 526 .{ .wire = .{ .ssh = .{ 527 .command = ssh_cmd_maybe, 528 } } }, 529 ); 530 531 // make sure push was successful 532 { 533 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 534 try std.testing.expectEqualStrings(&commit2, &oid_master); 535 } 536 537 // remove the remote tag 538 try client_repo.push( 539 io, 540 allocator, 541 "origin", 542 ":refs/tags/1.0.0", 543 false, 544 .{ .wire = .{ .ssh = .{ 545 .command = ssh_cmd_maybe, 546 } } }, 547 ); 548 549 // make sure push was successful 550 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 551 } 552 553 fn testPushCreatesMissingRepo( 554 comptime repo_kind: rp.RepoKind, 555 comptime transport_def: net.TransportDefinition, 556 io: std.Io, 557 allocator: std.mem.Allocator, 558 ) !void { 559 const temp_dir_name = "temp-testnet-push-create"; 560 561 const cwd = std.Io.Dir.cwd(); 562 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 563 if (temp_dir_or_err) |*temp_dir| { 564 temp_dir.close(io); 565 try cwd.deleteTree(io, temp_dir_name); 566 } else |_| {} 567 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 568 defer cwd.deleteTree(io, temp_dir_name) catch {}; 569 defer temp_dir.close(io); 570 571 var server_process = try runServer(io, allocator, temp_dir_name); 572 defer _ = server_process.kill(io); 573 574 const cwd_path = try std.process.currentPathAlloc(io, allocator); 575 defer allocator.free(cwd_path); 576 577 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 578 defer allocator.free(server_path); 579 580 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 581 defer allocator.free(client_path); 582 583 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 584 defer client_repo.deinit(io, allocator); 585 586 const commit1 = blk: { 587 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 588 defer hello_txt.close(io); 589 try hello_txt.writeStreamingAll(io, "hello, world!"); 590 try client_repo.add(io, allocator, &.{"hello.txt"}); 591 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 592 }; 593 594 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 595 596 { 597 if (.windows == builtin.os.tag) { 598 std.mem.replaceScalar(u8, server_path, '\\', '/'); 599 } 600 const separator = if (server_path[0] == '/') "" else "/"; 601 602 const remote_url = switch (transport_def) { 603 .file => unreachable, 604 .wire => |wire_kind| switch (wire_kind) { 605 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{http_port}), 606 .raw => unreachable, 607 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ ssh_port, separator, server_path }), 608 }, 609 }; 610 defer allocator.free(remote_url); 611 612 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 613 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 614 } 615 616 const is_ssh = switch (transport_def) { 617 .file => false, 618 .wire => |wire_kind| .ssh == wire_kind, 619 }; 620 // haxy generates a fresh host key on every run, so don't try to record 621 // it. accept the unknown host and skip writing to known_hosts. the key 622 // identity flag picks the ed25519 key created in runServer. 623 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 624 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 625 defer allocator.free(priv_key_path); 626 627 break :blk try std.fmt.allocPrint(allocator, "ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{priv_key_path}); 628 } else null; 629 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 630 631 try client_repo.push( 632 io, 633 allocator, 634 "origin", 635 "master", 636 false, 637 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 638 .command = ssh_cmd_maybe, 639 } } }, 640 ); 641 642 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 643 defer server_repo.deinit(io, allocator); 644 645 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 646 647 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 648 try std.testing.expectEqualStrings(&commit1, &oid_master); 649 } 650 651 fn testClone( 652 comptime repo_kind: rp.RepoKind, 653 comptime transport_def: net.TransportDefinition, 654 comptime shell_out_to_git: bool, 655 io: std.Io, 656 allocator: std.mem.Allocator, 657 ) !void { 658 const temp_dir_name = "temp-testnet-clone"; 659 660 // create the temp dir 661 const cwd = std.Io.Dir.cwd(); 662 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 663 if (temp_dir_or_err) |*temp_dir| { 664 temp_dir.close(io); 665 try cwd.deleteTree(io, temp_dir_name); 666 } else |_| {} 667 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 668 defer cwd.deleteTree(io, temp_dir_name) catch {}; 669 defer temp_dir.close(io); 670 671 // init server 672 var server_process = try runServer(io, allocator, temp_dir_name); 673 defer _ = server_process.kill(io); 674 675 const cwd_path = try std.process.currentPathAlloc(io, allocator); 676 defer allocator.free(cwd_path); 677 678 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 679 defer allocator.free(temp_path); 680 681 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 682 defer allocator.free(server_path); 683 684 // init server repo with default branch name as main 685 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 686 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 687 defer server_repo.deinit(io, allocator); 688 689 if (shell_out_to_git) { 690 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 691 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 692 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 693 } 694 695 // make a commit 696 { 697 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 698 defer hello_txt.close(io); 699 try hello_txt.writeStreamingAll(io, "hello, world!"); 700 try server_repo.add(io, allocator, &.{"hello.txt"}); 701 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 702 } 703 704 // tag first commit 705 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 706 707 // make a commit 708 { 709 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 710 defer goodbye_txt.close(io); 711 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 712 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 713 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 714 } 715 716 // export server repo 717 { 718 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 719 defer export_file.close(io); 720 } 721 722 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 723 defer allocator.free(client_path); 724 725 // get remote url 726 const remote_url = blk: { 727 if (.windows == builtin.os.tag) { 728 std.mem.replaceScalar(u8, server_path, '\\', '/'); 729 } 730 const separator = if (server_path[0] == '/') "" else "/"; 731 732 break :blk switch (transport_def) { 733 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 734 .file => try std.fmt.allocPrint(allocator, "server", .{}), // relative file paths work too 735 .wire => |wire_kind| switch (wire_kind) { 736 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{http_port}), 737 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{http_port}), 738 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ ssh_port, separator, server_path }), 739 }, 740 }; 741 }; 742 defer allocator.free(remote_url); 743 744 const is_ssh = switch (transport_def) { 745 .file => false, 746 .wire => |wire_kind| .ssh == wire_kind, 747 }; 748 749 if (shell_out_to_git) { 750 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 751 defer allocator.free(priv_key_path); 752 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}); 753 defer allocator.free(ssh_config_arg); 754 755 { 756 var process = try std.process.spawn(io, .{ 757 .argv = if (is_ssh) 758 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 759 else 760 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 761 .cwd = .{ .path = temp_path }, 762 .stdin = .ignore, 763 .stdout = .ignore, 764 .stderr = .ignore, 765 }); 766 const term = try process.wait(io); 767 if (term != .exited or term.exited != 0) { 768 return error.GitCommandFailed; 769 } 770 } 771 772 // make sure shallow clone was successful 773 { 774 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 775 hello_txt.close(io); 776 } 777 778 // make a third commit on the server 779 { 780 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 781 defer extra_txt.close(io); 782 try extra_txt.writeStreamingAll(io, "extra content"); 783 try server_repo.add(io, allocator, &.{"extra.txt"}); 784 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 785 } 786 787 // pull --unshallow to deepen the clone and get the new commit 788 { 789 var process = try std.process.spawn(io, .{ 790 .argv = if (is_ssh) 791 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 792 else 793 &.{ "git", "pull", "--unshallow" }, 794 .cwd = .{ .path = client_path }, 795 .stdin = .ignore, 796 .stdout = .ignore, 797 .stderr = .ignore, 798 }); 799 const term = try process.wait(io); 800 if (term != .exited or term.exited != 0) { 801 return error.GitCommandFailed; 802 } 803 } 804 805 // make sure unshallow pull was successful 806 { 807 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 808 extra_txt.close(io); 809 } 810 811 // delete client and clone again with --shallow-since 812 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 813 814 { 815 var process = try std.process.spawn(io, .{ 816 .argv = if (is_ssh) 817 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 818 else 819 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 820 .cwd = .{ .path = temp_path }, 821 .stdin = .ignore, 822 .stdout = .ignore, 823 .stderr = .ignore, 824 }); 825 const term = try process.wait(io); 826 if (term != .exited or term.exited != 0) { 827 return error.GitCommandFailed; 828 } 829 } 830 831 // make sure shallow clone was successful 832 { 833 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 834 hello_txt.close(io); 835 } 836 837 // delete client and clone again with --shallow-exclude 838 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 839 840 { 841 var process = try std.process.spawn(io, .{ 842 .argv = if (is_ssh) 843 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 844 else 845 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 846 .cwd = .{ .path = temp_path }, 847 .stdin = .ignore, 848 .stdout = .ignore, 849 .stderr = .ignore, 850 }); 851 const term = try process.wait(io); 852 if (term != .exited or term.exited != 0) { 853 return error.GitCommandFailed; 854 } 855 } 856 857 // make sure shallow clone was successful 858 { 859 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 860 hello_txt.close(io); 861 } 862 863 // delete client and clone again with --filter=blob:none 864 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 865 866 { 867 var process = try std.process.spawn(io, .{ 868 .argv = if (is_ssh) 869 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 870 else 871 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 872 .cwd = .{ .path = temp_path }, 873 .stdin = .ignore, 874 .stdout = .ignore, 875 .stderr = .ignore, 876 }); 877 const term = try process.wait(io); 878 if (term != .exited or term.exited != 0) { 879 return error.GitCommandFailed; 880 } 881 } 882 883 // make sure partial clone was successful 884 { 885 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 886 hello_txt.close(io); 887 } 888 889 // delete client and clone again with --filter=tree:0 890 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 891 892 { 893 var process = try std.process.spawn(io, .{ 894 .argv = if (is_ssh) 895 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 896 else 897 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 898 .cwd = .{ .path = temp_path }, 899 .stdin = .ignore, 900 .stdout = .ignore, 901 .stderr = .ignore, 902 }); 903 const term = try process.wait(io); 904 if (term != .exited or term.exited != 0) { 905 return error.GitCommandFailed; 906 } 907 } 908 909 // make sure treeless clone was successful 910 { 911 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 912 goodbye_txt.close(io); 913 } 914 } else { 915 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 916 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 917 defer allocator.free(priv_key_path); 918 919 break :blk try std.fmt.allocPrint(allocator, "ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{priv_key_path}); 920 } else null; 921 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 922 923 // clone repo 924 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 925 io, 926 allocator, 927 remote_url, 928 temp_path, 929 client_path, 930 .{ .wire = .{ .ssh = .{ 931 .command = ssh_cmd_maybe, 932 } } }, 933 ); 934 defer client_repo.deinit(io, allocator); 935 936 // make sure HEAD points to the right default branch 937 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 938 const head = try client_repo.head(io, &current_branch_buffer); 939 try std.testing.expectEqualStrings("main", head.ref.name); 940 941 // make sure clone was successful 942 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 943 defer hello_txt.close(io); 944 } 945 } 946 947 fn testFetchLarge( 948 comptime repo_kind: rp.RepoKind, 949 comptime transport_def: net.TransportDefinition, 950 comptime shell_out_to_git: bool, 951 io: std.Io, 952 allocator: std.mem.Allocator, 953 ) !void { 954 const temp_dir_name = "temp-testnet-fetch-large"; 955 956 // create the temp dir 957 const cwd = std.Io.Dir.cwd(); 958 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 959 if (temp_dir_or_err) |*temp_dir| { 960 temp_dir.close(io); 961 try cwd.deleteTree(io, temp_dir_name); 962 } else |_| {} 963 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 964 defer cwd.deleteTree(io, temp_dir_name) catch {}; 965 defer temp_dir.close(io); 966 967 // init server 968 var server_process = try runServer(io, allocator, temp_dir_name); 969 defer _ = server_process.kill(io); 970 971 const cwd_path = try std.process.currentPathAlloc(io, allocator); 972 defer allocator.free(cwd_path); 973 974 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 975 defer allocator.free(server_path); 976 977 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 978 defer server_repo.deinit(io, allocator); 979 980 var server_dir = try cwd.openDir(io, server_path, .{}); 981 defer server_dir.close(io); 982 983 // copy files from current repo into server dir 984 for (&[_][]const u8{"src"}) |dir_name| { 985 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 986 defer src_repo_dir.close(io); 987 988 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 989 defer dest_repo_dir.close(io); 990 991 try copyDir(io, src_repo_dir, dest_repo_dir); 992 993 try server_repo.add(io, allocator, &.{dir_name}); 994 } 995 996 // make a commit 997 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 998 999 // export server repo 1000 { 1001 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1002 defer export_file.close(io); 1003 } 1004 1005 if (shell_out_to_git) { 1006 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1007 } 1008 1009 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1010 defer allocator.free(client_path); 1011 1012 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1013 defer client_repo.deinit(io, allocator); 1014 1015 // add remote 1016 { 1017 if (.windows == builtin.os.tag) { 1018 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1019 } 1020 const separator = if (server_path[0] == '/') "" else "/"; 1021 1022 const remote_url = switch (transport_def) { 1023 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1024 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1025 .wire => |wire_kind| switch (wire_kind) { 1026 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{http_port}), 1027 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{http_port}), 1028 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ ssh_port, separator, server_path }), 1029 }, 1030 }; 1031 defer allocator.free(remote_url); 1032 1033 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1034 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1035 } 1036 1037 const is_ssh = switch (transport_def) { 1038 .file => false, 1039 .wire => |wire_kind| .ssh == wire_kind, 1040 }; 1041 1042 if (shell_out_to_git) { 1043 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1044 defer allocator.free(priv_key_path); 1045 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}); 1046 defer allocator.free(ssh_config_arg); 1047 1048 { 1049 var process = try std.process.spawn(io, .{ 1050 .argv = if (is_ssh) 1051 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1052 else 1053 &.{ "git", "pull", "origin", "master" }, 1054 .cwd = .{ .path = client_path }, 1055 .stdin = .ignore, 1056 .stdout = .ignore, 1057 .stderr = .ignore, 1058 }); 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