Skip to content
zit Artifact [bbf1994bbf]
Login

Artifact [bbf1994bbf]

Artifact bbf1994bbf64209cc0464d9970399bb99f0362eb38919cdd43c64882c1ed7a4a:


const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const countLines = support.countLines;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const seedHistory = support.seedHistory;
const spawnServe = support.spawnServe;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

test "cli -R: a bad --repo path says which mistake was made, not 'no checkout here'" {
    // The message for a DISCOVERY failure tells the operator to name the
    // repository with -R. Printed for a `-R` failure it advises supplying the
    // flag that was already supplied, and it reads identically whether the
    // path was mistyped or simply holds no repository -- so a stale path in a
    // script looks exactly like a bug in the verb. Both were reported
    // against `tarball`; both affected every `-R` verb.
    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, "f.txt", "x\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);

    // A path that does not exist names itself, and does not advise -R.
    var missing = try runZit(gpa, io, tmp.dir, &.{ "ls", "-R", "does-not-exist", "tip" });
    defer freeResult(gpa, &missing);
    try expectExit(missing.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, missing.stderr, "no such repository") != null);
    try std.testing.expect(std.mem.indexOf(u8, missing.stderr, "does-not-exist") != null);
    try std.testing.expect(std.mem.indexOf(u8, missing.stderr, "name it with -R") == null);

    // A real directory holding no repository is the OTHER mistake, and says so
    // differently — conflating the two is what cost the reporter time.
    try tmp.dir.createDir(io, "empty", .default_dir);
    var bare = try runZit(gpa, io, tmp.dir, &.{ "ls", "-R", "empty", "tip" });
    defer freeResult(gpa, &bare);
    try expectExit(bare.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, bare.stderr, "no repository at") != null);
    try std.testing.expect(std.mem.indexOf(u8, bare.stderr, "no .zit there") != null);

    // Without -R the discovery message is unchanged: it is correct there, and
    // it is what tells someone in the wrong directory what to do.
    var tmp_empty = std.testing.tmpDir(.{});
    defer tmp_empty.cleanup();
    var discovered = try runZit(gpa, io, tmp_empty.dir, &.{ "ls", "tip" });
    defer freeResult(gpa, &discovered);
    try expectExit(discovered.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, discovered.stderr, "no zit checkout here") != null);

    // The archive verbs this was filed against, from a directory that is not a
    // checkout: the primary complaint, pinned so it cannot regress.
    var away = std.testing.tmpDir(.{});
    defer away.cleanup();
    const store_bytes = try tmp.dir.readFileAlloc(io, ".zit", gpa, .limited(64 * 1024 * 1024));
    defer gpa.free(store_bytes);
    try away.dir.createDir(io, "store", .default_dir);
    var store_dir = try away.dir.openDir(io, "store", .{});
    defer store_dir.close(io);
    try writeFile(io, store_dir, ".zit", store_bytes);
    for ([_][]const u8{ "tarball", "zip" }) |verb| {
        var r = try runZit(gpa, io, away.dir, &.{ verb, "-R", "store", "tip", "--list" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        try std.testing.expect(std.mem.indexOf(u8, r.stdout, "f.txt") != null);
    }
}

test "cli -R: a read verb runs against a store from outside any checkout" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var store = std.testing.tmpDir(.{});
    defer store.cleanup();
    var away = std.testing.tmpDir(.{});
    defer away.cleanup();
    try seedHistory(gpa, io, store.dir, 5);

    // `away` is a plain directory with no `.zit` anywhere: today's discovery
    // walk finds nothing there, which is exactly what `-R` overrides.
    {
        var bare = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
        defer freeResult(gpa, &bare);
        try expectExit(bare.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, bare.stderr, "no zit checkout here") != null);
    }

    const cwd = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd);
    const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &store.sub_path });
    defer gpa.free(store_abs);

    var in_place = try runZit(gpa, io, store.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
    defer freeResult(gpa, &in_place);
    try expectExit(in_place.term, 0);

    // Same bytes from outside as from inside — the override changes WHERE the
    // store is found and nothing else.
    var via_r = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "-R", store_abs });
    defer freeResult(gpa, &via_r);
    try expectExit(via_r.term, 0);
    try std.testing.expectEqualStrings(in_place.stdout, via_r.stdout);
    try std.testing.expectEqual(@as(usize, 5), countLines(via_r.stdout));

    // `-R` composes with `-n` in one invocation (the two options land together
    // on the same verbs, so they are exercised together).
    var both = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "-R", store_abs, "-n", "2" });
    defer freeResult(gpa, &both);
    try expectExit(both.term, 0);
    try std.testing.expectEqual(@as(usize, 2), countLines(both.stdout));
    try std.testing.expect(std.mem.startsWith(u8, in_place.stdout, both.stdout));
}

test "cli -R: the long spelling, the .zit file itself, and a path holding no store" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var store = std.testing.tmpDir(.{});
    defer store.cleanup();
    var away = std.testing.tmpDir(.{});
    defer away.cleanup();
    try seedHistory(gpa, io, store.dir, 3);

    const cwd = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd);
    const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &store.sub_path });
    defer gpa.free(store_abs);
    const file_abs = try std.fs.path.join(gpa, &.{ store_abs, ".zit" });
    defer gpa.free(file_abs);
    const away_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &away.sub_path });
    defer gpa.free(away_abs);

    // `--repo <dir>` and `-R <dir>/.zit` both name the same store.
    var long = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "--repo", store_abs });
    defer freeResult(gpa, &long);
    try expectExit(long.term, 0);
    var byfile = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "-R", file_abs });
    defer freeResult(gpa, &byfile);
    try expectExit(byfile.term, 0);
    try std.testing.expectEqualStrings(long.stdout, byfile.stdout);
    try std.testing.expectEqual(@as(usize, 3), countLines(long.stdout));

    // A directory with no `.zit` is reported, never silently resolved to the
    // cwd. It used to report the DISCOVERY message, which advises naming the
    // repository with -R -- to someone who had just done that. The
    // property this test exists for is unchanged; the message now names which
    // of the two `-R` mistakes was made.
    var miss = try runZit(gpa, io, away.dir, &.{ "timeline", "-F", "oneline", "-R", away_abs });
    defer freeResult(gpa, &miss);
    try expectExit(miss.term, 1);
    try std.testing.expectEqualStrings("", miss.stdout);
    try std.testing.expect(std.mem.indexOf(u8, miss.stderr, "no repository at") != null);
    try std.testing.expect(std.mem.indexOf(u8, miss.stderr, "no .zit there") != null);
}

test "cli -R: every read verb accepts it; a bad value is refused rather than guessed" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var store = std.testing.tmpDir(.{});
    defer store.cleanup();
    var away = std.testing.tmpDir(.{});
    defer away.cleanup();
    try seedHistory(gpa, io, store.dir, 3);

    const cwd = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd);
    const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &store.sub_path });
    defer gpa.free(store_abs);

    // The operand-free read verbs all reach the store from outside a checkout.
    inline for (.{ "timeline", "leaves", "branch" }) |verb| {
        var res = try runZit(gpa, io, away.dir, &.{ verb, "-R", store_abs });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expect(res.stdout.len > 0);
    }

    // A dangling `-R`, an empty value, and a repeated option are all usage
    // errors: none may fall back to discovery.
    {
        var res = try runZit(gpa, io, away.dir, &.{ "timeline", "-R" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
    }
    {
        var res = try runZit(gpa, io, away.dir, &.{ "timeline", "-R", "" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "non-empty value") != null);
    }
    {
        var res = try runZit(gpa, io, away.dir, &.{ "timeline", "-R", store_abs, "--repo", store_abs });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "more than once") != null);
    }
}

test "cli -R: checkout-dependent verbs REJECT it by name rather than ignoring it" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var store = std.testing.tmpDir(.{});
    defer store.cleanup();
    var work = std.testing.tmpDir(.{});
    defer work.cleanup();
    try seedHistory(gpa, io, store.dir, 2);
    try initRepo(gpa, io, work.dir);
    try writeFile(io, work.dir, "w.txt", "w\n");

    const cwd = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd);
    const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &store.sub_path });
    defer gpa.free(store_abs);

    // Run these from a REAL checkout (`work`), so a verb that wrongly ignored
    // `-R` would succeed against the wrong repository instead of erroring for
    // an unrelated reason. That is what makes the exit-2 assertion meaningful.
    // `diff` left this list: its --from/--to mode honors -R, and
    // its worktree mode refuses in-verb (exit 1) — pinned by the
    // "cli diff -R" test.
    inline for (.{ "status", "extras", "clean", "addremove", "undo", "redo" }) |verb| {
        var res = try runZit(gpa, io, work.dir, &.{ verb, "-R", store_abs });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not supported here") != null);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "-R/--repo") != null);
    }
    // And the ones that take operands, including the write path.
    {
        var res = try runZit(gpa, io, work.dir, &.{ "add", "-R", store_abs, "w.txt" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not supported here") != null);
    }
    {
        var res = try runZit(gpa, io, work.dir, &.{ "commit", "-R", store_abs, "-m", "x" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not supported here") != null);
    }
    // `amend` writes only the repository, yet it still rewrites HISTORY, so `-R`
    // would mint a check-in in the named store from a directory whose checkout
    // the operator never named. The argv is otherwise VALID — `trunk` resolves
    // in the seeded store and `--comment` is a real override — so the only
    // reason it fails is the `-R` refusal, not a bad operand.
    {
        var res = try runZit(gpa, io, work.dir, &.{ "amend", "-R", store_abs, "trunk", "--comment", "x" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not supported here") != null);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "-R/--repo") != null);
    }
    // The rejection is a REFUSAL, not a no-op: nothing was added to the tree.
    try expectManaged(gpa, io, work.dir, &.{});
}

test "cli -R: pull, push, sync and remote work on a bare store with no working tree" {
    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", "one\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
    var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "base" });
    defer freeResult(gpa, &c1);
    try expectExitDiag(c1.term, 0, c1);
    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, tmp.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 away = std.testing.tmpDir(.{});
    defer away.cleanup();
    var cl = try runZit(gpa, io, away.dir, &.{ "clone", url, "bare" });
    defer freeResult(gpa, &cl);
    try expectExitDiag(cl.term, 0, cl);
    // No `open`: the store has no working tree. Every sync verb still works by -R.
    var rem = try runZit(gpa, io, away.dir, &.{ "remote", "-R", "bare" });
    defer freeResult(gpa, &rem);
    try expectExitDiag(rem.term, 0, rem);
    try std.testing.expect(std.mem.startsWith(u8, rem.stdout, "http://127.0.0.1:"));
    inline for (.{ "pull", "push", "sync" }) |verb| {
        var r = try runZit(gpa, io, away.dir, &.{ verb, "-R", "bare" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        try std.testing.expect(std.mem.indexOf(u8, r.stdout, ": already in sync") != null);
    }
    // And without -R, the same directory is not a checkout.
    var no = try runZit(gpa, io, away.dir, &.{"pull"});
    defer freeResult(gpa, &no);
    try expectExit(no.term, 1);
}