Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

runRepositoryTest

Runs the packfile indexing and checkout test.

The two testrepo repositories under testdata contain identical commit histories and contents.

To verify the contents of the packfiles using Git alone, run the following commands in an empty directory:

  1. git init --object-format=(sha1|sha256)
  2. git unpack-objects <path/to/testrepo.pack
  3. git fsck - will print one "dangling commit":
    • SHA-1: dd582c0720819ab7130b103635bd7271b9fd4feb
    • SHA-256: 7f444a92bd4572ee4a28b2c63059924a9ca1829138553ef3e7c41ee159afae7a
  4. git checkout $commit
git.runRepositoryTest
fn runRepositoryTest(io: Io, comptime format: Oid.Format, head_commit: []const u8) !void

File

Code

fn runRepositoryTest(io: Io, comptime format: Oid.Format, head_commit: []const u8) !void {
    const testrepo_pack = @embedFile("git/testdata/testrepo-" ++ @tagName(format) ++ ".pack");

    var git_dir = testing.tmpDir(.{});
    defer git_dir.cleanup();
    var pack_file = try git_dir.dir.createFile(io, "testrepo.pack", .{ .read = true });
    defer pack_file.close(io);
    try pack_file.writeStreamingAll(io, testrepo_pack);

    var pack_file_buffer: [2000]u8 = undefined;
    var pack_file_reader = pack_file.reader(io, &pack_file_buffer);

    var index_file = try git_dir.dir.createFile(io, "testrepo.idx", .{ .read = true });
    defer index_file.close(io);
    var index_file_buffer: [2000]u8 = undefined;
    var index_file_writer = index_file.writer(io, &index_file_buffer);
    try indexPack(testing.allocator, format, &pack_file_reader, &index_file_writer);

    // Arbitrary size limit on files read while checking the repository contents
    // (all files in the test repo are known to be smaller than this)
    const max_file_size = 8192;

    if (!skip_checksums) {
        const index_file_data = try git_dir.dir.readFileAlloc(io, "testrepo.idx", testing.allocator, .limited(max_file_size));
        defer testing.allocator.free(index_file_data);
        // testrepo.idx is generated by Git. The index created by this file should
        // match it exactly. Running `git verify-pack -v testrepo.pack` can verify
        // this.
        const testrepo_idx = @embedFile("git/testdata/testrepo-" ++ @tagName(format) ++ ".idx");
        try testing.expectEqualSlices(u8, testrepo_idx, index_file_data);
    }

    var index_file_reader = index_file.reader(io, &index_file_buffer);
    var repository: Repository = undefined;
    try repository.init(testing.allocator, format, &pack_file_reader, &index_file_reader);
    defer repository.deinit();

    var worktree = testing.tmpDir(.{ .iterate = true });
    defer worktree.cleanup();

    const commit_id = try Oid.parse(format, head_commit);

    var diagnostics: Diagnostics = .{ .allocator = testing.allocator };
    defer diagnostics.deinit();
    try repository.checkout(io, worktree.dir, commit_id, &diagnostics);
    try testing.expect(diagnostics.errors.items.len == 0);

    const expected_files: []const []const u8 = &.{
        "dir/file",
        "dir/subdir/file",
        "dir/subdir/file2",
        "dir2/file",
        "dir3/file",
        "dir3/file2",
        "file",
        "file2",
        "file3",
        "file4",
        "file5",
        "file6",
        "file7",
        "file8",
        "file9",
    };
    var actual_files: std.ArrayList([]u8) = .empty;
    defer actual_files.deinit(testing.allocator);
    defer for (actual_files.items) |file| testing.allocator.free(file);
    var walker = try worktree.dir.walk(testing.allocator);
    defer walker.deinit();
    while (try walker.next(io)) |entry| {
        if (entry.kind != .file) continue;
        const path = try testing.allocator.dupe(u8, entry.path);
        errdefer testing.allocator.free(path);
        mem.replaceScalar(u8, path, std.fs.path.sep, '/');
        try actual_files.append(testing.allocator, path);
    }
    mem.sortUnstable([]u8, actual_files.items, {}, struct {
        fn lessThan(_: void, a: []u8, b: []u8) bool {
            return mem.lessThan(u8, a, b);
        }
    }.lessThan);
    try testing.expectEqualDeep(expected_files, actual_files.items);

    const expected_file_contents =
        \\revision 1
        \\revision 2
        \\revision 4
        \\revision 5
        \\revision 7
        \\revision 8
        \\revision 9
        \\revision 10
        \\revision 12
        \\revision 13
        \\revision 14
        \\revision 18
        \\revision 19
        \\
    ;
    const actual_file_contents = try worktree.dir.readFileAlloc(io, "file", testing.allocator, .limited(max_file_size));
    defer testing.allocator.free(actual_file_contents);
    try testing.expectEqualStrings(expected_file_contents, actual_file_contents);
}