╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
219 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 220 221 try client_repo.fetch( 222 io, 223 allocator, 224 "origin", 225 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 226 .command = ssh_cmd_maybe, 227 } } }, 228 ); 229 230 // update the working dir 231 try client_repo.restore(io, allocator, "."); 232 233 // make sure fetch was successful 234 { 235 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 236 defer hello_txt.close(io); 237 238 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 239 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 240 241 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 242 try std.testing.expectEqualStrings(&commit1, &oid_master); 243 } 244 245 // make another commit 246 const commit2 = blk: { 247 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 248 defer goodbye_txt.close(io); 249 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 250 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 251 break :blk try server_repo.commit(io, allocator, .{ .message = "goodbye" }); 252 }; 253 254 try client_repo.fetch( 255 io, 256 allocator, 257 "origin", 258 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 259 .command = ssh_cmd_maybe, 260 } } }, 261 ); 262 263 // update the working dir 264 try client_repo.restore(io, allocator, "."); 265 266 // make sure fetch was successful 267 { 268 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 269 defer goodbye_txt.close(io); 270 271 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 272 try std.testing.expectEqualStrings(&commit2, &oid_master); 273 } 274 } 275 276 fn testPush( 277 comptime repo_kind: rp.RepoKind, 278 comptime transport_def: net.TransportDefinition, 279 comptime port: u16, 280 io: std.Io, 281 allocator: std.mem.Allocator, 282 ) !void { 283 const temp_dir_name = "temp-testnet-push"; 284 285 // create the temp dir 286 const cwd = std.Io.Dir.cwd(); 287 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 288 if (temp_dir_or_err) |*temp_dir| { 289 temp_dir.close(io); 290 try cwd.deleteTree(io, temp_dir_name); 291 } else |_| {} 292 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 293 defer cwd.deleteTree(io, temp_dir_name) catch {}; 294 defer temp_dir.close(io); 295 296 // init server 297 var server_process = try runServer(io, allocator, temp_dir_name, port); 298 defer _ = server_process.kill(io); 299 300 // register the repo under admin and locate its on-disk directory 301 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 302 defer allocator.free(server_path); 303 304 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 305 defer server_repo.deinit(io, allocator); 306 307 // add config 308 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 309 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 310 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 311 312 // export server repo 313 { 314 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 315 defer export_file.close(io); 316 } 317 318 const cwd_path = try std.process.currentPathAlloc(io, allocator); 319 defer allocator.free(cwd_path); 320 321 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 322 defer allocator.free(client_path); 323 324 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 325 defer client_repo.deinit(io, allocator); 326 327 // make a commit 328 const commit1 = blk: { 329 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 330 defer hello_txt.close(io); 331 try hello_txt.writeStreamingAll(io, "hello, world!"); 332 try client_repo.add(io, allocator, &.{"hello.txt"}); 333 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 334 }; 335 336 // add a tag 337 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 338 339 // add remote 340 { 341 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 342 defer allocator.free(remote_url); 343 344 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 345 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 346 } 347 348 const refspecs = &.{ 349 "refs/tags/1.0.0:refs/tags/1.0.0", 350 }; 351 352 const is_ssh = switch (transport_def) { 353 .file => false, 354 .wire => |wire_kind| .ssh == wire_kind, 355 }; 356 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 357 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 358 359 try client_repo.push( 360 io, 361 allocator, 362 "origin", 363 "master", 364 false, 365 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 366 .command = ssh_cmd_maybe, 367 } } }, 368 ); 369 370 // make sure push was successful 371 { 372 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 373 374 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 375 try std.testing.expectEqualStrings(&commit1, &oid_master); 376 } 377 378 // make a commit on the server 379 { 380 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 381 defer hello_txt.close(io); 382 try hello_txt.writeStreamingAll(io, "hello, world from the server!"); 383 try server_repo.add(io, allocator, &.{"hello.txt"}); 384 _ = try server_repo.commit(io, allocator, .{ .message = "new commit from the server" }); 385 } 386 387 // make another commit 388 const commit2 = blk: { 389 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 390 defer goodbye_txt.close(io); 391 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 392 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 393 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 394 }; 395 396 // can't push because server has commit not found locally 397 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 398 io, 399 allocator, 400 "origin", 401 "master", 402 false, 403 .{ .wire = .{ .ssh = .{ 404 .command = ssh_cmd_maybe, 405 } } }, 406 )); 407 408 // make a commit on the server with no parents, thus creating an incompatible git history 409 { 410 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 411 defer hello_txt.close(io); 412 try hello_txt.writeStreamingAll(io, "hello, world from the server again!"); 413 try server_repo.add(io, allocator, &.{"hello.txt"}); 414 _ = try server_repo.commit(io, allocator, .{ .message = "new git history on the server", .parent_oids = &.{} }); 415 } 416 417 // can't push because commit doesn't exist locally 418 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 419 io, 420 allocator, 421 "origin", 422 "master", 423 false, 424 .{ .wire = .{ .ssh = .{ 425 .command = ssh_cmd_maybe, 426 } } }, 427 )); 428 429 // retrieve the commit object 430 try client_repo.fetch( 431 io, 432 allocator, 433 "origin", 434 .{ .wire = .{ .ssh = .{ 435 .command = ssh_cmd_maybe, 436 } } }, 437 ); 438 439 // can't push because server's history is incompatible 440 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 441 io, 442 allocator, 443 "origin", 444 "master", 445 false, 446 .{ .wire = .{ .ssh = .{ 447 .command = ssh_cmd_maybe, 448 } } }, 449 )); 450 451 // test denyNonFastForwards 452 { 453 // set denyNonFastForwards on server 454 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 455 456 // save the server's current master ref 457 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 458 459 // force push should be rejected by server due to denyNonFastForwards 460 try std.testing.expectError(error.RemoteRejectedRef, client_repo.push( 461 io, 462 allocator, 463 "origin", 464 "master", 465 true, 466 .{ .wire = .{ .ssh = .{ 467 .command = ssh_cmd_maybe, 468 } } }, 469 )); 470 471 // verify the server ref was not updated (push was denied) 472 { 473 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 474 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 475 } 476 477 // remove denyNonFastForwards from server 478 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 479 } 480 481 // force push 482 try client_repo.push( 483 io, 484 allocator, 485 "origin", 486 "master", 487 true, 488 .{ .wire = .{ .ssh = .{ 489 .command = ssh_cmd_maybe, 490 } } }, 491 ); 492 493 // make sure push was successful 494 { 495 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 496 try std.testing.expectEqualStrings(&commit2, &oid_master); 497 } 498 499 // remove the remote tag 500 try client_repo.push( 501 io, 502 allocator, 503 "origin", 504 ":refs/tags/1.0.0", 505 false, 506 .{ .wire = .{ .ssh = .{ 507 .command = ssh_cmd_maybe, 508 } } }, 509 ); 510 511 // make sure push was successful 512 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 513 } 514 515 fn testPushCreatesMissingRepo( 516 comptime repo_kind: rp.RepoKind, 517 comptime transport_def: net.TransportDefinition, 518 comptime port: u16, 519 io: std.Io, 520 allocator: std.mem.Allocator, 521 ) !void { 522 const temp_dir_name = "temp-testnet-push-create"; 523 524 const cwd = std.Io.Dir.cwd(); 525 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 526 if (temp_dir_or_err) |*temp_dir| { 527 temp_dir.close(io); 528 try cwd.deleteTree(io, temp_dir_name); 529 } else |_| {} 530 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 531 defer cwd.deleteTree(io, temp_dir_name) catch {}; 532 defer temp_dir.close(io); 533 534 var server_process = try runServer(io, allocator, temp_dir_name, port); 535 defer _ = server_process.kill(io); 536 537 const cwd_path = try std.process.currentPathAlloc(io, allocator); 538 defer allocator.free(cwd_path); 539 540 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 541 defer allocator.free(client_path); 542 543 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 544 defer client_repo.deinit(io, allocator); 545 546 const commit1 = blk: { 547 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 548 defer hello_txt.close(io); 549 try hello_txt.writeStreamingAll(io, "hello, world!"); 550 try client_repo.add(io, allocator, &.{"hello.txt"}); 551 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 552 }; 553 554 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 555 556 // the repo is not pre-registered; the push should mint the repo event under 557 // admin and create the repo on disk 558 { 559 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 560 defer allocator.free(remote_url); 561 562 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 563 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 564 } 565 566 const is_ssh = switch (transport_def) { 567 .file => false, 568 .wire => |wire_kind| .ssh == wire_kind, 569 }; 570 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 571 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 572 573 try client_repo.push( 574 io, 575 allocator, 576 "origin", 577 "master", 578 false, 579 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 580 .command = ssh_cmd_maybe, 581 } } }, 582 ); 583 584 // the push registered admin/server; resolve its on-disk directory 585 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", false)).?; 586 defer allocator.free(server_path); 587 588 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 589 defer server_repo.deinit(io, allocator); 590 591 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 592 593 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 594 try std.testing.expectEqualStrings(&commit1, &oid_master); 595 } 596 597 fn testPushEvents( 598 comptime repo_kind: rp.RepoKind, 599 comptime transport_def: net.TransportDefinition, 600 comptime port: u16, 601 io: std.Io, 602 allocator: std.mem.Allocator, 603 ) !void { 604 const temp_dir_name = "temp-testnet-push-events"; 605 606 // create the temp dir 607 const cwd = std.Io.Dir.cwd(); 608 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 609 if (temp_dir_or_err) |*temp_dir| { 610 temp_dir.close(io); 611 try cwd.deleteTree(io, temp_dir_name); 612 } else |_| {} 613 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 614 defer cwd.deleteTree(io, temp_dir_name) catch {}; 615 defer temp_dir.close(io); 616 617 // init server 618 var server_process = try runServer(io, allocator, temp_dir_name, port); 619 defer _ = server_process.kill(io); 620 621 // register the repo under admin and locate its on-disk directory 622 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 623 defer allocator.free(server_path); 624 625 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 626 const ServerRepo = rp.Repo(.xit, repo_opts); 627 var server_repo = try ServerRepo.init(io, allocator, .{ .path = server_path }); 628 defer server_repo.deinit(io, allocator); 629 630 const cwd_path = try std.process.currentPathAlloc(io, allocator); 631 defer allocator.free(cwd_path); 632 633 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 634 defer allocator.free(client_path); 635 636 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 637 defer client_repo.deinit(io, allocator); 638 639 // add remote 640 { 641 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 642 defer allocator.free(remote_url); 643 644 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 645 } 646 647 const is_ssh = switch (transport_def) { 648 .file => false, 649 .wire => |wire_kind| .ssh == wire_kind, 650 }; 651 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 652 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 653 654 var arena = std.heap.ArenaAllocator.init(allocator); 655 defer arena.deinit(); 656 657 // 658 // define test events 659 // 660 661 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 662 const issue_event_id = evt.EventWithId.randomId(prng.random()); 663 664 const events_to_push = [_]evt.EventWithId{ 665 .{ 666 .id = std.fmt.bytesToHex(issue_event_id, .lower), 667 .author = .{ .name = "haxy", .email = "user@haxy" }, 668 .event = .{ 669 .issue = .{ 670 .title = "Login form clears password on validation error", 671 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 672 .tags = "bug priority-high ui", 673 }, 674 }, 675 }, 676 }; 677 678 // commit the issue on the client and push it to the server 679 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 680 try client_repo.push( 681 io, 682 allocator, 683 "origin", 684 "haxy/events", 685 false, 686 .{ .wire = .{ .ssh = .{ 687 .command = ssh_cmd_maybe, 688 } } }, 689 ); 690 691 // the push consumed the issue into the server repo's db 692 { 693 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 694 695 // get the map of issues 696 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 697 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 698 699 // get the issue out of the map 700 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 701 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 702 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 703 704 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.description); 705 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.tags); 706 } 707 708 // 709 // edit the issue 710 // 711 712 // this event edits the previous one because it has the same id 713 const events_to_push2 = [_]evt.EventWithId{ 714 .{ 715 .id = std.fmt.bytesToHex(issue_event_id, .lower), 716 .author = .{ .name = "haxy", .email = "user@haxy" }, 717 .event = .{ 718 .issue = .{ 719 .title = "Login form clears password on validation error", 720 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 721 .tags = "bug priority-low ui", 722 }, 723 }, 724 }, 725 }; 726 727 // commit the edit on the client and push it to the server 728 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 729 try client_repo.push( 730 io, 731 allocator, 732 "origin", 733 "haxy/events", 734 false, 735 .{ .wire = .{ .ssh = .{ 736 .command = ssh_cmd_maybe, 737 } } }, 738 ); 739 740 // the push consumed the edit into the server repo's db 741 { 742 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 743 744 // get the map of issues 745 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 746 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 747 748 // get the issue out of the map that was edited 749 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 750 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 751 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 752 753 // the description and tags were correctly edited 754 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.description); 755 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.tags); 756 } 757 } 758 759 fn testClone( 760 comptime repo_kind: rp.RepoKind, 761 comptime transport_def: net.TransportDefinition, 762 comptime shell_out_to_git: bool, 763 comptime port: u16, 764 io: std.Io, 765 allocator: std.mem.Allocator, 766 ) !void { 767 const temp_dir_name = "temp-testnet-clone"; 768 769 // create the temp dir 770 const cwd = std.Io.Dir.cwd(); 771 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 772 if (temp_dir_or_err) |*temp_dir| { 773 temp_dir.close(io); 774 try cwd.deleteTree(io, temp_dir_name); 775 } else |_| {} 776 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 777 defer cwd.deleteTree(io, temp_dir_name) catch {}; 778 defer temp_dir.close(io); 779 780 // init server 781 var server_process = try runServer(io, allocator, temp_dir_name, port); 782 defer _ = server_process.kill(io); 783 784 const cwd_path = try std.process.currentPathAlloc(io, allocator); 785 defer allocator.free(cwd_path); 786 787 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 788 defer allocator.free(temp_path); 789 790 // register the repo under admin and locate its on-disk directory 791 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 792 defer allocator.free(server_path); 793 794 // init server repo with default branch name as main 795 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 796 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 797 defer server_repo.deinit(io, allocator); 798 799 if (shell_out_to_git) { 800 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 801 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 802 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 803 } 804 805 // make a commit 806 { 807 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 808 defer hello_txt.close(io); 809 try hello_txt.writeStreamingAll(io, "hello, world!"); 810 try server_repo.add(io, allocator, &.{"hello.txt"}); 811 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 812 } 813 814 // tag first commit 815 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 816 817 // make a commit 818 { 819 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 820 defer goodbye_txt.close(io); 821 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 822 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 823 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 824 } 825 826 // export server repo 827 { 828 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 829 defer export_file.close(io); 830 } 831 832 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 833 defer allocator.free(client_path); 834 835 // get remote url 836 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 837 defer allocator.free(remote_url); 838 839 const is_ssh = switch (transport_def) { 840 .file => false, 841 .wire => |wire_kind| .ssh == wire_kind, 842 }; 843 844 if (shell_out_to_git) { 845 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 846 defer allocator.free(priv_key_path); 847 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 848 defer allocator.free(ssh_config_arg); 849 850 { 851 var process = try std.process.spawn(io, .{ 852 .argv = if (is_ssh) 853 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 854 else 855 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 856 .cwd = .{ .path = temp_path }, 857 .stdin = .ignore, 858 .stdout = .ignore, 859 .stderr = .ignore, 860 }); 861 const term = try process.wait(io); 862 if (term != .exited or term.exited != 0) { 863 return error.GitCommandFailed; 864 } 865 } 866 867 // make sure shallow clone was successful 868 { 869 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 870 hello_txt.close(io); 871 } 872 873 // make a third commit on the server 874 { 875 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 876 defer extra_txt.close(io); 877 try extra_txt.writeStreamingAll(io, "extra content"); 878 try server_repo.add(io, allocator, &.{"extra.txt"}); 879 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 880 } 881 882 // pull --unshallow to deepen the clone and get the new commit 883 { 884 var process = try std.process.spawn(io, .{ 885 .argv = if (is_ssh) 886 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 887 else 888 &.{ "git", "pull", "--unshallow" }, 889 .cwd = .{ .path = client_path }, 890 .stdin = .ignore, 891 .stdout = .ignore, 892 .stderr = .ignore, 893 }); 894 const term = try process.wait(io); 895 if (term != .exited or term.exited != 0) { 896 return error.GitCommandFailed; 897 } 898 } 899 900 // make sure unshallow pull was successful 901 { 902 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 903 extra_txt.close(io); 904 } 905 906 // delete client and clone again with --shallow-since 907 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 908 909 { 910 var process = try std.process.spawn(io, .{ 911 .argv = if (is_ssh) 912 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 913 else 914 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 915 .cwd = .{ .path = temp_path }, 916 .stdin = .ignore, 917 .stdout = .ignore, 918 .stderr = .ignore, 919 }); 920 const term = try process.wait(io); 921 if (term != .exited or term.exited != 0) { 922 return error.GitCommandFailed; 923 } 924 } 925 926 // make sure shallow clone was successful 927 { 928 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 929 hello_txt.close(io); 930 } 931 932 // delete client and clone again with --shallow-exclude 933 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 934 935 { 936 var process = try std.process.spawn(io, .{ 937 .argv = if (is_ssh) 938 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 939 else 940 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 941 .cwd = .{ .path = temp_path }, 942 .stdin = .ignore, 943 .stdout = .ignore, 944 .stderr = .ignore, 945 }); 946 const term = try process.wait(io); 947 if (term != .exited or term.exited != 0) { 948 return error.GitCommandFailed; 949 } 950 } 951 952 // make sure shallow clone was successful 953 { 954 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 955 hello_txt.close(io); 956 } 957 958 // delete client and clone again with --filter=blob:none 959 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 960 961 { 962 var process = try std.process.spawn(io, .{ 963 .argv = if (is_ssh) 964 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 965 else 966 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 967 .cwd = .{ .path = temp_path }, 968 .stdin = .ignore, 969 .stdout = .ignore, 970 .stderr = .ignore, 971 }); 972 const term = try process.wait(io); 973 if (term != .exited or term.exited != 0) { 974 return error.GitCommandFailed; 975 } 976 } 977 978 // make sure partial clone was successful 979 { 980 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 981 hello_txt.close(io); 982 } 983 984 // delete client and clone again with --filter=tree:0 985 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 986 987 { 988 var process = try std.process.spawn(io, .{ 989 .argv = if (is_ssh) 990 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 991 else 992 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 993 .cwd = .{ .path = temp_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 treeless clone was successful 1005 { 1006 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1007 goodbye_txt.close(io); 1008 } 1009 } else { 1010 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1011 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1012 1013 // clone repo 1014 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1015 io, 1016 allocator, 1017 remote_url, 1018 temp_path, 1019 client_path, 1020 null, 1021 .{ .wire = .{ .ssh = .{ 1022 .command = ssh_cmd_maybe, 1023 } } }, 1024 ); 1025 defer client_repo.deinit(io, allocator); 1026 1027 // make sure HEAD points to the right default branch 1028 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1029 const head = try client_repo.head(io, &current_branch_buffer); 1030 try std.testing.expectEqualStrings("main", head.ref.name); 1031 1032 // make sure clone was successful 1033 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1034 defer hello_txt.close(io); 1035 } 1036 } 1037 1038 fn testFetchLarge( 1039 comptime repo_kind: rp.RepoKind, 1040 comptime transport_def: net.TransportDefinition, 1041 comptime shell_out_to_git: bool, 1042 comptime port: u16, 1043 io: std.Io, 1044 allocator: std.mem.Allocator, 1045 ) !void { 1046 const temp_dir_name = "temp-testnet-fetch-large"; 1047 1048 // create the temp dir 1049 const cwd = std.Io.Dir.cwd(); 1050 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1051 if (temp_dir_or_err) |*temp_dir| { 1052 temp_dir.close(io); 1053 try cwd.deleteTree(io, temp_dir_name); 1054 } else |_| {} 1055 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1056 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1057 defer temp_dir.close(io); 1058 1059 // init server 1060 var server_process = try runServer(io, allocator, temp_dir_name, port); 1061 defer _ = server_process.kill(io); 1062 1063 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1064 defer allocator.free(cwd_path); 1065 1066 // register the repo under admin and locate its on-disk directory 1067 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1068 defer allocator.free(server_path); 1069 1070 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1071 defer server_repo.deinit(io, allocator); 1072 1073 var server_dir = try cwd.openDir(io, server_path, .{}); 1074 defer server_dir.close(io); 1075 1076 // copy files from current repo into server dir 1077 for (&[_][]const u8{"src"}) |dir_name| { 1078 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1079 defer src_repo_dir.close(io); 1080 1081 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1082 defer dest_repo_dir.close(io); 1083 1084 try copyDir(io, src_repo_dir, dest_repo_dir); 1085 1086 try server_repo.add(io, allocator, &.{dir_name}); 1087 } 1088 1089 // make a commit 1090 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1091 1092 // export server repo 1093 { 1094 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1095 defer export_file.close(io); 1096 } 1097 1098 if (shell_out_to_git) { 1099 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1100 } 1101 1102 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1103 defer allocator.free(client_path); 1104 1105 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1106 defer client_repo.deinit(io, allocator); 1107 1108 // add remote 1109 { 1110 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1111 defer allocator.free(remote_url); 1112 1113 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1114 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1115 } 1116 1117 const is_ssh = switch (transport_def) { 1118 .file => false, 1119 .wire => |wire_kind| .ssh == wire_kind, 1120 }; 1121 1122 if (shell_out_to_git) { 1123 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1124 defer allocator.free(priv_key_path); 1125 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1126 defer allocator.free(ssh_config_arg); 1127 1128 { 1129 var process = try std.process.spawn(io, .{ 1130 .argv = if (is_ssh) 1131 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1132 else 1133 &.{ "git", "pull", "origin", "master" }, 1134 .cwd = .{ .path = client_path }, 1135 .stdin = .ignore, 1136 .stdout = .ignore, 1137 .stderr = .ignore, 1138 }); 1139 const term = try process.wait(io); 1140 if (term != .exited or term.exited != 0) { 1141 return error.GitCommandFailed; 1142 } 1143 } 1144 1145 // make sure pull was successful 1146 { 1147 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1148 try std.testing.expectEqualStrings(&commit1, &oid_master); 1149 } 1150 1151 // make another commit on the server 1152 const commit2 = blk: { 1153 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1154 defer extra_txt.close(io); 1155 try extra_txt.writeStreamingAll(io, "extra content"); 1156 try server_repo.add(io, allocator, &.{"extra.txt"}); 1157 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1158 }; 1159 1160 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1161 { 1162 var process = try std.process.spawn(io, .{ 1163 .argv = if (is_ssh) 1164 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1165 else 1166 &.{ "git", "fetch", "origin", "master" }, 1167 .cwd = .{ .path = client_path }, 1168 .stdin = .ignore, 1169 .stdout = .ignore, 1170 .stderr = .ignore, 1171 }); 1172 const term = try process.wait(io); 1173 if (term != .exited or term.exited != 0) { 1174 return error.GitCommandFailed; 1175 } 1176 } 1177 1178 // make sure fetch with want-ref was successful 1179 { 1180 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1181 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1182 } 1183 } else { 1184 const refspecs = &.{ 1185 "+refs/heads/master:refs/heads/master", 1186 }; 1187 1188 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1189 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1190 1191 try client_repo.fetch( 1192 io, 1193 allocator, 1194 "origin", 1195 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1196 .command = ssh_cmd_maybe, 1197 } } }, 1198 ); 1199 1200 // update the working dir 1201 try client_repo.restore(io, allocator, "."); 1202 } 1203 1204 // make sure fetch was successful 1205 { 1206 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1207 try std.testing.expectEqualStrings(&commit1, &oid_master); 1208 } 1209 } 1210 1211 fn testPushLarge( 1212 comptime repo_kind: rp.RepoKind, 1213 comptime transport_def: net.TransportDefinition, 1214 comptime shell_out_to_git: bool, 1215 comptime port: u16, 1216 io: std.Io, 1217 allocator: std.mem.Allocator, 1218 ) !void { 1219 const temp_dir_name = "temp-testnet-push-large"; 1220 1221 // create the temp dir 1222 const cwd = std.Io.Dir.cwd(); 1223 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1224 if (temp_dir_or_err) |*temp_dir| { 1225 temp_dir.close(io); 1226 try cwd.deleteTree(io, temp_dir_name); 1227 } else |_| {} 1228 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1229 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1230 defer temp_dir.close(io); 1231 1232 // init server 1233 var server_process = try runServer(io, allocator, temp_dir_name, port); 1234 defer _ = server_process.kill(io); 1235 1236 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1237 defer allocator.free(cwd_path); 1238 1239 // register the repo under admin and locate its on-disk directory 1240 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1241 defer allocator.free(server_path); 1242 1243 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1244 defer server_repo.deinit(io, allocator); 1245 1246 // add config 1247 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1248 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1249 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1250 1251 // export server repo 1252 { 1253 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1254 defer export_file.close(io); 1255 } 1256 1257 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1258 defer allocator.free(client_path); 1259 1260 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1261 defer client_repo.deinit(io, allocator); 1262 1263 var client_dir = try cwd.openDir(io, client_path, .{}); 1264 defer client_dir.close(io); 1265 1266 { 1267 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1268 defer hello_txt.close(io); 1269 try hello_txt.writeStreamingAll(io, "hello, world!"); 1270 try client_repo.add(io, allocator, &.{"hello.txt"}); 1271 } 1272 1273 // copy files from current repo into client dir 1274 for (&[_][]const u8{"src"}) |dir_name| { 1275 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1276 defer src_repo_dir.close(io); 1277 1278 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1279 defer dest_repo_dir.close(io); 1280 1281 try copyDir(io, src_repo_dir, dest_repo_dir); 1282 1283 try client_repo.add(io, allocator, &.{dir_name}); 1284 } 1285 1286 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1287 1288 // change the files so git will send them as delta objects 1289 for (&[_][]const u8{"src"}) |dir_name| { 1290 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1291 defer dest_repo_dir.close(io); 1292 1293 { 1294 var iter = dest_repo_dir.iterate(); 1295 while (try iter.next(io)) |entry| { 1296 switch (entry.kind) { 1297 .file => { 1298 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1299 defer file.close(io); 1300 var writer = file.writer(io, &.{}); 1301 try writer.interface.writeAll("EDIT"); 1302 }, 1303 else => {}, 1304 } 1305 } 1306 } 1307 1308 try client_repo.add(io, allocator, &.{dir_name}); 1309 } 1310 1311 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1312 1313 // add remote 1314 { 1315 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1316 defer allocator.free(remote_url); 1317 1318 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1319 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1320 } 1321 1322 const is_ssh = switch (transport_def) { 1323 .file => false, 1324 .wire => |wire_kind| .ssh == wire_kind, 1325 }; 1326 1327 if (shell_out_to_git) { 1328 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1329 defer allocator.free(priv_key_path); 1330 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{ port + 1, priv_key_path }); 1331 defer allocator.free(ssh_config_arg); 1332 1333 // shell out to git so it will send delta objects 1334 var process = try std.process.spawn(io, .{ 1335 .argv = if (is_ssh) 1336 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1337 else 1338 &.{ "git", "push", "origin", "master" }, 1339 .cwd = .{ .path = client_path }, 1340 .stdin = .ignore, 1341 .stdout = .ignore, 1342 .stderr = .ignore, 1343 }); 1344 const term = try process.wait(io); 1345 if (term != .exited or term.exited != 0) { 1346 return error.GitCommandFailed; 1347 } 1348 } else { 1349 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name, port); 1350 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1351 1352 try client_repo.push( 1353 io, 1354 allocator, 1355 "origin", 1356 "master", 1357 false, 1358 .{ .wire = .{ .ssh = .{ 1359 .command = ssh_cmd_maybe, 1360 } } }, 1361 ); 1362 } 1363 1364 // make sure push was successful 1365 { 1366 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1367 try std.testing.expectEqualStrings(&commit2, &oid_master); 1368 1369 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1370 defer hello_txt.close(io); 1371 } 1372 } 1373 1374 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1375 var iter = src_dir.iterate(); 1376 while (try iter.next(io)) |entry| { 1377 switch (entry.kind) { 1378 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1379 .directory => { 1380 try dest_dir.createDirPath(io, entry.name); 1381 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1382 defer dest_entry_dir.close(io); 1383 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1384 defer src_entry_dir.close(io); 1385 try copyDir(io, src_entry_dir, dest_entry_dir); 1386 }, 1387 else => {}, 1388 } 1389 } 1390 } 1391 1392 // the dev SSH public key matching the private key written by runServer 1393 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1394 1395 // create the admin event repo with a user holding the dev SSH public key 1396 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1397 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1398 defer allocator.free(cwd_path); 1399 1400 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1401 defer allocator.free(admin_repo_path); 1402 1403 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1404 defer repo.deinit(io, allocator); 1405 1406 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1407 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1408 1409 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1410 const user_id = evt.EventWithId.randomId(prng.random()); 1411 1412 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1413 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1414 1415 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1416 .id = std.fmt.bytesToHex(user_id, .lower), 1417 .author = .{ .name = "admin", .email = "admin@example.test" }, 1418 .event = .{ .user = .{ 1419 .name = "admin", 1420 .email = "admin@example.test", 1421 .password_hash = password_hash, 1422 .ssh_keys = admin_ssh_key, 1423 } }, 1424 }}); 1425 } 1426 1427 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1428 // the event store 1429 fn repoOnDiskPath( 1430 io: std.Io, 1431 allocator: std.mem.Allocator, 1432 data_dir_name: []const u8, 1433 repo_name: []const u8, 1434 create: bool, 1435 ) !?[]u8 { 1436 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1437 defer allocator.free(cwd_path); 1438 1439 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1440 defer allocator.free(admin_repo_path); 1441 1442 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1443 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1444 } 1445 1446 // build the remote URL addressing admin/<repo_name> over the given transport 1447 fn remoteUrl( 1448 comptime transport_def: net.TransportDefinition, 1449 allocator: std.mem.Allocator, 1450 repo_name: []const u8, 1451 port: u16, 1452 ) ![]u8 { 1453 return switch (transport_def) { 1454 .file => unreachable, 1455 .wire => |wire_kind| switch (wire_kind) { 1456 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1457 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1458 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1459 }, 1460 }; 1461 } 1462 1463 // the ssh command the haxy git client should invoke, or null when not using 1464 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1465 // and skip writing to known_hosts. 1466 fn sshCommand( 1467 is_ssh: bool, 1468 allocator: std.mem.Allocator, 1469 cwd_path: []const u8, 1470 temp_dir_name: []const u8, 1471 port: u16, 1472 ) !?[]u8 { 1473 if (!is_ssh) return null; 1474 1475 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1476 defer allocator.free(priv_key_path); 1477 1478 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1479 } 1480