╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
716 const moment = try evt.currentMoment(repo_opts, &draft); 717 const newest = (try evt.PatchRev.readNewest(evt.EventDB(hash_kind), hash_kind, moment, &arena)) orelse return error.NotFound; 718 try std.testing.expectEqualSlices(u8, &second_revision_id, &newest.id); 719 } 720 721 // 722 // a removed target patch stays removed after another push 723 // 724 725 { 726 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 727 defer target.deinit(io, allocator); 728 try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, repo_opts, io, allocator, &target, &fork_id, .patch, .{ .name = "admin", .email = "admin@example.test" }); 729 } 730 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 731 { 732 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 733 defer target.deinit(io, allocator); 734 var arena = std.heap.ArenaAllocator.init(allocator); 735 defer arena.deinit(); 736 const moment = try evt.currentMoment(repo_opts, &target); 737 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 738 try std.testing.expect(patch.removed); 739 } 740 741 // 742 // restore the patch, merge a descendant of its source oid, and push the merge 743 // 744 745 { 746 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 747 defer target.deinit(io, allocator); 748 var arena = std.heap.ArenaAllocator.init(allocator); 749 defer arena.deinit(); 750 const moment = try evt.currentMoment(repo_opts, &target); 751 const removed = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 752 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 753 .id = fork_id_hex, 754 .timestamp = 4, 755 .author = .{ .name = "admin", .email = "admin@example.test" }, 756 .event = .{ .patch = removed.event }, 757 }}); 758 } 759 const merge_parents = [_][hash.hexLen(hash_kind)]u8{ base_oid, third_source_oid }; 760 const merge_oid = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 761 // later commits in the push must not be mistaken for the merge itself 762 _ = try client.commit(io, allocator, .{ .message = "after merge", .allow_empty = true }); 763 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 764 { 765 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 766 defer target.deinit(io, allocator); 767 var arena = std.heap.ArenaAllocator.init(allocator); 768 defer arena.deinit(); 769 const moment = try evt.currentMoment(repo_opts, &target); 770 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 771 try std.testing.expectEqual(.merged, patch.event.status.kind()); 772 const merged_status = switch (patch.event.status) { 773 .merged => |value| value, 774 else => return error.InvalidPatch, 775 }; 776 try std.testing.expectEqual(.source, merged_status.revision); 777 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 778 const result = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &result_id)) orelse return error.NotFound; 779 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 780 try std.testing.expectEqualStrings(&merge_oid, result.event.source_oid); 781 const imported = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &second_revision_id)) orelse return error.NotFound; 782 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 783 } 784 785 const other_url = try std.fmt.allocPrint(allocator, "git@localhost:admin:other+{s}", .{&fork_id_hex}); 786 defer allocator.free(other_url); 787 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 788 // early rejections need no input. give ssh eof so windows openssh 789 // doesn't hang closing stdin before forwarding the error. 790 const reject_ssh_cmd = try std.fmt.allocPrint(allocator, "{s} -n", .{ssh_cmd}); 791 defer allocator.free(reject_ssh_cmd); 792 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 793 794 // 795 // the fork remains cloneable after the target repo is removed 796 // 797 798 { 799 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 800 defer user_repo.deinit(io, allocator); 801 try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 802 } 803 const clone_path = try std.fs.path.join(allocator, &.{ temp_path, "fork-clone" }); 804 defer allocator.free(clone_path); 805 var fork_clone = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 806 io, 807 allocator, 808 remote_url, 809 clone_path, 810 clone_path, 811 null, 812 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 813 ); 814 defer fork_clone.deinit(io, allocator); 815 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 816 817 { 818 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 819 defer user_repo.deinit(io, allocator); 820 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 821 .{ 822 .id = std.fmt.bytesToHex(repo_id, .lower), 823 .timestamp = 2, 824 .author = .{ .name = "admin", .email = "admin@example.test" }, 825 .event = .{ .repo = .{ 826 .user_id = &user_id, 827 .name = "target", 828 .description = "", 829 } }, 830 }, 831 }); 832 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 833 defer admin.deinit(io, allocator); 834 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 835 .{ 836 .id = std.fmt.bytesToHex(user_id, .lower), 837 .timestamp = 2, 838 .author = .{ .name = "admin", .email = "admin@example.test" }, 839 .event = .{ .user = .{ 840 .name = "admin", 841 .email = "admin@example.test", 842 .password_hash = "", 843 .ssh_keys = "", 844 } }, 845 }, 846 }); 847 } 848 849 const denied_url = try std.fmt.allocPrint(allocator, "git@localhost:admin:target+{s}", .{&fork_id_hex}); 850 defer allocator.free(denied_url); 851 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 852 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 853 } 854 855 fn testPushCreatesMissingRepo( 856 comptime repo_kind: rp.RepoKind, 857 comptime transport_def: net.TransportDefinition, 858 comptime hash_kind: hash.HashKind, 859 comptime port: u16, 860 io: std.Io, 861 allocator: std.mem.Allocator, 862 ) !void { 863 var temp = std.testing.tmpDir(.{}); 864 defer temp.cleanup(); 865 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 866 defer allocator.free(temp_path); 867 868 var server_process = try runServer(io, allocator, temp_path, port); 869 defer _ = server_process.kill(io); 870 871 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 872 defer allocator.free(client_path); 873 874 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true, .hash = hash_kind }).init(io, allocator, .{ .path = client_path }); 875 defer client_repo.deinit(io, allocator); 876 877 const commit1 = blk: { 878 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 879 defer hello_txt.close(io); 880 try hello_txt.writeStreamingAll(io, "hello, world!"); 881 try client_repo.add(io, allocator, &.{"hello.txt"}); 882 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 883 }; 884 885 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 886 887 // the repo is not pre-registered; the push should mint the repo event under 888 // admin and create the repo on disk 889 { 890 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 891 defer allocator.free(remote_url); 892 893 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 894 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 895 } 896 897 const is_ssh = switch (transport_def) { 898 .file => false, 899 .wire => |wire_kind| .ssh == wire_kind, 900 }; 901 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 902 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 903 904 try client_repo.push( 905 io, 906 allocator, 907 "origin", 908 "master", 909 false, 910 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 911 .command = ssh_cmd_maybe, 912 } } }, 913 ); 914 915 // the push registered admin:testrepo in the client's hash format; resolve its on-disk directory 916 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", false)).?; 917 defer allocator.free(server_path); 918 919 var server_repo = try rp.Repo(.xit, .{ .is_test = true, .hash = hash_kind }).open(io, allocator, .{ .path = server_path }); 920 defer server_repo.deinit(io, allocator); 921 try std.testing.expect(try server_repo.isBare(io, allocator)); 922 923 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 924 925 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 926 try std.testing.expectEqualStrings(&commit1, &oid_master); 927 } 928 929 fn testPushEvents( 930 comptime repo_kind: rp.RepoKind, 931 comptime transport_def: net.TransportDefinition, 932 comptime hash_kind: hash.HashKind, 933 comptime port: u16, 934 io: std.Io, 935 allocator: std.mem.Allocator, 936 ) !void { 937 // create the temp dir 938 var temp = std.testing.tmpDir(.{}); 939 defer temp.cleanup(); 940 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 941 defer allocator.free(temp_path); 942 943 // init server 944 var server_process = try runServer(io, allocator, temp_path, port); 945 defer _ = server_process.kill(io); 946 947 // register the repo under admin and locate its on-disk directory 948 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 949 defer allocator.free(server_path); 950 951 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 952 const ServerRepo = rp.Repo(.xit, repo_opts); 953 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 954 defer server_repo.deinit(io, allocator); 955 956 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 957 defer allocator.free(client_path); 958 959 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 960 defer client_repo.deinit(io, allocator); 961 962 // add remote 963 { 964 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 965 defer allocator.free(remote_url); 966 967 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 968 } 969 970 const is_ssh = switch (transport_def) { 971 .file => false, 972 .wire => |wire_kind| .ssh == wire_kind, 973 }; 974 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 975 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 976 977 var arena = std.heap.ArenaAllocator.init(allocator); 978 defer arena.deinit(); 979 980 // 981 // define test events 982 // 983 984 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 985 const issue_event_id = evt.EventWithId.randomId(prng.random()); 986 987 const events_to_push = [_]evt.EventWithId{ 988 .{ 989 .id = std.fmt.bytesToHex(issue_event_id, .lower), 990 .author = .{ .name = "haxy", .email = "user@haxy" }, 991 .event = .{ 992 .issue = .{ 993 .title = "Login form clears password on validation error", 994 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 995 .labels = "bug priority-high ui", 996 }, 997 }, 998 }, 999 }; 1000 1001 // commit the issue on the client and push it to the server 1002 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 1003 try client_repo.push( 1004 io, 1005 allocator, 1006 "origin", 1007 "haxy/events", 1008 false, 1009 .{ .wire = .{ .ssh = .{ 1010 .command = ssh_cmd_maybe, 1011 } } }, 1012 ); 1013 1014 // the push consumed the issue into the server repo's db 1015 { 1016 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1017 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1018 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1019 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1020 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1021 1022 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1023 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.labels, issue.event.labels); 1024 } 1025 1026 // 1027 // edit the issue 1028 // 1029 1030 // this event edits the previous one because it has the same id 1031 const events_to_push2 = [_]evt.EventWithId{ 1032 .{ 1033 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1034 .author = .{ .name = "haxy", .email = "user@haxy" }, 1035 .event = .{ 1036 .issue = .{ 1037 .title = "Login form clears password on validation error", 1038 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1039 .labels = "bug priority-low ui", 1040 }, 1041 }, 1042 }, 1043 }; 1044 1045 // commit the edit on the client and push it to the server 1046 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1047 try client_repo.push( 1048 io, 1049 allocator, 1050 "origin", 1051 "haxy/events", 1052 false, 1053 .{ .wire = .{ .ssh = .{ 1054 .command = ssh_cmd_maybe, 1055 } } }, 1056 ); 1057 1058 // the push consumed the edit into the server repo's db 1059 { 1060 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1061 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1062 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1063 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1064 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1065 1066 // the description and labels were correctly edited 1067 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1068 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.labels, issue.event.labels); 1069 } 1070 } 1071 1072 fn testClone( 1073 comptime repo_kind: rp.RepoKind, 1074 comptime transport_def: net.TransportDefinition, 1075 comptime shell_out_to_git: bool, 1076 comptime hash_kind: hash.HashKind, 1077 comptime port: u16, 1078 io: std.Io, 1079 allocator: std.mem.Allocator, 1080 ) !void { 1081 // create the temp dir 1082 var temp = std.testing.tmpDir(.{}); 1083 defer temp.cleanup(); 1084 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1085 defer allocator.free(temp_path); 1086 1087 // init server 1088 var server_process = try runServer(io, allocator, temp_path, port); 1089 defer _ = server_process.kill(io); 1090 1091 // register the repo under admin and locate its on-disk directory 1092 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1093 defer allocator.free(server_path); 1094 1095 // init server repo with default branch name as main 1096 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1097 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = !shell_out_to_git }).init(io, allocator, .{ .bare = true, .path = server_path, .create_default_branch = "main" }); 1098 defer server_repo.deinit(io, allocator); 1099 1100 if (shell_out_to_git) { 1101 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1102 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1103 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1104 } 1105 1106 // make a commit 1107 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1108 1109 // tag first commit 1110 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1111 1112 // make a commit 1113 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1114 1115 // export server repo 1116 { 1117 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1118 defer export_file.close(io); 1119 } 1120 1121 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1122 defer allocator.free(client_path); 1123 1124 // get remote url 1125 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1126 defer allocator.free(remote_url); 1127 1128 const is_ssh = switch (transport_def) { 1129 .file => false, 1130 .wire => |wire_kind| .ssh == wire_kind, 1131 }; 1132 1133 if (shell_out_to_git) { 1134 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1135 defer allocator.free(ssh_cmd); 1136 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1137 defer allocator.free(ssh_config_arg); 1138 1139 { 1140 var process = try std.process.spawn(io, .{ 1141 .argv = if (is_ssh) 1142 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--depth", "1", remote_url, "client" } 1143 else 1144 &.{ "git", "clone", "--quiet", "--depth", "1", remote_url, "client" }, 1145 .cwd = .{ .path = temp_path }, 1146 .stdin = .ignore, 1147 .stdout = .ignore, 1148 .stderr = .inherit, 1149 }); 1150 defer process.kill(io); 1151 const term = try process.wait(io); 1152 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1153 } 1154 1155 // make sure shallow clone was successful 1156 { 1157 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1158 hello_txt.close(io); 1159 } 1160 1161 // make a third commit on the server 1162 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1163 1164 // pull --unshallow to deepen the clone and get the new commit 1165 { 1166 var process = try std.process.spawn(io, .{ 1167 .argv = if (is_ssh) 1168 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "--unshallow" } 1169 else 1170 &.{ "git", "pull", "--quiet", "--unshallow" }, 1171 .cwd = .{ .path = client_path }, 1172 .stdin = .ignore, 1173 .stdout = .ignore, 1174 .stderr = .inherit, 1175 }); 1176 defer process.kill(io); 1177 const term = try process.wait(io); 1178 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1179 } 1180 1181 // make sure unshallow pull was successful 1182 { 1183 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1184 extra_txt.close(io); 1185 } 1186 1187 // delete client and clone again with --shallow-since 1188 try temp.dir.deleteTree(io, "client"); 1189 1190 { 1191 var process = try std.process.spawn(io, .{ 1192 .argv = if (is_ssh) 1193 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" } 1194 else 1195 &.{ "git", "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" }, 1196 .cwd = .{ .path = temp_path }, 1197 .stdin = .ignore, 1198 .stdout = .ignore, 1199 .stderr = .inherit, 1200 }); 1201 defer process.kill(io); 1202 const term = try process.wait(io); 1203 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1204 } 1205 1206 // make sure shallow clone was successful 1207 { 1208 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1209 hello_txt.close(io); 1210 } 1211 1212 // delete client and clone again with --shallow-exclude 1213 try temp.dir.deleteTree(io, "client"); 1214 1215 { 1216 var process = try std.process.spawn(io, .{ 1217 .argv = if (is_ssh) 1218 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1219 else 1220 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1221 .cwd = .{ .path = temp_path }, 1222 .stdin = .ignore, 1223 .stdout = .ignore, 1224 .stderr = .inherit, 1225 }); 1226 defer process.kill(io); 1227 const term = try process.wait(io); 1228 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1229 } 1230 1231 // make sure shallow clone was successful 1232 { 1233 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1234 hello_txt.close(io); 1235 } 1236 1237 // delete client and clone again with --filter=blob:none 1238 try temp.dir.deleteTree(io, "client"); 1239 1240 { 1241 var process = try std.process.spawn(io, .{ 1242 .argv = if (is_ssh) 1243 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1244 else 1245 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1246 .cwd = .{ .path = temp_path }, 1247 .stdin = .ignore, 1248 .stdout = .ignore, 1249 .stderr = .inherit, 1250 }); 1251 defer process.kill(io); 1252 const term = try process.wait(io); 1253 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1254 } 1255 1256 // make sure partial clone was successful 1257 { 1258 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1259 hello_txt.close(io); 1260 } 1261 1262 // delete client and clone again with --filter=tree:0 1263 try temp.dir.deleteTree(io, "client"); 1264 1265 { 1266 var process = try std.process.spawn(io, .{ 1267 .argv = if (is_ssh) 1268 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1269 else 1270 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1271 .cwd = .{ .path = temp_path }, 1272 .stdin = .ignore, 1273 .stdout = .ignore, 1274 .stderr = .inherit, 1275 }); 1276 defer process.kill(io); 1277 const term = try process.wait(io); 1278 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1279 } 1280 1281 // make sure treeless clone was successful 1282 { 1283 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1284 goodbye_txt.close(io); 1285 } 1286 } else { 1287 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1288 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1289 1290 // clone repo 1291 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1292 io, 1293 allocator, 1294 remote_url, 1295 temp_path, 1296 client_path, 1297 null, 1298 .{ .transport = .{ .wire = .{ .ssh = .{ 1299 .command = ssh_cmd_maybe, 1300 } } } }, 1301 ); 1302 defer clone_result.deinit(io, allocator); 1303 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1304 1305 // make sure HEAD points to the right default branch 1306 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1307 const head = try client_repo.head(io, &current_branch_buffer); 1308 try std.testing.expectEqualStrings("main", head.ref.name); 1309 1310 // make sure clone was successful 1311 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1312 defer hello_txt.close(io); 1313 } 1314 } 1315 1316 fn testFetchLarge( 1317 comptime repo_kind: rp.RepoKind, 1318 comptime transport_def: net.TransportDefinition, 1319 comptime shell_out_to_git: bool, 1320 comptime hash_kind: hash.HashKind, 1321 comptime port: u16, 1322 io: std.Io, 1323 allocator: std.mem.Allocator, 1324 ) !void { 1325 // create the temp dir 1326 var temp = std.testing.tmpDir(.{}); 1327 defer temp.cleanup(); 1328 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1329 defer allocator.free(temp_path); 1330 1331 // init server 1332 var server_process = try runServer(io, allocator, temp_path, port); 1333 defer _ = server_process.kill(io); 1334 1335 // register the repo under admin and locate its on-disk directory 1336 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1337 defer allocator.free(server_path); 1338 1339 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1340 defer server_repo.deinit(io, allocator); 1341 1342 // make a commit with files from the current repo 1343 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1344 1345 // export server repo 1346 { 1347 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1348 defer export_file.close(io); 1349 } 1350 1351 if (shell_out_to_git) { 1352 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1353 } 1354 1355 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1356 defer allocator.free(client_path); 1357 1358 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1359 defer client_repo.deinit(io, allocator); 1360 1361 // add remote 1362 { 1363 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1364 defer allocator.free(remote_url); 1365 1366 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1367 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1368 } 1369 1370 const is_ssh = switch (transport_def) { 1371 .file => false, 1372 .wire => |wire_kind| .ssh == wire_kind, 1373 }; 1374 1375 if (shell_out_to_git) { 1376 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1377 defer allocator.free(ssh_cmd); 1378 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1379 defer allocator.free(ssh_config_arg); 1380 1381 { 1382 var process = try std.process.spawn(io, .{ 1383 .argv = if (is_ssh) 1384 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1385 else 1386 &.{ "git", "pull", "--quiet", "origin", "master" }, 1387 .cwd = .{ .path = client_path }, 1388 .stdin = .ignore, 1389 .stdout = .ignore, 1390 .stderr = .inherit, 1391 }); 1392 defer process.kill(io); 1393 const term = try process.wait(io); 1394 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1395 } 1396 1397 // make sure pull was successful 1398 { 1399 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1400 try std.testing.expectEqualStrings(&commit1, &oid_master); 1401 } 1402 1403 // make another commit on the server 1404 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1405 1406 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1407 { 1408 var process = try std.process.spawn(io, .{ 1409 .argv = if (is_ssh) 1410 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1411 else 1412 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1413 .cwd = .{ .path = client_path }, 1414 .stdin = .ignore, 1415 .stdout = .ignore, 1416 .stderr = .inherit, 1417 }); 1418 defer process.kill(io); 1419 const term = try process.wait(io); 1420 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1421 } 1422 1423 // make sure fetch with want-ref was successful 1424 { 1425 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1426 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1427 } 1428 } else { 1429 const refspecs = &.{ 1430 "+refs/heads/master:refs/heads/master", 1431 }; 1432 1433 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1434 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1435 1436 try client_repo.fetch( 1437 io, 1438 allocator, 1439 "origin", 1440 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1441 .command = ssh_cmd_maybe, 1442 } } }, 1443 ); 1444 1445 // update the working dir 1446 try client_repo.restore(io, allocator, "."); 1447 } 1448 1449 // make sure fetch was successful 1450 { 1451 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1452 try std.testing.expectEqualStrings(&commit1, &oid_master); 1453 } 1454 } 1455 1456 fn testPushLarge( 1457 comptime repo_kind: rp.RepoKind, 1458 comptime transport_def: net.TransportDefinition, 1459 comptime shell_out_to_git: bool, 1460 comptime hash_kind: hash.HashKind, 1461 comptime port: u16, 1462 io: std.Io, 1463 allocator: std.mem.Allocator, 1464 ) !void { 1465 // create the temp dir 1466 var temp = std.testing.tmpDir(.{}); 1467 defer temp.cleanup(); 1468 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1469 defer allocator.free(temp_path); 1470 1471 // init server 1472 var server_process = try runServer(io, allocator, temp_path, port); 1473 defer _ = server_process.kill(io); 1474 1475 // register the repo under admin and locate its on-disk directory 1476 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1477 defer allocator.free(server_path); 1478 1479 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1480 defer server_repo.deinit(io, allocator); 1481 1482 // add config 1483 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1484 1485 // export server repo 1486 { 1487 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1488 defer export_file.close(io); 1489 } 1490 1491 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1492 defer allocator.free(client_path); 1493 1494 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1495 defer client_repo.deinit(io, allocator); 1496 1497 var client_dir = try temp.dir.openDir(io, "client", .{}); 1498 defer client_dir.close(io); 1499 1500 { 1501 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1502 defer hello_txt.close(io); 1503 try hello_txt.writeStreamingAll(io, "hello, world!"); 1504 try client_repo.add(io, allocator, &.{"hello.txt"}); 1505 } 1506 1507 // copy files from current repo into client dir 1508 const cwd = std.Io.Dir.cwd(); 1509 for (&[_][]const u8{"src"}) |dir_name| { 1510 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1511 defer src_repo_dir.close(io); 1512 1513 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1514 defer dest_repo_dir.close(io); 1515 1516 try copyDir(io, src_repo_dir, dest_repo_dir); 1517 1518 try client_repo.add(io, allocator, &.{dir_name}); 1519 } 1520 1521 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1522 1523 // change the files so git will send them as delta objects 1524 for (&[_][]const u8{"src"}) |dir_name| { 1525 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1526 defer dest_repo_dir.close(io); 1527 1528 { 1529 var iter = dest_repo_dir.iterate(); 1530 while (try iter.next(io)) |entry| { 1531 switch (entry.kind) { 1532 .file => { 1533 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1534 defer file.close(io); 1535 var writer = file.writer(io, &.{}); 1536 try writer.interface.writeAll("EDIT"); 1537 }, 1538 else => {}, 1539 } 1540 } 1541 } 1542 1543 try client_repo.add(io, allocator, &.{dir_name}); 1544 } 1545 1546 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1547 1548 // add remote 1549 { 1550 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1551 defer allocator.free(remote_url); 1552 1553 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1554 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1555 } 1556 1557 const is_ssh = switch (transport_def) { 1558 .file => false, 1559 .wire => |wire_kind| .ssh == wire_kind, 1560 }; 1561 1562 if (shell_out_to_git) { 1563 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1564 defer allocator.free(ssh_cmd); 1565 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1566 defer allocator.free(ssh_config_arg); 1567 1568 // shell out to git so it will send delta objects 1569 var process = try std.process.spawn(io, .{ 1570 .argv = if (is_ssh) 1571 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1572 else 1573 &.{ "git", "push", "--quiet", "origin", "master" }, 1574 .cwd = .{ .path = client_path }, 1575 .stdin = .ignore, 1576 .stdout = .ignore, 1577 .stderr = .inherit, 1578 }); 1579 defer process.kill(io); 1580 const term = try process.wait(io); 1581 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1582 } else { 1583 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1584 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1585 1586 try client_repo.push( 1587 io, 1588 allocator, 1589 "origin", 1590 "master", 1591 false, 1592 .{ .wire = .{ .ssh = .{ 1593 .command = ssh_cmd_maybe, 1594 } } }, 1595 ); 1596 } 1597 1598 // make sure push was successful 1599 { 1600 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1601 try std.testing.expectEqualStrings(&commit2, &oid_master); 1602 1603 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1604 } 1605 } 1606 1607 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1608 var iter = src_dir.iterate(); 1609 while (try iter.next(io)) |entry| { 1610 switch (entry.kind) { 1611 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1612 .directory => { 1613 try dest_dir.createDirPath(io, entry.name); 1614 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1615 defer dest_entry_dir.close(io); 1616 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1617 defer src_entry_dir.close(io); 1618 try copyDir(io, src_entry_dir, dest_entry_dir); 1619 }, 1620 else => {}, 1621 } 1622 } 1623 } 1624 1625 // the dev SSH public key matching the private key written by runServer 1626 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1627 1628 // create the admin event repo with a user holding the dev SSH public key, plus 1629 // that user's repo 1630 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1631 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1632 defer allocator.free(admin_repo_path); 1633 1634 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1635 defer repo.deinit(io, allocator); 1636 1637 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1638 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1639 1640 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1641 const user_id = evt.EventWithId.randomId(prng.random()); 1642 1643 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1644 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1645 1646 const users_dir = try std.fs.path.join(allocator, &.{ data_path, "users" }); 1647 defer allocator.free(users_dir); 1648 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1649 .id = std.fmt.bytesToHex(user_id, .lower), 1650 .author = .{ .name = "admin", .email = "admin@example.test" }, 1651 .event = .{ .user = .{ 1652 .name = "admin", 1653 .email = "admin@example.test", 1654 .password_hash = password_hash, 1655 .ssh_keys = admin_ssh_key, 1656 } }, 1657 }}); 1658 1659 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 1660 defer allocator.free(user_repo_path); 1661 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1662 defer user_repo.deinit(io, allocator); 1663 } 1664 1665 // resolve admin:<repo_name> to its on-disk directory under <data_dir>/users via 1666 // the event store 1667 fn repoOnDiskPath( 1668 io: std.Io, 1669 allocator: std.mem.Allocator, 1670 data_path: []const u8, 1671 repo_name: []const u8, 1672 create: bool, 1673 ) !?[]u8 { 1674 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1675 defer allocator.free(admin_repo_path); 1676 const users_dir = try std.fs.path.join(allocator, &.{ data_path, "users" }); 1677 defer allocator.free(users_dir); 1678 1679 const location = (try evt.resolveOrCreateRepo( 1680 io, 1681 allocator, 1682 users_dir, 1683 admin_repo_path, 1684 "admin", 1685 repo_name, 1686 if (create) .{ .read_access = .public } else null, 1687 )) orelse return null; 1688 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1689 } 1690 1691 // build the remote URL addressing admin:<repo_name> over the given transport 1692 fn remoteUrl( 1693 comptime transport_def: net.TransportDefinition, 1694 allocator: std.mem.Allocator, 1695 repo_name: []const u8, 1696 port: u16, 1697 ) ![]u8 { 1698 return switch (transport_def) { 1699 .file => unreachable, 1700 .wire => |wire_kind| switch (wire_kind) { 1701 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin:{s}", .{ port, repo_name }), 1702 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin:{s}", .{ port, repo_name }), 1703 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin:{s}", .{repo_name}), 1704 }, 1705 }; 1706 } 1707 1708 // the ssh command the haxy git client should invoke, or null when not using 1709 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1710 // and skip writing to known_hosts. 1711 fn sshCommand( 1712 is_ssh: bool, 1713 allocator: std.mem.Allocator, 1714 temp_path: []const u8, 1715 port: u16, 1716 ) !?[]u8 { 1717 if (!is_ssh) return null; 1718 1719 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1720 defer allocator.free(priv_key_path); 1721 1722 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1723 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1724 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile={s} -o LogLevel=ERROR -o IdentitiesOnly=yes -i \"{s}\"", .{ port + 1, null_path, priv_key_path }); 1725 } 1726 1727 const ServerFiles = union(enum) { 1728 files: []const struct { path: []const u8, content: []const u8 }, 1729 dirs: []const []const u8, 1730 }; 1731 1732 /// commit files without writing the server's index or worktree. 1733 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 { 1734 const R = @TypeOf(repo.*); 1735 const opts = repo.self_repo_opts; 1736 const Index = xit.index.Index(.xit, opts); 1737 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1738 const Ctx = struct { 1739 core: *R.Core, 1740 io: std.Io, 1741 allocator: std.mem.Allocator, 1742 files: ServerFiles, 1743 metadata: xit.object.CommitMetadata(opts.hash), 1744 result: *[xit.hash.hexLen(opts.hash)]u8, 1745 1746 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1747 var reader = std.Io.Reader.fixed(content); 1748 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1749 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1750 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1751 defer ctx.allocator.free(path_parts); 1752 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1753 } 1754 1755 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1756 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1757 defer dir.close(ctx.io); 1758 var entries = dir.iterate(); 1759 while (try entries.next(ctx.io)) |entry| { 1760 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1761 defer ctx.allocator.free(child); 1762 switch (entry.kind) { 1763 .directory => try ctx.addDir(state, index, child), 1764 .file => { 1765 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1766 defer ctx.allocator.free(content); 1767 try ctx.addFile(state, index, child, content); 1768 }, 1769 else => {}, 1770 } 1771 } 1772 } 1773 1774 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1775 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1776 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1777 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1778 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); 1779 defer index.deinit(); 1780 switch (ctx.files) { 1781 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1782 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1783 } 1784 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1785 defer tree.deinit(); 1786 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1787 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1788 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1789 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .commit = try .init(ctx.metadata.message, ctx.result.*, opts.max_read_size) }); 1790 } 1791 }; 1792 try repo.core.db_file.lock(io, .exclusive); 1793 defer repo.core.db_file.unlock(io); 1794 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1795 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1796 .core = &repo.core, 1797 .io = io, 1798 .allocator = allocator, 1799 .files = files, 1800 .metadata = metadata, 1801 .result = &result, 1802 }); 1803 return result; 1804 } 1805