╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
89 90 fn runServer( 91 io: std.Io, 92 allocator: std.mem.Allocator, 93 comptime temp_dir_name: []const u8, 94 comptime port: u16, 95 ) !std.process.Child { 96 { 97 const priv_key_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/key", .{}); 98 defer priv_key_file.close(io); 99 try priv_key_file.writeStreamingAll(io, 100 \\-----BEGIN OPENSSH PRIVATE KEY----- 101 \\b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 102 \\QyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22JgAAAJA+7hikPu4Y 103 \\pAAAAAtzc2gtZWQyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22Jg 104 \\AAAEDVlopOMnKt/7by/IA8VZvQXUS/O6VLkixOqnnahUdPCKeIs8mJqigBZ5y84J4COgnA 105 \\JJ5bHPKy+lM2SliMXbYmAAAAC3JhZGFyQHJvYXJrAQI= 106 \\-----END OPENSSH PRIVATE KEY----- 107 \\ 108 ); 109 if (.windows != builtin.os.tag) { 110 try priv_key_file.setPermissions(io, @enumFromInt(0o600)); 111 } 112 } 113 114 // seed the admin event store so the server can resolve <owner>/<repo> paths 115 // and authenticate pushes from the dev key above 116 try setupAdmin(io, allocator, temp_dir_name); 117 118 const cwd_path = try std.process.currentPathAlloc(io, allocator); 119 defer allocator.free(cwd_path); 120 const haxy_path = try std.fs.path.join(allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 121 defer allocator.free(haxy_path); 122 123 const http_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port}); 124 const ssh_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port + 1}); 125 126 const process = try std.process.spawn(io, .{ 127 .argv = &.{ haxy_path, "serve", "--http-listen", http_listen_arg, "--ssh-listen", ssh_listen_arg, "--data-dir", temp_dir_name }, 128 .stdin = .ignore, 129 .stdout = .ignore, 130 .stderr = .ignore, 131 }); 132 133 const address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", port); 134 for (0..50) |_| { 135 const stream = address.connect(io, .{ .mode = .stream }) catch { 136 try std.Io.sleep(io, .fromMilliseconds(100), .real); 137 continue; 138 }; 139 stream.close(io); 140 break; 141 } 142 143 return process; 144 } 145 146 fn testFetch( 147 comptime repo_kind: rp.RepoKind, 148 comptime transport_def: net.TransportDefinition, 149 comptime port: u16, 150 io: std.Io, 151 allocator: std.mem.Allocator, 152 ) !void { 153 const temp_dir_name = "temp-testnet-fetch"; 154 155 // create the temp dir 156 const cwd = std.Io.Dir.cwd(); 157 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 158 if (temp_dir_or_err) |*temp_dir| { 159 temp_dir.close(io); 160 try cwd.deleteTree(io, temp_dir_name); 161 } else |_| {} 162 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 163 defer cwd.deleteTree(io, temp_dir_name) catch {}; 164 defer temp_dir.close(io); 165 166 // init server 167 var server_process = try runServer(io, allocator, temp_dir_name, port); 168 defer _ = server_process.kill(io); 169 170 // register the repo under admin and locate its on-disk directory 171 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 172 defer allocator.free(server_path); 173 174 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 175 defer server_repo.deinit(io, allocator); 176 177 // make a commit 178 const commit1 = blk: { 179 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 180 defer hello_txt.close(io); 181 try hello_txt.writeStreamingAll(io, "hello, world!"); 182 try server_repo.add(io, allocator, &.{"hello.txt"}); 183 break :blk try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 184 }; 185 186 // export server repo 187 { 188 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 189 defer export_file.close(io); 190 191 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowAnySHA1InWant", .value = "true" }); 192 } 193 194 // add a tag 195 _ = try server_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 196 197 const cwd_path = try std.process.currentPathAlloc(io, allocator); 198 defer allocator.free(cwd_path); 199 200 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 201 defer allocator.free(client_path); 202 203 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 204 defer client_repo.deinit(io, allocator); 205 206 // add remote 207 { 208 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 209 defer allocator.free(remote_url); 210 211 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 212 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 213 } 214 215 // create refspec with oid as a test 216 const oid_refspec = try std.fmt.allocPrint(allocator, "+{s}:refs/heads/foo", .{&commit1}); 217 defer allocator.free(oid_refspec); 218 219 const refspecs = &.{ 220 "+refs/heads/master:refs/heads/master", 221 oid_refspec, 222 }; 223 224 const is_ssh = switch (transport_def) { 225 .file => false, 226 .wire => |wire_kind| .ssh == wire_kind, 227 }; 228 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 229 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 230 231 try client_repo.fetch( 232 io, 233 allocator, 234 "origin", 235 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 236 .command = ssh_cmd_maybe, 237 } } }, 238 ); 239 240 // update the working dir 241 try client_repo.restore(io, allocator, "."); 242 243 // make sure fetch was successful 244 { 245 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 246 defer hello_txt.close(io); 247 248 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 249 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 250 251 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 252 try std.testing.expectEqualStrings(&commit1, &oid_master); 253 } 254 255 // make another commit 256 const commit2 = blk: { 257 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 258 defer goodbye_txt.close(io); 259 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 260 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 261 break :blk try server_repo.commit(io, allocator, .{ .message = "goodbye" }); 262 }; 263 264 try client_repo.fetch( 265 io, 266 allocator, 267 "origin", 268 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 269 .command = ssh_cmd_maybe, 270 } } }, 271 ); 272 273 // update the working dir 274 try client_repo.restore(io, allocator, "."); 275 276 // make sure fetch was successful 277 { 278 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 279 defer goodbye_txt.close(io); 280 281 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 282 try std.testing.expectEqualStrings(&commit2, &oid_master); 283 } 284 } 285 286 fn testPush( 287 comptime repo_kind: rp.RepoKind, 288 comptime transport_def: net.TransportDefinition, 289 comptime port: u16, 290 io: std.Io, 291 allocator: std.mem.Allocator, 292 ) !void { 293 const temp_dir_name = "temp-testnet-push"; 294 295 // create the temp dir 296 const cwd = std.Io.Dir.cwd(); 297 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 298 if (temp_dir_or_err) |*temp_dir| { 299 temp_dir.close(io); 300 try cwd.deleteTree(io, temp_dir_name); 301 } else |_| {} 302 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 303 defer cwd.deleteTree(io, temp_dir_name) catch {}; 304 defer temp_dir.close(io); 305 306 // init server 307 var server_process = try runServer(io, allocator, temp_dir_name, port); 308 defer _ = server_process.kill(io); 309 310 // register the repo under admin and locate its on-disk directory 311 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 312 defer allocator.free(server_path); 313 314 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 315 defer server_repo.deinit(io, allocator); 316 317 // add config 318 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 319 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 320 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 321 322 // export server repo 323 { 324 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 325 defer export_file.close(io); 326 } 327 328 const cwd_path = try std.process.currentPathAlloc(io, allocator); 329 defer allocator.free(cwd_path); 330 331 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 332 defer allocator.free(client_path); 333 334 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 335 defer client_repo.deinit(io, allocator); 336 337 // make a commit 338 const commit1 = blk: { 339 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 340 defer hello_txt.close(io); 341 try hello_txt.writeStreamingAll(io, "hello, world!"); 342 try client_repo.add(io, allocator, &.{"hello.txt"}); 343 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 344 }; 345 346 // add a tag 347 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 348 349 // add remote 350 { 351 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 352 defer allocator.free(remote_url); 353 354 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 355 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 356 } 357 358 const refspecs = &.{ 359 "refs/tags/1.0.0:refs/tags/1.0.0", 360 }; 361 362 const is_ssh = switch (transport_def) { 363 .file => false, 364 .wire => |wire_kind| .ssh == wire_kind, 365 }; 366 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 367 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 368 369 try client_repo.push( 370 io, 371 allocator, 372 "origin", 373 "master", 374 false, 375 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 376 .command = ssh_cmd_maybe, 377 } } }, 378 ); 379 380 // make sure push was successful 381 { 382 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 383 384 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 385 try std.testing.expectEqualStrings(&commit1, &oid_master); 386 } 387 388 // make a commit on the server 389 { 390 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 391 defer hello_txt.close(io); 392 try hello_txt.writeStreamingAll(io, "hello, world from the server!"); 393 try server_repo.add(io, allocator, &.{"hello.txt"}); 394 _ = try server_repo.commit(io, allocator, .{ .message = "new commit from the server" }); 395 } 396 397 // make another commit 398 const commit2 = blk: { 399 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 400 defer goodbye_txt.close(io); 401 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 402 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 403 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 404 }; 405 406 // can't push because server has commit not found locally 407 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 408 io, 409 allocator, 410 "origin", 411 "master", 412 false, 413 .{ .wire = .{ .ssh = .{ 414 .command = ssh_cmd_maybe, 415 } } }, 416 )); 417 418 // make a commit on the server with no parents, thus creating an incompatible git history 419 { 420 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 421 defer hello_txt.close(io); 422 try hello_txt.writeStreamingAll(io, "hello, world from the server again!"); 423 try server_repo.add(io, allocator, &.{"hello.txt"}); 424 _ = try server_repo.commit(io, allocator, .{ .message = "new git history on the server", .parent_oids = &.{} }); 425 } 426 427 // can't push because commit doesn't exist locally 428 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 429 io, 430 allocator, 431 "origin", 432 "master", 433 false, 434 .{ .wire = .{ .ssh = .{ 435 .command = ssh_cmd_maybe, 436 } } }, 437 )); 438 439 // retrieve the commit object 440 try client_repo.fetch( 441 io, 442 allocator, 443 "origin", 444 .{ .wire = .{ .ssh = .{ 445 .command = ssh_cmd_maybe, 446 } } }, 447 ); 448 449 // can't push because server's history is incompatible 450 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 451 io, 452 allocator, 453 "origin", 454 "master", 455 false, 456 .{ .wire = .{ .ssh = .{ 457 .command = ssh_cmd_maybe, 458 } } }, 459 )); 460 461 // test denyNonFastForwards 462 { 463 // set denyNonFastForwards on server 464 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 465 466 // save the server's current master ref 467 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 468 469 // force push should be rejected by server due to denyNonFastForwards 470 try std.testing.expectError(error.RemoteRejectedRef, client_repo.push( 471 io, 472 allocator, 473 "origin", 474 "master", 475 true, 476 .{ .wire = .{ .ssh = .{ 477 .command = ssh_cmd_maybe, 478 } } }, 479 )); 480 481 // verify the server ref was not updated (push was denied) 482 { 483 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 484 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 485 } 486 487 // remove denyNonFastForwards from server 488 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 489 } 490 491 // force push 492 try client_repo.push( 493 io, 494 allocator, 495 "origin", 496 "master", 497 true, 498 .{ .wire = .{ .ssh = .{ 499 .command = ssh_cmd_maybe, 500 } } }, 501 ); 502 503 // make sure push was successful 504 { 505 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 506 try std.testing.expectEqualStrings(&commit2, &oid_master); 507 } 508 509 // remove the remote tag 510 try client_repo.push( 511 io, 512 allocator, 513 "origin", 514 ":refs/tags/1.0.0", 515 false, 516 .{ .wire = .{ .ssh = .{ 517 .command = ssh_cmd_maybe, 518 } } }, 519 ); 520 521 // make sure push was successful 522 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 523 } 524 525 fn testPushFork( 526 comptime port: u16, 527 io: std.Io, 528 allocator: std.mem.Allocator, 529 ) !void { 530 const temp_dir_name = "temp-testnet-push-fork"; 531 const cwd = std.Io.Dir.cwd(); 532 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 533 if (temp_dir_or_err) |*temp_dir| { 534 temp_dir.close(io); 535 try cwd.deleteTree(io, temp_dir_name); 536 } else |_| {} 537 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 538 defer cwd.deleteTree(io, temp_dir_name) catch {}; 539 defer temp_dir.close(io); 540 541 var server_process = try runServer(io, allocator, temp_dir_name, port); 542 defer _ = server_process.kill(io); 543 544 const target_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "target", true)).?; 545 defer allocator.free(target_path); 546 { 547 var target_repo = try rp.Repo(.xit, .{}).init(io, allocator, .{ .path = target_path }); 548 defer target_repo.deinit(io, allocator); 549 const file = try target_repo.core.work_dir.createFile(io, "main.txt", .{ .truncate = true }); 550 defer file.close(io); 551 try file.writeStreamingAll(io, "base contents\n"); 552 try target_repo.add(io, allocator, &.{"main.txt"}); 553 _ = try target_repo.commit(io, allocator, .{ .author = "admin <admin@example.test>", .message = "initial code" }); 554 try target_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 555 } 556 557 const other_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "other", true)).?; 558 defer allocator.free(other_path); 559 560 const cwd_path = try std.process.currentPathAlloc(io, allocator); 561 defer allocator.free(cwd_path); 562 const admin_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "admin" }); 563 defer allocator.free(admin_path); 564 const repos_dir = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos" }); 565 defer allocator.free(repos_dir); 566 567 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 568 const fork_id = evt.EventWithId.randomId(prng.random()); 569 const fork_id_hex = std.fmt.bytesToHex(fork_id, .lower); 570 var user_id: [evt.event_id_size]u8 = undefined; 571 var repo_id: [evt.event_id_size]u8 = undefined; 572 const draft_path = blk: { 573 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 574 defer admin.deinit(io, allocator); 575 var arena = std.heap.ArenaAllocator.init(allocator); 576 defer arena.deinit(); 577 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 578 const target = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, "admin", "target")) orelse return error.NotFound; 579 repo_id = target.event_id; 580 @memcpy(&user_id, target.repo.event.user_id); 581 break :blk try fork.create(.{}, io, allocator, repos_dir, &admin, .{ 582 .id = fork_id_hex, 583 .user_id = user_id, 584 .repo_id = repo_id, 585 .title = "add a feature", 586 .description = "a draft patch", 587 .tags = "enhancement", 588 .author = .{ .name = "admin", .email = "admin@example.test" }, 589 .timestamp = 1, 590 }); 591 }; 592 defer allocator.free(draft_path); 593 594 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 595 defer allocator.free(client_path); 596 const ssh_cmd = (try sshCommand(true, allocator, cwd_path, temp_dir_name, port)).?; 597 defer allocator.free(ssh_cmd); 598 var client = try rp.Repo(.xit, .{ .is_test = true }).clone( 599 io, 600 allocator, 601 "git@localhost:admin/target", 602 client_path, 603 client_path, 604 null, 605 .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }, 606 ); 607 defer client.deinit(io, allocator); 608 609 const source_oid = blk: { 610 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 611 defer file.close(io); 612 try file.writeStreamingAll(io, "fork contents\n"); 613 try client.add(io, allocator, &.{"feature.txt"}); 614 break :blk try client.commit(io, allocator, .{ .message = "add feature" }); 615 }; 616 617 const remote_url = try std.fmt.allocPrint(allocator, "git@localhost:admin/target/patch:{s}/branch:master", .{&fork_id_hex}); 618 defer allocator.free(remote_url); 619 try client.addRemote(io, allocator, .{ .name = "patch", .value = remote_url }); 620 try client.push(io, allocator, "patch", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 621 622 var first_revision_id: [evt.event_id_size]u8 = undefined; 623 { 624 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 625 defer draft.deinit(io, allocator); 626 var arena = std.heap.ArenaAllocator.init(allocator); 627 defer arena.deinit(); 628 const moment = try evt.currentMoment(.{}, &draft); 629 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 630 try std.testing.expect(!patch.removed); 631 try std.testing.expectEqualStrings(&source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 632 const selected = patch.event.revision orelse return error.NotFound; 633 try std.testing.expectEqualStrings("refs/heads/master", selected.target_ref); 634 try std.testing.expectEqualStrings(&source_oid, selected.source_oid); 635 first_revision_id = try evt.parseEventId(&selected.id); 636 const revision = (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &first_revision_id)) orelse return error.NotFound; 637 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 638 try std.testing.expectEqualStrings(&source_oid, revision.event.source_oid); 639 } 640 641 // 642 // publish, then push another revision 643 // 644 645 { 646 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 647 defer admin.deinit(io, allocator); 648 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 649 defer target.deinit(io, allocator); 650 try target.addBranch(io, .{ .name = "feature" }); 651 try pch.publish(.{}, io, allocator, &admin, &target, draft_path, .{ 652 .id = fork_id_hex, 653 .user_id = user_id, 654 .repo_id = repo_id, 655 .author = .{ .name = "admin", .email = "admin@example.test" }, 656 .timestamp = 2, 657 }); 658 var arena = std.heap.ArenaAllocator.init(allocator); 659 defer arena.deinit(); 660 const moment = try evt.currentMoment(.{}, &target); 661 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 662 try evt.consume(.repo, .xit, .{}, io, allocator, &target, evt.events_ref, &.{.{ 663 .id = fork_id_hex, 664 .timestamp = 3, 665 .author = .{ .name = "admin", .email = "admin@example.test" }, 666 .event = .{ .patch = .{ 667 .title = "edited feature", 668 .description = patch.event.description, 669 .tags = patch.event.tags, 670 .target_patch_id = patch.event.target_patch_id, 671 .revision = patch.event.revision, 672 } }, 673 }}); 674 } 675 676 const feature_remote_url = try std.fmt.allocPrint(allocator, "git@localhost:admin/target/patch:{s}/branch:feature", .{&fork_id_hex}); 677 defer allocator.free(feature_remote_url); 678 try client.addRemote(io, allocator, .{ .name = "patch-to-feature", .value = feature_remote_url }); 679 680 const second_source_oid = blk: { 681 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 682 defer file.close(io); 683 try file.writeStreamingAll(io, "updated fork contents\n"); 684 try client.add(io, allocator, &.{"feature.txt"}); 685 break :blk try client.commit(io, allocator, .{ .message = "revise feature" }); 686 }; 687 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 688 689 var second_revision_id: [evt.event_id_size]u8 = undefined; 690 var second_patch_oid: [hash.hexLen(.sha1)]u8 = undefined; 691 { 692 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 693 defer draft.deinit(io, allocator); 694 var arena = std.heap.ArenaAllocator.init(allocator); 695 defer arena.deinit(); 696 const moment = try evt.currentMoment(.{}, &draft); 697 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 698 try std.testing.expect(patch.removed); 699 const newest = (try evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, &arena)) orelse return error.NotFound; 700 second_revision_id = newest.id; 701 @memcpy(&second_patch_oid, newest.record.patch_oid); 702 try std.testing.expect(!std.mem.eql(u8, &first_revision_id, &second_revision_id)); 703 try std.testing.expectEqualStrings(&second_source_oid, newest.record.event.source_oid); 704 try std.testing.expectEqualStrings("refs/heads/feature", newest.record.event.target_ref); 705 try std.testing.expectEqualStrings("edited feature", newest.record.event.message); 706 } 707 { 708 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 709 defer target.deinit(io, allocator); 710 var arena = std.heap.ArenaAllocator.init(allocator); 711 defer arena.deinit(); 712 const moment = try evt.currentMoment(.{}, &target); 713 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 714 try std.testing.expectEqualStrings("edited feature", patch.event.title); 715 const selected = patch.event.revision orelse return error.NotFound; 716 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(second_revision_id, .lower), &selected.id); 717 try std.testing.expectEqualStrings(&second_source_oid, selected.source_oid); 718 try std.testing.expectEqualStrings("refs/heads/feature", selected.target_ref); 719 } 720 721 // 722 // reject extra refs without applying the patch update 723 // 724 725 const third_source_oid = blk: { 726 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 727 defer file.close(io); 728 try file.writeStreamingAll(io, "rejected fork contents\n"); 729 try client.add(io, allocator, &.{"feature.txt"}); 730 break :blk try client.commit(io, allocator, .{ .message = "rejected revision" }); 731 }; 732 const extra_ref_rejected = if (client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ 733 .refspecs = &.{"refs/heads/master:refs/heads/extra"}, 734 .wire = .{ .ssh = .{ .command = ssh_cmd } }, 735 })) |_| false else |_| true; 736 try std.testing.expect(extra_ref_rejected); 737 { 738 var draft = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = draft_path }); 739 defer draft.deinit(io, allocator); 740 try std.testing.expectEqualStrings(&second_source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 741 try std.testing.expectEqual(null, try draft.readRef(io, .{ .kind = .head, .name = "extra" })); 742 var arena = std.heap.ArenaAllocator.init(allocator); 743 defer arena.deinit(); 744 const moment = try evt.currentMoment(.{}, &draft); 745 const newest = (try evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, &arena)) orelse return error.NotFound; 746 try std.testing.expectEqualSlices(u8, &second_revision_id, &newest.id); 747 } 748 749 // 750 // a removed target patch stays removed after another push 751 // 752 753 { 754 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 755 defer target.deinit(io, allocator); 756 try evt.remove(.repo, .xit, .{}, io, allocator, &target, &fork_id, .patch, .{ .name = "admin", .email = "admin@example.test" }); 757 } 758 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 759 { 760 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 761 defer target.deinit(io, allocator); 762 var arena = std.heap.ArenaAllocator.init(allocator); 763 defer arena.deinit(); 764 const moment = try evt.currentMoment(.{}, &target); 765 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 766 try std.testing.expect(patch.removed); 767 } 768 769 // 770 // restore the patch, merge its source oid, and push the merge 771 // 772 773 { 774 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 775 defer target.deinit(io, allocator); 776 var arena = std.heap.ArenaAllocator.init(allocator); 777 defer arena.deinit(); 778 const moment = try evt.currentMoment(.{}, &target); 779 const removed = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 780 try evt.consume(.repo, .xit, .{}, io, allocator, &target, evt.events_ref, &.{.{ 781 .id = fork_id_hex, 782 .timestamp = 4, 783 .author = .{ .name = "admin", .email = "admin@example.test" }, 784 .event = .{ .patch = removed.event }, 785 }}); 786 } 787 const merge_parents = [_][hash.hexLen(.sha1)]u8{ third_source_oid, second_source_oid }; 788 _ = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 789 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 790 { 791 var target = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = target_path }); 792 defer target.deinit(io, allocator); 793 var arena = std.heap.ArenaAllocator.init(allocator); 794 defer arena.deinit(); 795 const moment = try evt.currentMoment(.{}, &target); 796 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &fork_id)) orelse return error.NotFound; 797 try std.testing.expectEqual(.merged, patch.event.status); 798 const imported = (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &second_revision_id)) orelse return error.NotFound; 799 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 800 } 801 802 const other_url = try std.fmt.allocPrint(allocator, "git@localhost:admin/other/patch:{s}/branch:other", .{&fork_id_hex}); 803 defer allocator.free(other_url); 804 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 805 const wrong_repo_rejected = if (client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } })) |_| false else |_| true; 806 try std.testing.expect(wrong_repo_rejected); 807 808 // 809 // the fork remains cloneable after the target repo is removed 810 // 811 812 { 813 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 814 defer admin.deinit(io, allocator); 815 try evt.remove(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 816 } 817 const clone_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "fork-clone" }); 818 defer allocator.free(clone_path); 819 var fork_clone = try rp.Repo(.xit, .{ .is_test = true }).clone( 820 io, 821 allocator, 822 remote_url, 823 clone_path, 824 clone_path, 825 null, 826 .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }, 827 ); 828 defer fork_clone.deinit(io, allocator); 829 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 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(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 835 .{ 836 .id = std.fmt.bytesToHex(repo_id, .lower), 837 .timestamp = 2, 838 .author = .{ .name = "admin", .email = "admin@example.test" }, 839 .event = .{ .repo = .{ 840 .user_id = &user_id, 841 .name = "target", 842 .description = "", 843 } }, 844 }, 845 .{ 846 .id = std.fmt.bytesToHex(user_id, .lower), 847 .timestamp = 2, 848 .author = .{ .name = "admin", .email = "admin@example.test" }, 849 .event = .{ .user = .{ 850 .name = "admin", 851 .email = "admin@example.test", 852 .password_hash = "", 853 .ssh_keys = "", 854 } }, 855 }, 856 }); 857 } 858 859 const denied_url = try std.fmt.allocPrint(allocator, "git@localhost:admin/target/patch:{s}/branch:denied", .{&fork_id_hex}); 860 defer allocator.free(denied_url); 861 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 862 const unauthorized_rejected = if (client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } })) |_| false else |_| true; 863 try std.testing.expect(unauthorized_rejected); 864 } 865 866 fn testPushCreatesMissingRepo( 867 comptime repo_kind: rp.RepoKind, 868 comptime transport_def: net.TransportDefinition, 869 comptime port: u16, 870 io: std.Io, 871 allocator: std.mem.Allocator, 872 ) !void { 873 const temp_dir_name = "temp-testnet-push-create"; 874 875 const cwd = std.Io.Dir.cwd(); 876 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 877 if (temp_dir_or_err) |*temp_dir| { 878 temp_dir.close(io); 879 try cwd.deleteTree(io, temp_dir_name); 880 } else |_| {} 881 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 882 defer cwd.deleteTree(io, temp_dir_name) catch {}; 883 defer temp_dir.close(io); 884 885 var server_process = try runServer(io, allocator, temp_dir_name, port); 886 defer _ = server_process.kill(io); 887 888 const cwd_path = try std.process.currentPathAlloc(io, allocator); 889 defer allocator.free(cwd_path); 890 891 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 892 defer allocator.free(client_path); 893 894 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 895 defer client_repo.deinit(io, allocator); 896 897 const commit1 = blk: { 898 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 899 defer hello_txt.close(io); 900 try hello_txt.writeStreamingAll(io, "hello, world!"); 901 try client_repo.add(io, allocator, &.{"hello.txt"}); 902 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 903 }; 904 905 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 906 907 // the repo is not pre-registered; the push should mint the repo event under 908 // admin and create the repo on disk 909 { 910 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 911 defer allocator.free(remote_url); 912 913 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 914 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 915 } 916 917 const is_ssh = switch (transport_def) { 918 .file => false, 919 .wire => |wire_kind| .ssh == wire_kind, 920 }; 921 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 922 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 923 924 try client_repo.push( 925 io, 926 allocator, 927 "origin", 928 "master", 929 false, 930 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 931 .command = ssh_cmd_maybe, 932 } } }, 933 ); 934 935 // the push registered admin/server; resolve its on-disk directory 936 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", false)).?; 937 defer allocator.free(server_path); 938 939 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 940 defer server_repo.deinit(io, allocator); 941 942 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 943 944 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 945 try std.testing.expectEqualStrings(&commit1, &oid_master); 946 } 947 948 fn testPushEvents( 949 comptime repo_kind: rp.RepoKind, 950 comptime transport_def: net.TransportDefinition, 951 comptime port: u16, 952 io: std.Io, 953 allocator: std.mem.Allocator, 954 ) !void { 955 const temp_dir_name = "temp-testnet-push-events"; 956 957 // create the temp dir 958 const cwd = std.Io.Dir.cwd(); 959 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 960 if (temp_dir_or_err) |*temp_dir| { 961 temp_dir.close(io); 962 try cwd.deleteTree(io, temp_dir_name); 963 } else |_| {} 964 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 965 defer cwd.deleteTree(io, temp_dir_name) catch {}; 966 defer temp_dir.close(io); 967 968 // init server 969 var server_process = try runServer(io, allocator, temp_dir_name, port); 970 defer _ = server_process.kill(io); 971 972 // register the repo under admin and locate its on-disk directory 973 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 974 defer allocator.free(server_path); 975 976 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 977 const ServerRepo = rp.Repo(.xit, repo_opts); 978 var server_repo = try ServerRepo.init(io, allocator, .{ .path = server_path }); 979 defer server_repo.deinit(io, allocator); 980 981 const cwd_path = try std.process.currentPathAlloc(io, allocator); 982 defer allocator.free(cwd_path); 983 984 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 985 defer allocator.free(client_path); 986 987 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 988 defer client_repo.deinit(io, allocator); 989 990 // add remote 991 { 992 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 993 defer allocator.free(remote_url); 994 995 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 996 } 997 998 const is_ssh = switch (transport_def) { 999 .file => false, 1000 .wire => |wire_kind| .ssh == wire_kind, 1001 }; 1002 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1003 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1004 1005 var arena = std.heap.ArenaAllocator.init(allocator); 1006 defer arena.deinit(); 1007 1008 // 1009 // define test events 1010 // 1011 1012 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1013 const issue_event_id = evt.EventWithId.randomId(prng.random()); 1014 1015 const events_to_push = [_]evt.EventWithId{ 1016 .{ 1017 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1018 .author = .{ .name = "haxy", .email = "user@haxy" }, 1019 .event = .{ 1020 .issue = .{ 1021 .title = "Login form clears password on validation error", 1022 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 1023 .tags = "bug priority-high ui", 1024 }, 1025 }, 1026 }, 1027 }; 1028 1029 // commit the issue on the client and push it to the server 1030 try evt.consume(.repo, repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 1031 try client_repo.push( 1032 io, 1033 allocator, 1034 "origin", 1035 "haxy/events", 1036 false, 1037 .{ .wire = .{ .ssh = .{ 1038 .command = ssh_cmd_maybe, 1039 } } }, 1040 ); 1041 1042 // the push consumed the issue into the server repo's db 1043 { 1044 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1045 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1046 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1047 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1048 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1049 1050 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1051 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.event.tags); 1052 } 1053 1054 // 1055 // edit the issue 1056 // 1057 1058 // this event edits the previous one because it has the same id 1059 const events_to_push2 = [_]evt.EventWithId{ 1060 .{ 1061 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1062 .author = .{ .name = "haxy", .email = "user@haxy" }, 1063 .event = .{ 1064 .issue = .{ 1065 .title = "Login form clears password on validation error", 1066 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1067 .tags = "bug priority-low ui", 1068 }, 1069 }, 1070 }, 1071 }; 1072 1073 // commit the edit on the client and push it to the server 1074 try evt.consume(.repo, repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1075 try client_repo.push( 1076 io, 1077 allocator, 1078 "origin", 1079 "haxy/events", 1080 false, 1081 .{ .wire = .{ .ssh = .{ 1082 .command = ssh_cmd_maybe, 1083 } } }, 1084 ); 1085 1086 // the push consumed the edit into the server repo's db 1087 { 1088 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1089 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1090 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1091 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1092 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1093 1094 // the description and tags were correctly edited 1095 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1096 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.event.tags); 1097 } 1098 } 1099 1100 fn testClone( 1101 comptime repo_kind: rp.RepoKind, 1102 comptime transport_def: net.TransportDefinition, 1103 comptime shell_out_to_git: bool, 1104 comptime port: u16, 1105 io: std.Io, 1106 allocator: std.mem.Allocator, 1107 ) !void { 1108 const temp_dir_name = "temp-testnet-clone"; 1109 1110 // create the temp dir 1111 const cwd = std.Io.Dir.cwd(); 1112 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1113 if (temp_dir_or_err) |*temp_dir| { 1114 temp_dir.close(io); 1115 try cwd.deleteTree(io, temp_dir_name); 1116 } else |_| {} 1117 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1118 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1119 defer temp_dir.close(io); 1120 1121 // init server 1122 var server_process = try runServer(io, allocator, temp_dir_name, port); 1123 defer _ = server_process.kill(io); 1124 1125 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1126 defer allocator.free(cwd_path); 1127 1128 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 1129 defer allocator.free(temp_path); 1130 1131 // register the repo under admin and locate its on-disk directory 1132 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1133 defer allocator.free(server_path); 1134 1135 // init server repo with default branch name as main 1136 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1137 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 1138 defer server_repo.deinit(io, allocator); 1139 1140 if (shell_out_to_git) { 1141 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1142 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1143 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1144 } 1145 1146 // make a commit 1147 { 1148 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1149 defer hello_txt.close(io); 1150 try hello_txt.writeStreamingAll(io, "hello, world!"); 1151 try server_repo.add(io, allocator, &.{"hello.txt"}); 1152 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1153 } 1154 1155 // tag first commit 1156 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1157 1158 // make a commit 1159 { 1160 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 1161 defer goodbye_txt.close(io); 1162 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 1163 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 1164 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 1165 } 1166 1167 // export server repo 1168 { 1169 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1170 defer export_file.close(io); 1171 } 1172 1173 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1174 defer allocator.free(client_path); 1175 1176 // get remote url 1177 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1178 defer allocator.free(remote_url); 1179 1180 const is_ssh = switch (transport_def) { 1181 .file => false, 1182 .wire => |wire_kind| .ssh == wire_kind, 1183 }; 1184 1185 if (shell_out_to_git) { 1186 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1187 defer allocator.free(priv_key_path); 1188 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1189 defer allocator.free(ssh_config_arg); 1190 1191 { 1192 var process = try std.process.spawn(io, .{ 1193 .argv = if (is_ssh) 1194 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 1195 else 1196 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1197 .cwd = .{ .path = temp_path }, 1198 .stdin = .ignore, 1199 .stdout = .ignore, 1200 .stderr = .ignore, 1201 }); 1202 const term = try process.wait(io); 1203 if (term != .exited or term.exited != 0) { 1204 return error.GitCommandFailed; 1205 } 1206 } 1207 1208 // make sure shallow clone was successful 1209 { 1210 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1211 hello_txt.close(io); 1212 } 1213 1214 // make a third commit on the server 1215 { 1216 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1217 defer extra_txt.close(io); 1218 try extra_txt.writeStreamingAll(io, "extra content"); 1219 try server_repo.add(io, allocator, &.{"extra.txt"}); 1220 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1221 } 1222 1223 // pull --unshallow to deepen the clone and get the new commit 1224 { 1225 var process = try std.process.spawn(io, .{ 1226 .argv = if (is_ssh) 1227 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 1228 else 1229 &.{ "git", "pull", "--unshallow" }, 1230 .cwd = .{ .path = client_path }, 1231 .stdin = .ignore, 1232 .stdout = .ignore, 1233 .stderr = .ignore, 1234 }); 1235 const term = try process.wait(io); 1236 if (term != .exited or term.exited != 0) { 1237 return error.GitCommandFailed; 1238 } 1239 } 1240 1241 // make sure unshallow pull was successful 1242 { 1243 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 1244 extra_txt.close(io); 1245 } 1246 1247 // delete client and clone again with --shallow-since 1248 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1249 1250 { 1251 var process = try std.process.spawn(io, .{ 1252 .argv = if (is_ssh) 1253 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 1254 else 1255 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1256 .cwd = .{ .path = temp_path }, 1257 .stdin = .ignore, 1258 .stdout = .ignore, 1259 .stderr = .ignore, 1260 }); 1261 const term = try process.wait(io); 1262 if (term != .exited or term.exited != 0) { 1263 return error.GitCommandFailed; 1264 } 1265 } 1266 1267 // make sure shallow clone was successful 1268 { 1269 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1270 hello_txt.close(io); 1271 } 1272 1273 // delete client and clone again with --shallow-exclude 1274 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1275 1276 { 1277 var process = try std.process.spawn(io, .{ 1278 .argv = if (is_ssh) 1279 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 1280 else 1281 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1282 .cwd = .{ .path = temp_path }, 1283 .stdin = .ignore, 1284 .stdout = .ignore, 1285 .stderr = .ignore, 1286 }); 1287 const term = try process.wait(io); 1288 if (term != .exited or term.exited != 0) { 1289 return error.GitCommandFailed; 1290 } 1291 } 1292 1293 // make sure shallow clone was successful 1294 { 1295 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1296 hello_txt.close(io); 1297 } 1298 1299 // delete client and clone again with --filter=blob:none 1300 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1301 1302 { 1303 var process = try std.process.spawn(io, .{ 1304 .argv = if (is_ssh) 1305 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 1306 else 1307 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1308 .cwd = .{ .path = temp_path }, 1309 .stdin = .ignore, 1310 .stdout = .ignore, 1311 .stderr = .ignore, 1312 }); 1313 const term = try process.wait(io); 1314 if (term != .exited or term.exited != 0) { 1315 return error.GitCommandFailed; 1316 } 1317 } 1318 1319 // make sure partial clone was successful 1320 { 1321 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1322 hello_txt.close(io); 1323 } 1324 1325 // delete client and clone again with --filter=tree:0 1326 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1327 1328 { 1329 var process = try std.process.spawn(io, .{ 1330 .argv = if (is_ssh) 1331 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 1332 else 1333 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1334 .cwd = .{ .path = temp_path }, 1335 .stdin = .ignore, 1336 .stdout = .ignore, 1337 .stderr = .ignore, 1338 }); 1339 const term = try process.wait(io); 1340 if (term != .exited or term.exited != 0) { 1341 return error.GitCommandFailed; 1342 } 1343 } 1344 1345 // make sure treeless clone was successful 1346 { 1347 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1348 goodbye_txt.close(io); 1349 } 1350 } else { 1351 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1352 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1353 1354 // clone repo 1355 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1356 io, 1357 allocator, 1358 remote_url, 1359 temp_path, 1360 client_path, 1361 null, 1362 .{ .wire = .{ .ssh = .{ 1363 .command = ssh_cmd_maybe, 1364 } } }, 1365 ); 1366 defer client_repo.deinit(io, allocator); 1367 1368 // make sure HEAD points to the right default branch 1369 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1370 const head = try client_repo.head(io, &current_branch_buffer); 1371 try std.testing.expectEqualStrings("main", head.ref.name); 1372 1373 // make sure clone was successful 1374 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1375 defer hello_txt.close(io); 1376 } 1377 } 1378 1379 fn testFetchLarge( 1380 comptime repo_kind: rp.RepoKind, 1381 comptime transport_def: net.TransportDefinition, 1382 comptime shell_out_to_git: bool, 1383 comptime port: u16, 1384 io: std.Io, 1385 allocator: std.mem.Allocator, 1386 ) !void { 1387 const temp_dir_name = "temp-testnet-fetch-large"; 1388 1389 // create the temp dir 1390 const cwd = std.Io.Dir.cwd(); 1391 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1392 if (temp_dir_or_err) |*temp_dir| { 1393 temp_dir.close(io); 1394 try cwd.deleteTree(io, temp_dir_name); 1395 } else |_| {} 1396 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1397 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1398 defer temp_dir.close(io); 1399 1400 // init server 1401 var server_process = try runServer(io, allocator, temp_dir_name, port); 1402 defer _ = server_process.kill(io); 1403 1404 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1405 defer allocator.free(cwd_path); 1406 1407 // register the repo under admin and locate its on-disk directory 1408 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1409 defer allocator.free(server_path); 1410 1411 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1412 defer server_repo.deinit(io, allocator); 1413 1414 var server_dir = try cwd.openDir(io, server_path, .{}); 1415 defer server_dir.close(io); 1416 1417 // copy files from current repo into server dir 1418 for (&[_][]const u8{"src"}) |dir_name| { 1419 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1420 defer src_repo_dir.close(io); 1421 1422 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1423 defer dest_repo_dir.close(io); 1424 1425 try copyDir(io, src_repo_dir, dest_repo_dir); 1426 1427 try server_repo.add(io, allocator, &.{dir_name}); 1428 } 1429 1430 // make a commit 1431 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1432 1433 // export server repo 1434 { 1435 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1436 defer export_file.close(io); 1437 } 1438 1439 if (shell_out_to_git) { 1440 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1441 } 1442 1443 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1444 defer allocator.free(client_path); 1445 1446 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1447 defer client_repo.deinit(io, allocator); 1448 1449 // add remote 1450 { 1451 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1452 defer allocator.free(remote_url); 1453 1454 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1455 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1456 } 1457 1458 const is_ssh = switch (transport_def) { 1459 .file => false, 1460 .wire => |wire_kind| .ssh == wire_kind, 1461 }; 1462 1463 if (shell_out_to_git) { 1464 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1465 defer allocator.free(priv_key_path); 1466 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1467 defer allocator.free(ssh_config_arg); 1468 1469 { 1470 var process = try std.process.spawn(io, .{ 1471 .argv = if (is_ssh) 1472 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1473 else 1474 &.{ "git", "pull", "origin", "master" }, 1475 .cwd = .{ .path = client_path }, 1476 .stdin = .ignore, 1477 .stdout = .ignore, 1478 .stderr = .ignore, 1479 }); 1480 const term = try process.wait(io); 1481 if (term != .exited or term.exited != 0) { 1482 return error.GitCommandFailed; 1483 } 1484 } 1485 1486 // make sure pull was successful 1487 { 1488 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1489 try std.testing.expectEqualStrings(&commit1, &oid_master); 1490 } 1491 1492 // make another commit on the server 1493 const commit2 = blk: { 1494 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1495 defer extra_txt.close(io); 1496 try extra_txt.writeStreamingAll(io, "extra content"); 1497 try server_repo.add(io, allocator, &.{"extra.txt"}); 1498 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1499 }; 1500 1501 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1502 { 1503 var process = try std.process.spawn(io, .{ 1504 .argv = if (is_ssh) 1505 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1506 else 1507 &.{ "git", "fetch", "origin", "master" }, 1508 .cwd = .{ .path = client_path }, 1509 .stdin = .ignore, 1510 .stdout = .ignore, 1511 .stderr = .ignore, 1512 }); 1513 const term = try process.wait(io); 1514 if (term != .exited or term.exited != 0) { 1515 return error.GitCommandFailed; 1516 } 1517 } 1518 1519 // make sure fetch with want-ref was successful 1520 { 1521 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1522 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1523 } 1524 } else { 1525 const refspecs = &.{ 1526 "+refs/heads/master:refs/heads/master", 1527 }; 1528 1529 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1530 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1531 1532 try client_repo.fetch( 1533 io, 1534 allocator, 1535 "origin", 1536 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1537 .command = ssh_cmd_maybe, 1538 } } }, 1539 ); 1540 1541 // update the working dir 1542 try client_repo.restore(io, allocator, "."); 1543 } 1544 1545 // make sure fetch was successful 1546 { 1547 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1548 try std.testing.expectEqualStrings(&commit1, &oid_master); 1549 } 1550 } 1551 1552 fn testPushLarge( 1553 comptime repo_kind: rp.RepoKind, 1554 comptime transport_def: net.TransportDefinition, 1555 comptime shell_out_to_git: bool, 1556 comptime port: u16, 1557 io: std.Io, 1558 allocator: std.mem.Allocator, 1559 ) !void { 1560 const temp_dir_name = "temp-testnet-push-large"; 1561 1562 // create the temp dir 1563 const cwd = std.Io.Dir.cwd(); 1564 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1565 if (temp_dir_or_err) |*temp_dir| { 1566 temp_dir.close(io); 1567 try cwd.deleteTree(io, temp_dir_name); 1568 } else |_| {} 1569 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1570 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1571 defer temp_dir.close(io); 1572 1573 // init server 1574 var server_process = try runServer(io, allocator, temp_dir_name, port); 1575 defer _ = server_process.kill(io); 1576 1577 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1578 defer allocator.free(cwd_path); 1579 1580 // register the repo under admin and locate its on-disk directory 1581 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1582 defer allocator.free(server_path); 1583 1584 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1585 defer server_repo.deinit(io, allocator); 1586 1587 // add config 1588 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1589 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1590 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1591 1592 // export server repo 1593 { 1594 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1595 defer export_file.close(io); 1596 } 1597 1598 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1599 defer allocator.free(client_path); 1600 1601 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1602 defer client_repo.deinit(io, allocator); 1603 1604 var client_dir = try cwd.openDir(io, client_path, .{}); 1605 defer client_dir.close(io); 1606 1607 { 1608 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1609 defer hello_txt.close(io); 1610 try hello_txt.writeStreamingAll(io, "hello, world!"); 1611 try client_repo.add(io, allocator, &.{"hello.txt"}); 1612 } 1613 1614 // copy files from current repo into client dir 1615 for (&[_][]const u8{"src"}) |dir_name| { 1616 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1617 defer src_repo_dir.close(io); 1618 1619 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1620 defer dest_repo_dir.close(io); 1621 1622 try copyDir(io, src_repo_dir, dest_repo_dir); 1623 1624 try client_repo.add(io, allocator, &.{dir_name}); 1625 } 1626 1627 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1628 1629 // change the files so git will send them as delta objects 1630 for (&[_][]const u8{"src"}) |dir_name| { 1631 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1632 defer dest_repo_dir.close(io); 1633 1634 { 1635 var iter = dest_repo_dir.iterate(); 1636 while (try iter.next(io)) |entry| { 1637 switch (entry.kind) { 1638 .file => { 1639 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1640 defer file.close(io); 1641 var writer = file.writer(io, &.{}); 1642 try writer.interface.writeAll("EDIT"); 1643 }, 1644 else => {}, 1645 } 1646 } 1647 } 1648 1649 try client_repo.add(io, allocator, &.{dir_name}); 1650 } 1651 1652 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1653 1654 // add remote 1655 { 1656 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1657 defer allocator.free(remote_url); 1658 1659 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1660 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1661 } 1662 1663 const is_ssh = switch (transport_def) { 1664 .file => false, 1665 .wire => |wire_kind| .ssh == wire_kind, 1666 }; 1667 1668 if (shell_out_to_git) { 1669 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1670 defer allocator.free(priv_key_path); 1671 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1672 defer allocator.free(ssh_config_arg); 1673 1674 // shell out to git so it will send delta objects 1675 var process = try std.process.spawn(io, .{ 1676 .argv = if (is_ssh) 1677 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1678 else 1679 &.{ "git", "push", "origin", "master" }, 1680 .cwd = .{ .path = client_path }, 1681 .stdin = .ignore, 1682 .stdout = .ignore, 1683 .stderr = .ignore, 1684 }); 1685 const term = try process.wait(io); 1686 if (term != .exited or term.exited != 0) { 1687 return error.GitCommandFailed; 1688 } 1689 } else { 1690 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1691 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1692 1693 try client_repo.push( 1694 io, 1695 allocator, 1696 "origin", 1697 "master", 1698 false, 1699 .{ .wire = .{ .ssh = .{ 1700 .command = ssh_cmd_maybe, 1701 } } }, 1702 ); 1703 } 1704 1705 // make sure push was successful 1706 { 1707 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1708 try std.testing.expectEqualStrings(&commit2, &oid_master); 1709 1710 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1711 defer hello_txt.close(io); 1712 } 1713 } 1714 1715 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1716 var iter = src_dir.iterate(); 1717 while (try iter.next(io)) |entry| { 1718 switch (entry.kind) { 1719 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1720 .directory => { 1721 try dest_dir.createDirPath(io, entry.name); 1722 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1723 defer dest_entry_dir.close(io); 1724 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1725 defer src_entry_dir.close(io); 1726 try copyDir(io, src_entry_dir, dest_entry_dir); 1727 }, 1728 else => {}, 1729 } 1730 } 1731 } 1732 1733 // the dev SSH public key matching the private key written by runServer 1734 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1735 1736 // create the admin event repo with a user holding the dev SSH public key 1737 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1738 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1739 defer allocator.free(cwd_path); 1740 1741 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1742 defer allocator.free(admin_repo_path); 1743 1744 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1745 defer repo.deinit(io, allocator); 1746 1747 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1748 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1749 1750 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1751 const user_id = evt.EventWithId.randomId(prng.random()); 1752 1753 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1754 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1755 1756 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1757 .id = std.fmt.bytesToHex(user_id, .lower), 1758 .author = .{ .name = "admin", .email = "admin@example.test" }, 1759 .event = .{ .user = .{ 1760 .name = "admin", 1761 .email = "admin@example.test", 1762 .password_hash = password_hash, 1763 .ssh_keys = admin_ssh_key, 1764 } }, 1765 }}); 1766 } 1767 1768 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1769 // the event store 1770 fn repoOnDiskPath( 1771 io: std.Io, 1772 allocator: std.mem.Allocator, 1773 data_dir_name: []const u8, 1774 repo_name: []const u8, 1775 create: bool, 1776 ) !?[]u8 { 1777 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1778 defer allocator.free(cwd_path); 1779 1780 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1781 defer allocator.free(admin_repo_path); 1782 1783 const event_id_hex = (try evt.resolveOrCreateRepo( 1784 io, 1785 allocator, 1786 admin_repo_path, 1787 "admin", 1788 repo_name, 1789 if (create) .{ .read_access = .public } else null, 1790 )) orelse return null; 1791 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1792 } 1793 1794 // build the remote URL addressing admin/<repo_name> over the given transport 1795 fn remoteUrl( 1796 comptime transport_def: net.TransportDefinition, 1797 allocator: std.mem.Allocator, 1798 repo_name: []const u8, 1799 port: u16, 1800 ) ![]u8 { 1801 return switch (transport_def) { 1802 .file => unreachable, 1803 .wire => |wire_kind| switch (wire_kind) { 1804 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1805 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1806 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1807 }, 1808 }; 1809 } 1810 1811 // the ssh command the haxy git client should invoke, or null when not using 1812 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1813 // and skip writing to known_hosts. 1814 fn sshCommand( 1815 is_ssh: bool, 1816 allocator: std.mem.Allocator, 1817 cwd_path: []const u8, 1818 temp_dir_name: []const u8, 1819 port: u16, 1820 ) !?[]u8 { 1821 if (!is_ssh) return null; 1822 1823 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1824 defer allocator.free(priv_key_path); 1825 1826 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1827 } 1828