╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
103 defer allocator.free(cwd_path); 104 const haxy_path = try std.fs.path.join(allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 105 defer allocator.free(haxy_path); 106 107 const http_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{http_port}); 108 const ssh_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{ssh_port}); 109 110 const process = try std.process.spawn(io, .{ 111 .argv = &.{ haxy_path, "serve", "--http-listen", http_listen_arg, "--ssh-listen", ssh_listen_arg, "--data-dir", temp_dir_name }, 112 .stdin = .ignore, 113 .stdout = .ignore, 114 .stderr = .ignore, 115 }); 116 117 const address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", http_port); 118 for (0..50) |_| { 119 const stream = address.connect(io, .{ .mode = .stream }) catch { 120 try std.Io.sleep(io, .fromMilliseconds(100), .real); 121 continue; 122 }; 123 stream.close(io); 124 break; 125 } 126 127 return process; 128 } 129 130 fn testFetch( 131 comptime repo_kind: rp.RepoKind, 132 comptime transport_def: net.TransportDefinition, 133 io: std.Io, 134 allocator: std.mem.Allocator, 135 ) !void { 136 const temp_dir_name = "temp-testnet-fetch"; 137 138 // create the temp dir 139 const cwd = std.Io.Dir.cwd(); 140 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 141 if (temp_dir_or_err) |*temp_dir| { 142 temp_dir.close(io); 143 try cwd.deleteTree(io, temp_dir_name); 144 } else |_| {} 145 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 146 defer cwd.deleteTree(io, temp_dir_name) catch {}; 147 defer temp_dir.close(io); 148 149 // init server 150 var server_process = try runServer(io, allocator, temp_dir_name); 151 defer _ = server_process.kill(io); 152 153 // register the repo under admin and locate its on-disk directory 154 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 155 defer allocator.free(server_path); 156 157 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 158 defer server_repo.deinit(io, allocator); 159 160 // make a commit 161 const commit1 = blk: { 162 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 163 defer hello_txt.close(io); 164 try hello_txt.writeStreamingAll(io, "hello, world!"); 165 try server_repo.add(io, allocator, &.{"hello.txt"}); 166 break :blk try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 167 }; 168 169 // export server repo 170 { 171 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 172 defer export_file.close(io); 173 174 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowAnySHA1InWant", .value = "true" }); 175 } 176 177 // add a tag 178 _ = try server_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 179 180 const cwd_path = try std.process.currentPathAlloc(io, allocator); 181 defer allocator.free(cwd_path); 182 183 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 184 defer allocator.free(client_path); 185 186 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 187 defer client_repo.deinit(io, allocator); 188 189 // add remote 190 { 191 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 192 defer allocator.free(remote_url); 193 194 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 195 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 196 } 197 198 // create refspec with oid as a test 199 const oid_refspec = try std.fmt.allocPrint(allocator, "+{s}:refs/heads/foo", .{&commit1}); 200 defer allocator.free(oid_refspec); 201 202 const refspecs = &.{ 203 "+refs/heads/master:refs/heads/master", 204 oid_refspec, 205 }; 206 207 const is_ssh = switch (transport_def) { 208 .file => false, 209 .wire => |wire_kind| .ssh == wire_kind, 210 }; 211 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 212 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 213 214 try client_repo.fetch( 215 io, 216 allocator, 217 "origin", 218 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 219 .command = ssh_cmd_maybe, 220 } } }, 221 ); 222 223 // update the working dir 224 try client_repo.restore(io, allocator, "."); 225 226 // make sure fetch was successful 227 { 228 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 229 defer hello_txt.close(io); 230 231 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 232 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 233 234 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 235 try std.testing.expectEqualStrings(&commit1, &oid_master); 236 } 237 238 // make another commit 239 const commit2 = blk: { 240 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 241 defer goodbye_txt.close(io); 242 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 243 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 244 break :blk try server_repo.commit(io, allocator, .{ .message = "goodbye" }); 245 }; 246 247 try client_repo.fetch( 248 io, 249 allocator, 250 "origin", 251 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 252 .command = ssh_cmd_maybe, 253 } } }, 254 ); 255 256 // update the working dir 257 try client_repo.restore(io, allocator, "."); 258 259 // make sure fetch was successful 260 { 261 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 262 defer goodbye_txt.close(io); 263 264 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 265 try std.testing.expectEqualStrings(&commit2, &oid_master); 266 } 267 } 268 269 fn testPush( 270 comptime repo_kind: rp.RepoKind, 271 comptime transport_def: net.TransportDefinition, 272 io: std.Io, 273 allocator: std.mem.Allocator, 274 ) !void { 275 const temp_dir_name = "temp-testnet-push"; 276 277 // create the temp dir 278 const cwd = std.Io.Dir.cwd(); 279 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 280 if (temp_dir_or_err) |*temp_dir| { 281 temp_dir.close(io); 282 try cwd.deleteTree(io, temp_dir_name); 283 } else |_| {} 284 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 285 defer cwd.deleteTree(io, temp_dir_name) catch {}; 286 defer temp_dir.close(io); 287 288 // init server 289 var server_process = try runServer(io, allocator, temp_dir_name); 290 defer _ = server_process.kill(io); 291 292 // register the repo under admin and locate its on-disk directory 293 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 294 defer allocator.free(server_path); 295 296 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 297 defer server_repo.deinit(io, allocator); 298 299 // add config 300 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 301 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 302 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 303 304 // export server repo 305 { 306 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 307 defer export_file.close(io); 308 } 309 310 const cwd_path = try std.process.currentPathAlloc(io, allocator); 311 defer allocator.free(cwd_path); 312 313 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 314 defer allocator.free(client_path); 315 316 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 317 defer client_repo.deinit(io, allocator); 318 319 // make a commit 320 const commit1 = blk: { 321 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 322 defer hello_txt.close(io); 323 try hello_txt.writeStreamingAll(io, "hello, world!"); 324 try client_repo.add(io, allocator, &.{"hello.txt"}); 325 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 326 }; 327 328 // add a tag 329 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 330 331 // add remote 332 { 333 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 334 defer allocator.free(remote_url); 335 336 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 337 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 338 } 339 340 const refspecs = &.{ 341 "refs/tags/1.0.0:refs/tags/1.0.0", 342 }; 343 344 const is_ssh = switch (transport_def) { 345 .file => false, 346 .wire => |wire_kind| .ssh == wire_kind, 347 }; 348 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 349 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 350 351 try client_repo.push( 352 io, 353 allocator, 354 "origin", 355 "master", 356 false, 357 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 358 .command = ssh_cmd_maybe, 359 } } }, 360 ); 361 362 // make sure push was successful 363 { 364 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 365 366 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 367 try std.testing.expectEqualStrings(&commit1, &oid_master); 368 } 369 370 // make a commit on the server 371 { 372 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 373 defer hello_txt.close(io); 374 try hello_txt.writeStreamingAll(io, "hello, world from the server!"); 375 try server_repo.add(io, allocator, &.{"hello.txt"}); 376 _ = try server_repo.commit(io, allocator, .{ .message = "new commit from the server" }); 377 } 378 379 // make another commit 380 const commit2 = blk: { 381 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 382 defer goodbye_txt.close(io); 383 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 384 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 385 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 386 }; 387 388 // can't push because server has commit not found locally 389 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 390 io, 391 allocator, 392 "origin", 393 "master", 394 false, 395 .{ .wire = .{ .ssh = .{ 396 .command = ssh_cmd_maybe, 397 } } }, 398 )); 399 400 // make a commit on the server with no parents, thus creating an incompatible git history 401 { 402 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 403 defer hello_txt.close(io); 404 try hello_txt.writeStreamingAll(io, "hello, world from the server again!"); 405 try server_repo.add(io, allocator, &.{"hello.txt"}); 406 _ = try server_repo.commit(io, allocator, .{ .message = "new git history on the server", .parent_oids = &.{} }); 407 } 408 409 // can't push because commit doesn't exist locally 410 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 411 io, 412 allocator, 413 "origin", 414 "master", 415 false, 416 .{ .wire = .{ .ssh = .{ 417 .command = ssh_cmd_maybe, 418 } } }, 419 )); 420 421 // retrieve the commit object 422 try client_repo.fetch( 423 io, 424 allocator, 425 "origin", 426 .{ .wire = .{ .ssh = .{ 427 .command = ssh_cmd_maybe, 428 } } }, 429 ); 430 431 // can't push because server's history is incompatible 432 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 433 io, 434 allocator, 435 "origin", 436 "master", 437 false, 438 .{ .wire = .{ .ssh = .{ 439 .command = ssh_cmd_maybe, 440 } } }, 441 )); 442 443 // test denyNonFastForwards 444 { 445 // set denyNonFastForwards on server 446 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 447 448 // save the server's current master ref 449 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 450 451 // force push should be rejected by server due to denyNonFastForwards 452 try client_repo.push( 453 io, 454 allocator, 455 "origin", 456 "master", 457 true, 458 .{ .wire = .{ .ssh = .{ 459 .command = ssh_cmd_maybe, 460 } } }, 461 ); 462 463 // verify the server ref was not updated (push was denied) 464 { 465 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 466 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 467 } 468 469 // remove denyNonFastForwards from server 470 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 471 } 472 473 // force push 474 try client_repo.push( 475 io, 476 allocator, 477 "origin", 478 "master", 479 true, 480 .{ .wire = .{ .ssh = .{ 481 .command = ssh_cmd_maybe, 482 } } }, 483 ); 484 485 // make sure push was successful 486 { 487 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 488 try std.testing.expectEqualStrings(&commit2, &oid_master); 489 } 490 491 // remove the remote tag 492 try client_repo.push( 493 io, 494 allocator, 495 "origin", 496 ":refs/tags/1.0.0", 497 false, 498 .{ .wire = .{ .ssh = .{ 499 .command = ssh_cmd_maybe, 500 } } }, 501 ); 502 503 // make sure push was successful 504 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 505 } 506 507 fn testPushCreatesMissingRepo( 508 comptime repo_kind: rp.RepoKind, 509 comptime transport_def: net.TransportDefinition, 510 io: std.Io, 511 allocator: std.mem.Allocator, 512 ) !void { 513 const temp_dir_name = "temp-testnet-push-create"; 514 515 const cwd = std.Io.Dir.cwd(); 516 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 517 if (temp_dir_or_err) |*temp_dir| { 518 temp_dir.close(io); 519 try cwd.deleteTree(io, temp_dir_name); 520 } else |_| {} 521 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 522 defer cwd.deleteTree(io, temp_dir_name) catch {}; 523 defer temp_dir.close(io); 524 525 var server_process = try runServer(io, allocator, temp_dir_name); 526 defer _ = server_process.kill(io); 527 528 const cwd_path = try std.process.currentPathAlloc(io, allocator); 529 defer allocator.free(cwd_path); 530 531 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 532 defer allocator.free(client_path); 533 534 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 535 defer client_repo.deinit(io, allocator); 536 537 const commit1 = blk: { 538 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 539 defer hello_txt.close(io); 540 try hello_txt.writeStreamingAll(io, "hello, world!"); 541 try client_repo.add(io, allocator, &.{"hello.txt"}); 542 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 543 }; 544 545 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 546 547 // the repo is not pre-registered; the push should mint the repo event under 548 // admin and create the repo on disk 549 { 550 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 551 defer allocator.free(remote_url); 552 553 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 554 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 555 } 556 557 const is_ssh = switch (transport_def) { 558 .file => false, 559 .wire => |wire_kind| .ssh == wire_kind, 560 }; 561 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 562 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 563 564 try client_repo.push( 565 io, 566 allocator, 567 "origin", 568 "master", 569 false, 570 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 571 .command = ssh_cmd_maybe, 572 } } }, 573 ); 574 575 // the push registered admin/server; resolve its on-disk directory 576 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", false)).?; 577 defer allocator.free(server_path); 578 579 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 580 defer server_repo.deinit(io, allocator); 581 582 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 583 584 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 585 try std.testing.expectEqualStrings(&commit1, &oid_master); 586 } 587 588 fn testClone( 589 comptime repo_kind: rp.RepoKind, 590 comptime transport_def: net.TransportDefinition, 591 comptime shell_out_to_git: bool, 592 io: std.Io, 593 allocator: std.mem.Allocator, 594 ) !void { 595 const temp_dir_name = "temp-testnet-clone"; 596 597 // create the temp dir 598 const cwd = std.Io.Dir.cwd(); 599 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 600 if (temp_dir_or_err) |*temp_dir| { 601 temp_dir.close(io); 602 try cwd.deleteTree(io, temp_dir_name); 603 } else |_| {} 604 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 605 defer cwd.deleteTree(io, temp_dir_name) catch {}; 606 defer temp_dir.close(io); 607 608 // init server 609 var server_process = try runServer(io, allocator, temp_dir_name); 610 defer _ = server_process.kill(io); 611 612 const cwd_path = try std.process.currentPathAlloc(io, allocator); 613 defer allocator.free(cwd_path); 614 615 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 616 defer allocator.free(temp_path); 617 618 // register the repo under admin and locate its on-disk directory 619 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 620 defer allocator.free(server_path); 621 622 // init server repo with default branch name as main 623 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 624 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 625 defer server_repo.deinit(io, allocator); 626 627 if (shell_out_to_git) { 628 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 629 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 630 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 631 } 632 633 // make a commit 634 { 635 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 636 defer hello_txt.close(io); 637 try hello_txt.writeStreamingAll(io, "hello, world!"); 638 try server_repo.add(io, allocator, &.{"hello.txt"}); 639 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 640 } 641 642 // tag first commit 643 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 644 645 // make a commit 646 { 647 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 648 defer goodbye_txt.close(io); 649 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 650 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 651 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 652 } 653 654 // export server repo 655 { 656 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 657 defer export_file.close(io); 658 } 659 660 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 661 defer allocator.free(client_path); 662 663 // get remote url 664 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 665 defer allocator.free(remote_url); 666 667 const is_ssh = switch (transport_def) { 668 .file => false, 669 .wire => |wire_kind| .ssh == wire_kind, 670 }; 671 672 if (shell_out_to_git) { 673 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 674 defer allocator.free(priv_key_path); 675 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}", .{ ssh_port, priv_key_path }); 676 defer allocator.free(ssh_config_arg); 677 678 { 679 var process = try std.process.spawn(io, .{ 680 .argv = if (is_ssh) 681 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 682 else 683 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 684 .cwd = .{ .path = temp_path }, 685 .stdin = .ignore, 686 .stdout = .ignore, 687 .stderr = .ignore, 688 }); 689 const term = try process.wait(io); 690 if (term != .exited or term.exited != 0) { 691 return error.GitCommandFailed; 692 } 693 } 694 695 // make sure shallow clone was successful 696 { 697 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 698 hello_txt.close(io); 699 } 700 701 // make a third commit on the server 702 { 703 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 704 defer extra_txt.close(io); 705 try extra_txt.writeStreamingAll(io, "extra content"); 706 try server_repo.add(io, allocator, &.{"extra.txt"}); 707 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 708 } 709 710 // pull --unshallow to deepen the clone and get the new commit 711 { 712 var process = try std.process.spawn(io, .{ 713 .argv = if (is_ssh) 714 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 715 else 716 &.{ "git", "pull", "--unshallow" }, 717 .cwd = .{ .path = client_path }, 718 .stdin = .ignore, 719 .stdout = .ignore, 720 .stderr = .ignore, 721 }); 722 const term = try process.wait(io); 723 if (term != .exited or term.exited != 0) { 724 return error.GitCommandFailed; 725 } 726 } 727 728 // make sure unshallow pull was successful 729 { 730 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 731 extra_txt.close(io); 732 } 733 734 // delete client and clone again with --shallow-since 735 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 736 737 { 738 var process = try std.process.spawn(io, .{ 739 .argv = if (is_ssh) 740 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 741 else 742 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 743 .cwd = .{ .path = temp_path }, 744 .stdin = .ignore, 745 .stdout = .ignore, 746 .stderr = .ignore, 747 }); 748 const term = try process.wait(io); 749 if (term != .exited or term.exited != 0) { 750 return error.GitCommandFailed; 751 } 752 } 753 754 // make sure shallow clone was successful 755 { 756 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 757 hello_txt.close(io); 758 } 759 760 // delete client and clone again with --shallow-exclude 761 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 762 763 { 764 var process = try std.process.spawn(io, .{ 765 .argv = if (is_ssh) 766 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 767 else 768 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 769 .cwd = .{ .path = temp_path }, 770 .stdin = .ignore, 771 .stdout = .ignore, 772 .stderr = .ignore, 773 }); 774 const term = try process.wait(io); 775 if (term != .exited or term.exited != 0) { 776 return error.GitCommandFailed; 777 } 778 } 779 780 // make sure shallow clone was successful 781 { 782 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 783 hello_txt.close(io); 784 } 785 786 // delete client and clone again with --filter=blob:none 787 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 788 789 { 790 var process = try std.process.spawn(io, .{ 791 .argv = if (is_ssh) 792 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 793 else 794 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 795 .cwd = .{ .path = temp_path }, 796 .stdin = .ignore, 797 .stdout = .ignore, 798 .stderr = .ignore, 799 }); 800 const term = try process.wait(io); 801 if (term != .exited or term.exited != 0) { 802 return error.GitCommandFailed; 803 } 804 } 805 806 // make sure partial clone was successful 807 { 808 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 809 hello_txt.close(io); 810 } 811 812 // delete client and clone again with --filter=tree:0 813 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 814 815 { 816 var process = try std.process.spawn(io, .{ 817 .argv = if (is_ssh) 818 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 819 else 820 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 821 .cwd = .{ .path = temp_path }, 822 .stdin = .ignore, 823 .stdout = .ignore, 824 .stderr = .ignore, 825 }); 826 const term = try process.wait(io); 827 if (term != .exited or term.exited != 0) { 828 return error.GitCommandFailed; 829 } 830 } 831 832 // make sure treeless clone was successful 833 { 834 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 835 goodbye_txt.close(io); 836 } 837 } else { 838 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 839 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 840 841 // clone repo 842 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 843 io, 844 allocator, 845 remote_url, 846 temp_path, 847 client_path, 848 .{ .wire = .{ .ssh = .{ 849 .command = ssh_cmd_maybe, 850 } } }, 851 ); 852 defer client_repo.deinit(io, allocator); 853 854 // make sure HEAD points to the right default branch 855 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 856 const head = try client_repo.head(io, &current_branch_buffer); 857 try std.testing.expectEqualStrings("main", head.ref.name); 858 859 // make sure clone was successful 860 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 861 defer hello_txt.close(io); 862 } 863 } 864 865 fn testFetchLarge( 866 comptime repo_kind: rp.RepoKind, 867 comptime transport_def: net.TransportDefinition, 868 comptime shell_out_to_git: bool, 869 io: std.Io, 870 allocator: std.mem.Allocator, 871 ) !void { 872 const temp_dir_name = "temp-testnet-fetch-large"; 873 874 // create the temp dir 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 // init server 886 var server_process = try runServer(io, allocator, temp_dir_name); 887 defer _ = server_process.kill(io); 888 889 const cwd_path = try std.process.currentPathAlloc(io, allocator); 890 defer allocator.free(cwd_path); 891 892 // register the repo under admin and locate its on-disk directory 893 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 894 defer allocator.free(server_path); 895 896 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 897 defer server_repo.deinit(io, allocator); 898 899 var server_dir = try cwd.openDir(io, server_path, .{}); 900 defer server_dir.close(io); 901 902 // copy files from current repo into server dir 903 for (&[_][]const u8{"src"}) |dir_name| { 904 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 905 defer src_repo_dir.close(io); 906 907 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 908 defer dest_repo_dir.close(io); 909 910 try copyDir(io, src_repo_dir, dest_repo_dir); 911 912 try server_repo.add(io, allocator, &.{dir_name}); 913 } 914 915 // make a commit 916 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 917 918 // export server repo 919 { 920 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 921 defer export_file.close(io); 922 } 923 924 if (shell_out_to_git) { 925 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 926 } 927 928 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 929 defer allocator.free(client_path); 930 931 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 932 defer client_repo.deinit(io, allocator); 933 934 // add remote 935 { 936 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 937 defer allocator.free(remote_url); 938 939 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 940 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 941 } 942 943 const is_ssh = switch (transport_def) { 944 .file => false, 945 .wire => |wire_kind| .ssh == wire_kind, 946 }; 947 948 if (shell_out_to_git) { 949 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 950 defer allocator.free(priv_key_path); 951 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}", .{ ssh_port, priv_key_path }); 952 defer allocator.free(ssh_config_arg); 953 954 { 955 var process = try std.process.spawn(io, .{ 956 .argv = if (is_ssh) 957 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 958 else 959 &.{ "git", "pull", "origin", "master" }, 960 .cwd = .{ .path = client_path }, 961 .stdin = .ignore, 962 .stdout = .ignore, 963 .stderr = .ignore, 964 }); 965 const term = try process.wait(io); 966 if (term != .exited or term.exited != 0) { 967 return error.GitCommandFailed; 968 } 969 } 970 971 // make sure pull was successful 972 { 973 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 974 try std.testing.expectEqualStrings(&commit1, &oid_master); 975 } 976 977 // make another commit on the server 978 const commit2 = blk: { 979 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 980 defer extra_txt.close(io); 981 try extra_txt.writeStreamingAll(io, "extra content"); 982 try server_repo.add(io, allocator, &.{"extra.txt"}); 983 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 984 }; 985 986 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 987 { 988 var process = try std.process.spawn(io, .{ 989 .argv = if (is_ssh) 990 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 991 else 992 &.{ "git", "fetch", "origin", "master" }, 993 .cwd = .{ .path = client_path }, 994 .stdin = .ignore, 995 .stdout = .ignore, 996 .stderr = .ignore, 997 }); 998 const term = try process.wait(io); 999 if (term != .exited or term.exited != 0) { 1000 return error.GitCommandFailed; 1001 } 1002 } 1003 1004 // make sure fetch with want-ref was successful 1005 { 1006 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1007 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1008 } 1009 } else { 1010 const refspecs = &.{ 1011 "+refs/heads/master:refs/heads/master", 1012 }; 1013 1014 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1015 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1016 1017 try client_repo.fetch( 1018 io, 1019 allocator, 1020 "origin", 1021 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1022 .command = ssh_cmd_maybe, 1023 } } }, 1024 ); 1025 1026 // update the working dir 1027 try client_repo.restore(io, allocator, "."); 1028 } 1029 1030 // make sure fetch was successful 1031 { 1032 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1033 try std.testing.expectEqualStrings(&commit1, &oid_master); 1034 } 1035 } 1036 1037 fn testPushLarge( 1038 comptime repo_kind: rp.RepoKind, 1039 comptime transport_def: net.TransportDefinition, 1040 comptime shell_out_to_git: bool, 1041 io: std.Io, 1042 allocator: std.mem.Allocator, 1043 ) !void { 1044 const temp_dir_name = "temp-testnet-push-large"; 1045 1046 // create the temp dir 1047 const cwd = std.Io.Dir.cwd(); 1048 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1049 if (temp_dir_or_err) |*temp_dir| { 1050 temp_dir.close(io); 1051 try cwd.deleteTree(io, temp_dir_name); 1052 } else |_| {} 1053 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1054 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1055 defer temp_dir.close(io); 1056 1057 // init server 1058 var server_process = try runServer(io, allocator, temp_dir_name); 1059 defer _ = server_process.kill(io); 1060 1061 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1062 defer allocator.free(cwd_path); 1063 1064 // register the repo under admin and locate its on-disk directory 1065 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1066 defer allocator.free(server_path); 1067 1068 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1069 defer server_repo.deinit(io, allocator); 1070 1071 // add config 1072 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1073 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1074 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1075 1076 // export server repo 1077 { 1078 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1079 defer export_file.close(io); 1080 } 1081 1082 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1083 defer allocator.free(client_path); 1084 1085 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1086 defer client_repo.deinit(io, allocator); 1087 1088 var client_dir = try cwd.openDir(io, client_path, .{}); 1089 defer client_dir.close(io); 1090 1091 { 1092 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1093 defer hello_txt.close(io); 1094 try hello_txt.writeStreamingAll(io, "hello, world!"); 1095 try client_repo.add(io, allocator, &.{"hello.txt"}); 1096 } 1097 1098 // copy files from current repo into client dir 1099 for (&[_][]const u8{"src"}) |dir_name| { 1100 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1101 defer src_repo_dir.close(io); 1102 1103 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1104 defer dest_repo_dir.close(io); 1105 1106 try copyDir(io, src_repo_dir, dest_repo_dir); 1107 1108 try client_repo.add(io, allocator, &.{dir_name}); 1109 } 1110 1111 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1112 1113 // change the files so git will send them as delta objects 1114 for (&[_][]const u8{"src"}) |dir_name| { 1115 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1116 defer dest_repo_dir.close(io); 1117 1118 { 1119 var iter = dest_repo_dir.iterate(); 1120 while (try iter.next(io)) |entry| { 1121 switch (entry.kind) { 1122 .file => { 1123 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1124 defer file.close(io); 1125 var writer = file.writer(io, &.{}); 1126 try writer.interface.writeAll("EDIT"); 1127 }, 1128 else => {}, 1129 } 1130 } 1131 } 1132 1133 try client_repo.add(io, allocator, &.{dir_name}); 1134 } 1135 1136 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1137 1138 // add remote 1139 { 1140 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1141 defer allocator.free(remote_url); 1142 1143 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1144 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1145 } 1146 1147 const is_ssh = switch (transport_def) { 1148 .file => false, 1149 .wire => |wire_kind| .ssh == wire_kind, 1150 }; 1151 1152 if (shell_out_to_git) { 1153 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1154 defer allocator.free(priv_key_path); 1155 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}", .{ ssh_port, priv_key_path }); 1156 defer allocator.free(ssh_config_arg); 1157 1158 // shell out to git so it will send delta objects 1159 var process = try std.process.spawn(io, .{ 1160 .argv = if (is_ssh) 1161 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1162 else 1163 &.{ "git", "push", "origin", "master" }, 1164 .cwd = .{ .path = client_path }, 1165 .stdin = .ignore, 1166 .stdout = .ignore, 1167 .stderr = .ignore, 1168 }); 1169 const term = try process.wait(io); 1170 if (term != .exited or term.exited != 0) { 1171 return error.GitCommandFailed; 1172 } 1173 } else { 1174 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1175 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1176 1177 try client_repo.push( 1178 io, 1179 allocator, 1180 "origin", 1181 "master", 1182 false, 1183 .{ .wire = .{ .ssh = .{ 1184 .command = ssh_cmd_maybe, 1185 } } }, 1186 ); 1187 } 1188 1189 // make sure push was successful 1190 { 1191 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1192 try std.testing.expectEqualStrings(&commit2, &oid_master); 1193 1194 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1195 defer hello_txt.close(io); 1196 } 1197 } 1198 1199 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1200 var iter = src_dir.iterate(); 1201 while (try iter.next(io)) |entry| { 1202 switch (entry.kind) { 1203 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1204 .directory => { 1205 try dest_dir.createDirPath(io, entry.name); 1206 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1207 defer dest_entry_dir.close(io); 1208 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1209 defer src_entry_dir.close(io); 1210 try copyDir(io, src_entry_dir, dest_entry_dir); 1211 }, 1212 else => {}, 1213 } 1214 } 1215 } 1216 1217 // the dev SSH public key matching the private key written by runServer 1218 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1219 1220 // create the admin event repo with a user holding the dev SSH public key 1221 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1222 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1223 defer allocator.free(cwd_path); 1224 1225 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1226 defer allocator.free(admin_repo_path); 1227 1228 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1229 defer repo.deinit(io, allocator); 1230 1231 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1232 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1233 1234 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1235 const user_id = evt.EventWithId.randomId(prng.random()); 1236 1237 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1238 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1239 1240 try evt.commitAndConsume(evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1241 .id = std.fmt.bytesToHex(user_id, .lower), 1242 .event = .{ .user = .{ 1243 .name = "admin", 1244 .display_name = "Admin", 1245 .email = "admin@example.test", 1246 .password_hash = password_hash, 1247 .ssh_keys = admin_ssh_key, 1248 } }, 1249 }}); 1250 } 1251 1252 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1253 // the event store 1254 fn repoOnDiskPath( 1255 io: std.Io, 1256 allocator: std.mem.Allocator, 1257 data_dir_name: []const u8, 1258 repo_name: []const u8, 1259 create: bool, 1260 ) !?[]u8 { 1261 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1262 defer allocator.free(cwd_path); 1263 1264 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1265 defer allocator.free(admin_repo_path); 1266 1267 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1268 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1269 } 1270 1271 // build the remote URL addressing admin/<repo_name> over the given transport 1272 fn remoteUrl( 1273 comptime transport_def: net.TransportDefinition, 1274 allocator: std.mem.Allocator, 1275 repo_name: []const u8, 1276 ) ![]u8 { 1277 return switch (transport_def) { 1278 .file => unreachable, 1279 .wire => |wire_kind| switch (wire_kind) { 1280 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1281 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1282 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1283 }, 1284 }; 1285 } 1286 1287 // the ssh command the haxy git client should invoke, or null when not using 1288 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1289 // and skip writing to known_hosts. 1290 fn sshCommand( 1291 is_ssh: bool, 1292 allocator: std.mem.Allocator, 1293 cwd_path: []const u8, 1294 temp_dir_name: []const u8, 1295 ) !?[]u8 { 1296 if (!is_ssh) return null; 1297 1298 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1299 defer allocator.free(priv_key_path); 1300 1301 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ ssh_port, priv_key_path }); 1302 } 1303