╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
127 \\ 128 ); 129 if (.windows != builtin.os.tag) { 130 try host_key_file.setPermissions(io, @enumFromInt(0o600)); 131 } 132 133 // create priv client key 134 const priv_key_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/key", .{}); 135 defer priv_key_file.close(io); 136 try priv_key_file.writeStreamingAll(io, 137 \\-----BEGIN OPENSSH PRIVATE KEY----- 138 \\b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 139 \\QyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22JgAAAJA+7hikPu4Y 140 \\pAAAAAtzc2gtZWQyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22Jg 141 \\AAAEDVlopOMnKt/7by/IA8VZvQXUS/O6VLkixOqnnahUdPCKeIs8mJqigBZ5y84J4COgnA 142 \\JJ5bHPKy+lM2SliMXbYmAAAAC3JhZGFyQHJvYXJrAQI= 143 \\-----END OPENSSH PRIVATE KEY----- 144 \\ 145 ); 146 if (.windows != builtin.os.tag) { 147 try priv_key_file.setPermissions(io, @enumFromInt(0o600)); 148 } 149 150 // create pub key 151 const pub_key_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/key.pub", .{}); 152 defer pub_key_file.close(io); 153 try pub_key_file.writeStreamingAll(io, 154 \\ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark 155 \\ 156 ); 157 if (.windows != builtin.os.tag) { 158 try pub_key_file.setPermissions(io, @enumFromInt(0o600)); 159 } 160 161 // create authorized_keys file 162 const auth_keys_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/authorized_keys", .{}); 163 defer auth_keys_file.close(io); 164 try auth_keys_file.writeStreamingAll(io, 165 \\ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark 166 \\ 167 ); 168 if (.windows != builtin.os.tag) { 169 try auth_keys_file.setPermissions(io, @enumFromInt(0o600)); 170 } 171 172 // create known_hosts file 173 const known_hosts_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/known_hosts", .{}); 174 defer known_hosts_file.close(io); 175 const port_str = std.fmt.comptimePrint("{}", .{port}); 176 try known_hosts_file.writeStreamingAll(io, "[localhost]:" ++ port_str ++ " ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLWmlR+TyfvK9UoSDPe1eO3irvpUa6MtxCVHCaiDOi9XjQstxfRpM5tmVBotZ/Mkw2kJr/O0ylCWvzqexqsTiUQ="); 177 if (.windows != builtin.os.tag) { 178 try known_hosts_file.setPermissions(io, @enumFromInt(0o600)); 179 } 180 181 // create sshd_config file 182 const sshd_config_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/sshd_config", .{}); 183 defer sshd_config_file.close(io); 184 try sshd_config_file.writeStreamingAll(io, 185 \\AuthenticationMethods publickey 186 \\PubkeyAuthentication yes 187 \\PasswordAuthentication no 188 \\StrictModes no 189 \\ 190 ); 191 if (.windows != builtin.os.tag) { 192 try sshd_config_file.setPermissions(io, @enumFromInt(0o600)); 193 } 194 195 // create sshd.sh contents 196 const cwd_path = try std.process.currentPathAlloc(io, allocator); 197 defer allocator.free(cwd_path); 198 const host_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "host_key" }); 199 defer allocator.free(host_key_path); 200 const auth_keys_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "authorized_keys" }); 201 defer allocator.free(auth_keys_path); 202 const sshd_contents = try std.fmt.allocPrint( 203 allocator, 204 "#!/bin/sh\nexec $(which sshd) -p {} -f sshd_config -h \"{s}\" -D -e -o AuthorizedKeysFile=\"{s}\"", 205 .{ port, host_key_path, auth_keys_path }, 206 ); 207 defer allocator.free(sshd_contents); 208 209 // if path has a space char, it fucks up sshd 210 try std.testing.expect(null == std.mem.indexOfScalar(u8, auth_keys_path, ' ')); 211 212 // create sshd.sh 213 { 214 const sshd_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/sshd.sh", .{}); 215 defer sshd_file.close(io); 216 try sshd_file.writeStreamingAll(io, sshd_contents); 217 if (.windows != builtin.os.tag) { 218 try sshd_file.setPermissions(io, .executable_file); 219 } 220 } 221 return .{ 222 .core = .{ 223 .io = io, 224 .allocator = allocator, 225 .sshd_process = null, 226 .serve_process = null, 227 }, 228 }; 229 }, 230 }, 231 } 232 } 233 234 fn start(self: *Server(transport_def, temp_dir_name, port)) !void { 235 switch (transport_def) { 236 .file => {}, 237 .wire => |wire_kind| switch (wire_kind) { 238 .http => { 239 std.debug.assert(self.core.process == null); 240 241 const cwd_path = try std.process.currentPathAlloc(self.core.io, self.core.allocator); 242 defer self.core.allocator.free(cwd_path); 243 const haxy_path = try std.fs.path.join(self.core.allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 244 defer self.core.allocator.free(haxy_path); 245 const listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port}); 246 247 self.core.process = try std.process.spawn(self.core.io, .{ 248 .argv = &.{ haxy_path, "serve", "--http-listen", listen_arg, "--data-dir", temp_dir_name }, 249 .stdin = .ignore, 250 .stdout = .ignore, 251 .stderr = .ignore, 252 }); 253 }, 254 .raw => unreachable, 255 .ssh => { 256 std.debug.assert(self.core.sshd_process == null); 257 std.debug.assert(self.core.serve_process == null); 258 259 const cwd_path = try std.process.currentPathAlloc(self.core.io, self.core.allocator); 260 defer self.core.allocator.free(cwd_path); 261 const haxy_path = try std.fs.path.join(self.core.allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 262 defer self.core.allocator.free(haxy_path); 263 const ssh_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port + 1000}); 264 const http_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port + 2000}); 265 266 self.core.serve_process = try std.process.spawn(self.core.io, .{ 267 .argv = &.{ haxy_path, "serve", "--http-listen", http_listen_arg, "--ssh-listen", ssh_listen_arg, "--data-dir", temp_dir_name }, 268 .stdin = .ignore, 269 .stdout = .ignore, 270 .stderr = .ignore, 271 }); 272 273 const serve_address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", port + 1000); 274 for (0..50) |_| { 275 const stream = serve_address.connect(self.core.io, .{ .mode = .stream }) catch { 276 try std.Io.sleep(self.core.io, .fromMilliseconds(100), .real); 277 continue; 278 }; 279 var probe_buf = [_]u8{0} ** 16; 280 var probe_writer = stream.writer(self.core.io, &probe_buf); 281 try probe_writer.interface.writeAll("probe\n"); 282 try probe_writer.interface.flush(); 283 stream.close(self.core.io); 284 break; 285 } 286 287 self.core.sshd_process = try std.process.spawn(self.core.io, .{ 288 .argv = &.{"./sshd.sh"}, 289 .cwd = .{ .path = temp_dir_name }, 290 .stdin = .pipe, 291 .stdout = .ignore, 292 .stderr = .ignore, 293 }); 294 }, 295 }, 296 } 297 298 if (transport_def == .wire) { 299 const address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", port); 300 for (0..50) |_| { 301 const stream = address.connect(self.core.io, .{ .mode = .stream }) catch { 302 try std.Io.sleep(self.core.io, .fromMilliseconds(100), .real); 303 continue; 304 }; 305 stream.close(self.core.io); 306 break; 307 } 308 } 309 } 310 311 fn stop(self: *Server(transport_def, temp_dir_name, port)) void { 312 switch (transport_def) { 313 .file => {}, 314 .wire => |wire_kind| switch (wire_kind) { 315 .http => if (self.core.process) |*process| { 316 _ = process.kill(self.core.io); 317 self.core.process = null; 318 }, 319 .raw => unreachable, 320 .ssh => { 321 if (self.core.sshd_process) |*process| { 322 _ = process.kill(self.core.io); 323 self.core.sshd_process = null; 324 } 325 if (self.core.serve_process) |*process| { 326 _ = process.kill(self.core.io); 327 self.core.serve_process = null; 328 } 329 }, 330 }, 331 } 332 } 333 }; 334 } 335 336 fn uploadPackCommand( 337 comptime is_ssh: bool, 338 allocator: std.mem.Allocator, 339 cwd_path: []const u8, 340 comptime port: u16, 341 ) ![]const u8 { 342 return if (is_ssh) 343 std.fmt.allocPrint(allocator, "{s}/zig-out/bin/haxy ssh-helper --ssh-connect 127.0.0.1:{} --service upload-pack", .{ cwd_path, port + 1000 }) 344 else 345 allocator.dupe(u8, "git-upload-pack"); 346 } 347 348 fn receivePackCommand( 349 comptime is_ssh: bool, 350 allocator: std.mem.Allocator, 351 cwd_path: []const u8, 352 comptime port: u16, 353 ) ![]const u8 { 354 return if (is_ssh) 355 std.fmt.allocPrint(allocator, "{s}/zig-out/bin/haxy ssh-helper --ssh-connect 127.0.0.1:{} --service receive-pack", .{ cwd_path, port + 1000 }) 356 else 357 allocator.dupe(u8, "git-receive-pack"); 358 } 359 360 fn testFetch( 361 comptime repo_kind: rp.RepoKind, 362 comptime transport_def: net.TransportDefinition, 363 comptime port: u16, 364 io: std.Io, 365 allocator: std.mem.Allocator, 366 ) !void { 367 const temp_dir_name = "temp-testnet-fetch"; 368 369 // create the temp dir 370 const cwd = std.Io.Dir.cwd(); 371 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 372 if (temp_dir_or_err) |*temp_dir| { 373 temp_dir.close(io); 374 try cwd.deleteTree(io, temp_dir_name); 375 } else |_| {} 376 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 377 defer cwd.deleteTree(io, temp_dir_name) catch {}; 378 defer temp_dir.close(io); 379 380 // init server 381 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 382 try server.start(); 383 defer server.stop(); 384 385 const cwd_path = try std.process.currentPathAlloc(io, allocator); 386 defer allocator.free(cwd_path); 387 388 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 389 defer allocator.free(server_path); 390 391 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 392 defer server_repo.deinit(io, allocator); 393 394 // make a commit 395 const commit1 = blk: { 396 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 397 defer hello_txt.close(io); 398 try hello_txt.writeStreamingAll(io, "hello, world!"); 399 try server_repo.add(io, allocator, &.{"hello.txt"}); 400 break :blk try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 401 }; 402 403 // export server repo 404 { 405 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 406 defer export_file.close(io); 407 408 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowAnySHA1InWant", .value = "true" }); 409 } 410 411 // add a tag 412 _ = try server_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 413 414 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 415 defer allocator.free(client_path); 416 417 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 418 defer client_repo.deinit(io, allocator); 419 420 // add remote 421 { 422 if (.windows == builtin.os.tag) { 423 std.mem.replaceScalar(u8, server_path, '\\', '/'); 424 } 425 const separator = if (server_path[0] == '/') "" else "/"; 426 427 const remote_url = switch (transport_def) { 428 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 429 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 430 .wire => |wire_kind| switch (wire_kind) { 431 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 432 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 433 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 434 }, 435 }; 436 defer allocator.free(remote_url); 437 438 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 439 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 440 } 441 442 // create refspec with oid as a test 443 const oid_refspec = try std.fmt.allocPrint(allocator, "+{s}:refs/heads/foo", .{&commit1}); 444 defer allocator.free(oid_refspec); 445 446 const refspecs = &.{ 447 "+refs/heads/master:refs/heads/master", 448 oid_refspec, 449 }; 450 451 const is_ssh = switch (transport_def) { 452 .file => false, 453 .wire => |wire_kind| .ssh == wire_kind, 454 }; 455 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 456 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 457 defer allocator.free(known_hosts_path); 458 459 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 460 defer allocator.free(priv_key_path); 461 462 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 463 } else null; 464 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 465 466 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 467 defer allocator.free(upload_pack_command); 468 469 try client_repo.fetch( 470 io, 471 allocator, 472 "origin", 473 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 474 .command = ssh_cmd_maybe, 475 .upload_pack_command = upload_pack_command, 476 } } }, 477 ); 478 479 // update the working dir 480 try client_repo.restore(io, allocator, "."); 481 482 // make sure fetch was successful 483 { 484 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 485 defer hello_txt.close(io); 486 487 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 488 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 489 490 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 491 try std.testing.expectEqualStrings(&commit1, &oid_master); 492 } 493 494 // make another commit 495 const commit2 = blk: { 496 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 497 defer goodbye_txt.close(io); 498 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 499 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 500 break :blk try server_repo.commit(io, allocator, .{ .message = "goodbye" }); 501 }; 502 503 try client_repo.fetch( 504 io, 505 allocator, 506 "origin", 507 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 508 .command = ssh_cmd_maybe, 509 .upload_pack_command = upload_pack_command, 510 } } }, 511 ); 512 513 // update the working dir 514 try client_repo.restore(io, allocator, "."); 515 516 // make sure fetch was successful 517 { 518 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 519 defer goodbye_txt.close(io); 520 521 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 522 try std.testing.expectEqualStrings(&commit2, &oid_master); 523 } 524 } 525 526 fn testPush( 527 comptime repo_kind: rp.RepoKind, 528 comptime transport_def: net.TransportDefinition, 529 comptime port: u16, 530 io: std.Io, 531 allocator: std.mem.Allocator, 532 ) !void { 533 const temp_dir_name = "temp-testnet-push"; 534 535 // create the temp dir 536 const cwd = std.Io.Dir.cwd(); 537 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 538 if (temp_dir_or_err) |*temp_dir| { 539 temp_dir.close(io); 540 try cwd.deleteTree(io, temp_dir_name); 541 } else |_| {} 542 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 543 defer cwd.deleteTree(io, temp_dir_name) catch {}; 544 defer temp_dir.close(io); 545 546 // init server 547 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 548 try server.start(); 549 defer server.stop(); 550 551 const cwd_path = try std.process.currentPathAlloc(io, allocator); 552 defer allocator.free(cwd_path); 553 554 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 555 defer allocator.free(server_path); 556 557 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 558 defer server_repo.deinit(io, allocator); 559 560 // add config 561 switch (transport_def) { 562 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 563 .wire => { 564 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 565 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 566 }, 567 } 568 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 569 570 // export server repo 571 { 572 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 573 defer export_file.close(io); 574 } 575 576 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 577 defer allocator.free(client_path); 578 579 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 580 defer client_repo.deinit(io, allocator); 581 582 // make a commit 583 const commit1 = blk: { 584 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 585 defer hello_txt.close(io); 586 try hello_txt.writeStreamingAll(io, "hello, world!"); 587 try client_repo.add(io, allocator, &.{"hello.txt"}); 588 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 589 }; 590 591 // add a tag 592 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 593 594 // add remote 595 { 596 if (.windows == builtin.os.tag) { 597 std.mem.replaceScalar(u8, server_path, '\\', '/'); 598 } 599 const separator = if (server_path[0] == '/') "" else "/"; 600 601 const remote_url = switch (transport_def) { 602 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 603 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 604 .wire => |wire_kind| switch (wire_kind) { 605 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 606 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 607 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 608 }, 609 }; 610 defer allocator.free(remote_url); 611 612 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 613 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 614 } 615 616 const refspecs = &.{ 617 "refs/tags/1.0.0:refs/tags/1.0.0", 618 }; 619 620 const is_ssh = switch (transport_def) { 621 .file => false, 622 .wire => |wire_kind| .ssh == wire_kind, 623 }; 624 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 625 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 626 defer allocator.free(known_hosts_path); 627 628 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 629 defer allocator.free(priv_key_path); 630 631 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 632 } else null; 633 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 634 635 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 636 defer allocator.free(upload_pack_command); 637 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 638 defer allocator.free(receive_pack_command); 639 640 try client_repo.push( 641 io, 642 allocator, 643 "origin", 644 "master", 645 false, 646 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 647 .command = ssh_cmd_maybe, 648 .receive_pack_command = receive_pack_command, 649 } } }, 650 ); 651 652 // make sure push was successful 653 { 654 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 655 656 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 657 try std.testing.expectEqualStrings(&commit1, &oid_master); 658 } 659 660 // make a commit on the server 661 { 662 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 663 defer hello_txt.close(io); 664 try hello_txt.writeStreamingAll(io, "hello, world from the server!"); 665 try server_repo.add(io, allocator, &.{"hello.txt"}); 666 _ = try server_repo.commit(io, allocator, .{ .message = "new commit from the server" }); 667 } 668 669 // make another commit 670 const commit2 = blk: { 671 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 672 defer goodbye_txt.close(io); 673 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 674 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 675 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 676 }; 677 678 // can't push because server has commit not found locally 679 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 680 io, 681 allocator, 682 "origin", 683 "master", 684 false, 685 .{ .wire = .{ .ssh = .{ 686 .command = ssh_cmd_maybe, 687 .receive_pack_command = receive_pack_command, 688 } } }, 689 )); 690 691 // make a commit on the server with no parents, thus creating an incompatible git history 692 { 693 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 694 defer hello_txt.close(io); 695 try hello_txt.writeStreamingAll(io, "hello, world from the server again!"); 696 try server_repo.add(io, allocator, &.{"hello.txt"}); 697 _ = try server_repo.commit(io, allocator, .{ .message = "new git history on the server", .parent_oids = &.{} }); 698 } 699 700 // can't push because commit doesn't exist locally 701 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 702 io, 703 allocator, 704 "origin", 705 "master", 706 false, 707 .{ .wire = .{ .ssh = .{ 708 .command = ssh_cmd_maybe, 709 .receive_pack_command = receive_pack_command, 710 } } }, 711 )); 712 713 // retrieve the commit object 714 try client_repo.fetch( 715 io, 716 allocator, 717 "origin", 718 .{ .wire = .{ .ssh = .{ 719 .command = ssh_cmd_maybe, 720 .upload_pack_command = upload_pack_command, 721 } } }, 722 ); 723 724 // can't push because server's history is incompatible 725 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 726 io, 727 allocator, 728 "origin", 729 "master", 730 false, 731 .{ .wire = .{ .ssh = .{ 732 .command = ssh_cmd_maybe, 733 .receive_pack_command = receive_pack_command, 734 } } }, 735 )); 736 737 // test denyNonFastForwards (only for wire transports, file transport bypasses receive-pack) 738 switch (transport_def) { 739 .file => {}, 740 .wire => { 741 // set denyNonFastForwards on server 742 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 743 744 // save the server's current master ref 745 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 746 747 // force push should be rejected by server due to denyNonFastForwards 748 try client_repo.push( 749 io, 750 allocator, 751 "origin", 752 "master", 753 true, 754 .{ .wire = .{ .ssh = .{ 755 .command = ssh_cmd_maybe, 756 .receive_pack_command = receive_pack_command, 757 } } }, 758 ); 759 760 // verify the server ref was not updated (push was denied) 761 { 762 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 763 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 764 } 765 766 // remove denyNonFastForwards from server 767 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 768 }, 769 } 770 771 // force push 772 try client_repo.push( 773 io, 774 allocator, 775 "origin", 776 "master", 777 true, 778 .{ .wire = .{ .ssh = .{ 779 .command = ssh_cmd_maybe, 780 .receive_pack_command = receive_pack_command, 781 } } }, 782 ); 783 784 // make sure push was successful 785 { 786 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 787 try std.testing.expectEqualStrings(&commit2, &oid_master); 788 } 789 790 // remove the remote tag 791 try client_repo.push( 792 io, 793 allocator, 794 "origin", 795 ":refs/tags/1.0.0", 796 false, 797 .{ .wire = .{ .ssh = .{ 798 .command = ssh_cmd_maybe, 799 .receive_pack_command = receive_pack_command, 800 } } }, 801 ); 802 803 // make sure push was successful 804 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 805 } 806 807 fn testPushCreatesMissingRepo( 808 comptime repo_kind: rp.RepoKind, 809 comptime transport_def: net.TransportDefinition, 810 comptime port: u16, 811 io: std.Io, 812 allocator: std.mem.Allocator, 813 ) !void { 814 const temp_dir_name = "temp-testnet-push-create"; 815 816 const cwd = std.Io.Dir.cwd(); 817 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 818 if (temp_dir_or_err) |*temp_dir| { 819 temp_dir.close(io); 820 try cwd.deleteTree(io, temp_dir_name); 821 } else |_| {} 822 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 823 defer cwd.deleteTree(io, temp_dir_name) catch {}; 824 defer temp_dir.close(io); 825 826 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 827 try server.start(); 828 defer server.stop(); 829 830 const cwd_path = try std.process.currentPathAlloc(io, allocator); 831 defer allocator.free(cwd_path); 832 833 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 834 defer allocator.free(server_path); 835 836 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 837 defer allocator.free(client_path); 838 839 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 840 defer client_repo.deinit(io, allocator); 841 842 const commit1 = blk: { 843 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 844 defer hello_txt.close(io); 845 try hello_txt.writeStreamingAll(io, "hello, world!"); 846 try client_repo.add(io, allocator, &.{"hello.txt"}); 847 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 848 }; 849 850 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 851 852 { 853 if (.windows == builtin.os.tag) { 854 std.mem.replaceScalar(u8, server_path, '\\', '/'); 855 } 856 const separator = if (server_path[0] == '/') "" else "/"; 857 858 const remote_url = switch (transport_def) { 859 .file => unreachable, 860 .wire => |wire_kind| switch (wire_kind) { 861 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 862 .raw => unreachable, 863 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 864 }, 865 }; 866 defer allocator.free(remote_url); 867 868 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 869 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 870 } 871 872 const is_ssh = switch (transport_def) { 873 .file => false, 874 .wire => |wire_kind| .ssh == wire_kind, 875 }; 876 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 877 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 878 defer allocator.free(known_hosts_path); 879 880 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 881 defer allocator.free(priv_key_path); 882 883 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 884 } else null; 885 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 886 887 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 888 defer allocator.free(receive_pack_command); 889 890 try client_repo.push( 891 io, 892 allocator, 893 "origin", 894 "master", 895 false, 896 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 897 .command = ssh_cmd_maybe, 898 .receive_pack_command = receive_pack_command, 899 } } }, 900 ); 901 902 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 903 defer server_repo.deinit(io, allocator); 904 905 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 906 907 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 908 try std.testing.expectEqualStrings(&commit1, &oid_master); 909 } 910 911 fn testClone( 912 comptime repo_kind: rp.RepoKind, 913 comptime transport_def: net.TransportDefinition, 914 comptime port: u16, 915 comptime shell_out_to_git: bool, 916 io: std.Io, 917 allocator: std.mem.Allocator, 918 ) !void { 919 const temp_dir_name = "temp-testnet-clone"; 920 921 // create the temp dir 922 const cwd = std.Io.Dir.cwd(); 923 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 924 if (temp_dir_or_err) |*temp_dir| { 925 temp_dir.close(io); 926 try cwd.deleteTree(io, temp_dir_name); 927 } else |_| {} 928 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 929 defer cwd.deleteTree(io, temp_dir_name) catch {}; 930 defer temp_dir.close(io); 931 932 // init server 933 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 934 try server.start(); 935 defer server.stop(); 936 937 const cwd_path = try std.process.currentPathAlloc(io, allocator); 938 defer allocator.free(cwd_path); 939 940 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 941 defer allocator.free(temp_path); 942 943 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 944 defer allocator.free(server_path); 945 946 // init server repo with default branch name as main 947 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 948 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 949 defer server_repo.deinit(io, allocator); 950 951 if (shell_out_to_git) { 952 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 953 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 954 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 955 } 956 957 // make a commit 958 { 959 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 960 defer hello_txt.close(io); 961 try hello_txt.writeStreamingAll(io, "hello, world!"); 962 try server_repo.add(io, allocator, &.{"hello.txt"}); 963 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 964 } 965 966 // tag first commit 967 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 968 969 // make a commit 970 { 971 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 972 defer goodbye_txt.close(io); 973 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 974 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 975 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 976 } 977 978 // export server repo 979 { 980 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 981 defer export_file.close(io); 982 } 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 // get remote url 988 const remote_url = blk: { 989 if (.windows == builtin.os.tag) { 990 std.mem.replaceScalar(u8, server_path, '\\', '/'); 991 } 992 const separator = if (server_path[0] == '/') "" else "/"; 993 994 break :blk switch (transport_def) { 995 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 996 .file => try std.fmt.allocPrint(allocator, "server", .{}), // relative file paths work too 997 .wire => |wire_kind| switch (wire_kind) { 998 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 999 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1000 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1001 }, 1002 }; 1003 }; 1004 defer allocator.free(remote_url); 1005 1006 const is_ssh = switch (transport_def) { 1007 .file => false, 1008 .wire => |wire_kind| .ssh == wire_kind, 1009 }; 1010 1011 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 1012 defer allocator.free(upload_pack_command); 1013 1014 if (shell_out_to_git) { 1015 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1016 defer allocator.free(priv_key_path); 1017 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1018 defer allocator.free(ssh_config_arg); 1019 1020 { 1021 var process = try std.process.spawn(io, .{ 1022 .argv = if (is_ssh) 1023 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--depth", "1", remote_url, "client" } 1024 else 1025 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 1026 .cwd = .{ .path = temp_path }, 1027 .stdin = .ignore, 1028 .stdout = .ignore, 1029 .stderr = .ignore, 1030 }); 1031 const term = try process.wait(io); 1032 if (term != .exited) { 1033 return error.GitCommandFailed; 1034 } 1035 } 1036 1037 // make sure shallow clone was successful 1038 { 1039 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1040 hello_txt.close(io); 1041 } 1042 1043 // make a third commit on the server 1044 { 1045 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1046 defer extra_txt.close(io); 1047 try extra_txt.writeStreamingAll(io, "extra content"); 1048 try server_repo.add(io, allocator, &.{"extra.txt"}); 1049 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1050 } 1051 1052 // pull --unshallow to deepen the clone and get the new commit 1053 { 1054 var process = try std.process.spawn(io, .{ 1055 .argv = if (is_ssh) 1056 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "--unshallow" } 1057 else 1058 &.{ "git", "pull", "--unshallow" }, 1059 .cwd = .{ .path = client_path }, 1060 .stdin = .ignore, 1061 .stdout = .ignore, 1062 .stderr = .ignore, 1063 }); 1064 const term = try process.wait(io); 1065 if (term != .exited) { 1066 return error.GitCommandFailed; 1067 } 1068 } 1069 1070 // make sure unshallow pull was successful 1071 { 1072 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 1073 extra_txt.close(io); 1074 } 1075 1076 // delete client and clone again with --shallow-since 1077 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1078 1079 { 1080 var process = try std.process.spawn(io, .{ 1081 .argv = if (is_ssh) 1082 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--shallow-since=2000-01-01", remote_url, "client" } 1083 else 1084 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 1085 .cwd = .{ .path = temp_path }, 1086 .stdin = .ignore, 1087 .stdout = .ignore, 1088 .stderr = .ignore, 1089 }); 1090 const term = try process.wait(io); 1091 if (term != .exited) { 1092 return error.GitCommandFailed; 1093 } 1094 } 1095 1096 // make sure shallow clone was successful 1097 { 1098 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1099 hello_txt.close(io); 1100 } 1101 1102 // delete client and clone again with --shallow-exclude 1103 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1104 1105 { 1106 var process = try std.process.spawn(io, .{ 1107 .argv = if (is_ssh) 1108 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--shallow-exclude=v1", remote_url, "client" } 1109 else 1110 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 1111 .cwd = .{ .path = temp_path }, 1112 .stdin = .ignore, 1113 .stdout = .ignore, 1114 .stderr = .ignore, 1115 }); 1116 const term = try process.wait(io); 1117 if (term != .exited) { 1118 return error.GitCommandFailed; 1119 } 1120 } 1121 1122 // make sure shallow clone was successful 1123 { 1124 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1125 hello_txt.close(io); 1126 } 1127 1128 // delete client and clone again with --filter=blob:none 1129 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1130 1131 { 1132 var process = try std.process.spawn(io, .{ 1133 .argv = if (is_ssh) 1134 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--filter=blob:none", remote_url, "client" } 1135 else 1136 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 1137 .cwd = .{ .path = temp_path }, 1138 .stdin = .ignore, 1139 .stdout = .ignore, 1140 .stderr = .ignore, 1141 }); 1142 const term = try process.wait(io); 1143 if (term != .exited) { 1144 return error.GitCommandFailed; 1145 } 1146 } 1147 1148 // make sure partial clone was successful 1149 { 1150 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1151 hello_txt.close(io); 1152 } 1153 1154 // delete client and clone again with --filter=tree:0 1155 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 1156 1157 { 1158 var process = try std.process.spawn(io, .{ 1159 .argv = if (is_ssh) 1160 &.{ "git", "-c", ssh_config_arg, "clone", "--upload-pack", upload_pack_command, "--filter=tree:0", remote_url, "client" } 1161 else 1162 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 1163 .cwd = .{ .path = temp_path }, 1164 .stdin = .ignore, 1165 .stdout = .ignore, 1166 .stderr = .ignore, 1167 }); 1168 const term = try process.wait(io); 1169 if (term != .exited) { 1170 return error.GitCommandFailed; 1171 } 1172 } 1173 1174 // make sure treeless clone was successful 1175 { 1176 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1177 goodbye_txt.close(io); 1178 } 1179 } else { 1180 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1181 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1182 defer allocator.free(known_hosts_path); 1183 1184 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1185 defer allocator.free(priv_key_path); 1186 1187 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1188 } else null; 1189 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1190 1191 // clone repo 1192 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1193 io, 1194 allocator, 1195 remote_url, 1196 temp_path, 1197 client_path, 1198 .{ .wire = .{ .ssh = .{ 1199 .command = ssh_cmd_maybe, 1200 .upload_pack_command = upload_pack_command, 1201 } } }, 1202 ); 1203 defer client_repo.deinit(io, allocator); 1204 1205 // make sure HEAD points to the right default branch 1206 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1207 const head = try client_repo.head(io, &current_branch_buffer); 1208 try std.testing.expectEqualStrings("main", head.ref.name); 1209 1210 // make sure clone was successful 1211 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1212 defer hello_txt.close(io); 1213 } 1214 } 1215 1216 fn testFetchLarge( 1217 comptime repo_kind: rp.RepoKind, 1218 comptime transport_def: net.TransportDefinition, 1219 comptime port: u16, 1220 comptime shell_out_to_git: bool, 1221 io: std.Io, 1222 allocator: std.mem.Allocator, 1223 ) !void { 1224 const temp_dir_name = "temp-testnet-fetch-large"; 1225 1226 // create the temp dir 1227 const cwd = std.Io.Dir.cwd(); 1228 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1229 if (temp_dir_or_err) |*temp_dir| { 1230 temp_dir.close(io); 1231 try cwd.deleteTree(io, temp_dir_name); 1232 } else |_| {} 1233 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1234 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1235 defer temp_dir.close(io); 1236 1237 // init server 1238 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1239 try server.start(); 1240 defer server.stop(); 1241 1242 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1243 defer allocator.free(cwd_path); 1244 1245 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1246 defer allocator.free(server_path); 1247 1248 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1249 defer server_repo.deinit(io, allocator); 1250 1251 var server_dir = try cwd.openDir(io, server_path, .{}); 1252 defer server_dir.close(io); 1253 1254 // copy files from current repo into server dir 1255 for (&[_][]const u8{"src"}) |dir_name| { 1256 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1257 defer src_repo_dir.close(io); 1258 1259 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1260 defer dest_repo_dir.close(io); 1261 1262 try copyDir(io, src_repo_dir, dest_repo_dir); 1263 1264 try server_repo.add(io, allocator, &.{dir_name}); 1265 } 1266 1267 // make a commit 1268 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1269 1270 // export server repo 1271 { 1272 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1273 defer export_file.close(io); 1274 } 1275 1276 if (shell_out_to_git) { 1277 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1278 } 1279 1280 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1281 defer allocator.free(client_path); 1282 1283 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1284 defer client_repo.deinit(io, allocator); 1285 1286 // add remote 1287 { 1288 if (.windows == builtin.os.tag) { 1289 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1290 } 1291 const separator = if (server_path[0] == '/') "" else "/"; 1292 1293 const remote_url = switch (transport_def) { 1294 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1295 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1296 .wire => |wire_kind| switch (wire_kind) { 1297 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1298 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1299 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1300 }, 1301 }; 1302 defer allocator.free(remote_url); 1303 1304 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1305 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1306 } 1307 1308 const is_ssh = switch (transport_def) { 1309 .file => false, 1310 .wire => |wire_kind| .ssh == wire_kind, 1311 }; 1312 1313 const upload_pack_command = try uploadPackCommand(is_ssh, allocator, cwd_path, port); 1314 defer allocator.free(upload_pack_command); 1315 1316 if (shell_out_to_git) { 1317 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1318 defer allocator.free(priv_key_path); 1319 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1320 defer allocator.free(ssh_config_arg); 1321 1322 { 1323 var process = try std.process.spawn(io, .{ 1324 .argv = if (is_ssh) 1325 &.{ "git", "-c", ssh_config_arg, "pull", "--upload-pack", upload_pack_command, "origin", "master" } 1326 else 1327 &.{ "git", "pull", "origin", "master" }, 1328 .cwd = .{ .path = client_path }, 1329 .stdin = .ignore, 1330 .stdout = .ignore, 1331 .stderr = .ignore, 1332 }); 1333 const term = try process.wait(io); 1334 if (term != .exited) { 1335 return error.GitCommandFailed; 1336 } 1337 } 1338 1339 // make sure pull was successful 1340 { 1341 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1342 try std.testing.expectEqualStrings(&commit1, &oid_master); 1343 } 1344 1345 // make another commit on the server 1346 const commit2 = blk: { 1347 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1348 defer extra_txt.close(io); 1349 try extra_txt.writeStreamingAll(io, "extra content"); 1350 try server_repo.add(io, allocator, &.{"extra.txt"}); 1351 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1352 }; 1353 1354 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1355 { 1356 var process = try std.process.spawn(io, .{ 1357 .argv = if (is_ssh) 1358 &.{ "git", "-c", ssh_config_arg, "fetch", "--upload-pack", upload_pack_command, "origin", "master" } 1359 else 1360 &.{ "git", "fetch", "origin", "master" }, 1361 .cwd = .{ .path = client_path }, 1362 .stdin = .ignore, 1363 .stdout = .ignore, 1364 .stderr = .ignore, 1365 }); 1366 const term = try process.wait(io); 1367 if (term != .exited) { 1368 return error.GitCommandFailed; 1369 } 1370 } 1371 1372 // make sure fetch with want-ref was successful 1373 { 1374 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1375 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1376 } 1377 } else { 1378 const refspecs = &.{ 1379 "+refs/heads/master:refs/heads/master", 1380 }; 1381 1382 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1383 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1384 defer allocator.free(known_hosts_path); 1385 1386 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1387 defer allocator.free(priv_key_path); 1388 1389 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1390 } else null; 1391 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1392 1393 try client_repo.fetch( 1394 io, 1395 allocator, 1396 "origin", 1397 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1398 .command = ssh_cmd_maybe, 1399 .upload_pack_command = upload_pack_command, 1400 } } }, 1401 ); 1402 1403 // update the working dir 1404 try client_repo.restore(io, allocator, "."); 1405 } 1406 1407 // make sure fetch was successful 1408 { 1409 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1410 try std.testing.expectEqualStrings(&commit1, &oid_master); 1411 } 1412 } 1413 1414 fn testPushLarge( 1415 comptime repo_kind: rp.RepoKind, 1416 comptime transport_def: net.TransportDefinition, 1417 comptime port: u16, 1418 comptime shell_out_to_git: bool, 1419 io: std.Io, 1420 allocator: std.mem.Allocator, 1421 ) !void { 1422 const temp_dir_name = "temp-testnet-push-large"; 1423 1424 // create the temp dir 1425 const cwd = std.Io.Dir.cwd(); 1426 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1427 if (temp_dir_or_err) |*temp_dir| { 1428 temp_dir.close(io); 1429 try cwd.deleteTree(io, temp_dir_name); 1430 } else |_| {} 1431 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1432 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1433 defer temp_dir.close(io); 1434 1435 // init server 1436 var server = try Server(transport_def, temp_dir_name, port).init(io, allocator); 1437 try server.start(); 1438 defer server.stop(); 1439 1440 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1441 defer allocator.free(cwd_path); 1442 1443 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "repos", "server" }); 1444 defer allocator.free(server_path); 1445 1446 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1447 defer server_repo.deinit(io, allocator); 1448 1449 // add config 1450 switch (transport_def) { 1451 .file => try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }), 1452 .wire => { 1453 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1454 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1455 }, 1456 } 1457 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1458 1459 // export server repo 1460 { 1461 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1462 defer export_file.close(io); 1463 } 1464 1465 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1466 defer allocator.free(client_path); 1467 1468 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1469 defer client_repo.deinit(io, allocator); 1470 1471 var client_dir = try cwd.openDir(io, client_path, .{}); 1472 defer client_dir.close(io); 1473 1474 { 1475 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1476 defer hello_txt.close(io); 1477 try hello_txt.writeStreamingAll(io, "hello, world!"); 1478 try client_repo.add(io, allocator, &.{"hello.txt"}); 1479 } 1480 1481 // copy files from current repo into client dir 1482 for (&[_][]const u8{"src"}) |dir_name| { 1483 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1484 defer src_repo_dir.close(io); 1485 1486 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1487 defer dest_repo_dir.close(io); 1488 1489 try copyDir(io, src_repo_dir, dest_repo_dir); 1490 1491 try client_repo.add(io, allocator, &.{dir_name}); 1492 } 1493 1494 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1495 1496 // change the files so git will send them as delta objects 1497 for (&[_][]const u8{"src"}) |dir_name| { 1498 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1499 defer dest_repo_dir.close(io); 1500 1501 { 1502 var iter = dest_repo_dir.iterate(); 1503 while (try iter.next(io)) |entry| { 1504 switch (entry.kind) { 1505 .file => { 1506 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1507 defer file.close(io); 1508 var writer = file.writer(io, &.{}); 1509 try writer.interface.writeAll("EDIT"); 1510 }, 1511 else => {}, 1512 } 1513 } 1514 } 1515 1516 try client_repo.add(io, allocator, &.{dir_name}); 1517 } 1518 1519 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1520 1521 // add remote 1522 { 1523 if (.windows == builtin.os.tag) { 1524 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1525 } 1526 const separator = if (server_path[0] == '/') "" else "/"; 1527 1528 const remote_url = switch (transport_def) { 1529 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1530 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1531 .wire => |wire_kind| switch (wire_kind) { 1532 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1533 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1534 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1535 }, 1536 }; 1537 defer allocator.free(remote_url); 1538 1539 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1540 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1541 } 1542 1543 const is_ssh = switch (transport_def) { 1544 .file => false, 1545 .wire => |wire_kind| .ssh == wire_kind, 1546 }; 1547 1548 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 1549 defer allocator.free(receive_pack_command); 1550 1551 if (shell_out_to_git) { 1552 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1553 defer allocator.free(priv_key_path); 1554 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1555 defer allocator.free(ssh_config_arg); 1556 1557 // shell out to git so it will send delta objects 1558 var process = try std.process.spawn(io, .{ 1559 .argv = if (is_ssh) 1560 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" } 1561 else 1562 &.{ "git", "push", "origin", "master" }, 1563 .cwd = .{ .path = client_path }, 1564 .stdin = .ignore, 1565 .stdout = .ignore, 1566 .stderr = .ignore, 1567 }); 1568 const term = try process.wait(io); 1569 if (term != .exited) { 1570 return error.GitCommandFailed; 1571 } 1572 } else { 1573 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1574 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1575 defer allocator.free(known_hosts_path); 1576 1577 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1578 defer allocator.free(priv_key_path); 1579 1580 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1581 } else null; 1582 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1583 1584 try client_repo.push( 1585 io, 1586 allocator, 1587 "origin", 1588 "master", 1589 false, 1590 .{ .wire = .{ .ssh = .{ 1591 .command = ssh_cmd_maybe, 1592 .receive_pack_command = receive_pack_command, 1593 } } }, 1594 ); 1595 1596 if (transport_def == .file) { 1597 // update the working dir 1598 try server_repo.restore(io, allocator, "."); 1599 } 1600 } 1601 1602 // make sure push was successful 1603 { 1604 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1605 try std.testing.expectEqualStrings(&commit2, &oid_master); 1606 1607 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1608 defer hello_txt.close(io); 1609 } 1610 } 1611 1612 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1613 var iter = src_dir.iterate(); 1614 while (try iter.next(io)) |entry| { 1615 switch (entry.kind) { 1616 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1617 .directory => { 1618 try dest_dir.createDirPath(io, entry.name); 1619 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1620 defer dest_entry_dir.close(io); 1621 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1622 defer src_entry_dir.close(io); 1623 try copyDir(io, src_entry_dir, dest_entry_dir); 1624 }, 1625 else => {}, 1626 } 1627 } 1628 } 1629