Skip to content
zit Artifact [cf9b02d70a]
Login

Artifact [cf9b02d70a]

Artifact cf9b02d70a16740d67665ee41466de2f6b1fc1a3ded4fba85f0703f09742f586:


const std = @import("std");
const settings_fixture = @import("zit").settings_fixture;
const support = @import("support.zig");
const addManaged = support.addManaged;
const commitFile = support.commitFile;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const spawnServe = support.spawnServe;
const touch = support.touch;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

test "checkout settings: a versioned glob list splits on whitespace and quotes, and drops # lines" {
    // Expectations from fossil `glob.c glob_create` (whitespace separates,
    // quotes group) and `db.c db_get_versioned` (`#` lines stripped first).
    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, ".fossil-settings", .default_dir);
    try writeFile(io, tmp.dir, ".fossil-settings/ignore-glob", "# build leftovers\n*.o *.a\n\"sp ace.txt\"\n");
    try touch(io, tmp.dir, "one.o");
    try touch(io, tmp.dir, "two.a");
    try touch(io, tmp.dir, "sp ace.txt");
    try touch(io, tmp.dir, "keep.txt");

    var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
    defer freeResult(gpa, &ex);
    try expectExitDiag(ex.term, 0, ex);
    // The comment line is not a pattern, both whitespace-separated globs
    // apply, and the quoted one keeps its space. (`.fossil-settings/` is an
    // untracked extra too, hidden here only by the dotfiles default.)
    try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "keep.txt\n") != null);
    try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "one.o") == null);
    try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "two.a") == null);
    try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "sp ace.txt") == null);
}

test "checkout settings: the shadow note names the live file, fires only against a stored value, and .no-warn silences it" {
    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, ".fossil-settings", .default_dir);
    try writeFile(io, tmp.dir, ".fossil-settings/ignore-glob", "*.tmp\n");

    // No stored value yet: a versioned file alone is nothing to warn about,
    // and the provenance line names the file that is live.
    {
        var st = try runZit(gpa, io, tmp.dir, &.{ "settings", "ignore-glob" });
        defer freeResult(gpa, &st);
        try expectExitDiag(st.term, 0, st);
        try std.testing.expectEqualStrings("ignore-glob *.tmp\n", st.stdout);
        try std.testing.expectEqualStrings(
            "note: ignore-glob is set in .fossil-settings/ignore-glob\n",
            st.stderr,
        );
    }

    // A repository value under it IS a shadowing, and the write says so.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "ignore-glob", "*.o" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
        try std.testing.expect(std.mem.indexOf(
            u8,
            w.stderr,
            "note: .fossil-settings/ignore-glob overrides the ignore-glob setting in the repository tier",
        ) != null);
    }
    {
        var st = try runZit(gpa, io, tmp.dir, &.{ "settings", "ignore-glob" });
        defer freeResult(gpa, &st);
        try expectExitDiag(st.term, 0, st);
        try std.testing.expect(std.mem.indexOf(u8, st.stderr, "overrides the ignore-glob setting") != null);
    }

    // A `.no-warn` in the OTHER directory silences it: one setting, two
    // spellings, and the silence follows the setting. The listing still says
    // which file is live.
    try tmp.dir.createDir(io, ".zit-settings", .default_dir);
    try writeFile(io, tmp.dir, ".zit-settings/ignore-glob.no-warn", "");
    {
        var st = try runZit(gpa, io, tmp.dir, &.{ "settings", "ignore-glob" });
        defer freeResult(gpa, &st);
        try expectExitDiag(st.term, 0, st);
        try std.testing.expectEqualStrings("ignore-glob *.tmp\n", st.stdout);
        try std.testing.expectEqualStrings(
            "note: ignore-glob is set in .fossil-settings/ignore-glob\n",
            st.stderr,
        );
    }
}

test "checkout settings: every tier of ignore-glob reaches the sweep, and .zitignore adds to it" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "repo", .default_dir);
    var repo = try tmp.dir.openDir(io, "repo", .{});
    defer repo.close(io);
    try initRepo(gpa, io, repo);
    try touch(io, repo, "a.o");
    try touch(io, repo, "b.log");
    try touch(io, repo, "c.bak");
    try touch(io, repo, "keep.txt");

    const cfg = "../zit-global-settings";
    const env: []const [2][]const u8 = &.{.{ "ZIT_CONFIG", cfg }};

    // The GLOBAL tier now reaches the sweep, which is fossil's `db_get`
    // behaviour and was previously unreachable from a listing verb.
    var g = try runZitEnv(gpa, io, repo, env, &.{ "settings", "--global", "ignore-glob", "*.o" });
    defer freeResult(gpa, &g);
    try expectExitDiag(g.term, 0, g);
    {
        var ex = try runZitEnv(gpa, io, repo, env, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "a.o") == null);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "b.log") != null);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "c.bak") != null);
    }

    // A repository value shadows the global one, as everywhere else.
    var r = try runZitEnv(gpa, io, repo, env, &.{ "settings", "ignore-glob", "*.log" });
    defer freeResult(gpa, &r);
    try expectExitDiag(r.term, 0, r);
    // ... and `.zitignore` ADDS to whatever won rather than replacing it.
    try writeFile(io, repo, ".zitignore", "*.bak\n");
    {
        var ex = try runZitEnv(gpa, io, repo, env, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "a.o") != null);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "b.log") == null);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "c.bak") == null);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "keep.txt") != null);
    }
}

test "checkout settings: clean resolves keep-glob from the config it already read" {
    // clean used to open the store twice -- once for ignore-glob, once for
    // keep-glob through the sidecar-refreshing resolver. One open now answers
    // both, and the protection still holds.
    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 touch(io, tmp.dir, "junk.tmp");
    try touch(io, tmp.dir, "precious.key");

    var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "keep-glob", "*.key" });
    defer freeResult(gpa, &w);
    try expectExitDiag(w.term, 0, w);

    var res = try runZit(gpa, io, tmp.dir, &.{ "clean", "-n" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    try std.testing.expectEqualStrings("junk.tmp\n", res.stdout);
}

test "checkout settings: crnl-glob is the fallback spelling of crlf-glob, and crlf-glob wins" {
    // fossil reads `db_get("crlf-glob", db_get("crnl-glob",""))` (checkin.c),
    // so the older spelling answers only while the newer one is absent.
    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, ".zit-settings", .default_dir);
    try writeFile(io, tmp.dir, ".zit-settings/crnl-glob", "*.txt\n");

    // The class is now checked, and only `*.txt` may carry CR/LF.
    try writeFile(io, tmp.dir, "ok.txt", "a\r\nb\r\n");
    try writeFile(io, tmp.dir, "bad.dat", "a\r\nb\r\n");
    try addManaged(gpa, io, tmp.dir, &.{ "ok.txt", "bad.dat" });
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "bad.dat") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "CR/LF") != null);
    }

    // `crlf-glob` present anywhere REPLACES it: now `*.dat` is the declared
    // class and `ok.txt` is the file that is refused.
    try writeFile(io, tmp.dir, ".zit-settings/crlf-glob", "*.dat\n");
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "ok.txt") != null);
    }
}

test "checkout settings: encoding-glob gates UTF-16 and invalid UTF-8, and binary-glob does not claim them" {
    // fossil warns about "Unicode" and "invalid UTF-8" at commit time and lets
    // `encoding-glob` name the files that are exempt (checkin.c). zit keeps its
    // opt-in shape: the class is checked only once the glob is declared, and
    // then it refuses rather than prompts.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    // UTF-16 LE, "hi". The zero bytes are the high halves of ASCII units.
    const utf16 = "\xFF\xFEh\x00i\x00";
    const bad_utf8 = "hello \xC3\x28 there\n";

    // Undeclared: committed, and only mentioned in the receipt.
    try writeFile(io, tmp.dir, "seed.txt", utf16);
    try addManaged(gpa, io, tmp.dir, &.{"seed.txt"});
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "UTF-16") != null);
    }

    // Declaring binary-glob does NOT make a UTF-16 file
    // binary: it is scanned as 16-bit units, so it carries no NUL.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "binary-glob", "*.dat" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }
    try writeFile(io, tmp.dir, "wide.txt", utf16);
    try addManaged(gpa, io, tmp.dir, &.{"wide.txt"});
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "wide" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
    }

    // Declared: only `*.bin` may carry either encoding class.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "encoding-glob", "*.bin" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }
    try writeFile(io, tmp.dir, "w.txt", utf16);
    try writeFile(io, tmp.dir, "w.bin", utf16);
    try writeFile(io, tmp.dir, "bad.txt", bad_utf8);
    try addManaged(gpa, io, tmp.dir, &.{ "w.txt", "w.bin", "bad.txt" });
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "all" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "encoding-glob") != null);
    }
    // Each class names itself, so the operator knows which one they have.
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "wide", "w.txt" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "w.txt") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "UTF-16") != null);
    }
    // The exempt path commits on its own.
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "exempt", "w.bin" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
    }
    // The other encoding class is the same glob, with its own words.
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "bad", "bad.txt" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "not valid UTF-8") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "encoding-glob") != null);
    }

    // `*` is how a project turns the check back off, as for every other glob.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "encoding-glob", "*" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "off" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
    }
}

test "checkout settings: declaring encoding-glob alone does not turn binary files into refusals" {
    // fossil's `commit_warning` decides the binary class first and returns
    // from that branch, so `encoding-glob` is never consulted for a blob with
    // a NUL. Without that ordering, declaring encoding-glob would silently
    // start refusing every PNG and zip in the tree -- naming the wrong class
    // and pointing at a glob that cannot exempt them.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "encoding-glob", "*.txt" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }

    // A PNG header: a NUL, and bytes that are not valid UTF-8 either.
    try writeFile(io, tmp.dir, "pic.png", "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\xFF\xFE\x01");
    try addManaged(gpa, io, tmp.dir, &.{"pic.png"});
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "pic" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        // The receipt names the class the file is actually in.
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "pic.png is binary") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "encoding-glob") == null);
    }

    // Declaring the binary class is what starts refusing it, and it is the
    // binary glob that exempts it.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "binary-glob", "*.dat" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }
    try writeFile(io, tmp.dir, "other.png", "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\xFF\xFE\x01");
    try addManaged(gpa, io, tmp.dir, &.{"other.png"});
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "other" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "binary-glob") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "encoding-glob") == null);
    }
}

test "checkout settings: a repository crnl-glob gates the commit when crlf-glob is unset" {
    // The commit gate reads its settings out of the ONE config frame it opened
    // for the write lock, so the repository tier has to answer for both
    // spellings from that frame -- not from a second store open per name.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "crnl-glob", "*.bin" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
    }

    try writeFile(io, tmp.dir, "ok.bin", "a\r\nb\r\n");
    try writeFile(io, tmp.dir, "bad.txt", "a\r\nb\r\n");
    try addManaged(gpa, io, tmp.dir, &.{ "ok.bin", "bad.txt" });
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
        defer freeResult(gpa, &c);
        try expectExit(c.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "bad.txt") != null);
        try std.testing.expect(std.mem.indexOf(u8, c.stderr, "CR/LF") != null);
    }
    // The declared path goes in on its own.
    {
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one", "ok.bin" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
    }
}

test "checkout settings: settings manifest on writes the build stamps at the next update" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    var c1: std.process.RunResult = undefined;
    defer freeResult(gpa, &c1);
    _ = try commitFile(gpa, io, tmp.dir, "a.txt", "x\n", "root", &c1);

    // fossil's boolean spelling, which zit used to store and then match
    // against no selector letter at all -- so it produced nothing.
    var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "manifest", "on" });
    defer freeResult(gpa, &w);
    try expectExitDiag(w.term, 0, w);
    // Stored verbatim: the listing echoes what the operator wrote.
    try std.testing.expectEqualStrings("manifest on\n", w.stdout);

    var up = try runZit(gpa, io, tmp.dir, &.{"update"});
    defer freeResult(gpa, &up);
    try expectExitDiag(up.term, 0, up);
    try std.testing.expect(fileExists(io, tmp.dir, "manifest"));
    try std.testing.expect(fileExists(io, tmp.dir, "manifest.uuid"));
}

test "checkout settings: the new names are full settings at both stored tiers" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "repo", .default_dir);
    var repo = try tmp.dir.openDir(io, "repo", .{});
    defer repo.close(io);
    try initRepo(gpa, io, repo);

    const cfg = "../zit-global-settings";
    const env: []const [2][]const u8 = &.{.{ "ZIT_CONFIG", cfg }};

    const names = [_][2][]const u8{
        .{ "crnl-glob", "*.bat" },
        .{ "clean-glob", "*.tmp" },
        .{ "empty-dirs", "logs tmp" },
        .{ "encoding-glob", "*.utf16" },
        // item 8: a mirrored Fossil repository's file is not an error, even
        // though nothing reads it yet.
        .{ "mimetypes", "md text/markdown" },
        // item 13: a boolean-valued setting takes fossil's spellings at every
        // tier, so `yes` is not a usage error here.
        .{ "dotfiles", "yes" },
    };
    for (names) |n| {
        var w = try runZitEnv(gpa, io, repo, env, &.{ "settings", n[0], n[1] });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
        var r = try runZitEnv(gpa, io, repo, env, &.{ "settings", n[0], "--value" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        try std.testing.expectEqualStrings(n[1], std.mem.trimEnd(u8, r.stdout, "\n"));

        var g = try runZitEnv(gpa, io, repo, env, &.{ "settings", "--global", n[0], n[1] });
        defer freeResult(gpa, &g);
        try expectExitDiag(g.term, 0, g);
    }

    // `dotfiles on` round-trips as `on`, not re-spelled -- the frame stores
    // the operator's own text.
    var on = try runZitEnv(gpa, io, repo, env, &.{ "settings", "dotfiles", "on" });
    defer freeResult(gpa, &on);
    try expectExitDiag(on.term, 0, on);
    try std.testing.expectEqualStrings("dotfiles on\n", on.stdout);

    // And a versioned file supplies any of them.
    try repo.createDir(io, ".fossil-settings", .default_dir);
    try writeFile(io, repo, ".fossil-settings/mimetypes", "# rows\nzig text/plain\n");
    var m = try runZitEnv(gpa, io, repo, env, &.{ "settings", "mimetypes", "--value" });
    defer freeResult(gpa, &m);
    try expectExitDiag(m.term, 0, m);
    try std.testing.expectEqualStrings("zig text/plain\n", m.stdout);
}

test "checkout settings: a versioned autosync is ignored, and says so" {
    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, ".zit-settings", .default_dir);
    try writeFile(io, tmp.dir, ".zit-settings/autosync", "off\n");

    var st = try runZit(gpa, io, tmp.dir, &.{ "settings", "autosync" });
    defer freeResult(gpa, &st);
    try expectExitDiag(st.term, 0, st);
    // The file cannot supply the value, so the default still answers.
    try std.testing.expectEqualStrings("autosync on\n", st.stdout);
    try std.testing.expect(std.mem.indexOf(
        u8,
        st.stderr,
        "note: .zit-settings/autosync is versioned and ignored; autosync is a local setting",
    ) != null);
}

test "checkout settings: every tree-scanning verb hides dot names by default and shows them on request" {
    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 touch(io, tmp.dir, "plain.txt");
    try touch(io, tmp.dir, ".hidden");
    try tmp.dir.createDir(io, ".dir", .default_dir);
    try touch(io, tmp.dir, ".dir/inside");
    try tmp.dir.createDir(io, "sub", .default_dir);
    try touch(io, tmp.dir, "sub/.deep");

    // extras
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings("plain.txt\n", ex.stdout);
    }
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{ "extras", "--dotfiles" });
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings(".dir/inside\n.hidden\nplain.txt\nsub/.deep\n", ex.stdout);
    }

    // status --extra
    {
        var st = try runZit(gpa, io, tmp.dir, &.{ "status", "--extra" });
        defer freeResult(gpa, &st);
        try expectExitDiag(st.term, 0, st);
        try std.testing.expect(std.mem.indexOf(u8, st.stdout, ".hidden") == null);
        try std.testing.expect(std.mem.indexOf(u8, st.stdout, "plain.txt") != null);
    }
    {
        var st = try runZit(gpa, io, tmp.dir, &.{ "status", "--extra", "--dotfiles" });
        defer freeResult(gpa, &st);
        try expectExitDiag(st.term, 0, st);
        try std.testing.expect(std.mem.indexOf(u8, st.stdout, ".hidden") != null);
    }

    // clean --dry-run
    {
        var cl = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run" });
        defer freeResult(gpa, &cl);
        try expectExitDiag(cl.term, 0, cl);
        try std.testing.expectEqualStrings("plain.txt\n", cl.stdout);
    }
    {
        var cl = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run", "--dotfiles" });
        defer freeResult(gpa, &cl);
        try expectExitDiag(cl.term, 0, cl);
        try std.testing.expect(std.mem.indexOf(u8, cl.stdout, ".hidden") != null);
    }

    // addremove --dry-run
    {
        var ar = try runZit(gpa, io, tmp.dir, &.{ "addremove", "--dry-run" });
        defer freeResult(gpa, &ar);
        try expectExitDiag(ar.term, 0, ar);
        try std.testing.expectEqualStrings("ADDED plain.txt\n", ar.stdout);
    }
    {
        var ar = try runZit(gpa, io, tmp.dir, &.{ "addremove", "--dry-run", "--dotfiles" });
        defer freeResult(gpa, &ar);
        try expectExitDiag(ar.term, 0, ar);
        try std.testing.expect(std.mem.indexOf(u8, ar.stdout, "ADDED .hidden") != null);
    }

    // `add .` skips them, but naming one explicitly always adds it.
    {
        var a = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
        defer freeResult(gpa, &a);
        try expectExitDiag(a.term, 0, a);
        try std.testing.expectEqualStrings("ADDED  plain.txt\n", a.stdout);
    }
    {
        var a = try runZit(gpa, io, tmp.dir, &.{ "add", ".hidden" });
        defer freeResult(gpa, &a);
        try expectExitDiag(a.term, 0, a);
        try std.testing.expectEqualStrings("ADDED  .hidden\n", a.stdout);
    }
    try expectManaged(gpa, io, tmp.dir, &.{ ".hidden", "plain.txt" });
}

test "checkout settings: dotfiles spellings agree across tiers" {
    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 touch(io, tmp.dir, ".hidden");

    // The repository tier, in fossil's boolean spellings.
    for ([_][]const u8{ "on", "yes", "true", "2" }) |value| {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "dotfiles", value });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings(".hidden\n", ex.stdout);
    }
    for ([_][]const u8{ "off", "no", "false", "0" }) |value| {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "dotfiles", value });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings("", ex.stdout);
    }

    // A versioned `.zit-settings/dotfiles` wins over the stored `off`.
    try tmp.dir.createDir(io, ".zit-settings", .default_dir);
    try writeFile(io, tmp.dir, ".zit-settings/dotfiles", "true\n");
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expect(std.mem.indexOf(u8, ex.stdout, ".hidden") != null);
    }
}

test "checkout settings: add of a named dot directory expands it" {
    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.createDirPath(io, ".dir/.inner");
    try touch(io, tmp.dir, ".dir/inside");
    try touch(io, tmp.dir, ".dir/.inner/x");
    try tmp.dir.createDir(io, ".fossil-settings", .default_dir);
    try touch(io, tmp.dir, ".fossil-settings/ignore-glob");
    try touch(io, tmp.dir, "plain.txt");

    // Naming the directory adds what is beneath it, minus the dot names the
    // expansion itself discovers — fossil's `add .dir` shape.
    var a = try runZit(gpa, io, tmp.dir, &.{ "add", ".dir" });
    defer freeResult(gpa, &a);
    try expectExitDiag(a.term, 0, a);
    try std.testing.expectEqualStrings("ADDED  .dir/inside\n", a.stdout);

    // The versioned-settings directory is the workflow this rule exists for.
    var b = try runZit(gpa, io, tmp.dir, &.{ "add", ".fossil-settings" });
    defer freeResult(gpa, &b);
    try expectExitDiag(b.term, 0, b);
    try std.testing.expectEqualStrings("ADDED  .fossil-settings/ignore-glob\n", b.stdout);

    try expectManaged(gpa, io, tmp.dir, &.{ ".dir/inside", ".fossil-settings/ignore-glob" });

    // A plain directory argument still hides dot names, and the shared walk is
    // not poisoned by the exemption the two arguments above needed.
    var c = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);
    try std.testing.expectEqualStrings("ADDED  plain.txt\n", c.stdout);
}

test "checkout settings: fossil's checkout-state names are never listed and never added" {
    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, ".fslckout", "junk");
    try writeFile(io, tmp.dir, ".fslckout-journal", "junk");
    try writeFile(io, tmp.dir, "_FOSSIL_", "junk");
    try writeFile(io, tmp.dir, "_FOSSIL_-wal", "junk");
    try writeFile(io, tmp.dir, ".fos", "junk");
    try writeFile(io, tmp.dir, ".fos-shm", "junk");
    try tmp.dir.createDir(io, "sub", .default_dir);
    try writeFile(io, tmp.dir, "sub/_FOSSIL_", "junk");
    try writeFile(io, tmp.dir, "sub/.fslckout-wal", "junk");
    try writeFile(io, tmp.dir, "sub/keep.txt", "real\n");

    // Even with --dotfiles, which is what would otherwise reveal them.
    var ex = try runZit(gpa, io, tmp.dir, &.{ "extras", "--dotfiles" });
    defer freeResult(gpa, &ex);
    try expectExitDiag(ex.term, 0, ex);
    try std.testing.expectEqualStrings("sub/keep.txt\n", ex.stdout);

    var ar = try runZit(gpa, io, tmp.dir, &.{ "addremove", "--dotfiles" });
    defer freeResult(gpa, &ar);
    try expectExitDiag(ar.term, 0, ar);
    try expectManaged(gpa, io, tmp.dir, &.{"sub/keep.txt"});

    // Naming one explicitly is refused, at the root and below it.
    for ([_][]const u8{ ".fslckout", "_FOSSIL_", ".fos", "sub/_FOSSIL_" }) |name| {
        var a = try runZit(gpa, io, tmp.dir, &.{ "add", name });
        defer freeResult(gpa, &a);
        try expectExit(a.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, a.stderr, "control file") != null);
    }
    try expectManaged(gpa, io, tmp.dir, &.{"sub/keep.txt"});
}

test "checkout settings: the manifest stamps are hidden from extras and refused by add while they are written" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    var c1: std.process.RunResult = undefined;
    defer freeResult(gpa, &c1);
    _ = try commitFile(gpa, io, tmp.dir, "a.txt", "x\n", "root", &c1);

    // A file named `manifest` is ordinary content while no stamp selects it.
    try writeFile(io, tmp.dir, "manifest", "mine\n");
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings("manifest\n", ex.stdout);
        var a = try runZit(gpa, io, tmp.dir, &.{ "add", "manifest" });
        defer freeResult(gpa, &a);
        try expectExitDiag(a.term, 0, a);
        var r = try runZit(gpa, io, tmp.dir, &.{ "rm", "manifest" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
    }

    var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "manifest", "on" });
    defer freeResult(gpa, &w);
    try expectExitDiag(w.term, 0, w);
    var up = try runZit(gpa, io, tmp.dir, &.{"update"});
    defer freeResult(gpa, &up);
    try expectExitDiag(up.term, 0, up);
    try std.testing.expect(fileExists(io, tmp.dir, "manifest.uuid"));

    // Now the stamps are generated files: not extras, and not addable.
    var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
    defer freeResult(gpa, &ex);
    try expectExitDiag(ex.term, 0, ex);
    try std.testing.expectEqualStrings("", ex.stdout);

    var a = try runZit(gpa, io, tmp.dir, &.{ "add", "manifest" });
    defer freeResult(gpa, &a);
    try expectExit(a.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, a.stderr, "control file") != null);
    try expectManaged(gpa, io, tmp.dir, &.{"a.txt"});
}

test "checkout settings: update creates the empty-dirs directories, and only from the value it started with" {
    // fossil reads `empty-dirs` BEFORE the update runs (update.c), so a
    // versioned value the update itself brings in takes effect at the NEXT
    // update, not this one. zit reads it from the same config frame at the
    // same point, so the two agree.
    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"});
    const first = try commitHashDup(gpa, io, tmp.dir, "one");
    defer gpa.free(first);

    try tmp.dir.createDir(io, ".fossil-settings", .default_dir);
    try writeFile(io, tmp.dir, ".fossil-settings/empty-dirs", "build  tmp/cache\n");
    var add = try runZit(gpa, io, tmp.dir, &.{ "add", ".fossil-settings" });
    defer freeResult(gpa, &add);
    try expectExitDiag(add.term, 0, add);
    const second = try commitHashDup(gpa, io, tmp.dir, "two");
    defer gpa.free(second);

    // Back to the check-in that does not carry the setting, then clear whatever
    // the move already created so the next assertion is unambiguous.
    var back = try runZit(gpa, io, tmp.dir, &.{ "update", first });
    defer freeResult(gpa, &back);
    try expectExitDiag(back.term, 0, back);
    tmp.dir.deleteDir(io, "tmp/cache") catch {};
    tmp.dir.deleteDir(io, "tmp") catch {};
    tmp.dir.deleteDir(io, "build") catch {};

    // The update that BRINGS the setting in creates nothing: the value it read
    // was the pre-update one, which was empty.
    var bring = try runZit(gpa, io, tmp.dir, &.{"update"});
    defer freeResult(gpa, &bring);
    try expectExitDiag(bring.term, 0, bring);
    try std.testing.expect(fileExists(io, tmp.dir, ".fossil-settings/empty-dirs"));
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "build", .{}));

    // A preview creates nothing either.
    var dry = try runZit(gpa, io, tmp.dir, &.{ "update", "--dry-run" });
    defer freeResult(gpa, &dry);
    try expectExitDiag(dry.term, 0, dry);
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "build", .{}));

    // The next update sees it and lays down both names, parents included.
    var again = try runZit(gpa, io, tmp.dir, &.{"update"});
    defer freeResult(gpa, &again);
    try expectExitDiag(again.term, 0, again);
    var b = try tmp.dir.openDir(io, "build", .{});
    b.close(io);
    var t = try tmp.dir.openDir(io, "tmp/cache", .{});
    t.close(io);

    // Idempotent, and `zit clean` leaves them: they hold nothing, and the
    // --emptydirs walk only ever probes what a deletion emptied.
    var third = try runZit(gpa, io, tmp.dir, &.{"update"});
    defer freeResult(gpa, &third);
    try expectExitDiag(third.term, 0, third);
    var cl = try runZit(gpa, io, tmp.dir, &.{ "clean", "--force", "--emptydirs" });
    defer freeResult(gpa, &cl);
    try expectExitDiag(cl.term, 0, cl);
    var b2 = try tmp.dir.openDir(io, "build", .{});
    b2.close(io);
}

test "checkout settings: open creates the empty-dirs directories the check-in carries" {
    // `open` reads the value AFTER the materialization, because the versioned
    // file does not exist on disk before it. fossil's checkout reads the same
    // list out of the check-in it is installing, so the two see the same names.
    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 writeFile(io, tmp_a.dir, "hello.txt", "hi\n");
    try tmp_a.dir.createDir(io, ".fossil-settings", .default_dir);
    try writeFile(io, tmp_a.dir, ".fossil-settings/empty-dirs", "build  tmp/cache\n");
    try addManaged(gpa, io, tmp_a.dir, &.{ "hello.txt", ".fossil-settings/empty-dirs" });
    const tip = try commitHashDup(gpa, io, tmp_a.dir, "root");
    defer gpa.free(tip);

    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", 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"});
    defer freeResult(gpa, &op);
    try expectExitDiag(op.term, 0, op);
    var build = try b_dir.openDir(io, "build", .{});
    build.close(io);
    var cache = try b_dir.openDir(io, "tmp/cache", .{});
    cache.close(io);
}

/// The zit half of the settings fixture. The fixture itself lives in
/// `zit.settings_fixture` because `src/smoke.zig` S14 builds the same one on
/// the fossil side and compares the two tools byte for byte; this test is what
/// keeps zit's half pinned in a lane CI runs.
fn writeSettingsFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
    try dir.createDir(io, ".fossil-settings", .default_dir);
    for (settings_fixture.settings) |s| {
        const path = try std.fmt.allocPrint(gpa, ".fossil-settings/{s}", .{s.name});
        defer gpa.free(path);
        try writeFile(io, dir, path, s.value);
    }
    try writeFile(io, dir, settings_fixture.tracked_file.name, settings_fixture.tracked_file.value);
}

fn writeSettingsFixtureJunk(io: std.Io, dir: std.Io.Dir) !void {
    for (settings_fixture.junk_dirs) |d| try dir.createDir(io, d, .default_dir);
    for (settings_fixture.junk) |j| try writeFile(io, dir, j.path, j.data);
}

test "checkout settings: a fully populated .fossil-settings fixture drives extras and clean as one" {
    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 writeSettingsFixture(gpa, io, tmp.dir);
    {
        var add = try runZit(gpa, io, tmp.dir, &.{ "add", ".fossil-settings", settings_fixture.tracked_file.name });
        defer freeResult(gpa, &add);
        try expectExitDiag(add.term, 0, add);
    }
    const tip = try commitHashDup(gpa, io, tmp.dir, "settings fixture");
    defer gpa.free(tip);

    // The update the settings are already committed for: `manifest on` lays the
    // stamps down and `empty-dirs` creates its directory.
    {
        var up = try runZit(gpa, io, tmp.dir, &.{"update"});
        defer freeResult(gpa, &up);
        try expectExitDiag(up.term, 0, up);
    }
    try std.testing.expect(fileExists(io, tmp.dir, "manifest"));
    try std.testing.expect(fileExists(io, tmp.dir, "manifest.uuid"));
    var emptyd = try tmp.dir.openDir(io, "emptyd", .{});
    emptyd.close(io);

    try writeSettingsFixtureJunk(io, tmp.dir);

    // ignore-glob removes three of them whatever the separator; the manifest
    // stamps and the `.fos` name are never listed; `dotfiles off` hides
    // `.hidden` until it is asked for.
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings(
            "notes.txt\nprecious.dat\nscratch.tmp\nsub/deep.log\nwide.wide\n",
            ex.stdout,
        );
    }
    {
        var ex = try runZit(gpa, io, tmp.dir, &.{ "extras", "--dotfiles" });
        defer freeResult(gpa, &ex);
        try expectExitDiag(ex.term, 0, ex);
        try std.testing.expectEqualStrings(
            ".hidden\nnotes.txt\nprecious.dat\nscratch.tmp\nsub/deep.log\nwide.wide\n",
            ex.stdout,
        );
    }
    // The preview is the extras set minus what keep-glob holds back; the
    // clean-glob match is in it, because fossil's dry run skips the clean-glob
    // test entirely (checkin.c clean_cmd) and lists every non-kept candidate.
    {
        var cl = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run" });
        defer freeResult(gpa, &cl);
        try expectExitDiag(cl.term, 0, cl);
        try std.testing.expectEqualStrings(
            "notes.txt\nscratch.tmp\nsub/deep.log\nwide.wide\n",
            cl.stdout,
        );
        try std.testing.expect(std.mem.indexOf(u8, cl.stderr, "kept by keep-glob") != null);
    }
    // Nothing in the fixture warns: `ignore-glob.no-warn` covers the one name
    // that also gains a stored value below it.
    {
        var w = try runZit(gpa, io, tmp.dir, &.{ "settings", "ignore-glob", "*.zzz" });
        defer freeResult(gpa, &w);
        try expectExitDiag(w.term, 0, w);
        try std.testing.expectEqualStrings("", w.stderr);
    }
}