const std = @import("std");
const managed = @import("zit").managed;
const cli = @import("zit").cli;
const File = std.Io.File;
const support = @import("support.zig");
const MergeDiamond = support.MergeDiamond;
const addManaged = support.addManaged;
const buildMergeDiamond = support.buildMergeDiamond;
const checkinHash = support.checkinHash;
const commitHashDup = support.commitHashDup;
const expectCheckoutVersion = support.expectCheckoutVersion;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const expectMergeState = support.expectMergeState;
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 sectionExists = support.sectionExists;
const spawnServe = support.spawnServe;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
test "cli REGRESSION B6: tag add's two positionals are both required (exit 2, stderr)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var no_name = try runZit(gpa, io, tmp.dir, &.{ "tag", "add" });
defer freeResult(gpa, &no_name);
try expectExit(no_name.term, 2);
try std.testing.expectEqualStrings("", no_name.stdout);
try std.testing.expectEqualStrings(
"zit tag: no name given\ntry 'zit tag --help'\n",
no_name.stderr,
);
var no_checkin = try runZit(gpa, io, tmp.dir, &.{ "tag", "add", "v1" });
defer freeResult(gpa, &no_checkin);
try expectExit(no_checkin.term, 2);
try std.testing.expectEqualStrings("", no_checkin.stdout);
try std.testing.expectEqualStrings(
"zit tag: no check-in given\ntry 'zit tag --help'\n",
no_checkin.stderr,
);
}
test "cli REGRESSION B6: tag add on an unresolvable CHECKIN mints nothing (exit 1)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try b6BuildRoot(gpa, io, tmp.dir);
defer gpa.free(root);
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", "nosuchname" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("zit tag: check-in 'nosuchname' not found\n", res.stderr);
}
// Resolution precedes the mint, so the failed run left no tag behind.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "tag", "list" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
}
}
// --- REGRESSION B1 --------------------------------------------------------
//
// Same-second commits resolve to the WRONG check-in, silently (blocker B1/B2,
// docs/plans/CLI-VERB-BREAKDOWN.md §B1/B2). `repostate.zig`'s selection sites
// break event.mtime ties by artifact-hash order rather than DAG ancestry, so a
// three-commit chain minted inside one second can resolve `tip`/`trunk` to an
// ANCESTOR of the newest check-in. These tests must NOT sleep between commits
// — same-second IS the repro.
/// Mints the exact CLI repro from the breakdown doc: three same-second commits
/// c1/c2/c3 rewriting `f.txt` v1 → v2 → v3 on a linear trunk. Returns c3's
/// hash (caller frees); the checkout is left at c3 with `f.txt` == "v3\n".
fn buildSameSecondChain(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]const u8 {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "f.txt", "v1\n");
try addManaged(gpa, io, dir, &.{"f.txt"});
const c1 = try commitHashDup(gpa, io, dir, "c1");
defer gpa.free(c1);
try writeFile(io, dir, "f.txt", "v2\n");
const c2 = try commitHashDup(gpa, io, dir, "c2");
defer gpa.free(c2);
try writeFile(io, dir, "f.txt", "v3\n");
return commitHashDup(gpa, io, dir, "c3");
}
test "cli REGRESSION B1: same-second commits resolve tip and trunk to the newest check-in" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const c3 = try buildSameSecondChain(gpa, io, tmp.dir);
defer gpa.free(c3);
// Ground truth: the working file holds v3, and `leaves` — which reads the
// leaf table rather than the mtime-tie selection — names c3.
const on_disk = try readFile(gpa, io, tmp.dir, "f.txt");
defer gpa.free(on_disk);
try std.testing.expectEqualStrings("v3\n", on_disk);
// `cat f.txt tip` must be the newest content, not an ancestor's.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "f.txt", "tip" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v3\n", res.stdout);
}
// `cat f.txt trunk` must agree with `tip` — two symbolic names for the
// same check-in disagreeing in one binary is the disqualifying symptom.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "f.txt", "trunk" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v3\n", res.stdout);
}
// `info tip` must name c3 and report it as a leaf. SF5's tripwire: `tip`
// resolving to a non-leaf is self-contradictory in a linear history.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", "tip" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, c3));
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "leaf:") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "leaf: no") == null);
}
// Cross-verb agreement (§5.3's invariant): `info tip` and `leaves` must
// name the same check-in.
{
var res = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, c3[0..12]) != null);
}
// `timeline` must order newest-first: c3, then c2, then c1.
{
var res = try runZit(gpa, io, tmp.dir, &.{"timeline"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const at_c3 = std.mem.indexOf(u8, res.stdout, " c3").?;
const at_c2 = std.mem.indexOf(u8, res.stdout, " c2").?;
const at_c1 = std.mem.indexOf(u8, res.stdout, " c1").?;
try std.testing.expect(at_c3 < at_c2);
try std.testing.expect(at_c2 < at_c1);
}
}
test "cli REGRESSION B1 (SF1): update accepts a symbolic name and lands on the newest check-in" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const c3 = try buildSameSecondChain(gpa, io, tmp.dir);
defer gpa.free(c3);
// Move the checkout off the tip so `update trunk` has somewhere to travel.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "f.txt", "tip" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// SF1: `update` rejects symbolic names today ("invalid check-in ID
// 'trunk'"); it goes through the same resolution surface B1 touches.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "update", "trunk" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// CONTRACT PARTNER chain (§5.2.3): update → status → commit → open. The
// checkout must sit at c3 with a clean tree.
try expectCheckoutVersion(io, tmp.dir, c3);
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, cli.shortHash(c3)) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to report") != null);
}
// A commit on top of the updated checkout must record c3 as its parent —
// proof that `update trunk` resolved to the newest check-in, not an
// ancestor.
try writeFile(io, tmp.dir, "f.txt", "v4\n");
const c4 = try commitHashDup(gpa, io, tmp.dir, "c4");
defer gpa.free(c4);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", c4 });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, c3) != null);
}
}
test "cli REGRESSION B1: open materializes the newest same-second check-in into a fresh clone" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const c3 = try buildSameSecondChain(gpa, io, tmp_a.dir);
defer gpa.free(c3);
const b1_open_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, b1_open_port);
defer server.kill(io);
try waitForListen(io, b1_open_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{b1_open_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);
// `open` resolves the tip of trunk from the cloned repository's own state
// — the same mtime-tie selection B1 breaks.
{
var res = try runZit(gpa, io, b_dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, c3) != null);
}
try expectCheckoutVersion(io, b_dir, c3);
// B2: the materialized bytes must be v3, not an ancestor's v1/v2.
const opened = try readFile(gpa, io, b_dir, "f.txt");
defer gpa.free(opened);
try std.testing.expectEqualStrings("v3\n", opened);
}
test "cli REGRESSION B1: a same-second two-branch merge picks the correct base and is never up to date" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// CONTRACT PARTNER (§5.2.3): `mergeBaseOf` shares the mtime-tie selection
// being rewritten. A same-second diamond's two leaves share exactly one
// common ancestor; a wrong base silently re-applies or drops a side.
var fx = try buildMergeDiamond(gpa, io, tmp.dir, "A\nB\nC\n", "A\nB\nC1\n", "A1\nB\nC\n");
defer fx.deinit(gpa);
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", fx.other });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Never a false "already up to date": the two sides are genuinely divergent.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "up to date") == null);
// Correct base content: base A/B/C, current A1/B/C, other A/B/C1 → the
// three-way result takes BOTH disjoint edits. A base selected off the
// ancestor chain would conflict or drop one side.
const poem = try readFile(gpa, io, tmp.dir, "poem");
defer gpa.free(poem);
try std.testing.expectEqualStrings("A1\nB\nC1\n", poem);
// status must surface the pending merge parent.
try expectMergeState(io, tmp.dir, fx.current, fx.other);
}
// --- REGRESSION B5 --------------------------------------------------------
//
// A merge whose only change is an ADDED file is silently lost (blocker B5,
// docs/plans/CLI-VERB-BREAKDOWN.md §B5). `merge` writes the incoming file to
// the working tree but never inserts it into `.zit-managed`, so the file is
// simultaneously present on disk and untracked: `status` reports "nothing to
// report", `extras` lists the merged file as junk, and `commit` refuses with
// "nothing to commit (no changes)" — dropping the merge parent that `merge`
// recorded in `.zit-checkout`. The user's only escape is `undo`.
//
// The symmetric half of the same omission: a merge that DELETES a file leaves
// the path managed, so `status` reports a phantom "missing" and `commit`
// refuses with "cannot commit … missing from the working tree". Both ride the
// same `applyMerge` entry stream, so both are encoded here.
//
// ORDERING: the defect is a managed-set mutation, so each repro is built in
// both orderings that could hide it — the merge direction (feature→trunk and
// trunk→feature) and the added path's position in the sorted managed set
// (`feat.txt` inserts AFTER `base.txt`; `aaa.txt` inserts BEFORE it). Identical
// failures in both is what proves the mechanism rather than luck.
/// Builds the §B5 repro shape: root(`base.txt`) on trunk, then a `feature`
/// branch whose ONLY change is an added `feat.txt`. Leaves the checkout back on
/// trunk at the root, clean, ready for `zit merge feature`. Returns
/// {base, other, current} where `current` == `base` (trunk never advanced).
///
/// Hashes are captured from each `commit`'s own stdout, never from `info tip`:
/// with two same-mtime leaves `tip` legitimately picks either one, and reading
/// it here would make the fixture — not the defect — decide the outcome.
fn buildAddOnlyBranch(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !MergeDiamond {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "base.txt", "base\n");
try addManaged(gpa, io, dir, &.{"base.txt"});
const base = try commitHashDup(gpa, io, dir, "root");
errdefer gpa.free(base);
try writeFile(io, dir, "feat.txt", "feat\n");
try addManaged(gpa, io, dir, &.{"feat.txt"});
var branch = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "addfeat" });
defer freeResult(gpa, &branch);
try expectExit(branch.term, 0);
const other = try gpa.dupe(u8, try checkinHash(branch.stdout));
errdefer gpa.free(other);
var up = try runZit(gpa, io, dir, &.{ "update", base });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
const current = try gpa.dupe(u8, base);
return .{ .base = base, .other = other, .current = current };
}
/// Asserts `zit extras` reports an empty working tree — the on-disk oracle that
/// a merged-in file is TRACKED, not junk.
fn expectNoExtras(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
var res = try runZit(gpa, io, dir, &.{"extras"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
}
test "cli REGRESSION B5: an add-only merge registers the file, status shows it added, commit lands two parents" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var fx = try buildAddOnlyBranch(gpa, io, tmp.dir);
defer fx.deinit(gpa);
// The merge itself already works: it writes feat.txt and records the parent.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", "feature" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 file(s) changed") != null);
}
const merged = try readFile(gpa, io, tmp.dir, "feat.txt");
defer gpa.free(merged);
try std.testing.expectEqualStrings("feat\n", merged);
try expectMergeState(io, tmp.dir, fx.current, fx.other);
// THE DEFECT: the merged-in file must be managed. `feat.txt` sorts AFTER
// `base.txt`, so this is the append end of the sorted set.
try expectManaged(gpa, io, tmp.dir, &.{ "base.txt", "feat.txt" });
// …and therefore must not be reported as junk.
try expectNoExtras(gpa, io, tmp.dir);
// status must surface BOTH the pending merge (by naming its parent) and
// the merge-added file. "nothing to report" over a pending merge that
// changed a file is the silent half of the blocker.
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to report") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, fx.other) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "ADDED feat.txt") != null);
}
// commit must succeed and mint a real merge check-in.
const m = try commitHashDup(gpa, io, tmp.dir, "m");
defer gpa.free(m);
// Two parents: the primary is the pre-merge trunk tip, the merge parent is
// the feature tip. A dropped merge parent is a lost branch.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", m });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
var buf: [96]u8 = undefined;
const want_parent = try std.fmt.bufPrint(&buf, "parent: {s}\n", .{fx.current});
try std.testing.expect(std.mem.indexOf(u8, res.stdout, want_parent) != null);
var buf2: [96]u8 = undefined;
const want_merge = try std.fmt.bufPrint(&buf2, "merge: {s}\n", .{fx.other});
try std.testing.expect(std.mem.indexOf(u8, res.stdout, want_merge) != null);
}
// The pending state is consumed, not left dangling.
try expectMergeState(io, tmp.dir, m, "");
try expectNoExtras(gpa, io, tmp.dir);
}
test "cli REGRESSION B5: an add-only merge in the reverse direction registers the same way" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// SECOND ORDERING. The first test merges feature INTO a trunk checkout and
// inserts at the tail of the sorted managed set. This one merges the trunk
// tip INTO a `feature` checkout and inserts `aaa.txt` at the HEAD, before
// both existing entries. Same failure in both ⇒ the mechanism is the
// missing managed-set mutation, not the direction or the insert position.
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "base.txt", "base\n");
try addManaged(gpa, io, tmp.dir, &.{"base.txt"});
const root = try commitHashDup(gpa, io, tmp.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp.dir, "mark.txt", "mark\n");
try addManaged(gpa, io, tmp.dir, &.{"mark.txt"});
var branch = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "featwork" });
defer freeResult(gpa, &branch);
try expectExit(branch.term, 0);
const feature_tip = try gpa.dupe(u8, try checkinHash(branch.stdout));
defer gpa.free(feature_tip);
{
var up = try runZit(gpa, io, tmp.dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
try writeFile(io, tmp.dir, "aaa.txt", "aaa\n");
try addManaged(gpa, io, tmp.dir, &.{"aaa.txt"});
const trunk_tip = try commitHashDup(gpa, io, tmp.dir, "trunkadd");
defer gpa.free(trunk_tip);
{
var up = try runZit(gpa, io, tmp.dir, &.{ "update", feature_tip });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", trunk_tip });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 file(s) changed") != null);
}
// Head-of-set insertion.
try expectManaged(gpa, io, tmp.dir, &.{ "aaa.txt", "base.txt", "mark.txt" });
try expectNoExtras(gpa, io, tmp.dir);
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to report") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, trunk_tip) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "ADDED aaa.txt") != null);
}
const m = try commitHashDup(gpa, io, tmp.dir, "m");
defer gpa.free(m);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", m });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
var buf: [96]u8 = undefined;
try std.testing.expect(std.mem.indexOf(u8, res.stdout, try std.fmt.bufPrint(&buf, "parent: {s}\n", .{feature_tip})) != null);
var buf2: [96]u8 = undefined;
try std.testing.expect(std.mem.indexOf(u8, res.stdout, try std.fmt.bufPrint(&buf2, "merge: {s}\n", .{trunk_tip})) != null);
}
}
test "cli REGRESSION B5: a merge that deletes a file deregisters it instead of reporting it missing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// The symmetric half of the same omission, on the same `applyMerge` entry
// stream: `.delete` entries remove the file from disk but leave it in the
// managed set, so `status` invents a "missing" file that the user never
// deleted and `commit` refuses over it.
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "base.txt", "base\n");
try writeFile(io, tmp.dir, "gone.txt", "gone\n");
try addManaged(gpa, io, tmp.dir, &.{ "base.txt", "gone.txt" });
const root = try commitHashDup(gpa, io, tmp.dir, "root");
defer gpa.free(root);
{
var rm = try runZit(gpa, io, tmp.dir, &.{ "rm", "--hard", "gone.txt" });
defer freeResult(gpa, &rm);
try expectExit(rm.term, 0);
}
var branch = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "delfeat" });
defer freeResult(gpa, &branch);
try expectExit(branch.term, 0);
const feature_tip = try gpa.dupe(u8, try checkinHash(branch.stdout));
defer gpa.free(feature_tip);
{
var up = try runZit(gpa, io, tmp.dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", feature_tip });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// gone.txt is off disk, so it must be off the managed set too.
try std.testing.expect(!fileExists(io, tmp.dir, "gone.txt"));
try expectManaged(gpa, io, tmp.dir, &.{"base.txt"});
try expectNoExtras(gpa, io, tmp.dir);
// No phantom "missing" — the user deleted nothing.
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "MISSING gone.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, feature_tip) != null);
}
// …and commit lands the merge rather than refusing over the phantom.
const m = try commitHashDup(gpa, io, tmp.dir, "m");
defer gpa.free(m);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", m });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
var buf: [96]u8 = undefined;
try std.testing.expect(std.mem.indexOf(u8, res.stdout, try std.fmt.bufPrint(&buf, "merge: {s}\n", .{feature_tip})) != null);
}
}
test "cli REGRESSION B5: zit add of a merge-added file is idempotent" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// CONTRACT PARTNER: merge gains a write path into the same `ManagedSet`
// that `add` owns. Once merge registers the file, `add` must see it as
// already tracked — two mechanisms writing the same set must agree, or a
// user typing the obvious recovery command double-registers it.
var fx = try buildAddOnlyBranch(gpa, io, tmp.dir);
defer fx.deinit(gpa);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", "feature" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "feat.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// The notice is a stderr diagnostic, not stdout output — the stream `add`
// has always used for it (see the `zit add` idempotence tests above).
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "already tracked") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "ADDED feat.txt") == null);
try expectManaged(gpa, io, tmp.dir, &.{ "base.txt", "feat.txt" });
}
test "cli REGRESSION B5: cherrypick of an add-only revision registers the added file" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// CONTRACT PARTNER: cherrypick shares `applyMerge`, so an add-only revision
// cherry-picked onto trunk loses the file exactly the same way.
var fx = try buildAddOnlyBranch(gpa, io, tmp.dir);
defer fx.deinit(gpa);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", "--cherrypick", fx.other });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 file(s) changed") != null);
}
try expectManaged(gpa, io, tmp.dir, &.{ "base.txt", "feat.txt" });
try expectNoExtras(gpa, io, tmp.dir);
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "ADDED feat.txt") != null);
}
// A cherrypick is NOT a merge: it commits with one parent and a Q card, so
// only the registration and the commit path are asserted here.
const m = try commitHashDup(gpa, io, tmp.dir, "cp");
defer gpa.free(m);
}
test "cli REGRESSION B5: a convergent-content merge still commits its merge parent" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Both sides made the SAME edit, so the merge changes zero files but is
// still a real merge with a real parent to record. commit's "nothing to
// commit (no changes)" refusal must not fire over a pending merge: the
// merge parent IS the change, and refusing wedges the tree with `undo` as
// the only escape.
var fx = try buildMergeDiamond(gpa, io, tmp.dir, "A\nB\nC\n", "A\nB\nC1\n", "A\nB\nC1\n");
defer fx.deinit(gpa);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", fx.other });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "up to date") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 file(s) changed") != null);
}
try expectMergeState(io, tmp.dir, fx.current, fx.other);
const m = try commitHashDup(gpa, io, tmp.dir, "m");
defer gpa.free(m);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", m });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
var buf: [96]u8 = undefined;
try std.testing.expect(std.mem.indexOf(u8, res.stdout, try std.fmt.bufPrint(&buf, "parent: {s}\n", .{fx.current})) != null);
var buf2: [96]u8 = undefined;
try std.testing.expect(std.mem.indexOf(u8, res.stdout, try std.fmt.bufPrint(&buf2, "merge: {s}\n", .{fx.other})) != null);
}
try expectMergeState(io, tmp.dir, m, "");
}
test "cli REGRESSION B5: a fresh open materializes the merge-added file" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
var fx = try buildAddOnlyBranch(gpa, io, tmp_a.dir);
defer fx.deinit(gpa);
{
var res = try runZit(gpa, io, tmp_a.dir, &.{ "merge", "feature" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// The end-to-end proof the blocker is about: the merged file must reach the
// REPOSITORY, not just this working tree. If commit refuses, feat.txt only
// ever existed on one disk and the branch is lost.
const m = try commitHashDup(gpa, io, tmp_a.dir, "m");
defer gpa.free(m);
const b5_open_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, b5_open_port);
defer server.kill(io);
try waitForListen(io, b5_open_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{b5_open_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 res = try runZit(gpa, io, b_dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
const opened = try readFile(gpa, io, b_dir, "feat.txt");
defer gpa.free(opened);
try std.testing.expectEqualStrings("feat\n", opened);
try expectManaged(gpa, io, b_dir, &.{ "base.txt", "feat.txt" });
}
test "cli REGRESSION B5: undo of a pending add-only merge leaves nothing registered" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// CONTRACT-PARTNER GUARD — structurally incapable of being RED pre-fix.
// The abort path's job is to UNDO merge's managed-set mutation. Pre-fix
// merge performs no such mutation, so "the set is back to {base.txt}" is
// vacuously true and this scenario cannot exercise the defect. It becomes
// load-bearing only once the fix introduces the write, and it must stay
// green through the fix: a registration that undo does not reverse leaves a
// phantom managed entry for a file undo just deleted.
var fx = try buildAddOnlyBranch(gpa, io, tmp.dir);
defer fx.deinit(gpa);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", "feature" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var un = try runZit(gpa, io, tmp.dir, &.{"undo"});
defer freeResult(gpa, &un);
try expectExit(un.term, 0);
}
try std.testing.expect(!fileExists(io, tmp.dir, "feat.txt"));
try expectManaged(gpa, io, tmp.dir, &.{"base.txt"});
try expectNoExtras(gpa, io, tmp.dir);
try expectMergeState(io, tmp.dir, fx.current, "");
}
// --- REGRESSION B6 --------------------------------------------------------
//
// `tag` is read-only: only `tag list` exists, so a repository cannot be given
// a name for a check-in at all (blocker B6, docs/plans/CLI-VERB-BREAKDOWN.md
// line 27 / line 132: "tag | PARTIAL | read-only — `tag list` only, no
// create"). The fix mints a CONTROL artifact carrying `T +sym-NAME <hash>` —
// a SINGLETON (`+`), not a propagating (`*`) tag: repostate's `resolveName`
// (src/repostate.zig:891) picks the max-event.mtime check-in among ALL active
// sym rows, so a propagating sym would be inherited by every descendant and
// silently resolve to the newest one — a B1-class wrong answer. `applyTagOps`
// walks a singleton only as a cancel, so a singleton card's value stays
// pinned to its target.
//
// Plan §4 requires a ROUND TRIP, not just local visibility: create, see it in
// the tag renderings, then confirm the T-card survives `pack` and a clone — a
// synced artifact that does not survive sync is a worse lie than a missing
// verb. Note for the implementer: neither `info` nor `timeline` renders tags
// today (src/main.zig:4980 renderInfo prints id/branch/leaf/parents only;
// runTimeline never calls `tagsOf`), so `tag list CHECKIN` IS the per-check-in
// render surface these tests assert against — extending info/timeline is not
// in B6's minimal scope.
/// Mints a one-commit trunk and returns that check-in's 64-hex hash (caller
/// frees). `a.txt` holds "root\n"; the checkout is left on it.
fn b6BuildRoot(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]const u8 {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "a.txt", "root\n");
try addManaged(gpa, io, dir, &.{"a.txt"});
return commitHashDup(gpa, io, dir, "root");
}
test "cli REGRESSION B6: tag add creates a tag that tag list and tag list CHECKIN report" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try b6BuildRoot(gpa, io, tmp.dir);
defer gpa.free(root);
// THE MISSING CAPABILITY. Pre-fix this is `bad_subcommand` -> exit 2.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
}
// §5.1/§2 no-arg listing: only the new singleton symbolic release name
// appears; the propagating trunk machinery is excluded.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "tag", "list" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v1\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
// §5.2 CHECKIN listing: the singleton tag is on the named check-in.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "tag", "list", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v1\n", res.stdout);
}
// §8.1/§8.2 resolution: the name now names that check-in everywhere.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, root));
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "a.txt", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("root\n", res.stdout);
}
}
test "cli REGRESSION B6: a created tag pins to its check-in and is not inherited by a descendant (tag before commit)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try b6BuildRoot(gpa, io, tmp.dir);
defer gpa.free(root);
// ORDERING A: tag first, then mint the descendant. A propagating (`*`)
// sym card would be inherited by everything committed AFTER it.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
try writeFile(io, tmp.dir, "a.txt", "child\n");
const child = try commitHashDup(gpa, io, tmp.dir, "child");
defer gpa.free(child);
try std.testing.expect(!std.mem.eql(u8, root, child));
try b6ExpectPinned(gpa, io, tmp.dir, root, child);
}
test "cli REGRESSION B6: a created tag pins to its check-in and is not inherited by a descendant (commit before tag)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try b6BuildRoot(gpa, io, tmp.dir);
defer gpa.free(root);
// ORDERING B: mint the descendant FIRST, then tag the ancestor. The
// propagation walk in `applyTagOps` runs over already-crosslinked
// descendants here, a different code path from ordering A's inheritance
// at commit time. Identical behavior in both orderings is what proves the
// singleton mechanism rather than luck.
try writeFile(io, tmp.dir, "a.txt", "child\n");
const child = try commitHashDup(gpa, io, tmp.dir, "child");
defer gpa.free(child);
try std.testing.expect(!std.mem.eql(u8, root, child));
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
try b6ExpectPinned(gpa, io, tmp.dir, root, child);
}
/// The pinning oracle shared by both orderings: `v1` is on `root` only, the
/// descendant does NOT list it, and the name still resolves to `root` even
/// though `child` is strictly newer (max-mtime would pick `child` if the row
/// had propagated).
fn b6ExpectPinned(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
root: []const u8,
child: []const u8,
) !void {
{
var res = try runZit(gpa, io, dir, &.{ "tag", "list", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "v1\n") != null);
}
{
var res = try runZit(gpa, io, dir, &.{ "tag", "list", child });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "v1\n") == null);
}
{
var res = try runZit(gpa, io, dir, &.{ "info", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, root));
}
// The content oracle: the tagged revision's bytes, not the descendant's.
{
var res = try runZit(gpa, io, dir, &.{ "cat", "a.txt", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("root\n", res.stdout);
}
}
test "cli REGRESSION B6: a created tag survives pack" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try b6BuildRoot(gpa, io, tmp.dir);
defer gpa.free(root);
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{"pack"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// The T-card is permanent repository content: packing must not drop it.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "tag", "list" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v1\n", res.stdout);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "info", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, root));
}
}
test "cli REGRESSION B6: a created tag survives a clone" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const root = try b6BuildRoot(gpa, io, tmp_a.dir);
defer gpa.free(root);
{
var res = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "tag", "add", "v1", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
const b6_clone_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, b6_clone_port);
defer server.kill(io);
try waitForListen(io, b6_clone_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{b6_clone_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", 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);
// A tag that does not survive sync is a worse lie than a missing verb.
{
var res = try runZit(gpa, io, b_dir, &.{ "tag", "list" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("v1\n", res.stdout);
}
{
var res = try runZit(gpa, io, b_dir, &.{ "tag", "list", root });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "v1\n") != null);
}
{
var res = try runZit(gpa, io, b_dir, &.{ "info", "v1" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, root));
}
}
// --- REGRESSION B3 --------------------------------------------------------
//
// A symlink anywhere in the tree permanently wedges `commit` (blocker B3,
// docs/plans/CLI-VERB-BREAKDOWN.md §B3). `extras.zig` classifies symlinks as
// extras and the recursive `add`/`addremove` sweep reuses that walk without
// re-applying the exclusion, so `add .` TRACKS exactly what explicit `zit add
// sym.txt` and `commit` both REFUSE. `status` then calls a file that is on
// disk "missing", and the recovery (`zit rm sym.txt`) is undiscoverable.
//
// Contract of record (BLOCKER-FIX-PLAN.md §2 F-B3): consistent refusal —
// 0.1.0 does not version symlinks. The recursive paths skip them (add reports
// each skip on stderr, addremove withholds them silently), explicit `add`
// keeps refusing with the symlink wording, and `commit`'s refusal for an
// already-wedged repo names `zit rm PATH`.
test "cli REGRESSION B3: `add .` skips a symlink, so commit succeeds, status is clean and extras lists it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// The breakdown doc's repro verbatim:
// zit init -q && echo a > a.txt && ln -s a.txt sym.txt
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "a\n");
try tmp.dir.symLink(io, "a.txt", "sym.txt", .{});
// `zit add .` — must NOT track sym.txt, and must SAY it skipped it.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("ADDED a.txt\n", res.stdout);
try std.testing.expect(std.mem.indexOf(
u8,
res.stderr,
"skipped 'sym.txt': symlink (not versioned in 0.1.0)",
) != null);
}
try expectManaged(gpa, io, tmp.dir, &.{"a.txt"});
// `zit commit -m t` — must succeed. Today it dies with
// "cannot commit 'sym.txt': not a regular file" and the repo is stuck.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "t" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
// `zit status` — must NOT report sym.txt missing; it is on disk.
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "MISSING sym.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "sym.txt") == null);
}
// `zit extras` — must list sym.txt: untracked is where a symlink belongs.
{
var res = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("sym.txt\n", res.stdout);
}
}
test "cli REGRESSION B3: `addremove` withholds symlinks in both sweep orderings" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// BOTH orderings relative to the regular file the sweep also yields: the
// extras walk is sorted by path, so `aaa_sym` is visited BEFORE `m.txt`
// and `zzz_sym` AFTER it. A skip that only works on one side of a regular
// entry (e.g. a filter that latches off the previous item's kind) fails
// exactly one of these two assertions.
try writeFile(io, tmp.dir, "m.txt", "m\n");
try tmp.dir.symLink(io, "m.txt", "aaa_sym", .{});
try tmp.dir.symLink(io, "m.txt", "zzz_sym", .{});
{
var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Neither symlink is ADDED, in either ordering.
try std.testing.expectEqualStrings("ADDED m.txt\n", res.stdout);
}
// Neither symlink is tracked, and both remain extras.
try expectManaged(gpa, io, tmp.dir, &.{"m.txt"});
{
var res = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("aaa_sym\nzzz_sym\n", res.stdout);
}
// The whole point: commit is no longer wedged.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "t" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
}
test "cli REGRESSION B3: explicit `add sym.txt` refuses with the symlink wording, tracking nothing" {
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 tmp.dir.symLink(io, "a.txt", "sym.txt", .{});
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "sym.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
// Today: "zit add: 'sym.txt' is not a regular file" — a fifo and a symlink
// are not the same refusal and must not read the same.
try std.testing.expect(std.mem.indexOf(
u8,
res.stderr,
"symlink (not versioned in 0.1.0)",
) != null);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli REGRESSION B3: a wedged repo recovers via the `zit rm` the commit error names" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Build the wedge the way an F4-era `add .` left real repos: `sym.txt` is
// MANAGED and is a symlink on disk. Tracking it while it is still a
// regular file is the only construction that survives the fix (post-fix
// neither `add .` nor `add sym.txt` will register it) — the wedged repos
// this recovery exists for were created before the fix.
try writeFile(io, tmp.dir, "a.txt", "a\n");
try writeFile(io, tmp.dir, "sym.txt", "placeholder\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "sym.txt" });
try tmp.dir.deleteFile(io, "sym.txt");
try tmp.dir.symLink(io, "a.txt", "sym.txt", .{});
// commit refuses — correctly — but must name the way out. Today the
// message is "cannot commit 'sym.txt': not a regular file" and stops
// there, which is why the blocker was near-undiagnosable in the field.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "t" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit rm sym.txt") != null);
}
// Follow the recovery the message gives, verbatim.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "rm", "sym.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "t" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to report") != null);
}
// The symlink is still on disk and is now, correctly, an extra.
{
var res = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("sym.txt\n", res.stdout);
}
}
test "cli REGRESSION B3: `clean --dry-run` still lists a symlink as an extra" {
// CONTRACT-PARTNER GUARD, not a repro. clean is a pure READER of the
// extras sweep — it never inserts into the managed set — so it is
// structurally incapable of exhibiting the B3 wedge and was green before
// the fix. It is here because the fix widens `extras.Extras` entries to
// carry an is_symlink tag and touches clean's call site: clean must keep
// IGNORING that tag (symlinks stay deletable extras) while add/addremove
// filter on it. It must stay green through the fix.
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 tmp.dir.symLink(io, "a.txt", "sym.txt", .{});
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runZit(gpa, io, tmp.dir, &.{ "clean", "--dry-run" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("sym.txt\n", res.stdout);
}
// ==========================================================================
// REGRESSION B4 — non-atomic `mv` (records the rename before moving the file,
// with no rollback when the move fails).
//
// Repro of record (docs/plans/CLI-VERB-BREAKDOWN.md "B4 — non-atomic mv"),
// reproduced verbatim against a fresh trunk build before these tests were
// written:
//
// $ zit mv sub/a2.txt nodir/x.txt
// recorded 'sub/a2.txt' as 'nodir/x.txt' but could not move the working
// file: input/output error
// $ zit status # missing nodir/x.txt (file still at sub/a2.txt)
// $ zit mv nodir/x.txt sub/a2.txt # 'sub/a2.txt' already exists on disk
//
// A refused mv must be a ZERO state change, and the refusal chain is not
// observable through `status` alone: the managed set mv persists is the same
// set `extras` subtracts from and `commit` walks, so each of the three is
// asserted. The two failure causes below (missing dest parent → ENOENT,
// unwritable dest parent → EACCES) are the same defect reached two ways;
// identical wrong state in both is what shows the mechanism is the WRITE/MOVE
// ORDERING and not one errno.
// ==========================================================================
/// Sets up a repository with a single committed file at `sub/a2.txt` — the
/// baseline all four B4 tests start from. A committed baseline (rather than a
/// merely-added one) is what makes `commit`'s "nothing to commit" the honest
/// oracle for "nothing was recorded".
fn b4Baseline(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
try initRepo(gpa, io, dir);
try dir.createDir(io, "sub", .default_dir);
try writeFile(io, dir, "sub/a2.txt", "hello\n");
try addManaged(gpa, io, dir, &.{"sub/a2.txt"});
var res = try runCommitEnv(gpa, io, dir, .{}, &.{ "commit", "-m", "base" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
/// Asserts the repository is byte-for-byte the b4Baseline state: the file is
/// still at `sub/a2.txt` with its original bytes, the managed set still names
/// exactly that path, `status` reports nothing, `extras` is empty (a stranded
/// working file would show up here), and `commit` has nothing to record.
fn expectB4Untouched(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
try std.testing.expect(fileExists(io, dir, "sub/a2.txt"));
const bytes = try readFile(gpa, io, dir, "sub/a2.txt");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("hello\n", bytes);
try expectManaged(gpa, io, dir, &.{"sub/a2.txt"});
{
var res = try runZit(gpa, io, dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to report") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "missing") == null);
}
{
var res = try runZit(gpa, io, dir, &.{"extras"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
}
{
var res = try runCommitEnv(gpa, io, dir, .{ .epoch = "1234567891" }, &.{ "commit", "-m", "after" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("zit commit: nothing to commit (no changes)\n", res.stderr);
}
// The DIRECT oracle for "nothing was recorded". Everything above is
// indirect: commit's "no changes" reflects CONTENT only, so a stranded
// rename record passes all of it silently and then surfaces at the next
// commit that does carry a change, as a §4.7.6 old-name F card claiming a
// former name that no manifest ever held. Read the record itself.
{
var set = try managed.ManagedSet.read(gpa, io, dir);
defer set.deinit();
try std.testing.expect(set.pendingRenames().len == 0);
}
}
test "cli REGRESSION B4: a failed mv into a missing directory records nothing" {
// REGRESSION B4: the repro of record. `nodir` does not exist, so the
// rename cannot succeed; today mv persists the rename FIRST and then
// reports the failure, leaving the set naming a path that is not on disk.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try b4Baseline(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/a2.txt", "nodir/x.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
// A refusal, not a report of a half-done rename.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "recorded") == null);
// SF3 (mv-scoped): ENOENT must not be rendered as "input/output error",
// and the message must name the directory that is missing.
try std.testing.expectEqualStrings(
"zit mv: cannot move into 'nodir': no such directory\n",
res.stderr,
);
try std.testing.expect(!fileExists(io, tmp.dir, "nodir/x.txt"));
try expectB4Untouched(gpa, io, tmp.dir);
}
test "cli REGRESSION B4: a failed mv into an unwritable directory records nothing" {
// REGRESSION B4: the SAME defect reached by a different errno. `ro` exists
// (so a dest-parent-exists pre-check cannot catch it) but is not writable,
// so the rename itself fails with EACCES. Identical wrong state to the
// ENOENT case is what proves the defect is the record-before-move ordering
// rather than one specific failure cause — and this is the case that will
// still reach the rename handler after the fix, so it is the one that
// pins the rollback path.
if (comptime !File.Permissions.has_executable_bit) return error.SkipZigTest;
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try b4Baseline(gpa, io, tmp.dir);
try tmp.dir.createDir(io, "ro", .default_dir);
// Restored before tmp.cleanup, which
// needs the directory writable again.
defer tmp.dir.setFilePermissions(io, "ro", File.Permissions.fromMode(0o700), .{ .follow_symlinks = false }) catch {};
try tmp.dir.setFilePermissions(io, "ro", File.Permissions.fromMode(0o500), .{ .follow_symlinks = false });
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/a2.txt", "ro/x.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "recorded") == null);
// SF3 (mv-scoped): EACCES is neither "input/output error" nor a raw Zig
// error name — it is the same human phrase `clean` and `init` already print.
try std.testing.expectEqualStrings(
"zit mv: cannot move 'sub/a2.txt' to 'ro/x.txt': permission denied\n",
res.stderr,
);
try std.testing.expect(!fileExists(io, tmp.dir, "ro/x.txt"));
try expectB4Untouched(gpa, io, tmp.dir);
}
test "cli REGRESSION B4: a failed mv leaves the source tracked and still movable" {
// REGRESSION B4: the user-visible cost of the stranded record. The
// breakdown's documented recovery (`zit mv NEW OLD`) fails after a failed
// mv because the record and the filesystem disagree; the honest fix is
// that no recovery is needed at all — the source is untouched, so an
// ordinary mv of it just works. Today it does not: the source is no
// longer tracked, so mv refuses it.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try b4Baseline(gpa, io, tmp.dir);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/a2.txt", "nodir/x.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/a2.txt", "sub/c.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("RENAME sub/a2.txt sub/c.txt\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
try expectManaged(gpa, io, tmp.dir, &.{"sub/c.txt"});
try std.testing.expect(fileExists(io, tmp.dir, "sub/c.txt"));
try std.testing.expect(!fileExists(io, tmp.dir, "sub/a2.txt"));
}
test "cli REGRESSION B4: `zit mv NEW OLD` reverses a successful rename exactly" {
// CONTRACT-PARTNER GUARD, not a repro. Both renames here SUCCEED, so no
// rename-failure path is entered and the record/move ordering is
// unobservable — this scenario is structurally incapable of exhibiting the
// B4 defect and was green before the fix. It is here because the fix
// reorders mvCommit around the same recordRename bookkeeping: `mv b a`
// after `mv a b` must net the rename record back out, or the reversal
// would leave a stray old-name F card and `commit` would see a change.
// It must stay green through the fix — it is what makes `mv --help`'s
// "to reverse a rename, run `zit mv NEW OLD`" promise true.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try b4Baseline(gpa, io, tmp.dir);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/a2.txt", "sub/b.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "mv", "sub/b.txt", "sub/a2.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
try std.testing.expect(!fileExists(io, tmp.dir, "sub/b.txt"));
// Exactly the baseline again — including a commit that finds no change,
// which is the assertion that no stray rename record survived the round trip.
try expectB4Untouched(gpa, io, tmp.dir);
}