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