const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const checkinHash = support.checkinHash;
const chmodFile = support.chmodFile;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const stub_signer = support.stub_signer;
const writeFile = support.writeFile;
test "settings keep-glob: clean never deletes a kept file, extras still lists it" {
// The distinction that justifies a second glob list. `ignore-glob` removes a
// file from the extras set entirely; `keep-glob` leaves it visible and only
// makes it undeletable — "keep telling me this is here, never remove it",
// for local config and scratch.
//
// It carries more weight in zit than in fossil: fossil's `clean` is undoable
// by default and zit's is not, so this list is the only thing between a
// mistyped glob and permanent loss.
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, "t.txt", "tracked\n");
try addManaged(gpa, io, tmp.dir, &.{"t.txt"});
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
try writeFile(io, tmp.dir, ".env", "SECRET=1\n");
try writeFile(io, tmp.dir, "junk.tmp", "junk\n");
var set = try runZit(gpa, io, tmp.dir, &.{ "settings", "keep-glob", ".env" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
// extras is UNAFFECTED: a kept file is still an extra.
var ex = try runZit(gpa, io, tmp.dir, &.{ "extras", "--dotfiles" });
defer freeResult(gpa, &ex);
try expectExitDiag(ex.term, 0, ex);
try std.testing.expect(std.mem.indexOf(u8, ex.stdout, ".env") != null);
try std.testing.expect(std.mem.indexOf(u8, ex.stdout, "junk.tmp") != null);
// The preview omits the kept file and says how many it protected.
var dry = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run", "--dotfiles" });
defer freeResult(gpa, &dry);
try expectExitDiag(dry.term, 0, dry);
try std.testing.expect(std.mem.indexOf(u8, dry.stdout, "junk.tmp") != null);
try std.testing.expect(std.mem.indexOf(u8, dry.stdout, ".env") == null);
try std.testing.expect(std.mem.indexOf(u8, dry.stderr, "kept by keep-glob") != null);
// And the sweep agrees with the preview -- the property that matters, since
// a file previewed as safe but deleted here is unrecoverable.
var force = try runZit(gpa, io, tmp.dir, &.{ "clean", "--force", "--dotfiles" });
defer freeResult(gpa, &force);
try expectExitDiag(force.term, 0, force);
try std.testing.expect(tmp.dir.statFile(io, ".env", .{}) != error.FileNotFound);
try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "junk.tmp", .{}));
// A tracked .zit-settings/keep-glob overrides the stored value, which is how
// the protection travels to everyone working on the project.
try tmp.dir.createDir(io, ".zit-settings", .default_dir);
try writeFile(io, tmp.dir, ".zit-settings/keep-glob", "*.tmp\n");
try writeFile(io, tmp.dir, "junk.tmp", "back\n");
try writeFile(io, tmp.dir, ".env", "SECRET=1\n");
var dry2 = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run", "--dotfiles" });
defer freeResult(gpa, &dry2);
try expectExitDiag(dry2.term, 0, dry2);
// The versioned list protects *.tmp and says nothing about .env, so the
// repository's own value is fully replaced, not merged.
try std.testing.expect(std.mem.indexOf(u8, dry2.stdout, "junk.tmp") == null);
try std.testing.expect(std.mem.indexOf(u8, dry2.stdout, ".env") != null);
}
test "cli settings: main-branch reads and sets this repository's main line, and only this repository's" {
// A forge needs a repository's main branch and cannot guess it from
// names. Fossil keeps it local, so it never arrives by sync: it has to be
// readable, and settable where the repository is served.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "r", .default_dir);
var r = try tmp.dir.openDir(io, "r", .{});
defer r.close(io);
try initRepo(gpa, io, r);
try writeFile(io, r, "a.txt", "a\n");
try addManaged(gpa, io, r, &.{"a.txt"});
var root = try runCommitEnv(gpa, io, r, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExitDiag(root.term, 0, root);
// Unset: the default, and not listed as changed.
var def = try runZit(gpa, io, r, &.{ "settings", "main-branch" });
defer freeResult(gpa, &def);
try expectExitDiag(def.term, 0, def);
try std.testing.expectEqualStrings("main-branch trunk\n", def.stdout);
var changed0 = try runZit(gpa, io, r, &.{ "settings", "--changed" });
defer freeResult(gpa, &changed0);
try std.testing.expect(std.mem.indexOf(u8, changed0.stdout, "main-branch") == null);
try writeFile(io, r, "b.txt", "b\n");
try addManaged(gpa, io, r, &.{"b.txt"});
var mainline = try runCommitEnv(gpa, io, r, .{ .user = "u" }, &.{ "commit", "-m", "main", "--branch", "main" });
defer freeResult(gpa, &mainline);
try expectExitDiag(mainline.term, 0, mainline);
var set = try runZit(gpa, io, r, &.{ "settings", "main-branch", "main" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
try std.testing.expectEqualStrings("", set.stderr);
var changed1 = try runZit(gpa, io, r, &.{ "settings", "--changed" });
defer freeResult(gpa, &changed1);
try std.testing.expect(std.mem.indexOf(u8, changed1.stdout, "main-branch main\n") != null);
// Readable from a bare store, which is how a forge asks.
var bare = try runZit(gpa, io, tmp.dir, &.{ "settings", "main-branch", "--value", "-R", "r" });
defer freeResult(gpa, &bare);
try expectExitDiag(bare.term, 0, bare);
try std.testing.expectEqualStrings("main\n", bare.stdout);
// A fact about one repository: no --global value; a name a branch tag
// cannot carry is refused; a branch not there yet is a note, not a refusal.
var global = try runZit(gpa, io, r, &.{ "settings", "--global", "main-branch", "x" });
defer freeResult(gpa, &global);
try expectExit(global.term, 2);
var spaced = try runZit(gpa, io, r, &.{ "settings", "main-branch", "a b" });
defer freeResult(gpa, &spaced);
try expectExit(spaced.term, 2);
var ahead = try runZit(gpa, io, r, &.{ "settings", "main-branch", "next" });
defer freeResult(gpa, &ahead);
try expectExitDiag(ahead.term, 0, ahead);
try std.testing.expect(std.mem.indexOf(u8, ahead.stderr, "no open branch is named 'next'") != null);
// Empty resets it.
var reset = try runZit(gpa, io, r, &.{ "settings", "main-branch", "" });
defer freeResult(gpa, &reset);
try expectExitDiag(reset.term, 0, reset);
var after = try runZit(gpa, io, r, &.{ "settings", "main-branch" });
defer freeResult(gpa, &after);
try std.testing.expectEqualStrings("main-branch trunk\n", after.stdout);
}
test "cli settings: --changed lists only non-defaults, --value prints bare values, autosync pullonly round-trips, and the editor setting drives commit" {
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 none = try runZit(gpa, io, tmp.dir, &.{ "settings", "--changed" });
defer freeResult(gpa, &none);
try expectExitDiag(none.term, 0, none);
try std.testing.expectEqualStrings("", none.stdout);
var po = try runZit(gpa, io, tmp.dir, &.{ "settings", "autosync", "pullonly" });
defer freeResult(gpa, &po);
try expectExitDiag(po.term, 0, po);
var bad = try runZit(gpa, io, tmp.dir, &.{ "settings", "autosync", "sideways" });
defer freeResult(gpa, &bad);
try expectExit(bad.term, 2);
try writeFile(io, tmp.dir, "ed.sh", "#!/bin/sh\necho 'from the editor setting' > \"$1\"\n");
try chmodFile(io, tmp.dir, "ed.sh", 0o755);
var ed = try runZit(gpa, io, tmp.dir, &.{ "settings", "editor", "./ed.sh" });
defer freeResult(gpa, &ed);
try expectExitDiag(ed.term, 0, ed);
var changed = try runZit(gpa, io, tmp.dir, &.{ "settings", "--changed" });
defer freeResult(gpa, &changed);
try expectExitDiag(changed.term, 0, changed);
try std.testing.expectEqualStrings("autosync pullonly\neditor ./ed.sh\n", changed.stdout);
var val = try runZit(gpa, io, tmp.dir, &.{ "settings", "--value", "autosync" });
defer freeResult(gpa, &val);
try std.testing.expectEqualStrings("pullonly\n", val.stdout);
// No -m and no terminal: the configured editor supplies the comment.
try writeFile(io, tmp.dir, "a", "a\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
var cm = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{"commit"});
defer freeResult(gpa, &cm);
try expectExitDiag(cm.term, 0, cm);
var tl = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "1" });
defer freeResult(gpa, &tl);
try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "from the editor setting") != null);
}
test "cli settings -R: a bare store answers reads and takes repository- and global-tier writes" {
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();
// The store carries a checkout beside it, but every command below runs from
// `away` and reaches the store ONLY through -R, so the checkout is never in
// play -- exactly a forge's bare store, which the settings verb refused to
// serve at all until bare stores were supported.
try initRepo(gpa, io, store.dir);
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 away_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &away.sub_path });
defer gpa.free(away_abs);
// A bare read: the full listing, with the default value in effect.
{
var r = try runZit(gpa, io, away.dir, &.{ "settings", "-R", store_abs });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "autosync on") != null);
}
// A repository-tier write lands in the store's config table -- no checkout.
{
var r = try runZit(gpa, io, away.dir, &.{ "settings", "-R", store_abs, "autosync", "off" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expectEqualStrings("autosync off\n", r.stdout);
}
// ...and the next read through -R reports it back.
{
var r = try runZit(gpa, io, away.dir, &.{ "settings", "-R", store_abs, "autosync" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expectEqualStrings("autosync off\n", r.stdout);
}
// A bad -R path is one of the two -R mistakes, named as such -- not the
// discovery message that would tell an operator to supply the flag they
// already supplied.
{
var r = try runZit(gpa, io, away.dir, &.{ "settings", "-R", away_abs, "autosync" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "no repository at") != null);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "no .zit there") != null);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "name it with -R") == null);
}
}
test "settings: clearsign and pgp-command persist locally, drive commit without --sign, and the environment still wins" {
// PGP left two open questions; settings settled them with fossil's own answer:
// `clearsign` is a plain local boolean in no config group, `pgp-command` is
// SENSITIVE and propagates nowhere. Both therefore live in the repo-local
// config frame — which is what this exercises, through the real binary and
// a real second process reading the frame back.
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"});
// Defaults first, so the changes below are attributable.
var bare = try runZit(gpa, io, tmp.dir, &.{"settings"});
defer freeResult(gpa, &bare);
try expectExitDiag(bare.term, 0, bare);
try std.testing.expect(std.mem.indexOf(u8, bare.stdout, "clearsign off\n") != null);
// Unset now displays the DEFAULT in force, not an empty value: the listing
// answers "what is this setting" rather than "what has this repository
// stored", which is the whole point of resolving across tiers.
try std.testing.expect(std.mem.indexOf(u8, bare.stdout, "pgp-command gpg --clearsign -o \n") != null);
var set_cmd = try runZit(gpa, io, tmp.dir, &.{ "settings", "pgp-command", stub_signer });
defer freeResult(gpa, &set_cmd);
try expectExitDiag(set_cmd.term, 0, set_cmd);
var set_flag = try runZit(gpa, io, tmp.dir, &.{ "settings", "clearsign", "on" });
defer freeResult(gpa, &set_flag);
try expectExitDiag(set_flag.term, 0, set_flag);
// A FRESH process reads the v4 config frame back: this is the storage
// round trip, not an in-memory one.
var back = try runZit(gpa, io, tmp.dir, &.{"settings"});
defer freeResult(gpa, &back);
try expectExitDiag(back.term, 0, back);
try std.testing.expect(std.mem.indexOf(u8, back.stdout, "clearsign on\n") != null);
try std.testing.expect(std.mem.indexOf(u8, back.stdout, stub_signer) != null);
// The point of `clearsign`: no --sign on the command line, signed anyway.
// `.pgp = null` leaves $ZIT_PGP_COMMAND unset, so the stored setting is the
// only thing that can be supplying the signer.
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = null }, &.{ "commit", "-m", "by setting" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
const hash = try checkinHash(c.stdout);
var art = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
defer freeResult(gpa, &art);
try expectExitDiag(art.term, 0, art);
try std.testing.expect(std.mem.startsWith(u8, art.stdout, "-----BEGIN PGP SIGNED MESSAGE-----"));
// §4.1.1 still reaches the cards through the wrapper: the summary names the
// signature AND lists the cards it read from the stripped body.
var summary = try runZit(gpa, io, tmp.dir, &.{ "artifact", hash });
defer freeResult(gpa, &summary);
try expectExitDiag(summary.term, 0, summary);
try std.testing.expect(std.mem.indexOf(u8, summary.stdout, "signature: clearsigned") != null);
try std.testing.expect(std.mem.indexOf(u8, summary.stdout, "C by setting") != null);
// Precedence: the environment overrides the stored command, so an operator
// can redirect one invocation without editing the repository. `off` is
// fossil's disable spelling, and disabling while `clearsign` demands a
// signature is the contradiction that must be refused, not silently
// committed unsigned.
try writeFile(io, tmp.dir, "f.txt", "y\n");
var over = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = "off" }, &.{ "commit", "-m", "env wins" });
defer freeResult(gpa, &over);
try expectExit(over.term, 1);
try std.testing.expect(std.mem.indexOf(u8, over.stderr, "--sign failed") != null);
// Turning it back off restores the unsigned default.
var off = try runZit(gpa, io, tmp.dir, &.{ "settings", "clearsign", "off" });
defer freeResult(gpa, &off);
try expectExitDiag(off.term, 0, off);
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice", .pgp = null }, &.{ "commit", "-m", "plain" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
const hash2 = try checkinHash(c2.stdout);
var art2 = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash2 });
defer freeResult(gpa, &art2);
try expectExitDiag(art2.term, 0, art2);
try std.testing.expect(!std.mem.startsWith(u8, art2.stdout, "-----BEGIN"));
// A boolean setting's vocabulary is fixed and documented, so a bad value is
// a usage error (2), not a failure (1).
var bad = try runZit(gpa, io, tmp.dir, &.{ "settings", "clearsign", "yes" });
defer freeResult(gpa, &bad);
try expectExit(bad.term, 2);
}
test "settings: the global tier drives commits, and a repository value overrides it" {
// The cross-surface agreement, pinned because it was briefly WRONG: the
// listing resolved across tiers while the commit path still read the config
// frame directly, so `zit settings` reported `clearsign on` from the global
// file and the commit quietly did not sign. A settings display that
// disagrees with the behaviour it describes is worse than no global tier.
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 writeFile(io, repo, "f.txt", "x\n");
try addManaged(gpa, io, repo, &.{"f.txt"});
// Relative to the child's cwd (the repo), so it lands beside it in the
// test's tmp tree rather than inside the repository -- and, more to the
// point, nowhere near the operator's real settings file.
const cfg = "../zit-global-settings";
const env: []const [2][]const u8 = &.{.{ "ZIT_CONFIG", cfg }};
var set_flag = try runZitEnv(gpa, io, repo, env, &.{ "settings", "--global", "clearsign", "on" });
defer freeResult(gpa, &set_flag);
try expectExitDiag(set_flag.term, 0, set_flag);
var set_cmd = try runZitEnv(gpa, io, repo, env, &.{ "settings", "--global", "pgp-command", stub_signer });
defer freeResult(gpa, &set_cmd);
try expectExitDiag(set_cmd.term, 0, set_cmd);
// The listing reports the global tier as the source.
var listed = try runZitEnv(gpa, io, repo, env, &.{ "settings", "clearsign" });
defer freeResult(gpa, &listed);
try expectExitDiag(listed.term, 0, listed);
try std.testing.expectEqualStrings("clearsign on\n", listed.stdout);
try std.testing.expect(std.mem.indexOf(u8, listed.stderr, "global tier") != null);
// Nothing is stored in the repository, so a signed commit can only mean the
// commit path consulted the same global tier the listing just reported.
var c = try runCommitEnv(gpa, io, repo, .{ .user = "alice", .pgp = null, .zit_config = cfg }, &.{ "commit", "-m", "from global" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
const hash = try checkinHash(c.stdout);
var art = try runZit(gpa, io, repo, &.{ "artifact", "--raw", hash });
defer freeResult(gpa, &art);
try expectExitDiag(art.term, 0, art);
try std.testing.expect(std.mem.startsWith(u8, art.stdout, "-----BEGIN PGP SIGNED MESSAGE-----"));
// A repository value shadows the global one -- and does so for a BOOLEAN
// whose stored value equals its own default, which is only representable
// because the config frame records an explicit-set bit beside the value.
var off = try runZitEnv(gpa, io, repo, env, &.{ "settings", "clearsign", "off" });
defer freeResult(gpa, &off);
try expectExitDiag(off.term, 0, off);
try writeFile(io, repo, "f.txt", "y\n");
var c2 = try runCommitEnv(gpa, io, repo, .{ .user = "alice", .pgp = null, .zit_config = cfg }, &.{ "commit", "-m", "repo off" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
const hash2 = try checkinHash(c2.stdout);
var art2 = try runZit(gpa, io, repo, &.{ "artifact", "--raw", hash2 });
defer freeResult(gpa, &art2);
try expectExitDiag(art2.term, 0, art2);
try std.testing.expect(!std.mem.startsWith(u8, art2.stdout, "-----BEGIN"));
}
test "cli settings: .fossil-settings is honoured, and wins over .zit-settings" {
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, "keep.txt", "x\n");
try addManaged(gpa, io, tmp.dir, &.{"keep.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "t" }, &.{ "commit", "-m", "r" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
// A fossil-mirrored repository's own ignore policy, in fossil's spelling.
try tmp.dir.createDir(io, ".fossil-settings", .default_dir);
try writeFile(io, tmp.dir, ".fossil-settings/ignore-glob", "*.exe\n");
try writeFile(io, tmp.dir, "win.exe", "");
const t_ = std.testing;
{
var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &ex);
try expectExitDiag(ex.term, 0, ex);
try t_.expect(std.mem.indexOf(u8, ex.stdout, "win.exe") == null);
}
// Both spellings present: fossil's wins, so the zit file's *.log glob is
// inert and a.log stays visible -- documented precedence, not a union.
try tmp.dir.createDir(io, ".zit-settings", .default_dir);
try writeFile(io, tmp.dir, ".zit-settings/ignore-glob", "*.log\n");
try writeFile(io, tmp.dir, "a.log", "");
{
var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &ex);
try expectExitDiag(ex.term, 0, ex);
try t_.expect(std.mem.indexOf(u8, ex.stdout, "a.log") != null);
try t_.expect(std.mem.indexOf(u8, ex.stdout, "win.exe") == null);
}
// The settings tier reads fossil's directory too, as the versioned tier.
try writeFile(io, tmp.dir, ".fossil-settings/crlf-glob", "*.bat\n");
{
var st = try runZit(gpa, io, tmp.dir, &.{ "settings", "crlf-glob" });
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
try t_.expect(std.mem.indexOf(u8, st.stdout, "*.bat") != null);
// The provenance note is a diagnostic and rides stderr; it must
// name the file that is live rather than the tier.
try t_.expect(std.mem.indexOf(u8, st.stderr, ".fossil-settings/crlf-glob") != null);
}
}