const std = @import("std");
const repeat = @import("zit").repeat;
const checkout = @import("zit").checkout;
const artifact = @import("zit").artifact;
const File = std.Io.File;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendArtifactAs = support.appendArtifactAs;
const appendDiffManifest = support.appendDiffManifest;
const chmodFile = support.chmodFile;
const corruptSection = support.corruptSection;
const expectCheckout = support.expectCheckout;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const sectionExists = support.sectionExists;
const seedCheckout = support.seedCheckout;
const touch = support.touch;
const writeFile = support.writeFile;
/// An independent transcription of contracts/status.md §Help. Kept separate from
/// the binary's own `status_help` so the e2e compares two hand-made copies —
/// drift in either is caught (B-status-14/-16).
const status_help_expected =
\\usage: zit status [-v] [--classify] [--added] [--edited] [--missing]
\\ [--conflict] [--extra] [--differ] [--dotfiles]
\\ [-h|--help]
\\
\\Show the state of the working copy: the current branch, the current
\\check-in (if any), and the state of your tracked files.
\\
\\Under a pending merge, an edited file that still carries conflict
\\markers is reported as CONFLICT. Rows use Fossil's words (ADDED,
\\EDITED, MISSING, CONFLICT, EXTRA, MERGED_WITH) in an aligned column;
\\--classify is accepted and changes nothing. The kind flags narrow the
\\rows to those kinds; --differ is
\\--edited plus --extra, and --extra lists the untracked files 'zit
\\extras' would (names beginning with a dot only under --dotfiles, apart
\\from the files zit reads from the tree).
\\The report leads with Fossil's checkout block —
\\repository, local-root, checkout, parent, tags, comment — with
\\twelve-character names and the weekday in the times; 'zit changes'
\\is the same report without the block, as in Fossil.
\\
\\zit has no staging area. `status` reports the files you marked with
\\`zit add` and how each one stands in the working tree:
\\
\\ ADDED <path> tracked and new — it will enter your next check-in
\\ EDITED <path> tracked, and its bytes or mode differ from the check-in
\\ MISSING <path> tracked, but not present in the working tree
\\
\\A tracked file whose contents match the current check-in is not listed.
\\
\\Until your first `zit commit` the branch 'trunk' is unborn: there is no
\\check-in yet, so every tracked file is reported as ADDED (it will form
\\the root check-in). The current check-in, once one exists, is shown as its
\\full artifact hash.
\\
\\Two things `status` does not report, because it walks only your tracked
\\files: a file deleted from the check-in but no longer tracked (`zit diff`
\\lists it) and untracked "extra" files (`zit extras` lists them).
\\
\\options:
\\ -v, --verbose accepted; the block is printed either way
\\ --classify accepted; Fossil's words are the default
\\ --added, --edited, --missing, --conflict, --extra
\\ only rows of those kinds
\\ --differ --edited plus --extra
\\ -h, --help show this help and exit
\\
\\exit status:
\\ 0 the working-copy state was reported (with or without changes)
\\ 1 this is not a zit repository, or its state could not be read
\\ 2 usage error (an unknown option or an unexpected argument)
\\
\\aliases: changes
\\
;
test "cli status B-status-3/-7/-15: fresh repo, nothing tracked, header + nothing tracked yet" {
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 res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.endsWith(u8, res.stdout, "nothing tracked yet\n")); // after the checkout block
try std.testing.expectEqualStrings("", res.stderr); // report → stdout only
// Read-only: the checkout record is unchanged and no managed set was written.
try expectCheckout(io, tmp.dir, "", "trunk");
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli status B-status-3/-6/-8: unborn, two present tracked files → added in sorted order" {
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, "a");
try touch(io, tmp.dir, "b");
try addManaged(gpa, io, tmp.dir, &.{ "a", "b" });
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(
"ADDED a\nADDED b\n",
res.stdout,
);
try std.testing.expectEqualStrings("", res.stderr);
// Read-only: the tracked set still holds exactly {a, b}.
try expectManaged(gpa, io, tmp.dir, &.{ "a", "b" });
}
test "cli status B-status-5: a tracked file removed from disk → missing" {
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, "a");
try addManaged(gpa, io, tmp.dir, &.{"a"});
try tmp.dir.deleteFile(io, "a"); // gone from the working tree, still tracked
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(
"MISSING a\n",
res.stdout,
);
try std.testing.expectEqualStrings("", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{"a"}); // still tracked (read-only)
}
test "cli status B-status-5/-8: present/absent interleave in path order, not grouped by kind" {
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, "a");
try touch(io, tmp.dir, "b");
try touch(io, tmp.dir, "c");
try addManaged(gpa, io, tmp.dir, &.{ "a", "b", "c" });
try tmp.dir.deleteFile(io, "b"); // the middle path is the one gone
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(
"ADDED a\nMISSING b\nADDED c\n",
res.stdout,
);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-5: a tracked path replaced by a directory folds to missing" {
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, "a");
try addManaged(gpa, io, tmp.dir, &.{"a"});
try tmp.dir.deleteFile(io, "a");
try tmp.dir.createDir(io, "a", .default_dir); // non-regular now → missing
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(
"MISSING a\n",
res.stdout,
);
try std.testing.expectEqualStrings("", res.stderr);
}
/// Seeds a born checkout whose current check-in is a real, resolvable manifest
/// listing `files` — the shape `status` and `diff` both need now that both
/// compare the working tree against the check-in's effective F-set. Returns the
/// owned check-in hash so a test can quote it verbatim in the header line.
fn seedBornCheckin(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
label: []const u8,
files: []const artifact.FCard,
) ![]u8 {
const hash = try appendDiffManifest(gpa, io, dir, label, null, files);
errdefer gpa.free(hash);
try seedCheckout(io, dir, hash, "trunk");
return hash;
}
/// Appends `content` as a SHA-1 blob and returns the F card naming it at `path`.
/// The returned card borrows `path` and the returned hash, which the caller owns.
fn seedRegularFCard(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
path: []const u8,
content: []const u8,
) !artifact.FCard {
const blob = try appendArtifactAs(gpa, io, dir, .sha1, content);
return .{ .filename = path, .hash = blob, .perm = null, .old_name = null, .mode = .regular };
}
test "cli status B-status-16: born state, an edited tracked file is reported as edited" {
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", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
const card = try seedRegularFCard(gpa, io, tmp.dir, "a", "a0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
try writeFile(io, tmp.dir, "a", "a1\n"); // same path, different bytes
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "EDITED a\n";
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try expectCheckout(io, tmp.dir, born_hash, "trunk"); // read-only
}
test "cli status B-status-16: a rewrite with identical bytes (mtime bump) is not reported" {
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", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
const card = try seedRegularFCard(gpa, io, tmp.dir, "a", "a0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
// Rewrite byte-identical content: the mtime moves, the artifact hash does
// not. Classification is by content hash, so this is clean, not edited.
try writeFile(io, tmp.dir, "a", "a0\n");
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "nothing to report\n";
try std.testing.expect(std.mem.endsWith(u8, res.stdout, expected)); // after the checkout block
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-6/-16: edited, missing and newly added interleave in path order" {
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", "a0\n");
try writeFile(io, tmp.dir, "b", "b0\n");
try writeFile(io, tmp.dir, "d", "d0\n");
try addManaged(gpa, io, tmp.dir, &.{ "a", "b", "d" });
const card_a = try seedRegularFCard(gpa, io, tmp.dir, "a", "a0\n");
defer gpa.free(card_a.hash.?);
const card_b = try seedRegularFCard(gpa, io, tmp.dir, "b", "b0\n");
defer gpa.free(card_b.hash.?);
const card_d = try seedRegularFCard(gpa, io, tmp.dir, "d", "d0\n");
defer gpa.free(card_d.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{ card_a, card_b, card_d });
defer gpa.free(born_hash);
try writeFile(io, tmp.dir, "a", "a1\n"); // edited
try tmp.dir.deleteFile(io, "b"); // missing
try writeFile(io, tmp.dir, "c", "c0\n");
try addManaged(gpa, io, tmp.dir, &.{"c"}); // added: tracked, not in the check-in
// `d` is untouched → clean → no line.
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "EDITED a\nMISSING b\nADDED c\n";
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-16: an executable-bit-only change is edited" {
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 initRepo(gpa, io, tmp.dir);
const original = "#!/bin/zit\nsame\n";
try writeFile(io, tmp.dir, "script", original);
try addManaged(gpa, io, tmp.dir, &.{"script"});
const blob = try appendArtifactAs(gpa, io, tmp.dir, .sha1, original);
defer gpa.free(blob);
const card: artifact.FCard = .{
.filename = "script",
.hash = blob,
.perm = "x",
.old_name = null,
.mode = .executable,
};
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "executable-root", &.{card});
defer gpa.free(born_hash);
// The bytes match the baseline; only the mode differs from the F card's `x`.
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "EDITED script\n";
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-7/-16: born state, every tracked file clean → nothing to report" {
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, "p", "p0\n");
try addManaged(gpa, io, tmp.dir, &.{"p"});
const card = try seedRegularFCard(gpa, io, tmp.dir, "p", "p0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "nothing to report\n";
try std.testing.expect(std.mem.endsWith(u8, res.stdout, expected)); // after the checkout block
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-5/-16: a tracked path in the check-in replaced by a directory is missing" {
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", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
const card = try seedRegularFCard(gpa, io, tmp.dir, "a", "a0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
try tmp.dir.deleteFile(io, "a");
try tmp.dir.createDir(io, "a", .default_dir); // non-regular now → missing
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "MISSING a\n";
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-16: a file deleted from the check-in but untracked is still not reported" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// `gone` is in the check-in but was never added to the managed set here, so
// it is a deleted-from-check-in file: `--help` still defers those, and
// `status` iterates only the managed set, so it must stay silent.
const card = try seedRegularFCard(gpa, io, tmp.dir, "gone", "g0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "nothing to report\n";
try std.testing.expect(std.mem.endsWith(u8, res.stdout, expected)); // after the checkout block
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-10/-15: an unresolvable current check-in fails without a partial report" {
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", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
try seedCheckout(io, tmp.dir, repeat("a", 64), "trunk"); // no such artifact in the log
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: current check-in is not a valid manifest\n",
res.stderr,
);
}
test "cli status B-status-15: an unreadable tracked file fails the walk, leaving stdout empty" {
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 initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
// Unreadable, not absent: the no-follow stat still succeeds and reports a
// regular file, so the walk reaches `openFile` and takes EACCES. That is a
// comparison failure, not a dirty working copy — exit 1, and NOTHING on
// stdout, not even the header line that precedes the rows.
try chmodFile(io, tmp.dir, "a", 0o000);
defer chmodFile(io, tmp.dir, "a", 0o644) catch {};
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: cannot compare working tree: input/output error\n",
res.stderr,
);
}
test "cli status B-status-15: a walk that aborts after a printable row still leaves stdout empty" {
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 initRepo(gpa, io, tmp.dir);
// `a` sorts first and IS reportable (added); `z` is unreadable. Were the
// report streamed, stdout would already carry the header and "added a"
// before the failure — this is the negative space that check exists for.
try writeFile(io, tmp.dir, "a", "a0\n");
try writeFile(io, tmp.dir, "z", "z0\n");
try addManaged(gpa, io, tmp.dir, &.{ "a", "z" });
try chmodFile(io, tmp.dir, "z", 0o000);
defer chmodFile(io, tmp.dir, "z", 0o644) catch {};
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: cannot compare working tree: input/output error\n",
res.stderr,
);
}
test "cli status B-status-5/-16: a tracked path in the check-in replaced by a symlink is missing" {
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", "a0\n");
try addManaged(gpa, io, tmp.dir, &.{"a"});
const card = try seedRegularFCard(gpa, io, tmp.dir, "a", "a0\n");
defer gpa.free(card.hash.?);
const born_hash = try seedBornCheckin(gpa, io, tmp.dir, "root", &.{card});
defer gpa.free(born_hash);
// Replace the tracked regular file with a symlink whose target holds the
// check-in's exact bytes: only the no-follow stat distinguishes them. A
// followed link would compare equal and report nothing.
try tmp.dir.deleteFile(io, "a");
try writeFile(io, tmp.dir, "target", "a0\n");
tmp.dir.symLink(io, "target", "a", .{}) catch |e| switch (e) {
error.AccessDenied, error.Unexpected => return error.SkipZigTest,
else => return e,
};
var res = try runZit(gpa, io, tmp.dir, &.{"changes"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const expected = "MISSING a\n";
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-16: a current delta checkout with an absent B-card baseline fails" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
const absent_baseline = repeat("f", 64);
const delta = try appendDiffManifest(gpa, io, tmp.dir, "missing-baseline", absent_baseline, &.{});
defer gpa.free(delta);
try seedCheckout(io, tmp.dir, delta, "trunk");
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("zit status: cannot resolve baseline\n", res.stderr);
}
test "cli status B-status-17: an empty branch field displays as trunk" {
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 seedCheckout(io, tmp.dir, "", ""); // empty branch → defensive trunk default
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.endsWith(u8, res.stdout, "nothing tracked yet\n")); // after the checkout block
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-9/-15: not a zit repository → exit 1, stderr, empty stdout" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); // a bare tmpDir: no repo here or in any parent
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit: no zit checkout here (looked for .zit-checkout in this directory and its parents)\n" ++
"if this is a bare repository, name it with -R, or run 'zit open' to create a working tree\n",
res.stderr,
);
}
test "cli status B-status-11: a corrupt .zit-managed → exit 1, stderr, empty stdout" {
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 corruptSection(io, tmp.dir, .managed);
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout); // header not yet printed
try std.testing.expectEqualStrings(
"zit status: cannot read tracked files: .zit-checkout is corrupt\n",
res.stderr,
);
}
test "cli status B-status-10: a corrupt .zit-checkout → exit 1, stderr, empty stdout" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// version_len 0xffff with no payload: a malformed TLV → error.Corrupt. The
// file still exists, so discovery succeeds and the read is what fails.
{
var f = try tmp.dir.createFile(io, checkout.filename, .{});
defer f.close(io);
try f.writePositionalAll(io, &[_]u8{ 0xff, 0xff, 0xaa }, 0);
}
var res = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: cannot read checkout state: .zit-checkout is corrupt\n",
res.stderr,
);
}
test "cli status B-status-14/-16: --help and -h print the verbatim help to stdout, exit 0" {
const gpa = std.testing.allocator;
const io = std.testing.io;
inline for (.{ "--help", "-h" }) |flag| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); // no repo needed: help wins before discovery
var res = try runZit(gpa, io, tmp.dir, &.{ "status", flag });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(status_help_expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
}
test "cli status B-status-12: an unknown option → exit 2, usage on stderr, empty stdout" {
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 res = try runZit(gpa, io, tmp.dir, &.{ "status", "-x" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: unknown option '-x'\ntry 'zit status --help'\n",
res.stderr,
);
}
test "cli status B-status-13: an unexpected argument → exit 2, usage on stderr, empty stdout" {
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 res = try runZit(gpa, io, tmp.dir, &.{ "status", "extra" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit status: unexpected argument 'extra'\ntry 'zit status --help'\n",
res.stderr,
);
}
// --- top-level discoverability & help routing (DX D4) ----------------------
test "cli status B-status-16 DX-D4: the command list names status" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Bare `zit` and `zit help` both print the command list on stdout; a user
// scanning it must SEE `status` — the DX-D4 blocker was that it was absent,
// so `zit help status` implied the verb did not exist.
inline for (.{ &[_][]const u8{}, &[_][]const u8{"help"} }) |argv| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); // no repo needed: help is repo-independent
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "commands:") != null);
try std.testing.expect(std.mem.indexOf(
u8,
res.stdout,
"status show the working checkout state\n",
) != null);
try std.testing.expectEqualStrings("", res.stderr);
}
}
test "cli status B-status-16 DX-D4: zit help status prints the status help, not the command list" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); // repo-independent: help wins before any discovery
var res = try runZit(gpa, io, tmp.dir, &.{ "help", "status" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Byte-identical to `zit status --help` — the same independent transcription
// the --help e2e uses — proving the topic routed to the verb's own help.
try std.testing.expectEqualStrings(status_help_expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
test "cli status B-status-16 DX-D4: zit help <verb> routes each verb to its own help, not the command list" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// A verb's help carries `usage: zit <verb>` and never the top-level
// `commands:` block, so these two probes distinguish routed help from the
// fallback command list for every routed verb.
const Case = struct { verb: []const u8, usage: []const u8 };
const cases = [_]Case{
.{ .verb = "init", .usage = "usage: zit init" },
.{ .verb = "add", .usage = "usage: zit add" },
.{ .verb = "rm", .usage = "usage: zit rm" },
};
inline for (cases) |c| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, &.{ "help", c.verb });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, c.usage) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "commands:") == null);
try std.testing.expectEqualStrings("", res.stderr);
}
}
test "cli status: --classify, kind filters, --extra/--differ, -v header, CONFLICT under a pending merge" {
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", "one\ntwo\n");
try writeFile(io, tmp.dir, "gone.txt", "g\n");
try addManaged(gpa, io, tmp.dir, &.{ "f.txt", "gone.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "base" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "f.txt", "ONE\ntwo\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "b1", "--branch", "feat" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
var up = try runZit(gpa, io, tmp.dir, &.{ "update", "trunk" });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
try writeFile(io, tmp.dir, "f.txt", "uno\ntwo\n");
var c3 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "t1" });
defer freeResult(gpa, &c3);
try expectExitDiag(c3.term, 0, c3);
var mg = try runZit(gpa, io, tmp.dir, &.{ "merge", "feat" });
defer freeResult(gpa, &mg);
try expectExit(mg.term, 1); // conflict in f.txt
try tmp.dir.deleteFile(io, "gone.txt");
try writeFile(io, tmp.dir, "new.txt", "n\n");
try addManaged(gpa, io, tmp.dir, &.{"new.txt"});
try writeFile(io, tmp.dir, "extra.txt", "x\n");
var plain = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &plain);
try expectExitDiag(plain.term, 0, plain);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "MERGED_WITH ") != null);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "CONFLICT f.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "MISSING gone.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "ADDED new.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "extra.txt") == null);
var cls = try runZit(gpa, io, tmp.dir, &.{ "status", "--classify" });
defer freeResult(gpa, &cls);
try expectExitDiag(cls.term, 0, cls);
try std.testing.expect(std.mem.indexOf(u8, cls.stdout, "MERGED_WITH ") != null);
try std.testing.expect(std.mem.indexOf(u8, cls.stdout, "CONFLICT f.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, cls.stdout, "MISSING gone.txt\n") != null);
var only = try runZit(gpa, io, tmp.dir, &.{ "status", "--conflict" });
defer freeResult(gpa, &only);
try expectExitDiag(only.term, 0, only);
try std.testing.expect(std.mem.indexOf(u8, only.stdout, "CONFLICT f.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, only.stdout, "gone.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, only.stdout, "new.txt") == null);
var differ = try runZit(gpa, io, tmp.dir, &.{ "status", "--differ" });
defer freeResult(gpa, &differ);
try expectExitDiag(differ.term, 0, differ);
try std.testing.expect(std.mem.indexOf(u8, differ.stdout, "EXTRA extra.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, differ.stdout, "gone.txt") == null);
var verbose = try runZit(gpa, io, tmp.dir, &.{ "status", "-v" });
defer freeResult(gpa, &verbose);
try expectExitDiag(verbose.term, 0, verbose);
try std.testing.expect(std.mem.indexOf(u8, verbose.stdout, "repository: ") != null);
try std.testing.expect(std.mem.indexOf(u8, verbose.stdout, "checkout: ") != null);
try std.testing.expect(std.mem.indexOf(u8, verbose.stdout, "tags: trunk") != null);
try std.testing.expect(std.mem.indexOf(u8, verbose.stdout, "comment: t1 (user: u)") != null);
}
/// Push a file's mtime `seconds` into the past, so the tree-stat memo's
/// race window (which declines to record any freshly-written file) does not
/// suppress the behavior under test.
fn backdateFile(io: std.Io, dir: std.Io.Dir, path: []const u8, seconds: i96) !void {
var f = try dir.openFile(io, path, .{ .mode = .read_write });
defer f.close(io);
const now = std.Io.Timestamp.now(io, .real);
try f.setTimestamps(io, .{ .modify_timestamp = .{ .new = .{
.nanoseconds = now.nanoseconds - seconds * std.time.ns_per_s,
} } });
}
test "cli status: the tree-stat memo is written, never masks an edit, and its loss changes no answer" {
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", "alpha\n");
try writeFile(io, tmp.dir, "b.txt", "beta\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "t" }, &.{ "commit", "-m", "r" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const t_ = std.testing;
// Outside the race window, a clean scan hashes both files and persists
// the memo.
try backdateFile(io, tmp.dir, "a.txt", 10);
try backdateFile(io, tmp.dir, "b.txt", 10);
{
var st = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
try t_.expect(std.mem.indexOf(u8, st.stdout, "edited") == null);
}
_ = tmp.dir.statFile(io, ".zit-cache/treestat", .{}) catch
return error.TreeStatMemoNotWritten;
// An edit lands (new mtime and size): the warm memo must not mask it.
try writeFile(io, tmp.dir, "b.txt", "beta changed\n");
try backdateFile(io, tmp.dir, "b.txt", 5);
var warm = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &warm);
try expectExitDiag(warm.term, 0, warm);
try t_.expect(std.mem.indexOf(u8, warm.stdout, "EDITED b.txt") != null);
try t_.expect(std.mem.indexOf(u8, warm.stdout, "a.txt") == null);
// The ladder rule: deleting the advisory tier changes no answer, for
// status and for diff.
var cold_status_answer: []u8 = undefined;
{
try tmp.dir.deleteTree(io, ".zit-cache");
var cold = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &cold);
try expectExitDiag(cold.term, 0, cold);
cold_status_answer = try gpa.dupe(u8, cold.stdout);
}
defer gpa.free(cold_status_answer);
try t_.expectEqualStrings(warm.stdout, cold_status_answer);
var warm_diff = try runZit(gpa, io, tmp.dir, &.{"diff"});
defer freeResult(gpa, &warm_diff);
try expectExitDiag(warm_diff.term, 0, warm_diff);
{
try tmp.dir.deleteTree(io, ".zit-cache");
var cold_diff = try runZit(gpa, io, tmp.dir, &.{"diff"});
defer freeResult(gpa, &cold_diff);
try expectExitDiag(cold_diff.term, 0, cold_diff);
try t_.expectEqualStrings(cold_diff.stdout, warm_diff.stdout);
try t_.expect(std.mem.indexOf(u8, cold_diff.stdout, "beta changed") != null);
}
}