const std = @import("std");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const checkout = @import("zit").checkout;
const artifact = @import("zit").artifact;
const zit_checkin = @import("zit").checkin;
const cli = @import("zit").cli;
const File = std.Io.File;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendArtifactAs = support.appendArtifactAs;
const buildMergeDiamond = support.buildMergeDiamond;
const checkinHash = support.checkinHash;
const checkoutVersionAlloc = support.checkoutVersionAlloc;
const chmodFile = support.chmodFile;
const commitForkDup = support.commitForkDup;
const commitHashDup = support.commitHashDup;
const countLines = support.countLines;
const expectArtifactReadsBack = support.expectArtifactReadsBack;
const expectCheckoutVersion = support.expectCheckoutVersion;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectMergeState = support.expectMergeState;
const expectNoDiagnostic = support.expectNoDiagnostic;
const expectNonzeroExit = support.expectNonzeroExit;
const expectVerifies = support.expectVerifies;
const fileExists = support.fileExists;
const fileIndexUnknownOf = support.fileIndexUnknownOf;
const fileIndexWalkOf = support.fileIndexWalkOf;
const findF = support.findF;
const forceAutoRetune = support.forceAutoRetune;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const hasTag = support.hasTag;
const initRepo = support.initRepo;
const manifestReadsOf = support.manifestReadsOf;
const parentCards = support.parentCards;
const parseCheckin = support.parseCheckin;
const readFile = support.readFile;
const readSubmanifestSettings = support.readSubmanifestSettings;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const runZitStdin = support.runZitStdin;
const seedCheckout = support.seedCheckout;
const seedFossilMadeName = support.seedFossilMadeName;
const seedManyFiles = support.seedManyFiles;
const seedStdinStore = support.seedStdinStore;
const spawnServe = support.spawnServe;
const touch = support.touch;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
const zit_exe = support.zit_exe;
test "cli commit B-merge-8/-9/-11/-12: a merge commit mints the two-token P card, clears it, and round-trips" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// A clean disjoint diamond → the merge is conflict-free, so `zit commit`
// records the merge check-in.
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 mg = try runZit(gpa, io, tmp.dir, &.{ "merge", fx.other });
defer freeResult(gpa, &mg);
try expectExit(mg.term, 0);
try expectMergeState(io, tmp.dir, fx.current, fx.other);
// B-merge-8: exactly one P card, two tokens — the pre-merge checkout (the
// primary ancestor) first, the merge parent second.
const merge_commit = try commitHashDup(gpa, io, tmp.dir, "merge poem");
defer gpa.free(merge_commit);
var stored = try parseCheckin(gpa, io, tmp.dir, merge_commit);
defer stored.deinit();
var pcard_buf: [160]u8 = undefined;
const want_pcard = try std.fmt.bufPrint(&pcard_buf, "P {s} {s}\n", .{ fx.current, fx.other });
try std.testing.expect(std.mem.indexOf(u8, stored.bytes, want_pcard) != null);
try std.testing.expectEqual(@as(usize, 1), stored.m.p.len);
try std.testing.expectEqual(@as(usize, 2), stored.m.p[0].count);
// B-merge-11: a baseline manifest — no B card — whose F set is the full
// reconstructed file set (here the single merged file), never a delta.
try std.testing.expect(stored.m.b == null);
try std.testing.expectEqual(@as(usize, 1), stored.m.f.len);
try std.testing.expectEqualStrings("poem", stored.m.f[0].filename);
// B-merge-9: the commit consumes the pending merge — the checkout advances to
// the merge check-in with the merge parent cleared (one-shot).
try expectMergeState(io, tmp.dir, merge_commit, "");
// B-merge-12: `zit info` renders the merge parent through the existing
// `renderMergeParents` path (a `merge:` line naming the other side), with the
// pre-merge checkout still shown as the primary `parent:` line.
var info = try runZit(gpa, io, tmp.dir, &.{ "info", merge_commit });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
var pline: [160]u8 = undefined;
const want_parent_line = try std.fmt.bufPrint(&pline, "parent: {s}\n", .{fx.current});
try std.testing.expect(std.mem.indexOf(u8, info.stdout, want_parent_line) != null);
var mline: [160]u8 = undefined;
const want_merge_line = try std.fmt.bufPrint(&mline, "merge: {s}\n", .{fx.other});
try std.testing.expect(std.mem.indexOf(u8, info.stdout, want_merge_line) != null);
// B-merge-9: a second commit with no fresh `zit merge` is an ordinary
// single-parent check-in — the two-token path does not persist.
try writeFile(io, tmp.dir, "poem", "A1\nB\nC1\nD\n");
const next = try commitHashDup(gpa, io, tmp.dir, "follow-up");
defer gpa.free(next);
var stored2 = try parseCheckin(gpa, io, tmp.dir, next);
defer stored2.deinit();
try std.testing.expectEqual(@as(usize, 1), stored2.m.p.len);
try std.testing.expectEqual(@as(usize, 1), stored2.m.p[0].count);
try std.testing.expectEqualStrings(merge_commit, stored2.m.p[0].parents.?);
}
test "cli commit B-merge-10: a merge with unresolved conflict markers refuses, exit 1, mints nothing, keeps the merge pending" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Overlapping edits → a content conflict; markers land in `poem`.
var fx = try buildMergeDiamond(gpa, io, tmp.dir, "A\nB\nC\n", "A\nY\nC\n", "A\nX\nC\n");
defer fx.deinit(gpa);
var mg = try runZit(gpa, io, tmp.dir, &.{ "merge", fx.other });
defer freeResult(gpa, &mg);
try expectExit(mg.term, 1); // a conflict merge; the parent is recorded anyway
// The commit refuses while the marker is still in the working file.
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "try merge" });
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, "zit commit: unresolved conflict in poem\n") != null);
// Nothing minted: the checkout is un-advanced and the merge stays pending.
try expectMergeState(io, tmp.dir, fx.current, fx.other);
// Resolving the file (markers removed) lets the same commit through, and it
// still records the two-token merge P card and clears the pending merge.
try writeFile(io, tmp.dir, "poem", "A\nRESOLVED\nC\n");
const resolved = try commitHashDup(gpa, io, tmp.dir, "resolved merge");
defer gpa.free(resolved);
var stored = try parseCheckin(gpa, io, tmp.dir, resolved);
defer stored.deinit();
try std.testing.expectEqual(@as(usize, 1), stored.m.p.len);
try std.testing.expectEqual(@as(usize, 2), stored.m.p[0].count);
try expectMergeState(io, tmp.dir, resolved, "");
}
test "cli commit B-merge-9 negative space: a non-merge commit is byte-identical across independent repos (no P-card regression)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Two independent repos built through the SAME pinned-epoch, non-merge
// sequence must produce byte-for-byte identical child manifests, and each
// child carries a single-token P card — proof the merge extension never
// touches a commit with no pending merge.
const build = struct {
fn run(g: std.mem.Allocator, ioo: std.Io, dir: std.Io.Dir) ![]u8 {
try initRepo(g, ioo, dir);
try writeFile(ioo, dir, "poem", "A\nB\nC\n");
try addManaged(g, ioo, dir, &.{"poem"});
var r0 = try runCommitEnv(g, ioo, dir, .{ .user = "u", .epoch = "1000000000" }, &.{ "commit", "-m", "root" });
defer freeResult(g, &r0);
try expectExit(r0.term, 0);
try writeFile(ioo, dir, "poem", "A\nB\nC1\n");
var r1 = try runCommitEnv(g, ioo, dir, .{ .user = "u", .epoch = "1000000000" }, &.{ "commit", "-m", "child" });
defer freeResult(g, &r1);
try expectExit(r1.term, 0);
const h = try checkinHash(r1.stdout);
var stored = try parseCheckin(g, ioo, dir, h);
defer stored.deinit();
try std.testing.expectEqual(@as(usize, 1), stored.m.p.len);
try std.testing.expectEqual(@as(usize, 1), stored.m.p[0].count);
return g.dupe(u8, stored.bytes);
}
};
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
const a = try build.run(gpa, io, tmp_a.dir);
defer gpa.free(a);
const b = try build.run(gpa, io, tmp_b.dir);
defer gpa.free(b);
try std.testing.expectEqualSlices(u8, a, b);
}
test "cli commit B-merge-33/-34: a cherrypick commit mints one Q +REV card, single P parent, cleared one-shot" {
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, "poem", "A\nB\nC\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const r0 = try commitHashDup(gpa, io, tmp.dir, "root");
defer gpa.free(r0);
try writeFile(io, tmp.dir, "poem", "A\nB\nZ\n");
const r1 = try commitHashDup(gpa, io, tmp.dir, "rev");
defer gpa.free(r1);
var up = try runZit(gpa, io, tmp.dir, &.{ "update", r0 });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
try writeFile(io, tmp.dir, "poem", "Q\nB\nC\n");
const r2 = try commitForkDup(gpa, io, tmp.dir, "fork");
defer gpa.free(r2);
var cp = try runZit(gpa, io, tmp.dir, &.{ "merge", "--cherrypick", r1 });
defer freeResult(gpa, &cp);
try expectExit(cp.term, 0);
const commit = try commitHashDup(gpa, io, tmp.dir, "cherry commit");
defer gpa.free(commit);
var stored = try parseCheckin(gpa, io, tmp.dir, commit);
defer stored.deinit();
// B-merge-33: exactly one Q card, sign glued onto the target, NO baseline
// token — byte-asserted against §4.7.14's grammar.
var qbuf: [80]u8 = undefined;
const want_q = try std.fmt.bufPrint(&qbuf, "Q +{s}\n", .{r1});
try std.testing.expect(std.mem.indexOf(u8, stored.bytes, want_q) != null);
try std.testing.expectEqual(@as(usize, 1), stored.m.q.len);
try std.testing.expect(stored.m.q[0].sign == .cherrypick);
try std.testing.expectEqualStrings(r1, stored.m.q[0].target);
try std.testing.expect(stored.m.q[0].baseline == null);
// The P card is the ORDINARY single-token parent (R2) — REV is not a merge
// parent, so linkage gains no edge to it.
try std.testing.expectEqual(@as(usize, 1), stored.m.p.len);
try std.testing.expectEqual(@as(usize, 1), stored.m.p[0].count);
try std.testing.expectEqualStrings(r2, stored.m.p[0].parents.?);
// B-merge-34: the pending op is cleared one-shot; the checkout advanced.
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, tmp.dir, &ckbuf)).?;
try std.testing.expectEqualStrings(commit, ck.version);
try std.testing.expectEqualStrings("", ck.special_op);
// A following commit with no fresh cherrypick emits NO Q card.
try writeFile(io, tmp.dir, "poem", "Q\nB\nZ\nD\n");
const next = try commitHashDup(gpa, io, tmp.dir, "follow-up");
defer gpa.free(next);
var stored2 = try parseCheckin(gpa, io, tmp.dir, next);
defer stored2.deinit();
try std.testing.expectEqual(@as(usize, 0), stored2.m.q.len);
// B-merge-41: `zit info` renders the cherrypick provenance — a
// `cherrypick: <REV>` line derived from the parsed Q card (target = r1).
var info = try runZit(gpa, io, tmp.dir, &.{ "info", commit });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
var qline: [80]u8 = undefined;
const want_line = try std.fmt.bufPrint(&qline, "cherrypick: {s}\n", .{r1});
try std.testing.expect(std.mem.indexOf(u8, info.stdout, want_line) != null);
// Negative space: the plain follow-up check-in `next` carries no Q card, so
// its info output is byte-identical to its pre-B-merge-41 baseline — no
// cherrypick/backout line appears.
var ninfo = try runZit(gpa, io, tmp.dir, &.{ "info", next });
defer freeResult(gpa, &ninfo);
try expectExit(ninfo.term, 0);
var nbuf: [256]u8 = undefined;
const want_plain = try std.fmt.bufPrint(&nbuf, "{s}\nbranch: trunk\nleaf: open\nparent: {s}\n", .{ next, commit });
try std.testing.expectEqualStrings(want_plain, ninfo.stdout);
// The provenance line comes from the state, not a manifest
// read — `info` reads the same number of manifests with a Q card as
// without one.
var reads: [2]u64 = undefined;
for ([2][]const u8{ commit, next }, 0..) |name, i| {
var r = try runZitEnv(gpa, io, tmp.dir, &.{.{ "ZIT_MANIFEST_READS", "1" }}, &.{ "info", name });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
reads[i] = manifestReadsOf(&r) orelse return error.NoReadCount;
}
try std.testing.expectEqual(reads[1], reads[0]);
// Many names in one call are the single-name blocks in
// argv order, separated by one blank line; a name that does not resolve
// is reported on stderr, the rest are still described, and the exit is 1.
var both = try runZit(gpa, io, tmp.dir, &.{ "info", commit, "nope", next });
defer freeResult(gpa, &both);
try expectExit(both.term, 1);
const want_both = try std.mem.concat(gpa, u8, &.{ info.stdout, "\n", ninfo.stdout });
defer gpa.free(want_both);
try std.testing.expectEqualStrings(want_both, both.stdout);
try std.testing.expectEqualStrings("zit info: check-in 'nope' not found\n", both.stderr);
// `-F json`: one object per name in input order — the failed name is a
// row, not a hole — and the same facts the text block carries.
try writeFile(io, tmp.dir, "names.txt", try std.fmt.bufPrint(&nbuf, "{s}\nnope\n\n{s}\n", .{ commit, next }));
var js = try runZitStdin(gpa, io, tmp.dir, "names.txt", &.{ "info", "-F", "json", "-" });
defer freeResult(gpa, &js);
try expectExit(js.term, 1);
try std.testing.expectEqualStrings("", js.stderr);
var rows = std.mem.splitScalar(u8, std.mem.trimEnd(u8, js.stdout, "\n"), '\n');
const Row = struct {
name: []const u8,
hash: ?[]const u8 = null,
branch: ?[]const u8 = null,
leaf: ?[]const u8 = null,
parent: ?[]const u8 = null,
merge: ?[]const []const u8 = null,
cherrypick: ?[]const []const u8 = null,
backout: ?[]const []const u8 = null,
@"error": ?[]const u8 = null,
};
var n_rows: usize = 0;
while (rows.next()) |line| : (n_rows += 1) {
var parsed = try std.json.parseFromSlice(Row, gpa, line, .{});
defer parsed.deinit();
const row = parsed.value;
switch (n_rows) {
0 => {
try std.testing.expectEqualStrings(commit, row.name);
try std.testing.expectEqualStrings(commit, row.hash.?);
try std.testing.expectEqualStrings("trunk", row.branch.?);
try std.testing.expectEqualStrings("no", row.leaf.?);
try std.testing.expectEqual(@as(usize, 0), row.merge.?.len);
try std.testing.expectEqual(@as(usize, 1), row.cherrypick.?.len);
try std.testing.expectEqualStrings(r1, row.cherrypick.?[0]);
try std.testing.expectEqual(@as(usize, 0), row.backout.?.len);
try std.testing.expect(row.@"error" == null);
},
1 => {
try std.testing.expectEqualStrings("nope", row.name);
try std.testing.expectEqualStrings("not found", row.@"error".?);
try std.testing.expect(row.hash == null);
},
2 => {
try std.testing.expectEqualStrings(next, row.hash.?);
try std.testing.expectEqualStrings("open", row.leaf.?);
try std.testing.expectEqualStrings(commit, row.parent.?);
try std.testing.expectEqual(@as(usize, 0), row.cherrypick.?.len);
},
else => return error.TooManyRows,
}
}
try std.testing.expectEqual(@as(usize, 3), n_rows);
}
test "cli commit B-merge-33: a backout commit mints Q -REV" {
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, "poem", "A\nB\nC\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const r0 = try commitHashDup(gpa, io, tmp.dir, "root");
defer gpa.free(r0);
try writeFile(io, tmp.dir, "poem", "A\nB\nZ\n");
const r1 = try commitHashDup(gpa, io, tmp.dir, "rev");
defer gpa.free(r1);
try writeFile(io, tmp.dir, "poem", "A2\nB\nZ\n");
const r2 = try commitHashDup(gpa, io, tmp.dir, "later");
defer gpa.free(r2);
var bo = try runZit(gpa, io, tmp.dir, &.{ "merge", "--backout", r1 });
defer freeResult(gpa, &bo);
try expectExit(bo.term, 0);
const commit = try commitHashDup(gpa, io, tmp.dir, "backout commit");
defer gpa.free(commit);
var stored = try parseCheckin(gpa, io, tmp.dir, commit);
defer stored.deinit();
var qbuf: [80]u8 = undefined;
const want_q = try std.fmt.bufPrint(&qbuf, "Q -{s}\n", .{r1});
try std.testing.expect(std.mem.indexOf(u8, stored.bytes, want_q) != null);
try std.testing.expectEqual(@as(usize, 1), stored.m.q.len);
try std.testing.expect(stored.m.q[0].sign == .backout);
try std.testing.expectEqualStrings(r1, stored.m.q[0].target);
// Single-parent (R2) — a backout is never a merge parent.
try std.testing.expectEqual(@as(usize, 1), stored.m.p[0].count);
try std.testing.expectEqualStrings(r2, stored.m.p[0].parents.?);
// B-merge-41: `zit info` surfaces the backout provenance — a
// `backout: <REV>` line derived from the parsed Q card (target = r1).
var info = try runZit(gpa, io, tmp.dir, &.{ "info", commit });
defer freeResult(gpa, &info);
try expectExit(info.term, 0);
var qline: [80]u8 = undefined;
const want_line = try std.fmt.bufPrint(&qline, "backout: {s}\n", .{r1});
try std.testing.expect(std.mem.indexOf(u8, info.stdout, want_line) != null);
// Backing out the check-in the commit grows from mints no Q card: Fossil
// skips an operation on the check-in itself (checkin.c, `mid == vid`).
var self_bo = try runZit(gpa, io, tmp.dir, &.{ "merge", "--backout", commit });
defer freeResult(gpa, &self_bo);
try expectExitDiag(self_bo.term, 0, self_bo);
const undone = try commitHashDup(gpa, io, tmp.dir, "backout of itself");
defer gpa.free(undone);
const cards = try parentCards(gpa, io, tmp.dir, undone);
defer gpa.free(cards);
const want_cards = try std.fmt.allocPrint(gpa, "P {s}\n", .{commit});
defer gpa.free(want_cards);
try std.testing.expectEqualStrings(want_cards, cards);
}
test "cli commit delta dialect: a repository carrying delta manifests keeps emitting them — baseline reused, carried files complete" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
for ([_][]const u8{ "f1.txt", "f2.txt", "f3.txt", "f4.txt" }) |name| {
try writeFile(io, tmp.dir, name, name);
try addManaged(gpa, io, tmp.dir, &.{name});
}
const c1 = try commitHashDup(gpa, io, tmp.dir, "baseline");
defer gpa.free(c1);
// Hand-author a DELTA child of c1 (fossil-imported shape): new f1, f3
// deleted, f2/f4 carried by the baseline. Raw append, exactly as a sync
// ingest would land it.
const f1v2 = "f1 second version";
var blob_digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(f1v2, &blob_digest, .{});
var blob_hex_buf: [64]u8 = undefined;
const blob_hex = std.fmt.bufPrint(&blob_hex_buf, "{x}", .{&blob_digest}) catch unreachable;
var body: std.ArrayList(u8) = .empty;
defer body.deinit(gpa);
try body.print(gpa, "B {s}\nC delta\nD 2030-01-01T00:00:00\nF f1.txt {s}\nF f3.txt\nP {s}\nU u\n", .{ c1, blob_hex, c1 });
var seal_buf: [32]u8 = undefined;
const seal = artifact.selfChecksum(body.items, &seal_buf);
try body.print(gpa, "Z {s}\n", .{seal});
var name_buf: [64]u8 = undefined;
const delta_id = artifact.artifactName(.sha3_256, body.items, &name_buf);
{
var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
_ = try log.put(&blob_digest, f1v2);
var mdigest: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&mdigest, delta_id);
_ = try log.put(&mdigest, body.items);
try log.commit();
}
// Materialize the delta child (update resolves the baseline): f3 gone,
// f1 at its second version, f2/f4 carried.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "update", delta_id });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "f3.txt", .{}));
// Commit a change atop the delta parent. The dialect continues: the new
// manifest is a DELTA against c1 (the parent's own baseline — §4.7.3,
// never a delta of a delta).
try writeFile(io, tmp.dir, "f2.txt", "f2 changed");
const c3 = try commitHashDup(gpa, io, tmp.dir, "child atop delta");
defer gpa.free(c3);
var art = try runZit(gpa, io, tmp.dir, &.{ "artifact", c3 });
defer freeResult(gpa, &art);
try expectExitDiag(art.term, 0, art);
var bline_buf: [80]u8 = undefined;
const bline = try std.fmt.bufPrint(&bline_buf, "B {s}\n", .{c1});
try std.testing.expect(std.mem.indexOf(u8, art.stdout, bline) != null);
// The patch names what differs from the BASELINE: f1 (parent's change,
// carried forward), f2 (this commit), f3 as a delete marker — and NOT
// f4, which matches the baseline byte-for-byte.
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "F f1.txt ") != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "F f2.txt ") != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "F f3.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "F f4.txt") == null);
// Completeness on the derived surface: every carried file is in the
// child's effective set. Before the parent-baseline resolution landed, a
// delta parent's raw PATCH stood in for its file set — masked on the
// default path by whole-tree capture, but wrong as the baseline diff's
// ground truth, which this enumeration pins.
var ls = try runZit(gpa, io, tmp.dir, &.{ "ls", c3 });
defer freeResult(gpa, &ls);
try expectExitDiag(ls.term, 0, ls);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "f1.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "f2.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "f4.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "f3.txt") == null);
try std.testing.expectEqual(@as(usize, 3), std.mem.count(u8, ls.stdout, "\n"));
}
test "cli commit atop a delta whose baseline is a delta: one-level file set, written flat" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
for ([_][]const u8{ "a.txt", "b.txt", "c.txt" }) |name| {
try writeFile(io, tmp.dir, name, name);
try addManaged(gpa, io, tmp.dir, &.{name});
}
const base = try commitHashDup(gpa, io, tmp.dir, "base");
defer gpa.free(base);
// The erratum's Fossil 2.28 experiment, landed raw as a sync ingest
// would: d1 (B base) changes a.txt, d2 (B d1) changes b.txt. Fossil
// merges d2 one level, so its file set is {a.txt, b.txt}; c.txt, which
// d1 inherited from base, is absent.
const a2 = "a second version";
const b2 = "b second version";
var a2_digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(a2, &a2_digest, .{});
var b2_digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(b2, &b2_digest, .{});
var a2_hex_buf: [64]u8 = undefined;
const a2_hex = std.fmt.bufPrint(&a2_hex_buf, "{x}", .{&a2_digest}) catch unreachable;
var b2_hex_buf: [64]u8 = undefined;
const b2_hex = std.fmt.bufPrint(&b2_hex_buf, "{x}", .{&b2_digest}) catch unreachable;
var d1_body: std.ArrayList(u8) = .empty;
defer d1_body.deinit(gpa);
try d1_body.print(gpa, "B {s}\nC d1\nD 2030-01-01T00:00:00\nF a.txt {s}\nP {s}\nU u\n", .{ base, a2_hex, base });
var d1_seal_buf: [32]u8 = undefined;
try d1_body.print(gpa, "Z {s}\n", .{artifact.selfChecksum(d1_body.items, &d1_seal_buf)});
var d1_name_buf: [64]u8 = undefined;
const d1 = artifact.artifactName(.sha3_256, d1_body.items, &d1_name_buf);
var d2_body: std.ArrayList(u8) = .empty;
defer d2_body.deinit(gpa);
try d2_body.print(gpa, "B {s}\nC d2\nD 2030-01-02T00:00:00\nF b.txt {s}\nP {s}\nU u\n", .{ d1, b2_hex, d1 });
var d2_seal_buf: [32]u8 = undefined;
try d2_body.print(gpa, "Z {s}\n", .{artifact.selfChecksum(d2_body.items, &d2_seal_buf)});
var d2_name_buf: [64]u8 = undefined;
const d2 = artifact.artifactName(.sha3_256, d2_body.items, &d2_name_buf);
{
var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
_ = try log.put(&a2_digest, a2);
_ = try log.put(&b2_digest, b2);
var d1_digest: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&d1_digest, d1);
_ = try log.put(&d1_digest, d1_body.items);
var d2_digest: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&d2_digest, d2);
_ = try log.put(&d2_digest, d2_body.items);
try log.commit();
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "update", d2 });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "c.txt", .{}));
// Committing atop d2 must not name d1 (a delta) in a B card, so the new
// manifest is written flat over d2's one-level file set.
try writeFile(io, tmp.dir, "b.txt", "b edited");
const child = try commitHashDup(gpa, io, tmp.dir, "child atop d2");
defer gpa.free(child);
var art = try runZit(gpa, io, tmp.dir, &.{ "artifact", child });
defer freeResult(gpa, &art);
try expectExitDiag(art.term, 0, art);
try std.testing.expect(!std.mem.startsWith(u8, art.stdout, "B "));
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "\nB ") == null);
var pline_buf: [80]u8 = undefined;
const pline = try std.fmt.bufPrint(&pline_buf, "\nP {s}\n", .{d2});
try std.testing.expect(std.mem.indexOf(u8, art.stdout, pline) != null);
var aline_buf: [96]u8 = undefined;
const aline = try std.fmt.bufPrint(&aline_buf, "F a.txt {s}\n", .{a2_hex});
try std.testing.expect(std.mem.indexOf(u8, art.stdout, aline) != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "F b.txt ") != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, b2_hex) == null); // b.txt is the edit
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "c.txt") == null);
try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, art.stdout, "\nF "));
var cat = try runZit(gpa, io, tmp.dir, &.{ "cat", "b.txt", child });
defer freeResult(gpa, &cat);
try expectExitDiag(cat.term, 0, cat);
try std.testing.expectEqualStrings("b edited", cat.stdout);
}
test "cli commit: a parent whose F cards name SHA-1 blobs commits" {
// Any repository whose history predates SHA3 has parents with SHA-1 F
// cards. The commit's R-card pass looks each one up among the blobs it
// captured, which are always SHA3, so a 40-hex name is simply not one of
// them.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
const original = "same\n";
try writeFile(io, tmp.dir, "sha1.txt", original);
try addManaged(gpa, io, tmp.dir, &.{"sha1.txt"});
const blob = try appendArtifactAs(gpa, io, tmp.dir, .sha1, original);
defer gpa.free(blob);
const f = [_]artifact.FCard{.{ .filename = "sha1.txt", .hash = blob, .perm = null, .old_name = null, .mode = .regular }};
var manifest: artifact.Manifest = .{
.arena_state = std.heap.ArenaAllocator.init(gpa),
.kind = .manifest,
.a = null,
.b = null,
.c = .{ .comment = "sha1-root" },
.d = .{ .timestamp = "2020-01-01T00:00:00" },
.e = null,
.g = null,
.h = null,
.i = null,
.k = null,
.l = null,
.n = null,
.r = null,
.u = .{ .login = "u" },
.z = null,
.w = null,
.f = &f,
.j = &.{},
.m = &.{},
.p = &.{},
.q = &.{},
.t = &.{},
};
defer manifest.deinit();
const manifest_bytes = try artifact.create(&manifest, gpa);
defer gpa.free(manifest_bytes);
const checkin = try appendArtifactAs(gpa, io, tmp.dir, .sha1, manifest_bytes);
defer gpa.free(checkin);
try seedCheckout(io, tmp.dir, checkin, "trunk");
try writeFile(io, tmp.dir, "new.txt", "new\n");
try addManaged(gpa, io, tmp.dir, &.{"new.txt"});
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "on top of sha1", "--allow-fork" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
var ls = try runZit(gpa, io, tmp.dir, &.{"ls"});
defer freeResult(gpa, &ls);
try expectExit(ls.term, 0);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "sha1.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "new.txt") != null);
}
test "cli commit §4.7.6: chmod +x on a managed file is captured, recorded once, then quiesces" {
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 regular managed file, committed through zit's own capture path: §4.7.6
// permits omitting the perm token for a regular file, so its F card is
// mode-regular with no perm.
try writeFile(io, tmp.dir, "script", "#!/bin/sh\necho hi\n");
try addManaged(gpa, io, tmp.dir, &.{"script"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "regular" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
const root_hash = try gpa.dupe(u8, try checkinHash(root.stdout));
defer gpa.free(root_hash);
{
var mc = try parseCheckin(gpa, io, tmp.dir, root_hash);
defer mc.deinit();
const f = findF(mc.m, "script") orelse return error.MissingFCard;
try std.testing.expectEqual(artifact.FileMode.regular, f.mode);
try std.testing.expectEqual(@as(?[]const u8, null), f.perm);
}
// chmod +x — the content is unchanged; only the executable bit flips.
{
var script = try tmp.dir.openFile(io, "script", .{ .mode = .read_write });
defer script.close(io);
try script.setPermissions(io, File.Permissions.executable_file);
}
// The drift surfaces in diff exactly once (metadata-only M, no content read).
var drift = try runZit(gpa, io, tmp.dir, &.{ "diff", "--brief" });
defer freeResult(gpa, &drift);
try expectExit(drift.term, 0);
try std.testing.expectEqualStrings("M script\n", drift.stdout);
try std.testing.expectEqualStrings("", drift.stderr);
// commit records the mode change: a real child whose F card carries an `x`
// permission token (§4.7.6 — a token containing `x` marks the entry
// executable), not a silent revert to regular.
var rec = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "chmod +x" });
defer freeResult(gpa, &rec);
try expectExit(rec.term, 0);
const child_hash = try checkinHash(rec.stdout);
try std.testing.expect(!std.mem.eql(u8, child_hash, root_hash));
{
var mc = try parseCheckin(gpa, io, tmp.dir, child_hash);
defer mc.deinit();
const f = findF(mc.m, "script") orelse return error.MissingFCard;
try std.testing.expectEqual(artifact.FileMode.executable, f.mode);
try std.testing.expectEqualStrings("x", f.perm.?);
}
// The tree quiesces: diff is clean and a re-commit is a no-op (B-commit-9),
// where before the fix the drift re-appeared on every diff forever.
var clean = try runZit(gpa, io, tmp.dir, &.{ "diff", "--brief" });
defer freeResult(gpa, &clean);
try expectExit(clean.term, 0);
try std.testing.expectEqualStrings("", clean.stdout);
try std.testing.expectEqualStrings("", clean.stderr);
var noop = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "again" });
defer freeResult(gpa, &noop);
try expectExit(noop.term, 1);
try std.testing.expectEqualStrings("", noop.stdout);
try std.testing.expectEqualStrings("zit commit: nothing to commit (no changes)\n", noop.stderr);
}
const commit_ts = "2009-02-13T23:31:30.000";
/// An independent transcription of the B-commit-20 §Help block (must match
/// `help.zig`'s `commit_help` byte-for-byte; drift fails the test).
const commit_help_expected =
\\usage: zit commit [-m <message> | -M <file> | --editor <prog>] [-n|--dry-run]
\\ [--allow-empty] [--user <login>] [--branch <name>] [--close]
\\ [--allow-fork] [--no-sync] [--sign] [-q|--quiet]
\\ [--no-checks|--no-warnings] [--no-submanifest-advice]
\\ [--date-override <datetime>] [--] [<path>...]
\\ zit commit -R <store> -m <message> [--delete <path>]...
\\ [--rename <old> <new>]... [--stdin <path>] [--user <login>]
\\ [--branch <name>] [--parent <check-in>] [--allow-fork]
\\ [--date-override <datetime>]
\\
\\Record a check-in: save the current contents of your tracked files as a
\\new, permanent point in history on the current branch.
\\
\\zit has no staging area. `commit` reads each tracked file straight from
\\the working tree right now — there is nothing to stage first. With no
\\paths, every tracked file is captured; name one or more paths to commit
\\just those (the rest of the tree is carried forward unchanged).
\\
\\The first commit in a new repository creates the root check-in and starts
\\the default branch 'trunk'. Each check-in is named by its full artifact
\\hash (SHA3-256).
\\
\\--branch <name> starts a new branch at this check-in: the check-in moves
\\onto <name> and later commits inherit it. <name> must not already name an
\\open branch, and there must be a parent to fork from. --close marks this
\\check-in a closed leaf, so it no longer shows as an open head of its branch.
\\
\\History is immutable: a check-in is never rewritten. There is no amend and
\\no --force; to undo a file, change it and commit again. To stop tracking a
\\file (and record its deletion), use `zit rm` then commit.
\\
\\The comment comes from -m, from -M <file>, or from an editor: with no
\\-m on a terminal, commit opens $VISUAL (else $EDITOR, else --editor
\\<prog>) on a template whose '#' lines name the branch, the parent and
\\every file this commit captures; those lines are stripped, and an
\\empty comment aborts. The template lives in .zit-ci-comment in the
\\checkout root and stays there when the commit fails after the editor,
\\so 'commit -M .zit-ci-comment' retries without retyping. Off a
\\terminal, a missing -m is still a usage error. -n reports what would
\\be recorded and writes nothing. Only regular files are captured.
\\
\\On a repository with sub-manifests enabled and pinned, a commit that
\\drifts far enough from the analyzed optimum (draft-fossil-submanifests-00
\\§6.4) prints one advisory line and, interactively, offers to retune.
\\"Far enough" is zit's §6.4 relative margin m: a re-tune is advised only
\\when it would improve the analyzed read cost by more than 15% of the
\\pinned value's own cost. --no-submanifest-advice suppresses the advisory
\\entirely. Never affects whether the commit itself succeeds.
\\
\\With -R <store> and --stdin <path>, commit records the bytes on stdin as
\\<path>'s new content in a check-in in that store, every other file
\\carried forward from the parent, with no checkout created or consulted.
\\The parent is the tip of --branch when that branch is open, else of
\\the main branch; --parent names it instead, and one that is no longer
\\the tip of its branch is refused unless --allow-fork. The check-in
\\stays on its parent's branch, or starts --branch. stdout is the new
\\check-in's full hash; bytes equal to <path>'s content at the parent
\\write nothing, print the parent's hash and exit 3. A before-commit
\\check runs in a working area holding only <path>; --no-checks skips
\\it. Writers to one store wait for each other, so concurrent ones land
\\one after another. It never autosyncs.
\\
\\--delete <path> and --rename <old> <new>, each repeatable, join --stdin
\\(or stand without it) in ONE check-in, applied in this order: the
\\deletes, the renames, then stdin's bytes. --stdin naming a rename's
\\<new> gives the renamed file those bytes. A rename records what
\\'zit mv' and a commit record, so 'log --follow' and 'annotate' follow
\\it. Each names one file: a path the parent lacks, a directory, a
\\rename onto a file the parent holds (unless this command deletes it),
\\a rename onto itself, or one path named twice is refused, and nothing
\\is written.
\\
\\--date-override <datetime> records <datetime> as the check-in's time
\\instead of now, as Fossil's option does: 'now', YYYY-MM-DD, or
\\YYYY-MM-DD HH:MM[:SS[.SSS]] with a space or T, and optionally Z or
\\+HH:MM / -HH:MM after the time (RFC 3339, 2026-11-05T10:12:03.117Z,
\\is one of these). The time is kept to the millisecond. One that
\\does not parse, or falls before 1970, exits 1. With -R, a resend
\\that gives the same time, user and comment (and --branch and
\\--parent, when given) is answered by the check-in the first send
\\made, exit 0, and nothing is written, as the daemon answers one.
\\
\\options:
\\ -m, --message <message> the check-in comment
\\ -M, --message-file <file> read the comment from <file>
\\ --editor <prog> compose the comment in <prog> (default:
\\ $VISUAL, then $EDITOR, on a terminal)
\\ -n, --dry-run list what would be recorded; write nothing
\\ --allow-empty record a check-in with no file changes
\\ --user <login> author to record (default: $USER)
\\ --branch <name> start a new branch at this check-in
\\ --close mark this check-in a closed leaf
\\ --allow-fork commit even though the current check-in is no
\\ longer the tip of its branch — a deliberate
\\ fork (refused otherwise, naming the newer tip)
\\ --no-sync skip autosync (the pull before and push
\\ after) for this one commit; required sparse
\\ structural content may still be fetched
\\ --no-checks waive the binary-glob/crlf-glob/encoding-glob
\\ content gate for this one commit
\\ (--no-warnings is Fossil's spelling of the
\\ same waiver)
\\ --sign clearsign the manifest before storing it.
\\ The signed text IS the stored artifact, so
\\ every reader strips the wrapper to reach
\\ the cards and the signature travels with
\\ the check-in wherever it syncs.
\\ The signer is $ZIT_PGP_COMMAND, fossil's
\\ pgp-command contract: default
\\ 'gpg --clearsign -o ', invoked as
\\ COMMAND OUTFILE INFILE; a command whose
\\ basename starts with 'ssh' is invoked as
\\ COMMAND INFILE, writes INFILE.sig, and its
\\ armor is assembled here. 'off' disables
\\ signing. Refused — never committed
\\ unsigned — if the signer fails or alters
\\ the manifest it was given.
\\ -q, --quiet do not print the check-in line
\\ --no-submanifest-advice
\\ suppress the sub-manifest drift advisory
\\ --stdin <path> record stdin as <path>'s content in the store
\\ -R names (see above)
\\ --delete <path> with -R: remove <path> from the check-in
\\ --rename <old> <new> with -R: record <old> as renamed to <new>
\\ --parent <check-in> with --stdin, --rename or --delete: the
\\ check-in to build on (default: the tip)
\\ --date-override <datetime>
\\ record <datetime> as the check-in's time
\\ -h, --help show this help and exit
\\
\\exit status:
\\ 0 the check-in was created; with -R, also when the check-in these
\\ edits make is already in the store (its name is printed and
\\ nothing is written)
\\ 1 nothing to commit, a tracked file is missing, an empty message, an
\\ existing branch name, or this is not a zit repository
\\ 2 usage error (no message, an unknown option, or --stdin, --rename
\\ or --delete without -R)
\\ 3 with -R: the edits leave the parent's files as they were; the
\\ parent is printed and nothing was written
\\
\\aliases: ci, checkin
\\
;
test "cli commit B-commit-18a: --help and -h print the help to stdout, exit 0" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var r1 = try runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "--help" });
defer freeResult(gpa, &r1);
try expectExit(r1.term, 0);
try std.testing.expectEqualStrings(commit_help_expected, r1.stdout);
try std.testing.expectEqualStrings("", r1.stderr);
// -h wins even mixed with other tokens (no repo needed).
var r2 = try runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "-m", "x", "-h", "-z" });
defer freeResult(gpa, &r2);
try expectExit(r2.term, 0);
try std.testing.expectEqualStrings(commit_help_expected, r2.stdout);
}
test "cli commit --branch repostate §6.1/§6.2: a new check-in starts NAME and becomes its tip" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Root check-in on trunk.
try writeFile(io, tmp.dir, "f", "v1\n");
try addManaged(gpa, io, tmp.dir, &.{"f"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
const root_hash = try gpa.dupe(u8, try checkinHash(root.stdout));
defer gpa.free(root_hash);
// A child that forks onto branch 'feature'.
try writeFile(io, tmp.dir, "f", "v2\n");
var child = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "start feature" });
defer freeResult(gpa, &child);
try expectExit(child.term, 0);
// The success line names the branch it landed on (§6.1).
try std.testing.expect(std.mem.endsWith(u8, child.stdout, " on feature\n"));
const child_hash = try gpa.dupe(u8, try checkinHash(child.stdout));
defer gpa.free(child_hash);
try std.testing.expect(!std.mem.eql(u8, child_hash, root_hash));
// §6.2 branch-creation cards: the propagating branch/sym tags plus the
// sym-trunk cancellation, byte-checked in the minted manifest.
{
var mc = try parseCheckin(gpa, io, tmp.dir, child_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"));
// The branch card carries NAME as its value.
for (mc.m.t) |t| {
if (t.sign == .propagate and std.mem.eql(u8, t.name, "branch"))
try std.testing.expectEqualStrings("feature", t.value.?);
}
}
// Derived view (§6.3/§6.4): both branches exist, feature is current tip.
var br = try runZit(gpa, io, tmp.dir, &.{"branch"});
defer freeResult(gpa, &br);
try expectExit(br.term, 0);
try std.testing.expectEqualStrings("* feature\n trunk\n", br.stdout);
// The branch name resolves to the child check-in (`ls feature` lists it).
var lf = try runZit(gpa, io, tmp.dir, &.{ "ls", "feature" });
defer freeResult(gpa, &lf);
try expectExit(lf.term, 0);
try std.testing.expect(std.mem.indexOf(u8, lf.stdout, "f\n") != null);
}
test "cli commit --branch repostate §6.2: rejected (exit 1) when NAME already names an open branch" {
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", "v1\n");
try addManaged(gpa, io, tmp.dir, &.{"f"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
// Create branch 'feature' (now an open branch).
try writeFile(io, tmp.dir, "f", "v2\n");
var first = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "start" });
defer freeResult(gpa, &first);
try expectExit(first.term, 0);
// A second `--branch feature` must not re-create the existing open branch.
try writeFile(io, tmp.dir, "f", "v3\n");
var second = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "again" });
defer freeResult(gpa, &second);
try expectExit(second.term, 1);
try std.testing.expectEqualStrings("", second.stdout);
try std.testing.expect(std.mem.indexOf(u8, second.stderr, "already exists") != null);
}
test "cli commit: a --user or --branch the manifest cannot carry is refused and writes 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, "f", "v1\n");
try addManaged(gpa, io, tmp.dir, &.{"f"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
try writeFile(io, tmp.dir, "f", "v2\n");
const before = try tmp.dir.readFileAlloc(io, ".zit", gpa, .limited(1 << 24));
defer gpa.free(before);
const cases = [_]struct { argv: []const []const u8, says: []const u8 }{
.{ .argv = &.{ "commit", "-m", "x", "--user", "e\nZ fake" }, .says = "--user" },
.{ .argv = &.{ "commit", "-m", "x", "--user", "e\x01" }, .says = "--user" },
.{ .argv = &.{ "commit", "-m", "x", "--branch", "x\ny" }, .says = "--branch" },
};
for (cases) |c| {
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, c.argv);
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
if (std.mem.indexOf(u8, res.stderr, c.says) == null) {
std.debug.print("expected '{s}' in: {s}\n", .{ c.says, res.stderr });
return error.TestUnexpectedResult;
}
const after = try tmp.dir.readFileAlloc(io, ".zit", gpa, .limited(1 << 24));
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
// Spaces are fine, from --user and from the environment alike.
var spaced = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "John Doe" }, &.{ "commit", "-m", "x", "--branch", "my branch" });
defer freeResult(gpa, &spaced);
try expectExitDiag(spaced.term, 0, spaced);
try expectSpacedNames(gpa, io, tmp.dir, ".", try branchTip(gpa, io, tmp.dir, "my branch"), true);
try writeFile(io, tmp.dir, "f", "v3\n");
var flagged = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "y", "--user", "John Doe" });
defer freeResult(gpa, &flagged);
try expectExitDiag(flagged.term, 0, flagged);
try expectSpacedNames(gpa, io, tmp.dir, ".", try branchTip(gpa, io, tmp.dir, "my branch"), false);
}
/// The full hash `name` resolves to, in a buffer that lives until the next call.
fn branchTip(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, name: []const u8) ![]const u8 {
const S = struct {
var buf: [64]u8 = undefined;
};
var info = try infoJson(gpa, io, dir, ".", name);
defer freeResult(gpa, &info);
const key = "\"hash\":\"";
const at = (std.mem.indexOf(u8, info.stdout, key) orelse return error.TestUnexpectedResult) + key.len;
@memcpy(&S.buf, info.stdout[at..][0..64]);
return &S.buf;
}
test "cli commit --branch repostate §6.2: rejected (exit 1) on an unborn checkout" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// No root yet: there is no parent to fork a branch from (§6.2).
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "-m", "x" });
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, "needs a parent") != null);
}
test "cli commit --branch --close repostate §7.4: the check-in is a closed leaf on NAME" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "f", "v1\n");
try addManaged(gpa, io, tmp.dir, &.{"f"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
const root_hash = try gpa.dupe(u8, try checkinHash(root.stdout));
defer gpa.free(root_hash);
// Fork onto feature AND close the new leaf in one commit.
try writeFile(io, tmp.dir, "f", "v2\n");
var child = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--branch", "feature", "--close", "-m", "closed feature" });
defer freeResult(gpa, &child);
try expectExit(child.term, 0);
const child_hash = try gpa.dupe(u8, try checkinHash(child.stdout));
defer gpa.free(child_hash);
// §4.7 +closed is present alongside the branch cards.
{
var mc = try parseCheckin(gpa, io, tmp.dir, child_hash);
defer mc.deinit();
try std.testing.expect(hasTag(mc.m, .add, "closed"));
try std.testing.expect(hasTag(mc.m, .propagate, "branch"));
}
// §7.4: the closed check-in is not an open leaf, so `leaves` omits it; the
// still-open trunk root remains.
var lv = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &lv);
try expectExit(lv.term, 0);
try std.testing.expect(std.mem.indexOf(u8, lv.stdout, cli.shortHash(child_hash)) == null);
try std.testing.expect(std.mem.indexOf(u8, lv.stdout, cli.shortHash(root_hash)) != null);
// The branch view marks feature current and closed.
var br = try runZit(gpa, io, tmp.dir, &.{"branch"});
defer freeResult(gpa, &br);
try expectExit(br.term, 0);
try std.testing.expectEqualStrings("* feature (closed)\n trunk\n", br.stdout);
}
test "cli commit B-commit-5: a missing message is a usage error (exit 2, 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 runCommitEnv(gpa, io, tmp.dir, .{}, &.{"commit"});
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: a check-in message is required\n" ++
" give one with -m <message>,\n" ++
" run from a terminal to compose it in $EDITOR,\n" ++
" or set an editor: zit settings editor <command>\n" ++
"try 'zit commit --help'\n",
res.stderr,
);
}
test "cli commit B-commit-18b: an unknown option is a usage error (exit 2)" {
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 runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "-m", "x", "-z" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: unknown option '-z'\ntry 'zit commit --help'\n",
res.stderr,
);
}
test "cli commit B-commit-20 (DX): --amend/--force teach the immutable-history model, exit 2, stderr only" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// The reflex flag is rejected right after parse, before any repo discovery,
// so no repo is needed; the message must teach that history is immutable
// (check-ins are never rewritten) rather than print a bare "unknown option"
// that reads as a typo. Still a usage error: exit 2, stderr only, try-help kept.
inline for (.{
.{ &[_][]const u8{ "commit", "-m", "x", "--amend" }, "--amend" },
.{ &[_][]const u8{ "commit", "-m", "x", "--force" }, "--force" },
.{ &[_][]const u8{ "commit", "-m", "x", "-f" }, "-f" },
}) |case| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, case[0]);
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: '" ++ case[1] ++ "' is not supported. zit history is immutable — " ++
"check-ins are never rewritten; record a new check-in that supersedes the old one.\n" ++
"try 'zit commit --help'\n",
res.stderr,
);
}
}
test "cli commit B-commit-14: outside a repository is an expected failure (exit 1)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{}); // no init
defer tmp.cleanup();
var res = try runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "-m", "x" });
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 commit B-commit-4: an empty or whitespace-only message is rejected (exit 1), nothing written" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a.txt");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
inline for (.{ "", " " }) |msg| {
var res = try runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "-m", msg });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: aborting — empty check-in message\n",
res.stderr,
);
}
// Unborn trunk untouched: nothing was committed.
try expectCheckoutVersion(io, tmp.dir, "");
}
test "cli commit B-commit-9: a root with no tracked files has nothing to commit (exit 1)" {
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 runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "commit", "-m", "x" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: nothing to commit (no tracked files) — use 'zit add <file>'\n",
res.stderr,
);
try expectCheckoutVersion(io, tmp.dir, "");
}
test "cli commit B-commit-2/13/16/17: the root check-in mints trunk, decodes back through the library" {
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", "hello\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("a.txt added\n", res.stderr);
// Structural success line: `check-in <64-hex> on trunk` (B-commit-2/16/19).
const hash = try checkinHash(res.stdout);
try std.testing.expect(std.mem.endsWith(u8, res.stdout, " on trunk\n"));
// The checkout advanced to the new hash (B-commit-15).
try expectCheckoutVersion(io, tmp.dir, hash);
// Decode the manifest back through the library (B-commit-17).
var mc = try parseCheckin(gpa, io, tmp.dir, hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expectEqual(artifact.ArtifactType.manifest, m.kind);
try std.testing.expect(m.c != null and std.mem.eql(u8, m.c.?.comment, "first"));
try std.testing.expect(m.u != null and std.mem.eql(u8, m.u.?.login, "alice"));
// Root: no P card, an F card for a.txt, and the two trunk tags (B-commit-13).
try std.testing.expectEqual(@as(usize, 0), m.p.len);
const fa = findF(m, "a.txt") orelse return error.MissingFCard;
try std.testing.expectEqual(@as(usize, 64), fa.hash.?.len); // SHA3-256 blob name
try std.testing.expect(fa.mode == .regular);
try std.testing.expectEqual(@as(usize, 2), m.t.len);
try std.testing.expect(m.t[0].sign == .propagate and std.mem.eql(u8, m.t[0].name, "branch"));
try std.testing.expect(m.t[0].value != null and std.mem.eql(u8, m.t[0].value.?, "trunk"));
try std.testing.expect(m.t[1].sign == .propagate and std.mem.eql(u8, m.t[1].name, "sym-trunk"));
try std.testing.expect(m.t[1].value == null);
}
test "cli commit B-commit-7: the D card is the pinned UTC writer timestamp; commits are reproducible" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var hashes: [2][]u8 = undefined;
inline for (0..2) |i| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "same bytes\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "bob" }, &.{ "commit", "-m", "msg" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const hash = try checkinHash(res.stdout);
var mc = try parseCheckin(gpa, io, tmp.dir, hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expect(m.d != null);
try std.testing.expectEqualStrings(commit_ts, m.d.?.timestamp);
hashes[i] = try gpa.dupe(u8, hash);
}
defer gpa.free(hashes[0]);
defer gpa.free(hashes[1]);
// Identical inputs + pinned clock/author ⇒ identical manifest hash.
try std.testing.expectEqualStrings(hashes[0], hashes[1]);
}
test "cli commit B-commit-6: author is --user, else $USER, else an expected failure" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// (a) --user overrides the environment author.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "x\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "env-user" }, &.{ "commit", "-m", "m", "--user", "flag-user" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const hash = try checkinHash(res.stdout);
var mc = try parseCheckin(gpa, io, tmp.dir, hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expectEqualStrings("flag-user", m.u.?.login);
}
// (b) no --user ⇒ the $USER environment default.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "x\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "env-user" }, &.{ "commit", "-m", "m" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const hash = try checkinHash(res.stdout);
var mc = try parseCheckin(gpa, io, tmp.dir, hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expectEqualStrings("env-user", m.u.?.login);
}
// (c) neither ⇒ author-missing, exit 1, nothing written.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "x\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = null }, &.{ "commit", "-m", "m" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: cannot determine the check-in author; pass --user <login>\n",
res.stderr,
);
try expectCheckoutVersion(io, tmp.dir, "");
}
}
test "cli commit B-commit-3/9: a child check-in carries a P card; an unchanged re-commit is a no-op" {
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", "v1\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var r1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &r1);
try expectExit(r1.term, 0);
const root_hash = try gpa.dupe(u8, try checkinHash(r1.stdout));
defer gpa.free(root_hash);
// No change ⇒ B-commit-9 child no-op, checkout unchanged.
var rnoop = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "again" });
defer freeResult(gpa, &rnoop);
try expectExit(rnoop.term, 1);
try std.testing.expectEqualStrings("", rnoop.stdout);
try std.testing.expectEqualStrings("zit commit: nothing to commit (no changes)\n", rnoop.stderr);
try expectCheckoutVersion(io, tmp.dir, root_hash);
// Edit ⇒ a real child: new hash, one P card == root, no T card, checkout advanced.
try writeFile(io, tmp.dir, "a.txt", "v2\n");
var r2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "second" });
defer freeResult(gpa, &r2);
try expectExit(r2.term, 0);
const child_hash = try checkinHash(r2.stdout);
try std.testing.expect(!std.mem.eql(u8, child_hash, root_hash));
try expectCheckoutVersion(io, tmp.dir, child_hash);
var mc = try parseCheckin(gpa, io, tmp.dir, child_hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expectEqual(@as(usize, 1), m.p.len);
try std.testing.expect(m.p[0].parents != null and std.mem.eql(u8, m.p[0].parents.?, root_hash));
try std.testing.expectEqual(@as(usize, 0), m.t.len); // child inherits trunk by propagation
}
test "cli commit B-commit-10: a managed file missing/non-regular is refused, nothing written" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a.txt");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
// Remove the working-tree file (but keep it managed): an all-files commit
// must refuse, teaching `zit rm` for a genuine deletion.
try tmp.dir.deleteFile(io, "a.txt");
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "m" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: cannot commit 'a.txt': missing from the working tree (use 'zit rm' to stop tracking it)\n",
res.stderr,
);
try expectCheckoutVersion(io, tmp.dir, "");
}
test "cli commit B-commit-11: a partial commit re-hashes named files and carries the rest forward" {
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", "a1\n");
try writeFile(io, tmp.dir, "b.txt", "b1\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
var r1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &r1);
try expectExit(r1.term, 0);
const root_hash = try gpa.dupe(u8, try checkinHash(r1.stdout));
defer gpa.free(root_hash);
var root_mc = try parseCheckin(gpa, io, tmp.dir, root_hash);
defer root_mc.deinit();
const root_m = root_mc.m;
const b_orig_hash = try gpa.dupe(u8, (findF(root_m, "b.txt") orelse return error.MissingFCard).hash.?);
defer gpa.free(b_orig_hash);
// Edit both, but commit only a.txt: b.txt must be carried at its parent hash.
try writeFile(io, tmp.dir, "a.txt", "a2\n");
try writeFile(io, tmp.dir, "b.txt", "b2-uncommitted\n");
var r2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "partial", "a.txt" });
defer freeResult(gpa, &r2);
try expectExit(r2.term, 0);
const child_hash = try checkinHash(r2.stdout);
var mc = try parseCheckin(gpa, io, tmp.dir, child_hash);
defer mc.deinit();
const m = mc.m;
const fa = findF(m, "a.txt") orelse return error.MissingFCard;
const fb = findF(m, "b.txt") orelse return error.MissingFCard;
// a.txt re-hashed (differs from its root card); b.txt carried unchanged.
const fa_root = findF(root_m, "a.txt").?;
try std.testing.expect(!std.mem.eql(u8, fa.hash.?, fa_root.hash.?));
try std.testing.expectEqualStrings(b_orig_hash, fb.hash.?);
}
test "cli commit B-commit-11: a named unmanaged path is not tracked (exit 1)" {
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 addManaged(gpa, io, tmp.dir, &.{"a.txt"});
try writeFile(io, tmp.dir, "b.txt", "b\n"); // exists but never `zit add`ed
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "m", "b.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit commit: 'b.txt' is not tracked (use 'zit add' first)\n",
res.stderr,
);
try expectCheckoutVersion(io, tmp.dir, "");
}
test "cli commit B-commit-12: after `zit rm`, an all-files commit records the deletion" {
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 writeFile(io, tmp.dir, "b.txt", "b\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
var r1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &r1);
try expectExit(r1.term, 0);
// Forget b.txt, then commit: the new manifest omits b's F card (deletion).
var rrm = try runZit(gpa, io, tmp.dir, &.{ "rm", "b.txt" });
defer freeResult(gpa, &rrm);
try expectExit(rrm.term, 0);
var r2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "drop b", "--user", "e2e" });
defer freeResult(gpa, &r2);
try expectExit(r2.term, 0);
const child_hash = try checkinHash(r2.stdout);
var mc = try parseCheckin(gpa, io, tmp.dir, child_hash);
defer mc.deinit();
const m = mc.m;
try std.testing.expect(findF(m, "a.txt") != null);
try std.testing.expect(findF(m, "b.txt") == null); // deletion recorded
}
test "cli commit B-commit-19: -q suppresses the success line but still advances the checkout" {
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 addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-q", "-m", "quiet" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout); // no success line
try std.testing.expectEqualStrings("", res.stderr);
// The commit still happened: the checkout advanced off the unborn state.
const version = try checkoutVersionAlloc(gpa, io, tmp.dir);
defer gpa.free(version);
try std.testing.expectEqual(@as(usize, 64), version.len);
}
test "cli commit: a stale parent refuses naming the tip; --allow-fork forks deliberately" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "one\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
const root = try commitHashDup(gpa, io, tmp.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp.dir, "a.txt", "two\n");
const tip = try commitHashDup(gpa, io, tmp.dir, "tip");
defer gpa.free(tip);
// Move BEHIND the tip and edit: this parent now has a same-branch child.
{
var up = try runZit(gpa, io, tmp.dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
try writeFile(io, tmp.dir, "a.txt", "divergent\n");
// Refused, naming the newer tip and both remedies; nothing recorded.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "would-fork" });
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, tip) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "--allow-fork") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit update") != null);
}
try expectCheckoutVersion(io, tmp.dir, root);
// --allow-fork proceeds; the branch now has two open leaves.
{
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "forked", "--allow-fork" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
{
var lv = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &lv);
try expectExit(lv.term, 0);
var count: usize = 0;
var lines = std.mem.splitScalar(u8, lv.stdout, '\n');
while (lines.next()) |line| {
if (line.len != 0) count += 1;
}
try std.testing.expectEqual(@as(usize, 2), count);
}
}
// --- commit --stdin: one file's bytes into a store, no checkout -----------
/// `zit commit -R <store> --stdin <path> <extra...>` with `content` on stdin,
/// run from `dir` (the store is named relative to it).
fn commitStdin(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
store: []const u8,
path: []const u8,
content: []const u8,
extra: []const []const u8,
) !std.process.RunResult {
try writeFile(io, dir, "stdin.in", content);
var argv: [24][]const u8 = undefined;
const head = [_][]const u8{ "commit", "-R", store, "--stdin", path, "--user", "u" };
@memcpy(argv[0..head.len], &head);
std.debug.assert(head.len + extra.len <= argv.len);
@memcpy(argv[head.len..][0..extra.len], extra);
return runZitStdin(gpa, io, dir, "stdin.in", argv[0 .. head.len + extra.len]);
}
/// The full hash `commit --stdin` printed: exactly one 64-hex line, nothing
/// else. Returned without its newline, borrowing `stdout`.
fn stdinHash(stdout: []const u8) ![]const u8 {
try std.testing.expectEqual(@as(usize, 65), stdout.len);
try std.testing.expectEqual(@as(u8, '\n'), stdout[64]);
for (stdout[0..64]) |c| try std.testing.expect(std.ascii.isHex(c) and !std.ascii.isUpper(c));
return stdout[0..64];
}
/// Whether `zit cat PATH VERSION -R STORE` prints exactly `want`.
fn expectCatAt(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8, path: []const u8, version: []const u8, want: []const u8) !void {
var res = try runZit(gpa, io, dir, &.{ "cat", path, version, "-R", store });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(want, res.stdout);
}
/// `zit info -R STORE -F json NAME`, owned by the caller.
fn infoJson(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8, name: []const u8) !std.process.RunResult {
const res = try runZit(gpa, io, dir, &.{ "info", "-R", store, "-F", "json", name });
try expectExitDiag(res.term, 0, res);
return res;
}
fn expectJsonField(json: []const u8, comptime key: []const u8, value: []const u8) !void {
var buf: [160]u8 = undefined;
const want = try std.fmt.bufPrint(&buf, "\"" ++ key ++ "\":\"{s}\"", .{value});
if (std.mem.indexOf(u8, json, want) == null) {
std.debug.print("expected {s} in {s}\n", .{ want, json });
return error.TestUnexpectedResult;
}
}
fn leafCount(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8) !usize {
var res = try runZit(gpa, io, dir, &.{ "leaves", "-R", store });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
return countLines(res.stdout);
}
fn storeSize(io: std.Io, dir: std.Io.Dir, store: []const u8) !u64 {
var buf: [256]u8 = undefined;
const name = try std.fmt.bufPrint(&buf, "{s}/.zit", .{store});
return (try dir.statFile(io, name, .{})).size;
}
/// No working area survives beside the store.
fn expectNoWorkArea(io: std.Io, dir: std.Io.Dir, store: []const u8) !void {
var s = try dir.openDir(io, store, .{ .iterate = true });
defer s.close(io);
var it = s.iterate();
while (try it.next(io)) |e| {
if (std.mem.startsWith(u8, e.name, ".zit-tmp-")) {
std.debug.print("leftover working area: {s}\n", .{e.name});
return error.TestUnexpectedResult;
}
}
}
test "cli commit --stdin: records stdin as PATH in a store with no checkout, carries the rest, prints only the hash" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var one = try commitStdin(gpa, io, tmp.dir, "store", "notes/a.md", "hello\n", &.{ "-m", "note" });
defer freeResult(gpa, &one);
try expectExitDiag(one.term, 0, one);
try expectNoDiagnostic(one.stderr);
const h1 = try stdinHash(one.stdout);
try expectCatAt(gpa, io, tmp.dir, "store", "notes/a.md", h1, "hello\n");
try expectCatAt(gpa, io, tmp.dir, "store", "f.txt", h1, "f bytes\n");
try expectCatAt(gpa, io, tmp.dir, "store", "g.txt", h1, "g bytes\n");
try expectNoWorkArea(io, tmp.dir, "store");
// An existing file's new bytes; the note is carried forward this time.
var two = try commitStdin(gpa, io, tmp.dir, "store", "f.txt", "f edited\n", &.{ "-m", "edit" });
defer freeResult(gpa, &two);
try expectExitDiag(two.term, 0, two);
const h2 = try stdinHash(two.stdout);
try expectCatAt(gpa, io, tmp.dir, "store", "f.txt", h2, "f edited\n");
try expectCatAt(gpa, io, tmp.dir, "store", "notes/a.md", h2, "hello\n");
try expectCatAt(gpa, io, tmp.dir, "store", "g.txt", h2, "g bytes\n");
var info = try infoJson(gpa, io, tmp.dir, "store", h2);
defer freeResult(gpa, &info);
try expectJsonField(info.stdout, "parent", h1);
try expectJsonField(info.stdout, "branch", "trunk");
try std.testing.expectEqual(@as(usize, 1), try leafCount(gpa, io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
}
test "cli commit --stdin: the first commit into an empty store creates the root" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "store", .default_dir);
{
var s = try tmp.dir.openDir(io, "store", .{});
defer s.close(io);
try initRepo(gpa, io, s);
}
// The root starts the main branch: --branch has nothing to fork from.
const before = try storeSize(io, tmp.dir, "store");
var refused = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "a\n", &.{ "-m", "first", "--branch", "feat" });
defer freeResult(gpa, &refused);
try expectExit(refused.term, 1);
try std.testing.expectEqualStrings("", refused.stdout);
try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "--branch needs a parent") != null);
try std.testing.expectEqual(before, try storeSize(io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
var res = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "a\n", &.{ "-m", "first" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const h = try stdinHash(res.stdout);
try expectCatAt(gpa, io, tmp.dir, "store", "a.txt", h, "a\n");
var info = try infoJson(gpa, io, tmp.dir, "store", h);
defer freeResult(gpa, &info);
try expectJsonField(info.stdout, "branch", "trunk");
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "\"parent\":\"") == null);
try std.testing.expectEqual(@as(usize, 1), try leafCount(gpa, io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
}
test "cli commit --stdin: bytes equal to the parent's write nothing, print the parent, and exit 3" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var first = try commitStdin(gpa, io, tmp.dir, "store", "notes/a.md", "same\n", &.{ "-m", "note" });
defer freeResult(gpa, &first);
try expectExitDiag(first.term, 0, first);
const h = try stdinHash(first.stdout);
const before = try storeSize(io, tmp.dir, "store");
var again = try commitStdin(gpa, io, tmp.dir, "store", "notes/a.md", "same\n", &.{ "-m", "again" });
defer freeResult(gpa, &again);
try expectExit(again.term, 3);
try std.testing.expectEqualStrings(h, try stdinHash(again.stdout));
try std.testing.expectEqualStrings("", again.stderr);
try std.testing.expectEqual(before, try storeSize(io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
}
test "cli commit --stdin: a stale --parent is refused and writes nothing; --allow-fork forks" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var one = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "1\n", &.{ "-m", "one" });
defer freeResult(gpa, &one);
try expectExitDiag(one.term, 0, one);
const h1 = try stdinHash(one.stdout);
var two = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "2\n", &.{ "-m", "two", "--parent", h1 });
defer freeResult(gpa, &two);
try expectExitDiag(two.term, 0, two);
const h2 = try stdinHash(two.stdout);
// h1 was the tip when `two` named it; now it is not.
const before = try storeSize(io, tmp.dir, "store");
var stale = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "3\n", &.{ "-m", "three", "--parent", h1 });
defer freeResult(gpa, &stale);
try expectExit(stale.term, 1);
try std.testing.expectEqualStrings("", stale.stdout);
try std.testing.expect(std.mem.indexOf(u8, stale.stderr, "not the tip of branch 'trunk'") != null);
try std.testing.expect(std.mem.indexOf(u8, stale.stderr, h2) != null);
try std.testing.expectEqual(before, try storeSize(io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
var fork = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "3\n", &.{ "-m", "three", "--parent", h1, "--allow-fork" });
defer freeResult(gpa, &fork);
try expectExitDiag(fork.term, 0, fork);
const h3 = try stdinHash(fork.stdout);
var info = try infoJson(gpa, io, tmp.dir, "store", h3);
defer freeResult(gpa, &info);
try expectJsonField(info.stdout, "parent", h1);
try std.testing.expectEqual(@as(usize, 2), try leafCount(gpa, io, tmp.dir, "store"));
}
test "cli commit --stdin: --branch lands on that branch; without it the check-in stays on its parent's" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var trunk_info = try infoJson(gpa, io, tmp.dir, "store", "trunk");
defer freeResult(gpa, &trunk_info);
// A new branch, forked from the trunk tip.
var b1 = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "feat 1\n", &.{ "-m", "feat one", "--branch", "feat" });
defer freeResult(gpa, &b1);
try expectExitDiag(b1.term, 0, b1);
const f1 = try stdinHash(b1.stdout);
var info_b = try infoJson(gpa, io, tmp.dir, "store", f1);
defer freeResult(gpa, &info_b);
try expectJsonField(info_b.stdout, "branch", "feat");
// The branch exists now: --branch builds on its tip.
var b2 = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "feat 2\n", &.{ "-m", "feat two", "--branch", "feat" });
defer freeResult(gpa, &b2);
try expectExitDiag(b2.term, 0, b2);
const f2 = try stdinHash(b2.stdout);
var info_c = try infoJson(gpa, io, tmp.dir, "store", f2);
defer freeResult(gpa, &info_c);
try expectJsonField(info_c.stdout, "branch", "feat");
try expectJsonField(info_c.stdout, "parent", f1);
// No --branch: the parent's branch.
var b3 = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "feat 3\n", &.{ "-m", "feat three", "--parent", f2 });
defer freeResult(gpa, &b3);
try expectExitDiag(b3.term, 0, b3);
const f3 = try stdinHash(b3.stdout);
var info_d = try infoJson(gpa, io, tmp.dir, "store", f3);
defer freeResult(gpa, &info_d);
try expectJsonField(info_d.stdout, "branch", "feat");
try expectJsonField(info_d.stdout, "parent", f2);
// Trunk never moved.
var trunk_after = try infoJson(gpa, io, tmp.dir, "store", "trunk");
defer freeResult(gpa, &trunk_after);
try std.testing.expectEqualStrings(trunk_info.stdout, trunk_after.stdout);
try expectCatAt(gpa, io, tmp.dir, "store", "a.txt", "feat", "feat 3\n");
}
test "cli commit --stdin: eight concurrent writers all land, one after another, on one line of history" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
const writers = 8;
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(exe);
// All started before any finishes; each records its own exit status.
var script: std.ArrayList(u8) = .empty;
defer script.deinit(gpa);
for (0..writers) |i| {
var name_buf: [16]u8 = undefined;
var body_buf: [16]u8 = undefined;
try writeFile(io, tmp.dir, try std.fmt.bufPrint(&name_buf, "w{d}.in", .{i}), try std.fmt.bufPrint(&body_buf, "writer {d}\n", .{i}));
const line = try std.fmt.allocPrint(gpa, "'{0s}' commit -R store --stdin w{1d}.txt -m w{1d} --user u < w{1d}.in > w{1d}.out 2> w{1d}.err; echo $? > w{1d}.rc &\n", .{ exe, i });
defer gpa.free(line);
try script.appendSlice(gpa, line);
}
try script.appendSlice(gpa, "wait\n");
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
var sh = try std.process.run(gpa, io, .{
.argv = &.{ "/bin/sh", "-c", script.items },
.cwd = .{ .dir = tmp.dir },
.environ_map = &env,
});
defer freeResult(gpa, &sh);
try expectExitDiag(sh.term, 0, sh);
var hashes: [writers][64]u8 = undefined;
var parents: [writers][64]u8 = undefined;
for (0..writers) |i| {
var name_buf: [16]u8 = undefined;
const rc = try tmp.dir.readFileAlloc(io, try std.fmt.bufPrint(&name_buf, "w{d}.rc", .{i}), gpa, .limited(64));
defer gpa.free(rc);
const err = try tmp.dir.readFileAlloc(io, try std.fmt.bufPrint(&name_buf, "w{d}.err", .{i}), gpa, .limited(4096));
defer gpa.free(err);
if (!std.mem.eql(u8, rc, "0\n")) std.debug.print("writer {d} exited {s}: {s}\n", .{ i, rc, err });
try std.testing.expectEqualStrings("0\n", rc);
try std.testing.expectEqualStrings("", err);
const out = try tmp.dir.readFileAlloc(io, try std.fmt.bufPrint(&name_buf, "w{d}.out", .{i}), gpa, .limited(4096));
defer gpa.free(out);
@memcpy(&hashes[i], try stdinHash(out));
var info = try infoJson(gpa, io, tmp.dir, "store", &hashes[i]);
defer freeResult(gpa, &info);
const key = "\"parent\":\"";
const at = (std.mem.indexOf(u8, info.stdout, key) orelse return error.TestUnexpectedResult) + key.len;
@memcpy(&parents[i], info.stdout[at..][0..64]);
}
// Every hash is its own check-in, and no two share a parent: with one
// leaf, that is one line of history, each writer built on the last.
for (0..writers) |i| for (i + 1..writers) |j| {
try std.testing.expect(!std.mem.eql(u8, &hashes[i], &hashes[j]));
try std.testing.expect(!std.mem.eql(u8, &parents[i], &parents[j]));
};
try std.testing.expectEqual(@as(usize, 1), try leafCount(gpa, io, tmp.dir, "store"));
for (0..writers) |i| {
var name_buf: [16]u8 = undefined;
var want_buf: [16]u8 = undefined;
try expectCatAt(gpa, io, tmp.dir, "store", try std.fmt.bufPrint(&name_buf, "w{d}.txt", .{i}), "trunk", try std.fmt.bufPrint(&want_buf, "writer {d}\n", .{i}));
}
try expectNoWorkArea(io, tmp.dir, "store");
}
test "cli commit --stdin: a path that does not fit the parent's tree is refused and writes nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var seed = try commitStdin(gpa, io, tmp.dir, "store", "d/f", "a file\n", &.{ "-m", "d/f" });
defer freeResult(gpa, &seed);
try expectExitDiag(seed.term, 0, seed);
const before = try storeSize(io, tmp.dir, "store");
const cases = [_]struct { path: []const u8, says: []const u8 }{
// Under a file: the manifest would hold both d/f and d/f/x.
.{ .path = "d/f/x", .says = "'d/f' is a file in the parent" },
// A directory: d holds d/f.
.{ .path = "d", .says = "'d' is a directory in the parent" },
};
for (cases) |c| {
var res = try commitStdin(gpa, io, tmp.dir, "store", c.path, "z\n", &.{ "-m", "bad" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
if (std.mem.indexOf(u8, res.stderr, c.says) == null) {
std.debug.print("expected '{s}' in: {s}\n", .{ c.says, res.stderr });
return error.TestUnexpectedResult;
}
try std.testing.expectEqual(before, try storeSize(io, tmp.dir, "store"));
try expectNoWorkArea(io, tmp.dir, "store");
}
// The store is not wedged: the next commit lands.
var next = try commitStdin(gpa, io, tmp.dir, "store", "d/g", "fine\n", &.{ "-m", "fine" });
defer freeResult(gpa, &next);
try expectExitDiag(next.term, 0, next);
try expectCatAt(gpa, io, tmp.dir, "store", "d/g", try stdinHash(next.stdout), "fine\n");
}
fn storeBytes(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8) ![]u8 {
var buf: [256]u8 = undefined;
const name = try std.fmt.bufPrint(&buf, "{s}/.zit", .{store});
return dir.readFileAlloc(io, name, gpa, .limited(1 << 24));
}
test "cli commit --stdin: a user, branch or path a manifest cannot carry is refused and writes nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
const before = try storeBytes(gpa, io, tmp.dir, "store");
defer gpa.free(before);
// `commitStdin` passes `--user u` first; a later `--user` wins.
const cases = [_]struct { path: []const u8 = "ok.txt", extra: []const []const u8, says: []const u8 }{
// A newline in the U card would end it and start whatever follows.
.{ .extra = &.{ "-m", "m", "--user", "e\nZ fake" }, .says = "--user" },
.{ .extra = &.{ "-m", "m", "--user", "e\x01" }, .says = "--user" },
.{ .extra = &.{ "-m", "m", "--branch", "x\ny" }, .says = "--branch" },
.{ .path = "a\nb", .extra = &.{ "-m", "m" }, .says = "path" },
.{ .extra = &.{ "-m", "m", "--rename", "f.txt", "r\nn" }, .says = "path" },
// A delete names what the parent holds, whatever its bytes, so an
// absent one is refused as absent -- with the name escaped.
.{ .extra = &.{ "-m", "m", "--delete", "g\x01.txt" }, .says = "'g\\x01.txt' is not in the parent" },
// Fossil's strict rule for a new name: nothing above U+FFFF, no
// private use, no overlong form; and its reserved names at any depth.
.{ .path = "smile\xf0\x9f\x98\x80", .extra = &.{ "-m", "m" }, .says = "path" },
.{ .path = "priv\xee\x80\x80", .extra = &.{ "-m", "m" }, .says = "path" },
.{ .path = "over\xc0\xaf", .extra = &.{ "-m", "m" }, .says = "path" },
.{ .path = "sub/_FOSSIL_", .extra = &.{ "-m", "m" }, .says = "path" },
.{ .extra = &.{ "-m", "m", "--rename", "f.txt", "d/.fslckout" }, .says = "path" },
};
for (cases) |c| {
var res = try commitStdin(gpa, io, tmp.dir, "store", c.path, "z\n", c.extra);
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
if (std.mem.indexOf(u8, res.stderr, c.says) == null) {
std.debug.print("expected '{s}' in: {s}\n", .{ c.says, res.stderr });
return error.TestUnexpectedResult;
}
// The refusal never echoes the raw byte back to the terminal.
for (res.stderr) |b| try std.testing.expect(b == '\n' or (b >= ' ' and b != 0x7f));
const after = try storeBytes(gpa, io, tmp.dir, "store");
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
try expectNoWorkArea(io, tmp.dir, "store");
}
// Spaces are not control bytes: the card escapes carry them as `\s`.
var spaced = try commitStdin(gpa, io, tmp.dir, "store", "ok.txt", "z\n", &.{ "-m", "m", "--user", "John Doe", "--branch", "my branch" });
defer freeResult(gpa, &spaced);
try expectExitDiag(spaced.term, 0, spaced);
try expectSpacedNames(gpa, io, tmp.dir, "store", try stdinHash(spaced.stdout), true);
}
/// The check-in `h` carries user "John Doe" on branch "my branch", escaped in
/// its cards and read back whole.
fn expectSpacedNames(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8, h: []const u8, starts_branch: bool) !void {
var raw = try runZit(gpa, io, dir, &.{ "artifact", "-R", store, h, "--raw" });
defer freeResult(gpa, &raw);
try expectExitDiag(raw.term, 0, raw);
try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "\nU John\\sDoe\n") != null);
// Only the check-in that starts the branch carries its tag; later ones inherit it.
try std.testing.expectEqual(starts_branch, std.mem.indexOf(u8, raw.stdout, "\nT *branch * my\\sbranch\n") != null);
var log = try runZit(gpa, io, dir, &.{ "log", "-R", store, "--long", "-n", "1", h });
defer freeResult(gpa, &log);
try expectExitDiag(log.term, 0, log);
try std.testing.expect(std.mem.indexOf(u8, log.stdout, " John Doe ") != null);
var info = try infoJson(gpa, io, dir, store, h);
defer freeResult(gpa, &info);
try expectJsonField(info.stdout, "branch", "my branch");
}
test "cli commit -R: a name already in the parent may be deleted or renamed away whatever its bytes" {
const gpa = std.testing.allocator;
const io = std.testing.io;
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedFossilMadeName(gpa, io, tmp.dir);
var del = try runZit(gpa, io, tmp.dir, &.{ "commit", "-R", ".", "--delete", "c\x01d", "-m", "drop", "--user", "u" });
defer freeResult(gpa, &del);
try expectExitDiag(del.term, 0, del);
const h = try stdinHash(del.stdout);
var cat = try runZit(gpa, io, tmp.dir, &.{ "cat", "c\x01d", h, "-R", "." });
defer freeResult(gpa, &cat);
try expectExit(cat.term, 1);
try expectCatAt(gpa, io, tmp.dir, ".", "ok.txt", h, "ok\n");
}
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedFossilMadeName(gpa, io, tmp.dir);
var ren = try runZit(gpa, io, tmp.dir, &.{ "commit", "-R", ".", "--rename", "c\x01d", "cd.txt", "-m", "fix", "--user", "u" });
defer freeResult(gpa, &ren);
try expectExitDiag(ren.term, 0, ren);
try expectCatAt(gpa, io, tmp.dir, ".", "cd.txt", try stdinHash(ren.stdout), "odd\n");
// The new side stays strict.
var back = try runZit(gpa, io, tmp.dir, &.{ "commit", "-R", ".", "--rename", "cd.txt", "c\x01d", "-m", "undo", "--user", "u" });
defer freeResult(gpa, &back);
try expectExit(back.term, 1);
}
}
test "checkin core: a NUL in the user is refused as BadName and writes nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
const before = try storeBytes(gpa, io, tmp.dir, "store");
defer gpa.free(before);
{
var s = try tmp.dir.openDir(io, "store", .{});
defer s.close(io);
var diag: zit_checkin.Diag = .{};
var ctx: zit_checkin.Context = undefined;
try ctx.open(gpa, io, s, .{}, &diag);
defer ctx.deinit();
// argv cannot carry a NUL, so the core is driven directly.
try std.testing.expectError(error.BadName, zit_checkin.commit(&ctx, .{
.user = "e\x00x",
.comment = "m",
.now = .{ .epoch = 1_700_000_000 },
.edits = &.{.{ .put = .{ .path = "ok.txt", .bytes = "z\n" } }},
}, &diag));
try std.testing.expect(std.mem.indexOf(u8, diag.text(), "user") != null);
try std.testing.expectError(error.BadName, zit_checkin.commit(&ctx, .{
.user = "u",
.comment = "m",
.now = .{ .epoch = 1_700_000_000 },
.edits = &.{.{ .put = .{ .path = "a\x00b", .bytes = "z\n" } }},
}, &diag));
try std.testing.expect(std.mem.indexOf(u8, diag.text(), "path") != null);
}
const after = try storeBytes(gpa, io, tmp.dir, "store");
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
test "cli commit --stdin: a comment with a newline is escaped in the C card and reads back unchanged" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
var res = try commitStdin(gpa, io, tmp.dir, "store", "n.txt", "n\n", &.{ "-m", "two\nlines" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const h = try stdinHash(res.stdout);
var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", "-R", "store", h, "--raw" });
defer freeResult(gpa, &raw);
try expectExitDiag(raw.term, 0, raw);
try std.testing.expect(std.mem.startsWith(u8, raw.stdout, "C two\\nlines\n"));
var log = try runZit(gpa, io, tmp.dir, &.{ "log", "-R", "store", "--long", "-n", "1", h });
defer freeResult(gpa, &log);
try expectExitDiag(log.term, 0, log);
try std.testing.expect(std.mem.endsWith(u8, log.stdout, " u two\nlines\n"));
}
test "cli commit --stdin: before-commit runs over an area holding only PATH, and --no-checks skips it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedStdinStore(gpa, io, tmp.dir, "store");
// Passes only where PATH is present and the carried files are not.
{
var set = try runZit(gpa, io, tmp.dir, &.{ "settings", "-R", "store", "before-commit", "test -f a.txt && test ! -f f.txt" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
}
var ran = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "a\n", &.{ "-m", "checked" });
defer freeResult(gpa, &ran);
try expectExitDiag(ran.term, 0, ran);
_ = try stdinHash(ran.stdout);
{
var set = try runZit(gpa, io, tmp.dir, &.{ "settings", "-R", "store", "before-commit", "exit 7" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
}
const before = try storeSize(io, tmp.dir, "store");
var refused = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "b\n", &.{ "-m", "refused" });
defer freeResult(gpa, &refused);
try expectExit(refused.term, 1);
try std.testing.expectEqualStrings("", refused.stdout);
try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "before-commit failed") != null);
try std.testing.expectEqual(before, try storeSize(io, tmp.dir, "store"));
var skipped = try commitStdin(gpa, io, tmp.dir, "store", "a.txt", "b\n", &.{ "-m", "skipped", "--no-checks" });
defer freeResult(gpa, &skipped);
try expectExitDiag(skipped.term, 0, skipped);
try std.testing.expect(std.mem.indexOf(u8, skipped.stderr, "SKIPPING before-commit: exit 7") != null);
try expectCatAt(gpa, io, tmp.dir, "store", "a.txt", try stdinHash(skipped.stdout), "b\n");
try expectNoWorkArea(io, tmp.dir, "store");
}
test "cli commit --stdin: a sub-manifest store commits a tree check-in" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var init = try runZit(gpa, io, tmp.dir, &.{ "init", "--submanifest", "3", "store" });
defer freeResult(gpa, &init);
try expectExitDiag(init.term, 0, init);
{
var repo = try tmp.dir.openDir(io, "store", .{});
defer repo.close(io);
for (0..6) |d| {
const dir = try std.fmt.allocPrint(gpa, "d{d}", .{d});
defer gpa.free(dir);
try repo.createDir(io, dir, .default_dir);
for (0..6) |f| {
const name = try std.fmt.allocPrint(gpa, "d{d}/f{d}", .{ d, f });
defer gpa.free(name);
try writeFile(io, repo, name, name);
}
}
var add = try runZit(gpa, io, repo, &.{"addremove"});
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
var c = try runCommitEnv(gpa, io, repo, .{ .user = "u" }, &.{ "commit", "-q", "-m", "first" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
var res = try commitStdin(gpa, io, tmp.dir, "store", "d3/f4", "rewritten\n", &.{ "-m", "one file" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const h = try stdinHash(res.stdout);
var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", h, "--raw", "-R", "store" });
defer freeResult(gpa, &raw);
try expectExitDiag(raw.term, 0, raw);
try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "\nS ") != null);
try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "\nF ") == null);
try expectCatAt(gpa, io, tmp.dir, "store", "d3/f4", h, "rewritten\n");
try expectCatAt(gpa, io, tmp.dir, "store", "d5/f5", h, "d5/f5");
}
test "cli commit --stdin: needs -R, and its companions need it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "x.in", "x\n");
const cases = [_]struct { args: []const []const u8, says: []const u8 }{
.{ .args = &.{ "commit", "--stdin", "a.txt", "-m", "x", "--user", "u" }, .says = "--stdin needs -R" },
.{ .args = &.{ "commit", "--parent", "trunk", "-m", "x", "--user", "u" }, .says = "--parent needs --stdin" },
.{ .args = &.{ "commit", "-R", ".", "--stdin", "a.txt", "--user", "u" }, .says = "--stdin needs -m" },
.{ .args = &.{ "commit", "-R", ".", "--stdin", "a.txt", "-m", "x", "--user", "u", "b.txt" }, .says = "unexpected argument 'b.txt'" },
.{ .args = &.{ "commit", "-R", ".", "--stdin", "a.txt", "-m", "x", "--user", "u", "--dry-run" }, .says = "does not apply with --stdin" },
.{ .args = &.{ "commit", "-R", ".", "--stdin", "../a.txt", "-m", "x", "--user", "u" }, .says = "not a path a check-in can hold" },
};
const before = (try tmp.dir.statFile(io, ".zit", .{})).size;
for (cases) |c| {
var res = try runZitStdin(gpa, io, tmp.dir, "x.in", c.args);
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
if (std.mem.indexOf(u8, res.stderr, c.says) == null) {
std.debug.print("expected '{s}' in: {s}\n", .{ c.says, res.stderr });
return error.TestUnexpectedResult;
}
}
try std.testing.expectEqual(before, (try tmp.dir.statFile(io, ".zit", .{})).size);
try expectNoWorkArea(io, tmp.dir, ".");
}
test "cli commit submanifests §§5.1/5.4: a partial commit preserves unnamed tree-parent files" {
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 seedManyFiles(gpa, io, tmp.dir, 40);
var enable = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "3" });
defer freeResult(gpa, &enable);
try expectExitDiag(enable.term, 0, enable);
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &root);
try expectExitDiag(root.term, 0, root);
const root_hash = try checkinHash(root.stdout);
var root_manifest = try parseCheckin(gpa, io, tmp.dir, root_hash);
defer root_manifest.deinit();
try std.testing.expect(root_manifest.m.s.len != 0);
try writeFile(io, tmp.dir, "f000.txt", "changed\n");
var child = try runCommitEnv(
gpa,
io,
tmp.dir,
.{ .user = "u" },
&.{ "commit", "-m", "partial", "f000.txt" },
);
defer freeResult(gpa, &child);
try expectExitDiag(child.term, 0, child);
var changed = try runZit(gpa, io, tmp.dir, &.{ "cat", "f000.txt" });
defer freeResult(gpa, &changed);
try expectExit(changed.term, 0);
try std.testing.expectEqualStrings("changed\n", changed.stdout);
var unchanged = try runZit(gpa, io, tmp.dir, &.{ "cat", "f001.txt" });
defer freeResult(gpa, &unchanged);
try expectExit(unchanged.term, 0);
try std.testing.expectEqualStrings("x\n", unchanged.stdout);
}
test "cli commit submanifests §5.1 artifact §4.7.6: partial commit consumes carried rename" {
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 seedManyFiles(gpa, io, tmp.dir, 40);
var enable = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "3" });
defer freeResult(gpa, &enable);
try expectExitDiag(enable.term, 0, enable);
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &root);
try expectExitDiag(root.term, 0, root);
var move = try runZit(gpa, io, tmp.dir, &.{ "mv", "f000.txt", "renamed.txt" });
defer freeResult(gpa, &move);
try expectExitDiag(move.term, 0, move);
var rename = try runCommitEnv(
gpa,
io,
tmp.dir,
.{ .user = "u" },
&.{ "commit", "-m", "rename", "renamed.txt" },
);
defer freeResult(gpa, &rename);
try expectExitDiag(rename.term, 0, rename);
try writeFile(io, tmp.dir, "f001.txt", "changed\n");
var partial = try runCommitEnv(
gpa,
io,
tmp.dir,
.{ .user = "u" },
&.{ "commit", "-m", "other", "f001.txt" },
);
defer freeResult(gpa, &partial);
try expectExitDiag(partial.term, 0, partial);
var renamed = try runZit(gpa, io, tmp.dir, &.{ "cat", "renamed.txt" });
defer freeResult(gpa, &renamed);
try expectExit(renamed.term, 0);
try std.testing.expectEqualStrings("x\n", renamed.stdout);
var old = try runZit(gpa, io, tmp.dir, &.{ "cat", "f000.txt" });
defer freeResult(gpa, &old);
try expectExit(old.term, 1);
}
// ===========================================================================
// `zit commit` submanifests §6.4 — the post-commit drift advisory. "An
// implementation MAY advise of drift from the analyzed optimum at any time"
// — unconditional, independent of the `submanifest-auto-retune` gate §6.4
// puts on the re-tune half. These run only through the real binary's
// non-interactive path (test stdin is never a tty): the confirm-prompt's
// accepted-'y' branch is deliberately NOT covered here, matching
// `cmd/sync_dialogue.zig`'s own
// `readPasswordNoEcho` precedent, which states the same limit about itself.
// ===========================================================================
test "cli commit submanifests §6.4: a repository that never enabled sub-manifests gets no advisory" {
// Requirement 1: zero added behavior on the ordinary case — a repository
// that never turned submanifests on prints nothing new, ever.
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 seedManyFiles(gpa, io, tmp.dir, 40);
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "flat" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expect(std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") == null);
}
test "cli commit submanifests §6.4: no advisory when the pinned k is already the analyzed optimum" {
// k = 3 is the Appendix A.5 conformance vector's value for this exact
// 40-file synthetic set (the same fixture `seedManyFiles` builds elsewhere
// in this file) — i.e. it already IS the analyzed optimum, so `driftCheck`
// must find nothing to recommend.
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 seedManyFiles(gpa, io, tmp.dir, 40);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "3" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expect(std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") == null);
}
test "cli commit submanifests §6.4: genuine margin-clearing drift prints the exact advisory line" {
// k = 1 pins §6.3's coarsest supported partition against 200 names — a
// fixed, deliberate mismatch (the same deterministic "x\n"-content fixture
// every seed helper in this file uses, never a measured one) big enough
// that the analyzed optimum clears the §6.4 margin gate by a wide margin.
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 seedManyFiles(gpa, io, tmp.dir, 200);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &ci);
// Requirement 6: an advisory (present or not) never changes commit's own
// exit code — every commit in this test block, drifted or not, exits 0.
try expectExitDiag(ci.term, 0, ci);
try std.testing.expectEqualStrings(
"submanifest-k has drifted from 1 to 3 (65.3% read-cost improvement)\n",
ci.stdout[std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") orelse
return error.TestUnexpectedResult ..],
);
}
test "cli commit submanifests §6.4: --no-submanifest-advice suppresses the advisory even with real drift" {
// Requirement 2: the flag skips step 3 onward entirely — same genuine-drift
// fixture as the test above, but no line, no prompt, and the check-in's own
// success line is untouched.
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 seedManyFiles(gpa, io, tmp.dir, 200);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var ci = try runCommitEnv(
gpa,
io,
tmp.dir,
.{ .user = "u" },
&.{ "commit", "-m", "tree", "--no-submanifest-advice" },
);
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expect(std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") == null);
_ = try checkinHash(ci.stdout); // the ordinary success line still prints
}
// ===========================================================================
// `zit commit` submanifests §6.4 — `submanifest-auto-retune` dispatch. §6.4's
// opt-in shape: default false keeps the "manual and operator-initiated"
// re-tune in force byte-for-byte (the test directly below is the regression
// proof of that); true is the durable project-operator act §6.4 requires
// before ANY automatic re-tune may fire, after which drift no longer needs a
// fresh 'y' at every commit that crosses the margin gate.
// ===========================================================================
test "cli commit submanifests §6.4: auto-retune false (default) is byte-identical to the advisory-only path" {
// Regression proof, not just a new-behavior test: explicitly forcing the
// setting to its own default and re-running the exact genuine-drift
// fixture from the advisory-only tests above must print the identical
// advisory line, take no automatic action, and leave the pin untouched —
// the auto-dispatch addition changes nothing on this path.
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 seedManyFiles(gpa, io, tmp.dir, 200);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try forceAutoRetune(gpa, io, tmp.dir, false);
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expectEqualStrings(
"submanifest-k has drifted from 1 to 3 (65.3% read-cost improvement)\n",
ci.stdout[std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") orelse
return error.TestUnexpectedResult ..],
);
// No prompt is a tty-only concern (test stdin is never a tty, so the
// existing advisory-only tests already prove the non-interactive prompt
// path prints nothing further); what THIS test proves is the pin itself:
// the non-interactive, non-auto-retune path never writes it.
const settings = try readSubmanifestSettings(gpa, io, tmp.dir);
try std.testing.expectEqual(@as(u8, 1), settings.k);
}
test "cli commit submanifests §6.4: auto-retune true dispatches the retune with no prompt" {
// §6.4's core case: with the durable opt-in set, genuine drift is
// applied directly — no prompt (works non-interactively, unlike the
// manual-confirm path), the pin actually moves (proved by a config
// readback, not just stdout), and the same "next commit rewrites every
// leaf, once" note the manual path prints appears here too.
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 seedManyFiles(gpa, io, tmp.dir, 200);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try forceAutoRetune(gpa, io, tmp.dir, true);
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expectEqualStrings(
"submanifest-k has drifted from 1 to 3 (65.3% read-cost improvement)\n" ++
"submanifest-k: 1 -> 3\n" ++
"note: the next commit shares only the sub-manifests whose bytes are unchanged under\n" ++
"the new k: all, some or none. No history is rewritten and no reader behaves differently.\n",
ci.stdout[std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") orelse
return error.TestUnexpectedResult ..],
);
// The durable proof: the pin actually moved in storage, not just on stdout.
const settings = try readSubmanifestSettings(gpa, io, tmp.dir);
try std.testing.expectEqual(@as(u8, 3), settings.k);
}
test "cli commit submanifests §6.4: auto-retune true with no drift is a no-op" {
// The opt-in changes dispatch, not the drift gate: with the pin already at
// the analyzed optimum, `driftCheck` recommends nothing, so there is
// nothing for auto-retune to dispatch — same silence as the advisory-only
// no-drift test above, and the pin stays exactly where it was.
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 seedManyFiles(gpa, io, tmp.dir, 40);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "submanifest", "enable", "--k", "3" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try forceAutoRetune(gpa, io, tmp.dir, true);
var ci = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "tree" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
try std.testing.expect(std.mem.indexOf(u8, ci.stdout, "submanifest-k has drifted") == null);
const settings = try readSubmanifestSettings(gpa, io, tmp.dir);
try std.testing.expectEqual(@as(u8, 3), settings.k);
}
test "cli commit on a sparse store: succeeds, omits R, and converges back to the dense server" {
// Sparse-cone RFC sections 4.3 and 5.3: an in-cone commit on a sparse
// STORE cannot compute the whole-tree R aggregate (elided content), MUST
// omit R rather than fail or fabricate, and the resulting ordinary
// check-in pushes back to a dense peer, where a dense clone materializes
// the full tree including the sparse-committed change.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try tmp_a.dir.createDir(io, "tools", .default_dir);
try writeFile(io, tmp_a.dir, "src/a.zig", "one\n");
try writeFile(io, tmp_a.dir, "tools/t.bin", "payload\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/t.bin" });
var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, port);
defer server.kill(io);
try waitForListen(io, port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", "--cone=src", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
var op = try runZit(gpa, io, b_dir, &.{ "open", "--cone=src" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
// The sparse commit: edit in-cone, commit. Must succeed with R omitted.
try writeFile(io, b_dir, "src/a.zig", "two\n");
const sparse_tip = try commitHashDup(gpa, io, b_dir, "sparse edit");
defer gpa.free(sparse_tip);
// The minted manifest carries no R card but still carries the out-of-cone
// F card, hash intact.
var info = try runZit(gpa, io, b_dir, &.{ "artifact", sparse_tip });
defer freeResult(gpa, &info);
try expectExitDiag(info.term, 0, info);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "\nR ") == null);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, "tools/t.bin") != null);
// Push back; a dense clone from the server sees the sparse edit AND the
// untouched out-of-cone file.
var push = try runZit(gpa, io, b_dir, &.{ "push", url });
defer freeResult(gpa, &push);
try expectExitDiag(push.term, 0, push);
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
var dense_clone = try runZit(gpa, io, tmp_c.dir, &.{ "clone", "--no-open", url, "repo" });
defer freeResult(gpa, &dense_clone);
try expectExitDiag(dense_clone.term, 0, dense_clone);
var c_dir = try tmp_c.dir.openDir(io, "repo", .{});
defer c_dir.close(io);
var oc = try runZit(gpa, io, c_dir, &.{"open"});
defer freeResult(gpa, &oc);
try expectExitDiag(oc.term, 0, oc);
const edited = try readFile(gpa, io, c_dir, "src/a.zig");
defer gpa.free(edited);
try std.testing.expectEqualStrings("two\n", edited);
const carried = try readFile(gpa, io, c_dir, "tools/t.bin");
defer gpa.free(carried);
try std.testing.expectEqualStrings("payload\n", carried);
}
test "cli commit: -M reads a file, --editor composes over a template, an empty comment aborts and keeps the file, -n writes nothing, --allow-empty records" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "one\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
// Off a terminal, no -m is still the usage error (unchanged contract).
var bare = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{"commit"});
defer freeResult(gpa, &bare);
try expectExit(bare.term, 2);
// Dry run: the change list, nothing recorded.
var dry = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-n" });
defer freeResult(gpa, &dry);
try expectExitDiag(dry.term, 0, dry);
try std.testing.expect(std.mem.indexOf(u8, dry.stderr, "ADDED a.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, dry.stderr, "nothing was written") != null);
var tl0 = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "0" });
defer freeResult(gpa, &tl0);
try std.testing.expectEqualStrings("", tl0.stdout);
// --editor: the template names the change set; the editor's text is the
// comment. The editor runs with the checkout root as cwd, so relative
// paths reach it.
try writeFile(io, tmp.dir, "ed.sh", "#!/bin/sh\ncp \"$1\" seen.txt\nprintf 'from editor\\n' >> \"$1\"\n");
try chmodFile(io, tmp.dir, "ed.sh", 0o755);
var ed = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--editor", "./ed.sh" });
defer freeResult(gpa, &ed);
try expectExitDiag(ed.term, 0, ed);
const seen = try readFile(gpa, io, tmp.dir, "seen.txt");
defer gpa.free(seen);
try std.testing.expect(std.mem.indexOf(u8, seen, "# branch: trunk") != null);
try std.testing.expect(std.mem.indexOf(u8, seen, "# ADDED a.txt") != null);
try std.testing.expect(!fileExists(io, tmp.dir, ".zit-ci-comment")); // retired on success
var tl1 = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "1" });
defer freeResult(gpa, &tl1);
try std.testing.expect(std.mem.indexOf(u8, tl1.stdout, "from editor") != null);
try std.testing.expect(std.mem.indexOf(u8, tl1.stdout, "# branch") == null);
// An editor that leaves the comment empty aborts; the template stays for -M.
try writeFile(io, tmp.dir, "a.txt", "two\n");
try writeFile(io, tmp.dir, "noop.sh", "#!/bin/sh\n:\n");
try chmodFile(io, tmp.dir, "noop.sh", 0o755);
var empty = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "--editor", "./noop.sh" });
defer freeResult(gpa, &empty);
try expectExit(empty.term, 1);
try std.testing.expect(std.mem.indexOf(u8, empty.stderr, "empty check-in message") != null);
try std.testing.expect(fileExists(io, tmp.dir, ".zit-ci-comment"));
// -M reads the comment from a file.
try writeFile(io, tmp.dir, "msg.txt", "via file\n");
var mf = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-M", "msg.txt" });
defer freeResult(gpa, &mf);
try expectExitDiag(mf.term, 0, mf);
var tl2 = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "1" });
defer freeResult(gpa, &tl2);
try std.testing.expect(std.mem.indexOf(u8, tl2.stdout, "via file") != null);
// --allow-empty records a check-in with no file changes; without it, refused.
var refused = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "nothing" });
defer freeResult(gpa, &refused);
try expectExit(refused.term, 1);
var allowed = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "nothing", "--allow-empty" });
defer freeResult(gpa, &allowed);
try expectExitDiag(allowed.term, 0, allowed);
}
test "cli commit: content policy is opt-in, grandfathers what it inherits, and refuses what a commit introduces" {
// Goal: the half of content policy that a before-commit hook cannot do —
// a rule that binds a fresh clone with nothing trusted, because it
// executes nothing. Three properties, in the order they bite an adopter:
// undeclared means unchecked; declaring does not retroactively break a
// tree that already violates; what the commit INTRODUCES is refused.
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 repository that never declared anything commits binary and CR/LF
// exactly as before — which is why adding this feature broke nothing.
try writeFile(io, tmp.dir, "seed.txt", "plain\n");
try writeFile(io, tmp.dir, "legacy.bin", "a\x00b\n");
try addManaged(gpa, io, tmp.dir, &.{ "seed.txt", "legacy.bin" });
var seed = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &seed);
try expectExitDiag(seed.term, 0, seed);
// Declaring the policy as a TRACKED file — no `zit settings`, no trust —
// succeeds even though legacy.bin already violates it. Adoption in a real
// repository has to be possible without first fixing the whole tree.
try tmp.dir.createDir(io, ".zit-settings", .default_dir);
try writeFile(io, tmp.dir, ".zit-settings/binary-glob", "*.ok\n");
try addManaged(gpa, io, tmp.dir, &.{".zit-settings/binary-glob"});
var declare = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "declare" });
defer freeResult(gpa, &declare);
try expectExitDiag(declare.term, 0, declare);
// A NEW violating file is refused, naming the file, the glob that would
// permit it, and the one-off waiver.
try writeFile(io, tmp.dir, "fresh.bin", "a\x00b\n");
try addManaged(gpa, io, tmp.dir, &.{"fresh.bin"});
var refused = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "fresh" });
defer freeResult(gpa, &refused);
try expectNonzeroExit(refused.term);
for ([_][]const u8{ "fresh.bin", "binary-glob", "--no-checks" }) |needle| {
try std.testing.expect(std.mem.indexOf(u8, refused.stderr, needle) != null);
}
// Nothing was minted, and the refusal is sticky while the file stays
// tracked — a policy that forgot its objection after one try is no policy.
var again = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "fresh" });
defer freeResult(gpa, &again);
try expectNonzeroExit(again.term);
var tl = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "9" });
defer freeResult(gpa, &tl);
try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "declare") != null);
try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "fresh") == null);
// Clear the pending violation first: the refusal names the FIRST offender
// and stops, so leaving fresh.bin tracked would mask what the next arm is
// actually testing.
var untrack = try runZit(gpa, io, tmp.dir, &.{ "rm", "fresh.bin" });
defer freeResult(gpa, &untrack);
try expectExit(untrack.term, 0);
// Grandfathering is per-content, not per-path: editing the inherited file
// re-checks it, so nothing stays permanently exempt.
try writeFile(io, tmp.dir, "legacy.bin", "a\x00b\x00c\n");
var edited = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "edit" });
defer freeResult(gpa, &edited);
try expectNonzeroExit(edited.term);
try std.testing.expect(std.mem.indexOf(u8, edited.stderr, "legacy.bin") != null);
// --no-checks waives the whole check for one commit.
var waived = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "waived", "--no-checks" });
defer freeResult(gpa, &waived);
try expectExitDiag(waived.term, 0, waived);
}
test "cli commit: a commit extends the file index over the new check-ins, never rebuilds it" {
// As first shipped, the index validated against an exact log tail, so ANY
// commit, pull or push threw the whole thing away and the next file-history
// query rebuilt it from the first check-in — 164s on a mirror of SQLite. On
// a repository that receives commits the fast path would almost never be
// there, which is the case that matters for a forge.
//
// The invariant: the walk that builds the index is bounded by the check-ins
// it has NOT already seen. A query that follows no change walks nothing; a
// query after one commit walks one.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "a-seed\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var tip = try commitHashDup(gpa, io, tmp.dir, "seed");
defer gpa.free(tip);
const history = 15;
var i: usize = 0;
while (i < history) : (i += 1) {
var body: [16]u8 = undefined;
try writeFile(io, tmp.dir, "a.txt", std.fmt.bufPrint(&body, "a{d}\n", .{i}) catch unreachable);
gpa.free(tip);
tip = try commitHashDup(gpa, io, tmp.dir, "edit");
}
// The derive builds the index as it captures — fossil's mlink, at ingest —
// so the FIRST file-history query already finds it and walks nothing.
// Before, the first query paid a full pass over the log to build it, and
// on a forge that restarted more often than that pass took the index
// never existed at all.
const seam = [_][2][]const u8{.{ "ZIT_FILEINDEX_WALK", "1" }};
var built = try runZitEnv(gpa, io, tmp.dir, &seam, &.{ "log", tip, "a.txt" });
defer freeResult(gpa, &built);
try expectExit(built.term, 0);
const first = fileIndexWalkOf(&built) orelse return error.NoCounter;
if (first != 0) {
std.debug.print("\nthe first file-history query walked {d} check-ins; the derive should have built the index already\n", .{first});
return error.IndexNotBuiltByDerive;
}
try std.testing.expectEqual(@as(u64, 0), fileIndexUnknownOf(&built) orelse return error.NoCounter);
try std.testing.expectEqual(@as(usize, history + 1), std.mem.count(u8, built.stdout, "checkin "));
// One commit: the next open extends the derive over the new check-in and
// the index with it, so the query still walks nothing and still answers.
try writeFile(io, tmp.dir, "a.txt", "a-new\n");
gpa.free(tip);
tip = try commitHashDup(gpa, io, tmp.dir, "one more");
var extended = try runZitEnv(gpa, io, tmp.dir, &seam, &.{ "log", tip, "a.txt" });
defer freeResult(gpa, &extended);
try expectExit(extended.term, 0);
try std.testing.expectEqual(@as(u64, 0), fileIndexWalkOf(&extended) orelse return error.NoCounter);
try std.testing.expectEqual(@as(usize, history + 2), std.mem.count(u8, extended.stdout, "checkin "));
// The lazy build is now the fallback for a store whose cache predates the
// derive-built index. Remove the index and it is rebuilt on demand — in
// checkpointed chunks, so an interrupted build resumes rather than
// restarts — with the same answer, and the next query walks nothing.
try tmp.dir.deleteFile(io, ".zit-cache/finfo");
var rebuilt = try runZitEnv(gpa, io, tmp.dir, &seam, &.{ "log", tip, "a.txt" });
defer freeResult(gpa, &rebuilt);
try expectExit(rebuilt.term, 0);
try std.testing.expectEqual(@as(u64, history + 2), fileIndexWalkOf(&rebuilt) orelse return error.NoCounter);
try std.testing.expectEqualStrings(extended.stdout, rebuilt.stdout);
var warm = try runZitEnv(gpa, io, tmp.dir, &seam, &.{ "log", tip, "a.txt" });
defer freeResult(gpa, &warm);
try expectExit(warm.term, 0);
try std.testing.expectEqual(@as(u64, 0), fileIndexWalkOf(&warm) orelse return error.NoCounter);
}
/// The delta-chain depth of check-in `hash`'s manifest frame in `dir`'s store.
fn manifestDepth(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) !u8 {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
var digest: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&digest, hash);
return log.depthOf(log.lookup(&digest) orelse return error.TestUnexpectedResult);
}
test "cli commit skip-deltas: one-file commits on a 5,000-file flat tree store no manifest whole, and pack keeps it so" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
const files = 5000;
var name_buf: [32]u8 = undefined;
for (0..files) |i| {
const name = try std.fmt.bufPrint(&name_buf, "f{d:0>4}.txt", .{i});
try writeFile(io, tmp.dir, name, name);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
// heights[n] is the check-in at height n along trunk; the root is 0.
const commits = 36;
var heights: [commits + 1][]const u8 = undefined;
var made: usize = 0;
defer for (heights[0..made]) |h| gpa.free(h);
heights[0] = try commitHashDup(gpa, io, tmp.dir, "root");
made = 1;
const root_size = (try tmp.dir.statFile(io, ".zit", .{})).size;
var content_buf: [32]u8 = undefined;
for (1..commits + 1) |n| {
const name = try std.fmt.bufPrint(&name_buf, "f{d:0>4}.txt", .{(n * 131) % files});
try writeFile(io, tmp.dir, name, try std.fmt.bufPrint(&content_buf, "edit {d}", .{n}));
heights[n] = try commitHashDup(gpa, io, tmp.dir, "one file");
made += 1;
}
// Under parent chains capped at 16, heights 17 and 34 each stored a whole
// manifest of about 400 KB; skip bases store none past the root.
const grown = (try tmp.dir.statFile(io, ".zit", .{})).size - root_size;
try std.testing.expect(grown < 100 * 1024);
for (heights, 0..) |h, n| try std.testing.expectEqual(@as(u8, @popCount(n)), try manifestDepth(gpa, io, tmp.dir, h));
try expectVerifies(gpa, io, tmp.dir);
// A branch off the tip (height 37 along its primary line), trunk work on
// the same parent, and a merge of the branch (height 38).
try writeFile(io, tmp.dir, "f0001.txt", "branch edit");
const feat = blk: {
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "feat work", "--branch", "feat" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
break :blk try gpa.dupe(u8, try checkinHash(res.stdout));
};
defer gpa.free(feat);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "update", "trunk" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
try writeFile(io, tmp.dir, "f0002.txt", "trunk edit");
const trunk = try commitHashDup(gpa, io, tmp.dir, "trunk work");
defer gpa.free(trunk);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "merge", "feat" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
const merged = try commitHashDup(gpa, io, tmp.dir, "merge feat");
defer gpa.free(merged);
const lines = [_]struct { hash: []const u8, height: u64 }{
.{ .hash = feat, .height = commits + 1 },
.{ .hash = trunk, .height = commits + 1 },
.{ .hash = merged, .height = commits + 2 },
};
for (lines) |l| {
try std.testing.expectEqual(@as(u8, @popCount(l.height)), try manifestDepth(gpa, io, tmp.dir, l.hash));
try expectArtifactReadsBack(gpa, io, tmp.dir, l.hash);
}
try expectVerifies(gpa, io, tmp.dir);
// Pack keeps the structure: no manifest comes back whole, the depths
// still follow popcount, and the store does not grow.
const before_pack = (try tmp.dir.statFile(io, ".zit", .{})).size;
{
var res = try runZit(gpa, io, tmp.dir, &.{"pack"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
try std.testing.expect((try tmp.dir.statFile(io, ".zit", .{})).size <= before_pack);
try expectVerifies(gpa, io, tmp.dir);
for (heights, 0..) |h, n| try std.testing.expectEqual(@as(u8, @popCount(n)), try manifestDepth(gpa, io, tmp.dir, h));
for (lines) |l| {
try std.testing.expectEqual(@as(u8, @popCount(l.height)), try manifestDepth(gpa, io, tmp.dir, l.hash));
try expectArtifactReadsBack(gpa, io, tmp.dir, l.hash);
}
}