╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
451 "origin", 452 "master", 453 true, 454 .{ .wire = .{ .ssh = .{ 455 .command = ssh_cmd_maybe, 456 } } }, 457 )); 458 459 // verify the server ref was not updated (push was denied) 460 { 461 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 462 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 463 } 464 465 // remove denyNonFastForwards from server 466 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 467 } 468 469 // force push 470 try client_repo.push( 471 io, 472 allocator, 473 "origin", 474 "master", 475 true, 476 .{ .wire = .{ .ssh = .{ 477 .command = ssh_cmd_maybe, 478 } } }, 479 ); 480 481 // make sure push was successful 482 { 483 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 484 try std.testing.expectEqualStrings(&commit2, &oid_master); 485 } 486 487 // remove the remote tag 488 try client_repo.push( 489 io, 490 allocator, 491 "origin", 492 ":refs/tags/1.0.0", 493 false, 494 .{ .wire = .{ .ssh = .{ 495 .command = ssh_cmd_maybe, 496 } } }, 497 ); 498 499 // make sure push was successful 500 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 501 } 502 503 fn testPushFork( 504 comptime hash_kind: hash.HashKind, 505 comptime port: u16, 506 io: std.Io, 507 allocator: std.mem.Allocator, 508 ) !void { 509 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind }; 510 511 var temp = std.testing.tmpDir(.{}); 512 defer temp.cleanup(); 513 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 514 defer allocator.free(temp_path); 515 516 var server_process = try runServer(io, allocator, temp_path, port); 517 defer _ = server_process.kill(io); 518 519 const target_path = (try repoOnDiskPath(io, allocator, temp_path, "target", true)).?; 520 defer allocator.free(target_path); 521 const base_oid = blk: { 522 var target_repo = try rp.Repo(.xit, repo_opts).init(io, allocator, .{ .bare = true, .path = target_path }); 523 defer target_repo.deinit(io, allocator); 524 break :blk try commitServer(&target_repo, io, allocator, .{ .files = &.{.{ .path = "main.txt", .content = "base contents\n" }} }, .{ .author = "admin <admin@example.test>", .message = "initial code" }); 525 }; 526 527 const other_path = (try repoOnDiskPath(io, allocator, temp_path, "other", true)).?; 528 defer allocator.free(other_path); 529 530 const admin_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "admin" }); 531 defer allocator.free(admin_path); 532 const users_dir = try std.Io.Dir.path.join(allocator, &.{ temp_path, "users" }); 533 defer allocator.free(users_dir); 534 535 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 536 const fork_id = evt.EventWithId.randomId(prng.random()); 537 const fork_id_hex = std.fmt.bytesToHex(fork_id, .lower); 538 var user_id: [evt.event_id_size]u8 = undefined; 539 var repo_id: [evt.event_id_size]u8 = undefined; 540 const draft_path = blk: { 541 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 542 defer admin.deinit(io, allocator); 543 var arena = std.heap.ArenaAllocator.init(allocator); 544 defer arena.deinit(); 545 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 546 const target = (try evt.readRepoByOwnerAndName(io, allocator, &arena, moment, users_dir, "admin", "target", null)) orelse return error.NotFound; 547 repo_id = target.event_id; 548 user_id = try evt.parseEventId(&target.repo.event.user_id); 549 // patches are off on a new repo 550 try evt.updateRepo(io, allocator, users_dir, &user_id, .{ .name = "admin", .email = "admin@example.test" }, "target", "target", target.repo.event.description, target.repo.event.read_access, .read, .read, .read); 551 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 552 defer user_repo.deinit(io, allocator); 553 break :blk try fork.create(.{}, io, allocator, users_dir, .{ 554 .id = fork_id_hex, 555 .user_id = user_id, 556 .repo_id = repo_id, 557 .repo_user_id = user_id, 558 .title = "add a feature", 559 .description = "a draft patch", 560 .labels = "enhancement", 561 .target_branch = "master", 562 .author = .{ .name = "admin", .email = "admin@example.test" }, 563 .timestamp = 1, 564 }); 565 }; 566 defer allocator.free(draft_path); 567 568 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 569 defer allocator.free(client_path); 570 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 571 defer allocator.free(ssh_cmd); 572 var client = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 573 io, 574 allocator, 575 "git@localhost:admin:target", 576 client_path, 577 client_path, 578 null, 579 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 580 ); 581 defer client.deinit(io, allocator); 582 583 const source_oid = blk: { 584 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 585 defer file.close(io); 586 try file.writeStreamingAll(io, "fork contents\n"); 587 try client.add(io, allocator, &.{"feature.txt"}); 588 break :blk try client.commit(io, allocator, .{ .message = "add feature" }); 589 }; 590 591 const remote_url = try allocator.print("git@localhost:admin:target+{s}", .{&fork_id_hex}); 592 defer allocator.free(remote_url); 593 try client.addRemote(io, allocator, .{ .name = "patch", .value = remote_url }); 594 try client.push(io, allocator, "patch", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 595 596 var first_revision_id: [evt.event_id_size]u8 = undefined; 597 { 598 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 599 defer draft.deinit(io, allocator); 600 try std.testing.expect(try draft.isBare(io, allocator)); 601 var arena = std.heap.ArenaAllocator.init(allocator); 602 defer arena.deinit(); 603 const moment = try evt.currentMoment(repo_opts, &draft); 604 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 605 try std.testing.expect(!patch.removed); 606 try std.testing.expectEqualStrings(&source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 607 const selected = patch.event.revision orelse return error.NotFound; 608 try std.testing.expectEqualStrings("master", patch.event.target_branch); 609 try std.testing.expectEqualStrings(&source_oid, selected.source_oid); 610 try std.testing.expectEqual(@as(?u64, 1), selected.commit_count); 611 first_revision_id = try evt.parseEventId(&selected.id); 612 const revision = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &first_revision_id)) orelse return error.NotFound; 613 try std.testing.expectEqualStrings(&source_oid, revision.event.source_oid); 614 } 615 616 // 617 // publish, then push another revision 618 // 619 620 { 621 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 622 defer admin.deinit(io, allocator); 623 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 624 defer user_repo.deinit(io, allocator); 625 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 626 defer target.deinit(io, allocator); 627 // pushing a draft does not create haxy state or a mergeability cache 628 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 629 try std.testing.expectEqual(null, try (try target.core.latestMoment()).getCursor(hash.hashInt(hash_kind, evt.Patch.patch_id_to_mergeability_key))); 630 try target.addBranch(io, .{ .name = "feature" }); 631 try pch.publish(repo_opts, io, allocator, .{ .users_dir = users_dir }, &admin, &target, .{ 632 .id = fork_id_hex, 633 .user_id = user_id, 634 .repo_id = repo_id, 635 .author = .{ .name = "admin", .email = "admin@example.test" }, 636 .timestamp = 2, 637 }); 638 var arena = std.heap.ArenaAllocator.init(allocator); 639 defer arena.deinit(); 640 const moment = try evt.currentMoment(repo_opts, &target); 641 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 642 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &fork_id, .{ .fields = .{ 643 .title = "edited feature", 644 .description = patch.event.description, 645 .labels = patch.event.labels, 646 .target_branch = "feature", 647 } }, .{ .name = "admin", .email = "admin@example.test" }); 648 } 649 650 const feature_remote_url = try allocator.print("git@localhost:admin:target+{s}", .{&fork_id_hex}); 651 defer allocator.free(feature_remote_url); 652 try client.addRemote(io, allocator, .{ .name = "patch-to-feature", .value = feature_remote_url }); 653 654 const second_source_oid = blk: { 655 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 656 defer file.close(io); 657 try file.writeStreamingAll(io, "updated fork contents\n"); 658 try client.add(io, allocator, &.{"feature.txt"}); 659 break :blk try client.commit(io, allocator, .{ .message = "revise feature" }); 660 }; 661 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 662 663 var second_revision_id: [evt.event_id_size]u8 = undefined; 664 var second_patch_oid: [hash.hexLen(hash_kind)]u8 = undefined; 665 { 666 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 667 defer draft.deinit(io, allocator); 668 var arena = std.heap.ArenaAllocator.init(allocator); 669 defer arena.deinit(); 670 const moment = try evt.currentMoment(repo_opts, &draft); 671 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 672 try std.testing.expect(patch.removed); 673 const newest = (try evt.PatchRev.readNewest(evt.EventDB(hash_kind), hash_kind, moment, &arena)) orelse return error.NotFound; 674 second_revision_id = newest.id; 675 @memcpy(&second_patch_oid, newest.record.patch_oid); 676 try std.testing.expect(!std.mem.eql(u8, &first_revision_id, &second_revision_id)); 677 try std.testing.expectEqualStrings(&second_source_oid, newest.record.event.source_oid); 678 try std.testing.expectEqualStrings("edited feature", newest.record.event.message); 679 } 680 { 681 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 682 defer target.deinit(io, allocator); 683 var arena = std.heap.ArenaAllocator.init(allocator); 684 defer arena.deinit(); 685 const moment = try evt.currentMoment(repo_opts, &target); 686 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 687 try std.testing.expectEqualStrings("edited feature", patch.event.title); 688 const selected = patch.event.revision orelse return error.NotFound; 689 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(second_revision_id, .lower), &selected.id); 690 try std.testing.expectEqualStrings(&second_source_oid, selected.source_oid); 691 try std.testing.expectEqual(@as(?u64, 2), selected.commit_count); 692 try std.testing.expectEqualStrings("feature", patch.event.target_branch); 693 } 694 695 // 696 // reject extra refs without applying the patch update 697 // 698 699 const third_source_oid = blk: { 700 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 701 defer file.close(io); 702 try file.writeStreamingAll(io, "rejected fork contents\n"); 703 try client.add(io, allocator, &.{"feature.txt"}); 704 break :blk try client.commit(io, allocator, .{ .message = "rejected revision" }); 705 }; 706 const extra_ref_rejected = if (client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ 707 .refspecs = &.{"refs/heads/master:refs/heads/extra"}, 708 .wire = .{ .ssh = .{ .command = ssh_cmd } }, 709 })) |_| false else |_| true; 710 try std.testing.expect(extra_ref_rejected); 711 { 712 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 713 defer draft.deinit(io, allocator); 714 try std.testing.expectEqualStrings(&second_source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 715 try std.testing.expectEqual(null, try draft.readRef(io, .{ .kind = .head, .name = "extra" })); 716 var arena = std.heap.ArenaAllocator.init(allocator); 717 defer arena.deinit(); 718 const moment = try evt.currentMoment(repo_opts, &draft); 719 const newest = (try evt.PatchRev.readNewest(evt.EventDB(hash_kind), hash_kind, moment, &arena)) orelse return error.NotFound; 720 try std.testing.expectEqualSlices(u8, &second_revision_id, &newest.id); 721 } 722 723 // 724 // a removed target patch stays removed after another push 725 // 726 727 { 728 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 729 defer target.deinit(io, allocator); 730 try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, repo_opts, io, allocator, &target, &fork_id, .patch, .{ .name = "admin", .email = "admin@example.test" }); 731 } 732 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 733 { 734 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 735 defer target.deinit(io, allocator); 736 var arena = std.heap.ArenaAllocator.init(allocator); 737 defer arena.deinit(); 738 const moment = try evt.currentMoment(repo_opts, &target); 739 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 740 try std.testing.expect(patch.removed); 741 } 742 743 // 744 // restore the patch, merge a descendant of its source oid, and push the merge 745 // 746 747 { 748 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 749 defer target.deinit(io, allocator); 750 var arena = std.heap.ArenaAllocator.init(allocator); 751 defer arena.deinit(); 752 const moment = try evt.currentMoment(repo_opts, &target); 753 const removed = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 754 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 755 .id = fork_id_hex, 756 .timestamp = 4, 757 .author = .{ .name = "admin", .email = "admin@example.test" }, 758 .event = .{ .patch = removed.event }, 759 }}); 760 } 761 const merge_parents = [_][hash.hexLen(hash_kind)]u8{ base_oid, third_source_oid }; 762 const merge_oid = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 763 // later commits in the push must not be mistaken for the merge itself 764 _ = try client.commit(io, allocator, .{ .message = "after merge", .allow_empty = true }); 765 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 766 { 767 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 768 defer target.deinit(io, allocator); 769 var arena = std.heap.ArenaAllocator.init(allocator); 770 defer arena.deinit(); 771 const moment = try evt.currentMoment(repo_opts, &target); 772 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 773 try std.testing.expectEqual(.merged, patch.event.status.kind()); 774 const merged_status = switch (patch.event.status) { 775 .merged => |value| value, 776 else => return error.InvalidPatch, 777 }; 778 try std.testing.expectEqual(.source, merged_status.revision); 779 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 780 const result = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &result_id)) orelse return error.NotFound; 781 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 782 try std.testing.expectEqualStrings(&merge_oid, result.event.source_oid); 783 const imported = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &second_revision_id)) orelse return error.NotFound; 784 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 785 } 786 787 const other_url = try allocator.print("git@localhost:admin:other+{s}", .{&fork_id_hex}); 788 defer allocator.free(other_url); 789 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 790 // early rejections need no input. give ssh eof so windows openssh 791 // doesn't hang closing stdin before forwarding the error. 792 const reject_ssh_cmd = try allocator.print("{s} -n", .{ssh_cmd}); 793 defer allocator.free(reject_ssh_cmd); 794 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 795 796 // 797 // the fork remains cloneable after the target repo is removed 798 // 799 800 { 801 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 802 defer user_repo.deinit(io, allocator); 803 try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 804 } 805 const clone_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "fork-clone" }); 806 defer allocator.free(clone_path); 807 var fork_clone = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 808 io, 809 allocator, 810 remote_url, 811 clone_path, 812 clone_path, 813 null, 814 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 815 ); 816 defer fork_clone.deinit(io, allocator); 817 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 818 819 { 820 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 821 defer user_repo.deinit(io, allocator); 822 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 823 .{ 824 .id = std.fmt.bytesToHex(repo_id, .lower), 825 .timestamp = 2, 826 .author = .{ .name = "admin", .email = "admin@example.test" }, 827 .event = .{ .repo = .{ 828 .user_id = std.fmt.bytesToHex(user_id, .lower), 829 .name = "target", 830 .description = "", 831 .patch_role = .read, 832 } }, 833 }, 834 }); 835 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 836 defer admin.deinit(io, allocator); 837 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 838 .{ 839 .id = std.fmt.bytesToHex(user_id, .lower), 840 .timestamp = 2, 841 .author = .{ .name = "admin", .email = "admin@example.test" }, 842 .event = .{ .user = .{ 843 .name = "admin", 844 .email = "admin@example.test", 845 .password_hash = "", 846 .ssh_keys = "", 847 } }, 848 }, 849 }); 850 } 851 852 const denied_url = try allocator.print("git@localhost:admin:target+{s}", .{&fork_id_hex}); 853 defer allocator.free(denied_url); 854 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 855 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 856 } 857 858 fn testPushCreatesMissingRepo( 859 comptime repo_kind: rp.RepoKind, 860 comptime transport_def: net.TransportDefinition, 861 comptime hash_kind: hash.HashKind, 862 comptime port: u16, 863 io: std.Io, 864 allocator: std.mem.Allocator, 865 ) !void { 866 var temp = std.testing.tmpDir(.{}); 867 defer temp.cleanup(); 868 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 869 defer allocator.free(temp_path); 870 871 var server_process = try runServer(io, allocator, temp_path, port); 872 defer _ = server_process.kill(io); 873 874 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 875 defer allocator.free(client_path); 876 877 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true, .hash = hash_kind }).init(io, allocator, .{ .path = client_path }); 878 defer client_repo.deinit(io, allocator); 879 880 const commit1 = blk: { 881 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 882 defer hello_txt.close(io); 883 try hello_txt.writeStreamingAll(io, "hello, world!"); 884 try client_repo.add(io, allocator, &.{"hello.txt"}); 885 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 886 }; 887 888 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 889 890 // the repo is not pre-registered; the push should mint the repo event under 891 // admin and create the repo on disk 892 { 893 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 894 defer allocator.free(remote_url); 895 896 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 897 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 898 } 899 900 const is_ssh = switch (transport_def) { 901 .file => false, 902 .wire => |wire_kind| .ssh == wire_kind, 903 }; 904 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 905 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 906 907 try client_repo.push( 908 io, 909 allocator, 910 "origin", 911 "master", 912 false, 913 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 914 .command = ssh_cmd_maybe, 915 } } }, 916 ); 917 918 // the push registered admin:testrepo in the client's hash format; resolve its on-disk directory 919 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", false)).?; 920 defer allocator.free(server_path); 921 922 var server_repo = try rp.Repo(.xit, .{ .is_test = true, .hash = hash_kind }).open(io, allocator, .{ .path = server_path }); 923 defer server_repo.deinit(io, allocator); 924 try std.testing.expect(try server_repo.isBare(io, allocator)); 925 926 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 927 928 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 929 try std.testing.expectEqualStrings(&commit1, &oid_master); 930 } 931 932 fn testPushEvents( 933 comptime repo_kind: rp.RepoKind, 934 comptime transport_def: net.TransportDefinition, 935 comptime hash_kind: hash.HashKind, 936 comptime port: u16, 937 io: std.Io, 938 allocator: std.mem.Allocator, 939 ) !void { 940 // create the temp dir 941 var temp = std.testing.tmpDir(.{}); 942 defer temp.cleanup(); 943 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 944 defer allocator.free(temp_path); 945 946 // init server 947 var server_process = try runServer(io, allocator, temp_path, port); 948 defer _ = server_process.kill(io); 949 950 // register the repo under admin and locate its on-disk directory 951 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 952 defer allocator.free(server_path); 953 954 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 955 const ServerRepo = rp.Repo(.xit, repo_opts); 956 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 957 defer server_repo.deinit(io, allocator); 958 959 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 960 defer allocator.free(client_path); 961 962 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 963 defer client_repo.deinit(io, allocator); 964 965 // add remote 966 { 967 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 968 defer allocator.free(remote_url); 969 970 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 971 } 972 973 const is_ssh = switch (transport_def) { 974 .file => false, 975 .wire => |wire_kind| .ssh == wire_kind, 976 }; 977 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 978 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 979 980 var arena = std.heap.ArenaAllocator.init(allocator); 981 defer arena.deinit(); 982 983 // 984 // define test events 985 // 986 987 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 988 const issue_event_id = evt.EventWithId.randomId(prng.random()); 989 990 const events_to_push = [_]evt.EventWithId{ 991 .{ 992 .id = std.fmt.bytesToHex(issue_event_id, .lower), 993 .author = .{ .name = "haxy", .email = "user@haxy" }, 994 .event = .{ 995 .issue = .{ 996 .title = "Login form clears password on validation error", 997 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 998 .labels = "bug priority-high ui", 999 }, 1000 }, 1001 }, 1002 }; 1003 1004 // commit the issue on the client and push it to the server 1005 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 1006 try client_repo.push( 1007 io, 1008 allocator, 1009 "origin", 1010 "haxy/events", 1011 false, 1012 .{ .wire = .{ .ssh = .{ 1013 .command = ssh_cmd_maybe, 1014 } } }, 1015 ); 1016 1017 // the push consumed the issue into the server repo's db 1018 { 1019 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1020 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1021 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1022 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1023 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1024 1025 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1026 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.labels, issue.event.labels); 1027 } 1028 1029 // 1030 // edit the issue 1031 // 1032 1033 // this event edits the previous one because it has the same id 1034 const events_to_push2 = [_]evt.EventWithId{ 1035 .{ 1036 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1037 .author = .{ .name = "haxy", .email = "user@haxy" }, 1038 .event = .{ 1039 .issue = .{ 1040 .title = "Login form clears password on validation error", 1041 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1042 .labels = "bug priority-low ui", 1043 }, 1044 }, 1045 }, 1046 }; 1047 1048 // commit the edit on the client and push it to the server 1049 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1050 try client_repo.push( 1051 io, 1052 allocator, 1053 "origin", 1054 "haxy/events", 1055 false, 1056 .{ .wire = .{ .ssh = .{ 1057 .command = ssh_cmd_maybe, 1058 } } }, 1059 ); 1060 1061 // the push consumed the edit into the server repo's db 1062 { 1063 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1064 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1065 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1066 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1067 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1068 1069 // the description and labels were correctly edited 1070 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1071 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.labels, issue.event.labels); 1072 } 1073 } 1074 1075 fn testClone( 1076 comptime repo_kind: rp.RepoKind, 1077 comptime transport_def: net.TransportDefinition, 1078 comptime shell_out_to_git: bool, 1079 comptime hash_kind: hash.HashKind, 1080 comptime port: u16, 1081 io: std.Io, 1082 allocator: std.mem.Allocator, 1083 ) !void { 1084 // create the temp dir 1085 var temp = std.testing.tmpDir(.{}); 1086 defer temp.cleanup(); 1087 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1088 defer allocator.free(temp_path); 1089 1090 // init server 1091 var server_process = try runServer(io, allocator, temp_path, port); 1092 defer _ = server_process.kill(io); 1093 1094 // register the repo under admin and locate its on-disk directory 1095 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1096 defer allocator.free(server_path); 1097 1098 // init server repo with default branch name as main 1099 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1100 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = !shell_out_to_git }).init(io, allocator, .{ .bare = true, .path = server_path, .create_default_branch = "main" }); 1101 defer server_repo.deinit(io, allocator); 1102 1103 if (shell_out_to_git) { 1104 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1105 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1106 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1107 } 1108 1109 // make a commit 1110 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1111 1112 // tag first commit 1113 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1114 1115 // make a commit 1116 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1117 1118 // export server repo 1119 { 1120 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1121 defer export_file.close(io); 1122 } 1123 1124 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 1125 defer allocator.free(client_path); 1126 1127 // get remote url 1128 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1129 defer allocator.free(remote_url); 1130 1131 const is_ssh = switch (transport_def) { 1132 .file => false, 1133 .wire => |wire_kind| .ssh == wire_kind, 1134 }; 1135 1136 if (shell_out_to_git) { 1137 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1138 defer allocator.free(ssh_cmd); 1139 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1140 defer allocator.free(ssh_config_arg); 1141 1142 { 1143 var process = try std.process.spawn(io, .{ 1144 .argv = if (is_ssh) 1145 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--depth", "1", remote_url, "client" } 1146 else 1147 &.{ "git", "clone", "--quiet", "--depth", "1", remote_url, "client" }, 1148 .cwd = .{ .path = temp_path }, 1149 .stdin = .ignore, 1150 .stdout = .ignore, 1151 .stderr = .inherit, 1152 }); 1153 defer process.kill(io); 1154 const term = try process.wait(io); 1155 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1156 } 1157 1158 // make sure shallow clone was successful 1159 { 1160 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1161 hello_txt.close(io); 1162 } 1163 1164 // make a third commit on the server 1165 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1166 1167 // pull --unshallow to deepen the clone and get the new commit 1168 { 1169 var process = try std.process.spawn(io, .{ 1170 .argv = if (is_ssh) 1171 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "--unshallow" } 1172 else 1173 &.{ "git", "pull", "--quiet", "--unshallow" }, 1174 .cwd = .{ .path = client_path }, 1175 .stdin = .ignore, 1176 .stdout = .ignore, 1177 .stderr = .inherit, 1178 }); 1179 defer process.kill(io); 1180 const term = try process.wait(io); 1181 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1182 } 1183 1184 // make sure unshallow pull was successful 1185 { 1186 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1187 extra_txt.close(io); 1188 } 1189 1190 // delete client and clone again with --shallow-since 1191 try temp.dir.deleteTree(io, "client"); 1192 1193 { 1194 var process = try std.process.spawn(io, .{ 1195 .argv = if (is_ssh) 1196 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" } 1197 else 1198 &.{ "git", "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" }, 1199 .cwd = .{ .path = temp_path }, 1200 .stdin = .ignore, 1201 .stdout = .ignore, 1202 .stderr = .inherit, 1203 }); 1204 defer process.kill(io); 1205 const term = try process.wait(io); 1206 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1207 } 1208 1209 // make sure shallow clone was successful 1210 { 1211 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1212 hello_txt.close(io); 1213 } 1214 1215 // delete client and clone again with --shallow-exclude 1216 try temp.dir.deleteTree(io, "client"); 1217 1218 { 1219 var process = try std.process.spawn(io, .{ 1220 .argv = if (is_ssh) 1221 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1222 else 1223 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1224 .cwd = .{ .path = temp_path }, 1225 .stdin = .ignore, 1226 .stdout = .ignore, 1227 .stderr = .inherit, 1228 }); 1229 defer process.kill(io); 1230 const term = try process.wait(io); 1231 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1232 } 1233 1234 // make sure shallow clone was successful 1235 { 1236 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1237 hello_txt.close(io); 1238 } 1239 1240 // delete client and clone again with --filter=blob:none 1241 try temp.dir.deleteTree(io, "client"); 1242 1243 { 1244 var process = try std.process.spawn(io, .{ 1245 .argv = if (is_ssh) 1246 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1247 else 1248 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1249 .cwd = .{ .path = temp_path }, 1250 .stdin = .ignore, 1251 .stdout = .ignore, 1252 .stderr = .inherit, 1253 }); 1254 defer process.kill(io); 1255 const term = try process.wait(io); 1256 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1257 } 1258 1259 // make sure partial clone was successful 1260 { 1261 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1262 hello_txt.close(io); 1263 } 1264 1265 // delete client and clone again with --filter=tree:0 1266 try temp.dir.deleteTree(io, "client"); 1267 1268 { 1269 var process = try std.process.spawn(io, .{ 1270 .argv = if (is_ssh) 1271 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1272 else 1273 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1274 .cwd = .{ .path = temp_path }, 1275 .stdin = .ignore, 1276 .stdout = .ignore, 1277 .stderr = .inherit, 1278 }); 1279 defer process.kill(io); 1280 const term = try process.wait(io); 1281 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1282 } 1283 1284 // make sure treeless clone was successful 1285 { 1286 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1287 goodbye_txt.close(io); 1288 } 1289 } else { 1290 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1291 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1292 1293 // clone repo 1294 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1295 io, 1296 allocator, 1297 remote_url, 1298 temp_path, 1299 client_path, 1300 null, 1301 .{ .transport = .{ .wire = .{ .ssh = .{ 1302 .command = ssh_cmd_maybe, 1303 } } } }, 1304 ); 1305 defer clone_result.deinit(io, allocator); 1306 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1307 1308 // make sure HEAD points to the right default branch 1309 var current_branch_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = @splat(0); 1310 const head = try client_repo.head(io, &current_branch_buffer); 1311 try std.testing.expectEqualStrings("main", head.ref.name); 1312 1313 // make sure clone was successful 1314 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1315 defer hello_txt.close(io); 1316 } 1317 } 1318 1319 fn testFetchLarge( 1320 comptime repo_kind: rp.RepoKind, 1321 comptime transport_def: net.TransportDefinition, 1322 comptime shell_out_to_git: bool, 1323 comptime hash_kind: hash.HashKind, 1324 comptime port: u16, 1325 io: std.Io, 1326 allocator: std.mem.Allocator, 1327 ) !void { 1328 // create the temp dir 1329 var temp = std.testing.tmpDir(.{}); 1330 defer temp.cleanup(); 1331 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1332 defer allocator.free(temp_path); 1333 1334 // init server 1335 var server_process = try runServer(io, allocator, temp_path, port); 1336 defer _ = server_process.kill(io); 1337 1338 // register the repo under admin and locate its on-disk directory 1339 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1340 defer allocator.free(server_path); 1341 1342 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1343 defer server_repo.deinit(io, allocator); 1344 1345 // make a commit with files from the current repo 1346 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1347 1348 // export server repo 1349 { 1350 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1351 defer export_file.close(io); 1352 } 1353 1354 if (shell_out_to_git) { 1355 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1356 } 1357 1358 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 1359 defer allocator.free(client_path); 1360 1361 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1362 defer client_repo.deinit(io, allocator); 1363 1364 // add remote 1365 { 1366 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1367 defer allocator.free(remote_url); 1368 1369 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1370 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1371 } 1372 1373 const is_ssh = switch (transport_def) { 1374 .file => false, 1375 .wire => |wire_kind| .ssh == wire_kind, 1376 }; 1377 1378 if (shell_out_to_git) { 1379 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1380 defer allocator.free(ssh_cmd); 1381 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1382 defer allocator.free(ssh_config_arg); 1383 1384 { 1385 var process = try std.process.spawn(io, .{ 1386 .argv = if (is_ssh) 1387 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1388 else 1389 &.{ "git", "pull", "--quiet", "origin", "master" }, 1390 .cwd = .{ .path = client_path }, 1391 .stdin = .ignore, 1392 .stdout = .ignore, 1393 .stderr = .inherit, 1394 }); 1395 defer process.kill(io); 1396 const term = try process.wait(io); 1397 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1398 } 1399 1400 // make sure pull was successful 1401 { 1402 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1403 try std.testing.expectEqualStrings(&commit1, &oid_master); 1404 } 1405 1406 // make another commit on the server 1407 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1408 1409 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1410 { 1411 var process = try std.process.spawn(io, .{ 1412 .argv = if (is_ssh) 1413 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1414 else 1415 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1416 .cwd = .{ .path = client_path }, 1417 .stdin = .ignore, 1418 .stdout = .ignore, 1419 .stderr = .inherit, 1420 }); 1421 defer process.kill(io); 1422 const term = try process.wait(io); 1423 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1424 } 1425 1426 // make sure fetch with want-ref was successful 1427 { 1428 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1429 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1430 } 1431 } else { 1432 const refspecs = &.{ 1433 "+refs/heads/master:refs/heads/master", 1434 }; 1435 1436 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1437 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1438 1439 try client_repo.fetch( 1440 io, 1441 allocator, 1442 "origin", 1443 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1444 .command = ssh_cmd_maybe, 1445 } } }, 1446 ); 1447 1448 // update the working dir 1449 try client_repo.restore(io, allocator, "."); 1450 } 1451 1452 // make sure fetch was successful 1453 { 1454 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1455 try std.testing.expectEqualStrings(&commit1, &oid_master); 1456 } 1457 } 1458 1459 fn testPushLarge( 1460 comptime repo_kind: rp.RepoKind, 1461 comptime transport_def: net.TransportDefinition, 1462 comptime shell_out_to_git: bool, 1463 comptime hash_kind: hash.HashKind, 1464 comptime port: u16, 1465 io: std.Io, 1466 allocator: std.mem.Allocator, 1467 ) !void { 1468 // create the temp dir 1469 var temp = std.testing.tmpDir(.{}); 1470 defer temp.cleanup(); 1471 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1472 defer allocator.free(temp_path); 1473 1474 // init server 1475 var server_process = try runServer(io, allocator, temp_path, port); 1476 defer _ = server_process.kill(io); 1477 1478 // register the repo under admin and locate its on-disk directory 1479 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1480 defer allocator.free(server_path); 1481 1482 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1483 defer server_repo.deinit(io, allocator); 1484 1485 // add config 1486 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1487 1488 // export server repo 1489 { 1490 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1491 defer export_file.close(io); 1492 } 1493 1494 const client_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "client" }); 1495 defer allocator.free(client_path); 1496 1497 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1498 defer client_repo.deinit(io, allocator); 1499 1500 var client_dir = try temp.dir.openDir(io, "client", .{}); 1501 defer client_dir.close(io); 1502 1503 { 1504 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1505 defer hello_txt.close(io); 1506 try hello_txt.writeStreamingAll(io, "hello, world!"); 1507 try client_repo.add(io, allocator, &.{"hello.txt"}); 1508 } 1509 1510 // copy files from current repo into client dir 1511 const cwd = std.Io.Dir.cwd(); 1512 for (&[_][]const u8{"src"}) |dir_name| { 1513 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1514 defer src_repo_dir.close(io); 1515 1516 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1517 defer dest_repo_dir.close(io); 1518 1519 try copyDir(io, src_repo_dir, dest_repo_dir); 1520 1521 try client_repo.add(io, allocator, &.{dir_name}); 1522 } 1523 1524 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1525 1526 // change the files so git will send them as delta objects 1527 for (&[_][]const u8{"src"}) |dir_name| { 1528 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1529 defer dest_repo_dir.close(io); 1530 1531 { 1532 var iter = dest_repo_dir.iterate(); 1533 while (try iter.next(io)) |entry| { 1534 switch (entry.kind) { 1535 .file => { 1536 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1537 defer file.close(io); 1538 var writer = file.writer(io, &.{}); 1539 try writer.interface.writeAll("EDIT"); 1540 }, 1541 else => {}, 1542 } 1543 } 1544 } 1545 1546 try client_repo.add(io, allocator, &.{dir_name}); 1547 } 1548 1549 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1550 1551 // add remote 1552 { 1553 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1554 defer allocator.free(remote_url); 1555 1556 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1557 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1558 } 1559 1560 const is_ssh = switch (transport_def) { 1561 .file => false, 1562 .wire => |wire_kind| .ssh == wire_kind, 1563 }; 1564 1565 if (shell_out_to_git) { 1566 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1567 defer allocator.free(ssh_cmd); 1568 const ssh_config_arg = try allocator.print("core.sshCommand={s}", .{ssh_cmd}); 1569 defer allocator.free(ssh_config_arg); 1570 1571 // shell out to git so it will send delta objects 1572 var process = try std.process.spawn(io, .{ 1573 .argv = if (is_ssh) 1574 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1575 else 1576 &.{ "git", "push", "--quiet", "origin", "master" }, 1577 .cwd = .{ .path = client_path }, 1578 .stdin = .ignore, 1579 .stdout = .ignore, 1580 .stderr = .inherit, 1581 }); 1582 defer process.kill(io); 1583 const term = try process.wait(io); 1584 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1585 } else { 1586 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1587 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1588 1589 try client_repo.push( 1590 io, 1591 allocator, 1592 "origin", 1593 "master", 1594 false, 1595 .{ .wire = .{ .ssh = .{ 1596 .command = ssh_cmd_maybe, 1597 } } }, 1598 ); 1599 } 1600 1601 // make sure push was successful 1602 { 1603 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1604 try std.testing.expectEqualStrings(&commit2, &oid_master); 1605 1606 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1607 } 1608 } 1609 1610 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1611 var iter = src_dir.iterate(); 1612 while (try iter.next(io)) |entry| { 1613 switch (entry.kind) { 1614 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1615 .directory => { 1616 try dest_dir.createDirPath(io, entry.name); 1617 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1618 defer dest_entry_dir.close(io); 1619 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1620 defer src_entry_dir.close(io); 1621 try copyDir(io, src_entry_dir, dest_entry_dir); 1622 }, 1623 else => {}, 1624 } 1625 } 1626 } 1627 1628 // the dev SSH public key matching the private key written by runServer 1629 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1630 1631 // create the admin event repo with a user holding the dev SSH public key, plus 1632 // that user's repo 1633 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1634 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1635 defer allocator.free(admin_repo_path); 1636 1637 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1638 defer repo.deinit(io, allocator); 1639 1640 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1641 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1642 1643 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1644 const user_id = evt.EventWithId.randomId(prng.random()); 1645 1646 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1647 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1648 1649 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1650 defer allocator.free(users_dir); 1651 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1652 .id = std.fmt.bytesToHex(user_id, .lower), 1653 .author = .{ .name = "admin", .email = "admin@example.test" }, 1654 .event = .{ .user = .{ 1655 .name = "admin", 1656 .email = "admin@example.test", 1657 .password_hash = password_hash, 1658 .ssh_keys = admin_ssh_key, 1659 } }, 1660 }}); 1661 1662 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 1663 defer allocator.free(user_repo_path); 1664 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 1665 defer user_repo.deinit(io, allocator); 1666 } 1667 1668 // resolve admin:<repo_name> to its on-disk directory under <data_dir>/users via 1669 // the event store 1670 fn repoOnDiskPath( 1671 io: std.Io, 1672 allocator: std.mem.Allocator, 1673 data_path: []const u8, 1674 repo_name: []const u8, 1675 create: bool, 1676 ) !?[]u8 { 1677 const admin_repo_path = try std.Io.Dir.path.join(allocator, &.{ data_path, "admin" }); 1678 defer allocator.free(admin_repo_path); 1679 const users_dir = try std.Io.Dir.path.join(allocator, &.{ data_path, "users" }); 1680 defer allocator.free(users_dir); 1681 1682 const location = (try evt.resolveOrCreateRepo( 1683 io, 1684 allocator, 1685 users_dir, 1686 admin_repo_path, 1687 "admin", 1688 repo_name, 1689 if (create) .{ .read_access = .public } else null, 1690 )) orelse return null; 1691 return try evt.repoPath(allocator, users_dir, &location.owner_id, &location.repo_id); 1692 } 1693 1694 // build the remote URL addressing admin:<repo_name> over the given transport 1695 fn remoteUrl( 1696 comptime transport_def: net.TransportDefinition, 1697 allocator: std.mem.Allocator, 1698 repo_name: []const u8, 1699 port: u16, 1700 ) ![]u8 { 1701 return switch (transport_def) { 1702 .file => unreachable, 1703 .wire => |wire_kind| switch (wire_kind) { 1704 .http => try allocator.print("http://localhost:{}/admin:{s}", .{ port, repo_name }), 1705 .raw => try allocator.print("git://localhost:{}/admin:{s}", .{ port, repo_name }), 1706 .ssh => try allocator.print("git@localhost:admin:{s}", .{repo_name}), 1707 }, 1708 }; 1709 } 1710 1711 // the ssh command the haxy git client should invoke, or null when not using 1712 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1713 // and skip writing to known_hosts. 1714 fn sshCommand( 1715 is_ssh: bool, 1716 allocator: std.mem.Allocator, 1717 temp_path: []const u8, 1718 port: u16, 1719 ) !?[]u8 { 1720 if (!is_ssh) return null; 1721 1722 const priv_key_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "key" }); 1723 defer allocator.free(priv_key_path); 1724 1725 if (builtin.target.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1726 const null_path = if (builtin.target.os.tag == .windows) "NUL" else "/dev/null"; 1727 return try allocator.print("ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile={s} -o LogLevel=ERROR -o IdentitiesOnly=yes -i \"{s}\"", .{ port + 1, null_path, priv_key_path }); 1728 } 1729 1730 const ServerFiles = union(enum) { 1731 files: []const struct { path: []const u8, content: []const u8 }, 1732 dirs: []const []const u8, 1733 }; 1734 1735 /// commit files without writing the server's index or worktree. 1736 fn commitServer(repo: anytype, io: std.Io, allocator: std.mem.Allocator, files: ServerFiles, metadata: xit.object.CommitMetadata(repo.self_repo_opts.hash)) ![xit.hash.hexLen(repo.self_repo_opts.hash)]u8 { 1737 const R = @TypeOf(repo.*); 1738 const opts = repo.self_repo_opts; 1739 const Index = xit.index.Index(.xit, opts); 1740 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1741 const Ctx = struct { 1742 core: *R.Core, 1743 io: std.Io, 1744 allocator: std.mem.Allocator, 1745 files: ServerFiles, 1746 metadata: xit.object.CommitMetadata(opts.hash), 1747 result: *[xit.hash.hexLen(opts.hash)]u8, 1748 1749 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1750 var reader = std.Io.Reader.fixed(content); 1751 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1752 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1753 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1754 defer ctx.allocator.free(path_parts); 1755 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1756 } 1757 1758 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1759 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1760 defer dir.close(ctx.io); 1761 var entries = dir.iterate(); 1762 while (try entries.next(ctx.io)) |entry| { 1763 const child = try std.Io.Dir.path.join(ctx.allocator, &.{ path, entry.name }); 1764 defer ctx.allocator.free(child); 1765 switch (entry.kind) { 1766 .directory => try ctx.addDir(state, index, child), 1767 .file => { 1768 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1769 defer ctx.allocator.free(content); 1770 try ctx.addFile(state, index, child, content); 1771 }, 1772 else => {}, 1773 } 1774 } 1775 } 1776 1777 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1778 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1779 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1780 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1781 var index = if (head_oid) |oid| try Index.initFromCommit(state.readOnly(), ctx.io, ctx.allocator, &oid) else try Index.init(state.readOnly(), ctx.io, ctx.allocator); 1782 defer index.deinit(); 1783 switch (ctx.files) { 1784 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1785 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1786 } 1787 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1788 defer tree.deinit(); 1789 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1790 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1791 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1792 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .commit = try .init(ctx.metadata.message, ctx.result.*, opts.max_read_size) }); 1793 } 1794 }; 1795 try repo.core.db_file.lock(io, .exclusive); 1796 defer repo.core.db_file.unlock(io); 1797 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1798 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1799 .core = &repo.core, 1800 .io = io, 1801 .allocator = allocator, 1802 .files = files, 1803 .metadata = metadata, 1804 .result = &result, 1805 }); 1806 return result; 1807 } 1808