const std = @import("std");
const repeat = @import("zit").repeat;
const artifact = @import("zit").artifact;
const support = @import("support.zig");
const amendHash = support.amendHash;
const expectExit = support.expectExit;
const freeResult = support.freeResult;
const hasTag = support.hasTag;
const parseCheckin = support.parseCheckin;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const seedRootCheckin = support.seedRootCheckin;
const writeFile = support.writeFile;
// --- amend (contracts/amend.md B-amend-1..17) -------------------------------
/// An independent transcription of `help.zig`'s `amend_help` block (must match
/// byte-for-byte; drift fails B-amend-13).
const amend_help_expected =
\\zit amend NAME [--comment TEXT] [--user NAME] [--date ISO] [--branch NAME]
\\
\\Attach display overrides to a check-in by minting one control artifact.
\\History is never rewritten; the original check-in is untouched.
\\
\\ --comment TEXT override the displayed comment
\\ --user NAME override the displayed author
\\ --date ISO override the displayed date (YYYY-MM-DDTHH:MM:SS)
\\ --branch NAME move the check-in (and its descendants) to branch NAME
\\ -h, --help show this help
\\
\\At least one override flag is required.
\\
\\exit status:
\\ 0 success
\\ 1 operational failure (not a repository, unreadable state, a refused
\\ action) — the diagnostic on stderr names it
\\ 2 usage error (an unknown option or unexpected argument)
\\
;
test "cli amend B-amend-1: --comment mints a control that overrides the displayed comment" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--comment", "overridden text" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
try std.testing.expectEqualStrings("", am.stderr); // B-amend-16 stream discipline
const control_hash = try gpa.dupe(u8, try amendHash(am.stdout));
defer gpa.free(control_hash);
// A NEW, distinct artifact (not the check-in) carries the override.
try std.testing.expect(!std.mem.eql(u8, control_hash, root_hash));
// The minted artifact is a CONTROL (§5.2 D,T,U,Z) carrying `+comment` targeted
// at the check-in, never `*` (§5.4).
{
var mc = try parseCheckin(gpa, io, tmp.dir, control_hash);
defer mc.deinit();
try std.testing.expectEqual(artifact.ArtifactType.control, mc.m.kind);
try std.testing.expect(mc.m.d != null and mc.m.u != null and mc.m.z != null);
try std.testing.expect(mc.m.c == null and mc.m.f.len == 0 and mc.m.p.len == 0);
var found = false;
for (mc.m.t) |t| {
if (t.sign == .add and std.mem.eql(u8, t.name, "comment")) {
found = true;
try std.testing.expectEqualStrings("overridden text", t.value.?);
try std.testing.expect(t.target != null);
try std.testing.expectEqualStrings(root_hash, t.target.?);
}
}
try std.testing.expect(found);
}
// Option B observables: timeline comment column, info `comment:` line, and
// the raw tag view all reflect the override after crosslink.
var tl = try runZit(gpa, io, tmp.dir, &.{"timeline"});
defer freeResult(gpa, &tl);
try expectExit(tl.term, 0);
try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "overridden text") != null);
var info = try runZit(gpa, io, tmp.dir, &.{ "info", root_hash });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "comment: overridden text") != null);
var tg = try runZit(gpa, io, tmp.dir, &.{ "tag", "list", "--raw", root_hash });
defer freeResult(gpa, &tg);
try expectExit(tg.term, 0);
try std.testing.expect(std.mem.indexOf(u8, tg.stdout, "comment overridden text") != null);
}
test "cli amend B-amend-2: --user/--date surface in info; a malformed --date is a usage error" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--user", "alice", "--date", "2020-06-07T08:09:10" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
_ = try amendHash(am.stdout);
var info = try runZit(gpa, io, tmp.dir, &.{ "info", root_hash });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "user:") != null);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "alice") != null);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "date:") != null);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "2020-06-07T08:09:10") != null);
// `log --long` prints the amended event time too, not the D card's.
var lg = try runZit(gpa, io, tmp.dir, &.{ "log", "--long", root_hash });
defer freeResult(gpa, &lg);
try expectExit(lg.term, 0);
try std.testing.expect(std.mem.indexOf(u8, lg.stdout, "2020-06-07T08:09:10") != null);
// A malformed --date is rejected (exit 2) before anything is minted (§4.6.4).
var bad = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--date", "2020-06-07" });
defer freeResult(gpa, &bad);
try expectExit(bad.term, 2);
try std.testing.expectEqualStrings("", bad.stdout);
try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "invalid date") != null);
try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "try 'zit amend --help'") != null);
}
test "cli amend B-amend-3: --branch moves the check-in to the new branch (repo-state §6.2)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
// A later mtime (higher epoch) so the branch override supersedes the commit's
// own `branch=trunk` tag (repo-state §5.3 last-wins).
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender", .epoch = "2000000000" }, &.{ "amend", root_hash, "--branch", "feature" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
const control_hash = try gpa.dupe(u8, try amendHash(am.stdout));
defer gpa.free(control_hash);
// The §6.2 branch-move tag set, retargeted at the check-in.
{
var mc = try parseCheckin(gpa, io, tmp.dir, control_hash);
defer mc.deinit();
try std.testing.expect(hasTag(mc.m, .propagate, "branch"));
try std.testing.expect(hasTag(mc.m, .propagate, "sym-feature"));
try std.testing.expect(hasTag(mc.m, .cancel, "sym-trunk"));
for (mc.m.t) |t| try std.testing.expect(t.target != null); // never `*`
}
var info = try runZit(gpa, io, tmp.dir, &.{ "info", root_hash });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "branch: feature") != null);
var br = try runZit(gpa, io, tmp.dir, &.{"branch"});
defer freeResult(gpa, &br);
try expectExit(br.term, 0);
try std.testing.expect(std.mem.indexOf(u8, br.stdout, "feature") != null);
}
test "cli amend B-amend-4: several flags mint ONE control artifact" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender", .epoch = "2000000000" }, &.{ "amend", root_hash, "--comment", "c", "--user", "u2", "--branch", "feature" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
// Exactly one success line ⇒ one control artifact (never one per flag).
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, am.stdout, "\n"));
const control_hash = try gpa.dupe(u8, try amendHash(am.stdout));
defer gpa.free(control_hash);
var mc = try parseCheckin(gpa, io, tmp.dir, control_hash);
defer mc.deinit();
try std.testing.expectEqual(artifact.ArtifactType.control, mc.m.kind);
// All the overrides ride in this single artifact's T run.
try std.testing.expect(hasTag(mc.m, .add, "comment"));
try std.testing.expect(hasTag(mc.m, .add, "user"));
try std.testing.expect(hasTag(mc.m, .propagate, "branch"));
try std.testing.expect(hasTag(mc.m, .propagate, "sym-feature"));
}
test "cli amend B-amend-5: history is append-only — the manifest is untouched, no new check-in" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--comment", "shown" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
// The original manifest still parses byte-for-byte the same: same kind, same
// C-card comment "orig" (the override lives on a separate control artifact).
{
var mm = try parseCheckin(gpa, io, tmp.dir, root_hash);
defer mm.deinit();
try std.testing.expectEqual(artifact.ArtifactType.manifest, mm.m.kind);
try std.testing.expectEqualStrings("orig", mm.m.c.?.comment);
}
// The timeline gains no new check-in — a control artifact is not a check-in.
var tl = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &tl);
try expectExit(tl.term, 0);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, tl.stdout, "\n"));
// A subsequent commit on the checkout still behaves — history moved forward.
try writeFile(io, tmp.dir, "f", "v2\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "next" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
}
test "cli amend B-amend-6: amend is repeatable and last-wins by event mtime" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
// The greater-mtime control wins regardless of ingest order — mint the later
// one (higher epoch) first, then an earlier one.
var late = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender", .epoch = "2000000000" }, &.{ "amend", root_hash, "--comment", "second" });
defer freeResult(gpa, &late);
try expectExit(late.term, 0);
var early = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender", .epoch = "1500000000" }, &.{ "amend", root_hash, "--comment", "first" });
defer freeResult(gpa, &early);
try expectExit(early.term, 0);
// Two distinct control artifacts.
try std.testing.expect(!std.mem.eql(u8, try amendHash(late.stdout), try amendHash(early.stdout)));
var info = try runZit(gpa, io, tmp.dir, &.{ "info", root_hash });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "comment: second") != null);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "comment: first") == null);
}
test "cli amend B-amend-8: no override flag is a usage error, nothing minted" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runZit(gpa, io, tmp.dir, &.{ "amend", root_hash });
defer freeResult(gpa, &am);
try expectExit(am.term, 2);
try std.testing.expectEqualStrings("", am.stdout);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "no override given") != null);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "try 'zit amend --help'") != null);
}
test "cli amend B-amend-9: an unresolvable NAME fails with exit 1, nothing minted" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var am = try runZit(gpa, io, tmp.dir, &.{ "amend", repeat("d", 64), "--comment", "x" });
defer freeResult(gpa, &am);
try expectExit(am.term, 1);
try std.testing.expectEqualStrings("", am.stdout);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "not found") != null);
}
test "cli amend B-amend-10: a NAME that names a control artifact is refused by its type" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
// First amend mints a control artifact; feeding ITS hash back to amend must
// refuse — T cards target check-ins, not control artifacts.
var first = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--comment", "x" });
defer freeResult(gpa, &first);
try expectExit(first.term, 0);
const control_hash = try gpa.dupe(u8, try amendHash(first.stdout));
defer gpa.free(control_hash);
var am = try runZit(gpa, io, tmp.dir, &.{ "amend", control_hash, "--comment", "y" });
defer freeResult(gpa, &am);
try expectExit(am.term, 1);
try std.testing.expectEqualStrings("", am.stdout);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "not a check-in") != null);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "tag") != null); // §5.1 external name
}
test "cli amend B-amend-11: an empty value and a repeated flag are usage errors" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
var empty = try runZit(gpa, io, tmp.dir, &.{ "amend", root_hash, "--comment", "" });
defer freeResult(gpa, &empty);
try expectExit(empty.term, 2);
try std.testing.expectEqualStrings("", empty.stdout);
try std.testing.expect(std.mem.indexOf(u8, empty.stderr, "non-empty") != null);
var rep = try runZit(gpa, io, tmp.dir, &.{ "amend", root_hash, "--comment", "a", "--comment", "b" });
defer freeResult(gpa, &rep);
try expectExit(rep.term, 2);
try std.testing.expectEqualStrings("", rep.stdout);
try std.testing.expect(std.mem.indexOf(u8, rep.stderr, "more than once") != null);
}
test "cli amend B-amend-12: amend dispatches to the real handler; a surviving reflex still teaches" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
// `zit amend NAME --comment` succeeds — it is a real verb, not the reflex lesson.
var am = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "amender" }, &.{ "amend", root_hash, "--comment", "real" });
defer freeResult(gpa, &am);
try expectExit(am.term, 0);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "is not a zit command") == null);
// A surviving reflex entry (rebase) still teaches the immutable-history lesson.
var rb = try runZit(gpa, io, tmp.dir, &.{"rebase"});
defer freeResult(gpa, &rb);
try expectExit(rb.term, 2);
try std.testing.expect(std.mem.indexOf(u8, rb.stderr, "is not a zit command") != null);
}
test "cli amend B-amend-13: -h/--help writes the verbatim help to stdout, exit 0, mints nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Help resolves nothing, so it works even outside a repository.
var h = try runZit(gpa, io, tmp.dir, &.{ "amend", "--help" });
defer freeResult(gpa, &h);
try expectExit(h.term, 0);
try std.testing.expectEqualStrings(amend_help_expected, h.stdout);
try std.testing.expectEqualStrings("", h.stderr);
// -h wins over an otherwise-invalid trailing token.
var h2 = try runZit(gpa, io, tmp.dir, &.{ "amend", "--bogus", "-h" });
defer freeResult(gpa, &h2);
try expectExit(h2.term, 0);
try std.testing.expectEqualStrings(amend_help_expected, h2.stdout);
}
test "cli amend B-amend-14/16: usage taxonomy and stream discipline" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root_hash = try seedRootCheckin(gpa, io, tmp.dir);
defer gpa.free(root_hash);
// A missing NAME.
var no_name = try runZit(gpa, io, tmp.dir, &.{"amend"});
defer freeResult(gpa, &no_name);
try expectExit(no_name.term, 2);
try std.testing.expectEqualStrings("", no_name.stdout);
try std.testing.expect(std.mem.indexOf(u8, no_name.stderr, "no name given") != null);
// An unknown option.
var unknown = try runZit(gpa, io, tmp.dir, &.{ "amend", root_hash, "--nope" });
defer freeResult(gpa, &unknown);
try expectExit(unknown.term, 2);
try std.testing.expectEqualStrings("", unknown.stdout);
try std.testing.expect(std.mem.indexOf(u8, unknown.stderr, "unknown option '--nope'") != null);
// A second positional.
var extra = try runZit(gpa, io, tmp.dir, &.{ "amend", root_hash, "--comment", "x", "also" });
defer freeResult(gpa, &extra);
try expectExit(extra.term, 2);
try std.testing.expectEqualStrings("", extra.stdout);
try std.testing.expect(std.mem.indexOf(u8, extra.stderr, "unexpected argument 'also'") != null);
}
test "cli amend B-amend-15: outside any repository, exit 1 with the standard diagnostic" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// No `zit init` here.
var am = try runZit(gpa, io, tmp.dir, &.{ "amend", "abc", "--comment", "x" });
defer freeResult(gpa, &am);
try expectExit(am.term, 1);
try std.testing.expectEqualStrings("", am.stdout);
try std.testing.expect(std.mem.indexOf(u8, am.stderr, "no zit checkout here") != null);
}
test "cli amend B-amend-17: `zit help amend` teaches amend's help; the command list names it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var h = try runZit(gpa, io, tmp.dir, &.{ "help", "amend" });
defer freeResult(gpa, &h);
try expectExit(h.term, 0);
try std.testing.expectEqualStrings(amend_help_expected, h.stdout);
var top = try runZit(gpa, io, tmp.dir, &.{"help"});
defer freeResult(gpa, &top);
try expectExit(top.term, 0);
try std.testing.expect(std.mem.indexOf(u8, top.stdout, "amend") != null);
}