Skip to content
zit Artifact [599991850c]
Login

Artifact [599991850c]

Artifact 599991850c7959180c6dddd94d6ca3ba16e05c9855c3a5b90b5db08f99870f73:


const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const chmodFile = support.chmodFile;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

test "cli tarball and zip: the marker is consumed, so both operands still arrive" {
    // These two spell one parser, and it let the marker fall into the VERSION
    // slot instead of consuming it: VERSION then landed in OUTPUTFILE and
    // OUTPUTFILE was refused as one argument too many. A caller that adopted
    // the marker uniformly lost both verbs outright, and because the failure
    // needs a SECOND operand to appear, a one-operand check would have passed.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "a\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
    const tip = try commitHashDup(gpa, io, tmp.dir, "seed");
    defer gpa.free(tip);

    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "tarball", "--", "trunk", "out.tar.gz" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
    }
    try std.testing.expect((try tmp.dir.statFile(io, "out.tar.gz", .{})).size > 0);
    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "zip", "--", "trunk", "out.zip" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
    }
    try std.testing.expect((try tmp.dir.statFile(io, "out.zip", .{})).size > 0);
}

/// One member read back out of a tar stream: enough of the header to assert
/// what the archive verbs promise, plus the content bytes.
const TarMember = struct {
    name: []const u8,
    /// Octal permission bits from the header's mode field.
    mode: u32,
    type_flag: u8,
    mtime: u64,
    uid: u32,
    gid: u32,
    link_target: []const u8,
    content: []const u8,
};

fn tarOctal(field: []const u8) u64 {
    var v: u64 = 0;
    for (field) |c| {
        if (c < '0' or c > '7') break; // NUL- or space-terminated
        v = v * 8 + (c - '0');
    }
    return v;
}

fn tarCstr(field: []const u8) []const u8 {
    const end = std.mem.indexOfScalar(u8, field, 0) orelse field.len;
    return field[0..end];
}

/// Gunzip `bytes` and walk the tar blocks into members. Deliberately hand
/// rolled rather than run through a tar library: the point is to assert the
/// bytes this repository writes, and a library that disagreed with the writer
/// in the same direction twice would hide exactly the bug worth catching.
/// Understands the GNU long-name escape, since tracked paths outrun ustar.
/// The decoded archive and its members. `members` borrow `tar`, so both are
/// freed together by `deinit`.
const TarArchive = struct {
    tar: []u8,
    members: []TarMember,

    fn deinit(self: *TarArchive, gpa: std.mem.Allocator) void {
        gpa.free(self.members);
        gpa.free(self.tar);
        self.* = undefined;
    }
};

fn readTarGz(gpa: std.mem.Allocator, bytes: []const u8) !TarArchive {
    var fr = std.Io.Reader.fixed(bytes);
    const window = try gpa.alloc(u8, std.compress.flate.max_window_len);
    defer gpa.free(window);
    var dc = std.compress.flate.Decompress.init(&fr, .gzip, window);
    const tar = try dc.reader.allocRemaining(gpa, .limited(64 * 1024 * 1024));
    errdefer gpa.free(tar);
    // An archive that decoded to nothing would make every assertion below pass
    // vacuously, which is the one way this reader could hide the bug it exists
    // to catch.
    try std.testing.expect(tar.len >= 512);

    var out: std.ArrayListUnmanaged(TarMember) = .empty;
    errdefer out.deinit(gpa);
    var off: usize = 0;
    var pending_name: ?[]const u8 = null;
    while (off + 512 <= tar.len) {
        const h = tar[off..][0..512];
        var all_zero = true;
        for (h) |b| {
            if (b != 0) {
                all_zero = false;
                break;
            }
        }
        if (all_zero) break; // end-of-archive marker
        off += 512;
        const size: usize = @intCast(tarOctal(h[124..136]));
        const flag = h[156];
        const body = tar[off..][0..size];
        off += (size + 511) / 512 * 512;

        if (flag == 'L') { // GNU long name: the NEXT header's real path
            pending_name = tarCstr(body);
            continue;
        }
        const prefix = tarCstr(h[345..500]);
        const short = tarCstr(h[0..100]);
        // These fixtures stay inside ustar's 100-byte name field or use the
        // GNU escape; a prefix split would need an allocation this reader
        // deliberately does not make, so assert rather than silently join.
        try std.testing.expectEqual(@as(usize, 0), prefix.len);
        const name = pending_name orelse short;
        pending_name = null;
        try out.append(gpa, .{
            .name = name,
            .mode = @intCast(tarOctal(h[100..108])),
            .type_flag = flag,
            .mtime = tarOctal(h[136..148]),
            .uid = @intCast(tarOctal(h[108..116])),
            .gid = @intCast(tarOctal(h[116..124])),
            .link_target = tarCstr(h[157..257]),
            .content = body,
        });
    }
    return .{ .tar = tar, .members = try out.toOwnedSlice(gpa) };
}

test "cli tarball: the member set, the modes, and byte-identical repeats" {
    // Goal: tarball's stated properties, end to end against the real binary — the
    // archive equals the check-in minus control files, an executable stays
    // executable, and two runs of one VERSION produce identical bytes.
    // Method: commit a tree with a nested path and an executable, archive it,
    // and read the tar back out block by block.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try tmp.dir.createDir(io, "src", .default_dir);
    try tmp.dir.createDirPath(io, "deep/a/b/c");
    try writeFile(io, tmp.dir, "README", "readme\n");
    try writeFile(io, tmp.dir, "src/main.c", "int main(){}\n");
    try writeFile(io, tmp.dir, "configure", "#!/bin/sh\n");
    try writeFile(io, tmp.dir, "deep/a/b/c/nested.txt", "deep\n");
    // The executable bit is what makes the F card's perm token carry an `x`,
    // and losing it is the archetypal release bug: right files, right bytes,
    // and ./configure will not run.
    try chmodFile(io, tmp.dir, "configure", 0o755);
    try addManaged(gpa, io, tmp.dir, &.{
        "README", "src/main.c", "configure", "deep/a/b/c/nested.txt",
    });
    var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "base" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);

    var made = try runZit(gpa, io, tmp.dir, &.{ "tarball", "tip", "out.tar.gz", "--name", "rel-1" });
    defer freeResult(gpa, &made);
    try expectExitDiag(made.term, 0, made);

    const archive_bytes = try readFile(gpa, io, tmp.dir, "out.tar.gz");
    defer gpa.free(archive_bytes);
    var arch = try readTarGz(gpa, archive_bytes);
    defer arch.deinit(gpa);
    const members = arch.members;

    // Canonical filename order, every member under the one top directory, and
    // NO control file — `.zit`, `.zit-checkout`, and the cache all sit in the
    // same tree this archive was cut from.
    try std.testing.expectEqual(@as(usize, 4), members.len);
    try std.testing.expectEqualStrings("rel-1/README", members[0].name);
    try std.testing.expectEqualStrings("rel-1/configure", members[1].name);
    try std.testing.expectEqualStrings("rel-1/deep/a/b/c/nested.txt", members[2].name);
    try std.testing.expectEqualStrings("rel-1/src/main.c", members[3].name);
    for (members) |m| {
        try std.testing.expect(std.mem.indexOf(u8, m.name, ".zit") == null);
        // No identity is embedded, so two machines agree.
        try std.testing.expectEqual(@as(u32, 0), m.uid);
        try std.testing.expectEqual(@as(u32, 0), m.gid);
        try std.testing.expectEqual(@as(u8, '0'), m.type_flag);
    }

    // Contents survive verbatim.
    try std.testing.expectEqualStrings("readme\n", members[0].content);
    try std.testing.expectEqualStrings("#!/bin/sh\n", members[1].content);
    try std.testing.expectEqualStrings("deep\n", members[2].content);
    try std.testing.expectEqualStrings("int main(){}\n", members[3].content);

    // Modes come from the F card, not from the disk the archive was written on.
    try std.testing.expectEqual(@as(u32, 0o644), members[0].mode);
    try std.testing.expectEqual(@as(u32, 0o755), members[1].mode); // configure
    try std.testing.expectEqual(@as(u32, 0o644), members[2].mode);

    // Every member carries the CHECK-IN's time, one value, never the clock.
    for (members) |m| try std.testing.expectEqual(members[0].mtime, m.mtime);

    // A BARE store archives too, with no checkout anywhere: cutting a release
    // from a forge mirror is the primary use of this verb, not an edge case.
    // (This assertion exists because the `-R` wiring was silently dropped once
    // — the dispatch handed the verb the pre-override environment — and every
    // other test in this file ran from inside a checkout, so none noticed.)
    try tmp.dir.createDir(io, "bare", .default_dir);
    var bare = try tmp.dir.openDir(io, "bare", .{});
    defer bare.close(io);
    const store_bytes = try tmp.dir.readFileAlloc(io, ".zit", gpa, .limited(64 * 1024 * 1024));
    defer gpa.free(store_bytes);
    try writeFile(io, bare, ".zit", store_bytes);
    var bare_list = try runZit(gpa, io, tmp.dir, &.{ "tarball", "-R", "bare", "tip", "--list", "--name", "rel-1" });
    defer freeResult(gpa, &bare_list);
    try expectExitDiag(bare_list.term, 0, bare_list);
    try std.testing.expectEqualStrings(
        "rel-1/README\nrel-1/configure\nrel-1/deep/a/b/c/nested.txt\nrel-1/src/main.c\n",
        bare_list.stdout,
    );

    // Reproducible: a second run of the same VERSION is byte-identical.
    var again = try runZit(gpa, io, tmp.dir, &.{ "tarball", "tip", "again.tar.gz", "--name", "rel-1" });
    defer freeResult(gpa, &again);
    try expectExitDiag(again.term, 0, again);
    const again_bytes = try readFile(gpa, io, tmp.dir, "again.tar.gz");
    defer gpa.free(again_bytes);
    try std.testing.expectEqualSlices(u8, archive_bytes, again_bytes);
}

test "cli tarball: --list previews, globs select, and an elided file refuses" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try tmp.dir.createDir(io, "src", .default_dir);
    try tmp.dir.createDir(io, "src/vendor", .default_dir);
    try writeFile(io, tmp.dir, "README", "readme\n");
    try writeFile(io, tmp.dir, "src/a.c", "a\n");
    try writeFile(io, tmp.dir, "src/vendor/big.c", "vendored\n");
    try addManaged(gpa, io, tmp.dir, &.{ "README", "src/a.c", "src/vendor/big.c" });
    var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "base" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);

    // --list names the members and writes nothing, even with an OUTPUTFILE.
    var listed = try runZit(gpa, io, tmp.dir, &.{ "tarball", "tip", "preview.tar.gz", "--list" });
    defer freeResult(gpa, &listed);
    try expectExitDiag(listed.term, 0, listed);
    try std.testing.expectEqualStrings(
        "preview/README\npreview/src/a.c\npreview/src/vendor/big.c\n",
        listed.stdout,
    );
    try std.testing.expect(!fileExists(io, tmp.dir, "preview.tar.gz"));

    // The top directory is derived from the output name with the archive
    // suffix stripped; /dev/null has no usable name, so the check-in stands in.
    var devnull = try runZit(gpa, io, tmp.dir, &.{ "tarball", "tip", "/dev/null", "--list" });
    defer freeResult(gpa, &devnull);
    try expectExitDiag(devnull.term, 0, devnull);
    try std.testing.expect(std.mem.indexOf(u8, devnull.stdout, "null/README") == null);

    // Exclude wins over include on a file matching both — fossil's rule.
    var globbed = try runZit(gpa, io, tmp.dir, &.{
        "tarball", "tip", "g.tar.gz", "--list", "--include", "*.c", "--exclude", "src/vendor/*",
    });
    defer freeResult(gpa, &globbed);
    try expectExitDiag(globbed.term, 0, globbed);
    try std.testing.expectEqualStrings("g/src/a.c\n", globbed.stdout);
}

test "cli tarball: a sparse store refuses by name rather than writing a partial archive" {
    // Goal: the failure mode that looks like success. A sparse clone holds no
    // bytes for out-of-cone files, and an archive quietly missing them would
    // have the right shape and the wrong contents. Method: serve a repo, clone
    // it with a cone, and archive the tip.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp_a = std.testing.tmpDir(.{});
    defer tmp_a.cleanup();
    try initRepo(gpa, io, tmp_a.dir);
    try tmp_a.dir.createDir(io, "src", .default_dir);
    try tmp_a.dir.createDir(io, "tools", .default_dir);
    try writeFile(io, tmp_a.dir, "README", "readme\n");
    try writeFile(io, tmp_a.dir, "src/a.c", "a\n");
    try writeFile(io, tmp_a.dir, "tools/t.bin", "tools\n");
    try addManaged(gpa, io, tmp_a.dir, &.{ "README", "src/a.c", "tools/t.bin" });
    var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);

    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, tmp_a.dir, port);
    defer server.kill(io);
    try waitForListen(io, port);
    const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
    defer gpa.free(url);

    var tmp_b = std.testing.tmpDir(.{});
    defer tmp_b.cleanup();
    var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", "--cone=src", url, "repo" });
    defer freeResult(gpa, &clone);
    try expectExitDiag(clone.term, 0, clone);
    var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
    defer b_dir.close(io);
    var op = try runZit(gpa, io, b_dir, &.{ "open", "--cone=src" });
    defer freeResult(gpa, &op);
    try expectExitDiag(op.term, 0, op);

    // The in-cone file archives on its own; the elided ones refuse BY NAME.
    var refused = try runZit(gpa, io, b_dir, &.{ "tarball", "tip", "part.tar.gz" });
    defer freeResult(gpa, &refused);
    try expectExit(refused.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "partial archive") != null);
    try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "tools/t.bin") != null);
    // Named, not counted: the remedy differs per path, so a bare count would
    // send the operator back to guess which files were missing.
    try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "cone add") != null);
    try std.testing.expect(!fileExists(io, b_dir, "part.tar.gz"));

    // Narrowed to what the store actually holds, the same command succeeds.
    var ok = try runZit(gpa, io, b_dir, &.{ "tarball", "tip", "in-cone.tar.gz", "--include", "src/*" });
    defer freeResult(gpa, &ok);
    try expectExitDiag(ok.term, 0, ok);
    try std.testing.expect(fileExists(io, b_dir, "in-cone.tar.gz"));
}