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