const std = @import("std");
const zit = @import("zit");
const support = @import("support.zig");
const addManaged = support.addManaged;
const chmodFile = support.chmodFile;
const commitForkDup = support.commitForkDup;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const initTreeRepo = support.initTreeRepo;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const spawnServe = support.spawnServe;
const stub_signer = support.stub_signer;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
test "cli crosslink: derived records with changed files; cache and derive byte-identical" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// c1 (root): two adds. c2: modify + delete + add. c3: a rename, which the
// change set spells as delete + add. c4: SIGNED, pinning that user and
// files survive the clearsign wrapper.
try writeFile(io, tmp.dir, "a.txt", "alpha\n");
try writeFile(io, tmp.dir, "b.txt", "beta\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
try writeFile(io, tmp.dir, "a.txt", "alpha2\n");
try writeFile(io, tmp.dir, "c.txt", "gamma\n");
var rm = try runZit(gpa, io, tmp.dir, &.{ "rm", "--hard", "b.txt" });
defer freeResult(gpa, &rm);
try expectExitDiag(rm.term, 0, rm);
var ad = try runZit(gpa, io, tmp.dir, &.{ "add", "c.txt" });
defer freeResult(gpa, &ad);
try expectExitDiag(ad.term, 0, ad);
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var mv = try runZit(gpa, io, tmp.dir, &.{ "mv", "c.txt", "d.txt" });
defer freeResult(gpa, &mv);
try expectExitDiag(mv.term, 0, mv);
var c3 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "three" });
defer freeResult(gpa, &c3);
try expectExit(c3.term, 0);
try writeFile(io, tmp.dir, "a.txt", "alpha3\n");
var c4 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "signer", .pgp = stub_signer }, &.{ "commit", "-m", "four", "--sign" });
defer freeResult(gpa, &c4);
try expectExit(c4.term, 0);
var out = try runZit(gpa, io, tmp.dir, &.{"crosslink"});
defer freeResult(gpa, &out);
try expectExitDiag(out.term, 0, out);
const t_ = std.testing;
// The versioned header is the first line.
try t_.expect(std.mem.startsWith(u8, out.stdout, "crosslink 1\n"));
// c1: root — both files are adds.
try t_.expect(std.mem.indexOf(u8, out.stdout, "file A a.txt\nfile A b.txt\n") != null);
// c2: modify + add + delete, in canonical path order per record.
try t_.expect(std.mem.indexOf(u8, out.stdout, "file M a.txt\nfile D b.txt\nfile A c.txt\n") != null);
// c3: the rename spelled as delete + add.
try t_.expect(std.mem.indexOf(u8, out.stdout, "file D c.txt\nfile A d.txt\n") != null);
// Without --lines nothing is counted, printed or stored.
try t_.expect(std.mem.indexOf(u8, out.stdout, "\nlines ") == null);
try t_.expectError(error.FileNotFound, tmp.dir.statFile(io, ".zit-cache/lines", .{}));
// c4: signed — the wrapper neither hides the user nor the change.
try t_.expect(std.mem.indexOf(u8, out.stdout, "user signer\n") != null);
try t_.expect(std.mem.indexOf(u8, out.stdout, "comment 4\nfour\n") != null);
// Nothing in this repository is unknown.
try t_.expect(std.mem.indexOf(u8, out.stdout, "files unknown") == null);
// Four records, each closed.
var n: usize = 0;
var lines = std.mem.splitScalar(u8, out.stdout, '\n');
while (lines.next()) |l| {
if (std.mem.eql(u8, l, "end")) n += 1;
}
try t_.expectEqual(@as(usize, 4), n);
// THE pin: cache-served output is byte-identical to the log derivation.
var audit = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--no-cache" });
defer freeResult(gpa, &audit);
try expectExitDiag(audit.term, 0, audit);
try t_.expectEqualStrings(out.stdout, audit.stdout);
}
test "cli crosslink: lines match diff --numstat; binary, mode-only and unreadable files" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "text.txt", "a\nb\nc\n");
try writeFile(io, tmp.dir, "bin.dat", "x\x00y");
try writeFile(io, tmp.dir, "run.sh", "echo\n");
try addManaged(gpa, io, tmp.dir, &.{ "text.txt", "bin.dat", "run.sh" });
const c1 = try commitHashDup(gpa, io, tmp.dir, "one");
defer gpa.free(c1);
try writeFile(io, tmp.dir, "text.txt", "a\nB\nc\nd\n");
try writeFile(io, tmp.dir, "bin.dat", "x\x00z");
try chmodFile(io, tmp.dir, "run.sh", 0o755);
const c2 = try commitHashDup(gpa, io, tmp.dir, "two");
defer gpa.free(c2);
var out = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &out);
try expectExitDiag(out.term, 0, out);
try t_.expect(std.mem.indexOf(u8, recordOf(out.stdout, c1).?, "file A bin.dat\nlines - -\nfile A run.sh\nlines 1 0\nfile A text.txt\nlines 3 0") != null);
// A binary file is - -, a mode-only change 0 0 (numstat leaves it out).
try t_.expect(std.mem.indexOf(u8, recordOf(out.stdout, c2).?, "file M bin.dat\nlines - -\nfile M run.sh\nlines 0 0\nfile M text.txt\nlines 2 1") != null);
var ns = try runZit(gpa, io, tmp.dir, &.{ "diff", "--checkin", c2, "--numstat" });
defer freeResult(gpa, &ns);
try expectExitDiag(ns.term, 0, ns);
try t_.expectEqualStrings("-\t-\tbin.dat\n2\t1\ttext.txt\n", ns.stdout);
// The counts are stored for the next run, which serves them unchanged,
// and the derivation without any cache prints the same bytes.
const stored = try tmp.dir.statFile(io, ".zit-cache/lines", .{});
try t_.expect(stored.size > 0);
var again = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &again);
try expectExitDiag(again.term, 0, again);
try t_.expectEqualStrings(out.stdout, again.stdout);
var audit = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines", "--no-cache" });
defer freeResult(gpa, &audit);
try expectExitDiag(audit.term, 0, audit);
try t_.expectEqualStrings(out.stdout, audit.stdout);
// A check-in made after the counts were stored is counted on the next
// run, beside the stored ones, and still matches the fresh derivation.
try writeFile(io, tmp.dir, "text.txt", "a\nB\n");
const c3 = try commitHashDup(gpa, io, tmp.dir, "three");
defer gpa.free(c3);
var grown = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &grown);
try expectExitDiag(grown.term, 0, grown);
try t_.expect(std.mem.endsWith(u8, recordOf(grown.stdout, c3).?, "file M text.txt\nlines 0 2"));
var grown_audit = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines", "--no-cache" });
defer freeResult(gpa, &grown_audit);
try expectExitDiag(grown_audit.term, 0, grown_audit);
try t_.expectEqualStrings(grown.stdout, grown_audit.stdout);
// c4 touches only a path no shunned blob reaches. Its stored counts are
// altered by hand, so the run after the shun shows whether they were
// kept or counted again.
try writeFile(io, tmp.dir, "other.txt", "o\n");
try addManaged(gpa, io, tmp.dir, &.{"other.txt"});
const c4 = try commitHashDup(gpa, io, tmp.dir, "four");
defer gpa.free(c4);
var c4_run = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &c4_run);
try expectExitDiag(c4_run.term, 0, c4_run);
try t_.expect(std.mem.endsWith(u8, recordOf(c4_run.stdout, c4).?, "file A other.txt\nlines 1 0"));
{
const sidecar = try tmp.dir.readFileAlloc(io, ".zit-cache/lines", gpa, .limited(1 << 20));
defer gpa.free(sidecar);
// name | n u32 (= 1) | added u32 | removed u32
const added_at = std.mem.indexOf(u8, sidecar, c4).? + c4.len + 4;
std.mem.writeInt(u32, sidecar[added_at..][0..4], 99, .little);
try tmp.dir.writeFile(io, .{ .sub_path = ".zit-cache/lines", .data = sidecar });
}
// Shun c2's text.txt: its counts can no longer be computed, so its lines
// line goes, and so does c3's, which read it as the old side, in both
// outputs, while every other file keeps its own.
var ls = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long", c2 });
defer freeResult(gpa, &ls);
try expectExitDiag(ls.term, 0, ls);
const text_line = lineEndingWith(ls.stdout, " text.txt").?;
const blob = text_line[0..std.mem.indexOfScalar(u8, text_line, ' ').?];
var sh = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", blob });
defer freeResult(gpa, &sh);
try expectExitDiag(sh.term, 0, sh);
var gone = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &gone);
try expectExitDiag(gone.term, 0, gone);
try t_.expect(std.mem.endsWith(u8, recordOf(gone.stdout, c2).?, "file M run.sh\nlines 0 0\nfile M text.txt"));
try t_.expect(std.mem.endsWith(u8, recordOf(gone.stdout, c3).?, "file M text.txt"));
try t_.expect(std.mem.indexOf(u8, recordOf(gone.stdout, c1).?, "file A text.txt\nlines 3 0") != null);
// c4 was not counted again: the shun recounts only what it can reach.
try t_.expect(std.mem.endsWith(u8, recordOf(gone.stdout, c4).?, "file A other.txt\nlines 99 0"));
try tmp.dir.deleteFile(io, ".zit-cache/lines");
freeResult(gpa, &gone);
gone = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines" });
try expectExitDiag(gone.term, 0, gone);
var gone_audit = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--lines", "--no-cache" });
defer freeResult(gpa, &gone_audit);
try expectExitDiag(gone_audit.term, 0, gone_audit);
try t_.expectEqualStrings(gone.stdout, gone_audit.stdout);
}
test "cli crosslink: lines look each old side up by name, the counts the whole tree gives" {
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 linesByNameHistory(gpa, io, tmp.dir, false);
var sub = try runZit(gpa, io, tmp.dir, &.{ "init", "--submanifest", "3", "store" });
defer freeResult(gpa, &sub);
try expectExitDiag(sub.term, 0, sub);
var store = try tmp.dir.openDir(io, "store", .{});
defer store.close(io);
try linesByNameHistory(gpa, io, store, true);
}
test "cli crosslink: a cone clone of a tree store counts lines through the whole walk" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
var init = try runZit(gpa, io, tmp_a.dir, &.{ "init", "--submanifest", "3", "store" });
defer freeResult(gpa, &init);
try expectExitDiag(init.term, 0, init);
var src = try tmp_a.dir.openDir(io, "store", .{});
defer src.close(io);
var name_buf: [32]u8 = undefined;
for ([_][]const u8{ "src", "tools" }) |d| {
try src.createDir(io, d, .default_dir);
for (0..6) |f| {
const name = try std.fmt.bufPrint(&name_buf, "{s}/f{d}.txt", .{ d, f });
try writeFile(io, src, name, name);
}
}
var add = try runZit(gpa, io, src, &.{"addremove"});
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
const c1 = try commitHashDup(gpa, io, src, "one");
defer gpa.free(c1);
try writeFile(io, src, "src/f2.txt", "one\ntwo\n");
const c2 = try commitHashDup(gpa, io, src, "two");
defer gpa.free(c2);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, src, 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", "--cone=src", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var dir = try tmp_b.dir.openDir(io, "repo", .{});
defer dir.close(io);
// A store with a cone is answered by the whole walk, never the search.
const seam = [_][2][]const u8{.{ "ZIT_TREE_FLATTENS", "1" }};
var named = try runZitEnv(gpa, io, dir, &seam, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &named);
try expectExitDiag(named.term, 0, named);
try t_.expect(std.mem.indexOf(u8, named.stderr, "tree-flattens: 0\n") == null);
const rec = try std.mem.concat(gpa, u8, &.{ recordOf(named.stdout, c2).?, "\n" });
defer gpa.free(rec);
try t_.expect(std.mem.indexOf(u8, rec, "file M src/f2.txt\nlines 2 1\n") != null);
const stored = try readFile(gpa, io, dir, ".zit-cache/lines");
defer gpa.free(stored);
try dir.deleteFile(io, ".zit-cache/lines");
const whole_env = [_][2][]const u8{.{ "ZIT_LINES_WHOLE_TREE", "1" }};
var whole = try runZitEnv(gpa, io, dir, &whole_env, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &whole);
try expectExitDiag(whole.term, 0, whole);
try t_.expectEqualStrings(whole.stdout, named.stdout);
const whole_stored = try readFile(gpa, io, dir, ".zit-cache/lines");
defer gpa.free(whole_stored);
try t_.expectEqualSlices(u8, whole_stored, stored);
}
/// Make a history in the checkout `dir` -- adds, edits, a delete, a
/// binary file, an executable bit, a rename with an edit, a merge -- and
/// hold `crosslink --lines` looking old sides up by name to the run that
/// reads each parent's whole file set: the same stream and the same stored
/// counts. In a tree store (`tree`) the lookup walks no whole tree.
fn linesByNameHistory(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, tree: bool) !void {
const t_ = std.testing;
var name_buf: [32]u8 = undefined;
for (0..4) |d| {
const sub = try std.fmt.bufPrint(&name_buf, "d{d}", .{d});
try dir.createDir(io, sub, .default_dir);
for (0..8) |f| {
const name = try std.fmt.bufPrint(&name_buf, "d{d}/f{d}.txt", .{ d, f });
try writeFile(io, dir, name, name);
}
}
try writeFile(io, dir, "a.txt", "one\ntwo\nthree\n");
try writeFile(io, dir, "b.txt", "beta\n");
try writeFile(io, dir, "c.txt", "c1\nc2\nc3\n");
try writeFile(io, dir, "bin.dat", "x\x00y");
try writeFile(io, dir, "run.sh", "echo\n");
var add = try runZit(gpa, io, dir, &.{"addremove"});
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
const c1 = try commitHashDup(gpa, io, dir, "one");
defer gpa.free(c1);
try writeFile(io, dir, "a.txt", "one\nTWO\nthree\nfour\n");
try writeFile(io, dir, "bin.dat", "x\x00z");
try writeFile(io, dir, "d2/f3.txt", "edited\nlines\n");
try chmodFile(io, dir, "run.sh", 0o755);
var rm = try runZit(gpa, io, dir, &.{ "rm", "--hard", "b.txt" });
defer freeResult(gpa, &rm);
try expectExitDiag(rm.term, 0, rm);
const c2 = try commitHashDup(gpa, io, dir, "two");
defer gpa.free(c2);
var mv = try runZit(gpa, io, dir, &.{ "mv", "c.txt", "d3/moved.txt" });
defer freeResult(gpa, &mv);
try expectExitDiag(mv.term, 0, mv);
try writeFile(io, dir, "d3/moved.txt", "c1\nC2\nc3\nc4\n");
const c3 = try commitHashDup(gpa, io, dir, "three");
defer gpa.free(c3);
// A fork off c2, merged back into c3's line.
var up = try runZit(gpa, io, dir, &.{ "update", c2 });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
try writeFile(io, dir, "d0/f1.txt", "side\n");
const side = try commitForkDup(gpa, io, dir, "side");
defer gpa.free(side);
var back = try runZit(gpa, io, dir, &.{ "update", c3 });
defer freeResult(gpa, &back);
try expectExitDiag(back.term, 0, back);
var merge = try runZit(gpa, io, dir, &.{ "merge", side });
defer freeResult(gpa, &merge);
try expectExitDiag(merge.term, 0, merge);
const c4 = try commitHashDup(gpa, io, dir, "merge");
defer gpa.free(c4);
const seam = [_][2][]const u8{.{ "ZIT_TREE_FLATTENS", "1" }};
var named = try runZitEnv(gpa, io, dir, &seam, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &named);
try expectExitDiag(named.term, 0, named);
const stored = try readFile(gpa, io, dir, ".zit-cache/lines");
defer gpa.free(stored);
// What the history must show, so agreement below is not agreement on
// nothing: an edit, a binary, a mode-only change, a delete, the rename
// with its edit counted against no old side, and the merge counted
// against its primary parent.
// A tree store lists a check-in's deletes after its other changes, so
// each file is found on its own.
const Want = struct { checkin: []const u8, lines: []const u8 };
const wants = [_]Want{
.{ .checkin = c2, .lines = "file M a.txt\nlines 2 1\n" },
.{ .checkin = c2, .lines = "file D b.txt\nlines 0 1\n" },
.{ .checkin = c2, .lines = "file M bin.dat\nlines - -\n" },
.{ .checkin = c2, .lines = "file M d2/f3.txt\nlines 2 1\n" },
.{ .checkin = c2, .lines = "file M run.sh\nlines 0 0\n" },
.{ .checkin = c3, .lines = "file D c.txt\nlines 0 3\n" },
.{ .checkin = c3, .lines = "file A d3/moved.txt\nlines 4 0\nrename c.txt d3/moved.txt\n" },
.{ .checkin = c4, .lines = "file M d0/f1.txt\nlines 1 1\n" },
};
for (wants) |w| {
const rec = try std.mem.concat(gpa, u8, &.{ recordOf(named.stdout, w.checkin).?, "\n" });
defer gpa.free(rec);
try t_.expect(std.mem.indexOf(u8, rec, w.lines) != null);
}
try t_.expect(std.mem.indexOf(u8, named.stdout, "files unknown") == null);
// The reference: every old side read from the parent's whole file set.
try dir.deleteFile(io, ".zit-cache/lines");
const whole_env = [_][2][]const u8{ .{ "ZIT_TREE_FLATTENS", "1" }, .{ "ZIT_LINES_WHOLE_TREE", "1" } };
var whole = try runZitEnv(gpa, io, dir, &whole_env, &.{ "crosslink", "--lines" });
defer freeResult(gpa, &whole);
try expectExitDiag(whole.term, 0, whole);
try t_.expectEqualStrings(whole.stdout, named.stdout);
const whole_stored = try readFile(gpa, io, dir, ".zit-cache/lines");
defer gpa.free(whole_stored);
try t_.expectEqualSlices(u8, whole_stored, stored);
// The whole-tree run walks a tree per counted check-in that has a
// parent; the lookup by name walks none.
try t_.expect(std.mem.indexOf(u8, named.stderr, "tree-flattens: 0\n") != null);
if (tree) {
try t_.expect(std.mem.indexOf(u8, whole.stderr, "tree-flattens: 0\n") == null);
}
}
/// The first line of `text` that ends with `suffix`.
fn lineEndingWith(text: []const u8, suffix: []const u8) ?[]const u8 {
var lines = std.mem.splitScalar(u8, text, '\n');
while (lines.next()) |l| if (std.mem.endsWith(u8, l, suffix)) return l;
return null;
}
test "cli crosslink: a cherry-pick and a backout each name their Q card; a plain check-in names none" {
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);
// Backed out from a later check-in: a backout of the check-in a commit
// grows from records no Q card, as in Fossil.
try writeFile(io, tmp.dir, "poem", "A2\nB\nZ\n");
const later = try commitHashDup(gpa, io, tmp.dir, "later");
defer gpa.free(later);
var bo = try runZit(gpa, io, tmp.dir, &.{ "merge", "--backout", r1 });
defer freeResult(gpa, &bo);
try expectExit(bo.term, 0);
const backout = try commitHashDup(gpa, io, tmp.dir, "backout");
defer gpa.free(backout);
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 fork = try commitForkDup(gpa, io, tmp.dir, "fork");
defer gpa.free(fork);
var cp = try runZit(gpa, io, tmp.dir, &.{ "merge", "--cherrypick", r1 });
defer freeResult(gpa, &cp);
try expectExit(cp.term, 0);
const pick = try commitHashDup(gpa, io, tmp.dir, "pick");
defer gpa.free(pick);
var out = try runZit(gpa, io, tmp.dir, &.{"crosslink"});
defer freeResult(gpa, &out);
try expectExitDiag(out.term, 0, out);
const t_ = std.testing;
var buf: [80]u8 = undefined;
try t_.expectEqualStrings(
try std.fmt.bufPrint(&buf, "cherrypick -{s}", .{r1}),
lineIn(recordOf(out.stdout, backout).?, "cherrypick ").?,
);
try t_.expectEqualStrings(
try std.fmt.bufPrint(&buf, "cherrypick +{s}", .{r1}),
lineIn(recordOf(out.stdout, pick).?, "cherrypick ").?,
);
for ([_][]const u8{ r0, r1, later, fork }) |plain| {
try t_.expect(lineIn(recordOf(out.stdout, plain).?, "cherrypick ") == null);
}
var audit = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--no-cache" });
defer freeResult(gpa, &audit);
try expectExitDiag(audit.term, 0, audit);
try t_.expectEqualStrings(out.stdout, audit.stdout);
}
/// The crosslink record for check-in `hash`: from its `checkin` line to its `end`.
fn recordOf(stream: []const u8, hash: []const u8) ?[]const u8 {
var buf: [80]u8 = undefined;
const head = std.fmt.bufPrint(&buf, "checkin {s} ", .{hash}) catch return null;
const start = std.mem.indexOf(u8, stream, head) orelse return null;
const end = std.mem.indexOfPos(u8, stream, start, "\nend\n") orelse return null;
return stream[start..end];
}
/// The first line of `record` that starts with `prefix`.
fn lineIn(record: []const u8, prefix: []const u8) ?[]const u8 {
var lines = std.mem.splitScalar(u8, record, '\n');
while (lines.next()) |l| if (std.mem.startsWith(u8, l, prefix)) return l;
return null;
}
test "crosslink reports a tree check-in's files instead of 'files unknown'" {
// The derive computes every check-in's change set at ingest, trees
// included (by S-card diff), and the crosslink stream carries it. A tree
// used to read as `files unknown` there, because diffing one meant
// flattening it and the derive runs on every cold open.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const tip = try initTreeRepo(gpa, io, tmp.dir);
defer gpa.free(tip);
try writeFile(io, tmp.dir, "b/y.txt", "y2\n");
gpa.free(try commitHashDup(gpa, io, tmp.dir, "touch b"));
var cl = try runZit(gpa, io, tmp.dir, &.{"crosslink"});
defer freeResult(gpa, &cl);
try expectExitDiag(cl.term, 0, cl);
try std.testing.expect(std.mem.indexOf(u8, cl.stdout, "files unknown") == null);
// The root tree: every file an add. The second: exactly the touched one.
try std.testing.expect(std.mem.indexOf(u8, cl.stdout, "file A a/x.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, cl.stdout, "file M b/y.txt") != null);
}
test "crosslink captures a tree check-in's files once the sub-manifest it arrived before is stored" {
// A split push, or a mirror converging over several pulls: the check-in
// lands, its new node only later. The cache built in between holds the
// check-in as `files unknown`; the next read after the node lands must
// not keep serving that.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const tip = try initTreeRepo(gpa, io, tmp.dir);
defer gpa.free(tip);
const leaf = try support.leafSubManifest(gpa, &.{"new/late.txt"});
defer gpa.free(leaf);
const leaf_name = try support.sha3HexAlloc(gpa, leaf);
defer gpa.free(leaf_name);
const cards = try std.fmt.allocPrint(gpa, "C late\nD 2026-11-14T00:00:01\nP {s}\nS new/late.txt {s}\nU u\n", .{ tip, leaf_name });
defer gpa.free(cards);
const tree = try support.withZCard(gpa, cards);
defer gpa.free(tree);
const late = try support.appendArtifact(gpa, io, tmp.dir, tree);
defer gpa.free(late);
{
var cl = try runZit(gpa, io, tmp.dir, &.{"crosslink"});
defer freeResult(gpa, &cl);
try expectExitDiag(cl.term, 0, cl);
const rec = recordOf(cl.stdout, late) orelse return error.RecordMissing;
try std.testing.expect(lineIn(rec, "files unknown") != null);
}
gpa.free(try support.appendArtifact(gpa, io, tmp.dir, leaf));
var cached = try runZit(gpa, io, tmp.dir, &.{"crosslink"});
defer freeResult(gpa, &cached);
try expectExitDiag(cached.term, 0, cached);
const rec = recordOf(cached.stdout, late) orelse return error.RecordMissing;
try std.testing.expect(lineIn(rec, "files unknown") == null);
try std.testing.expectEqualStrings("file A new/late.txt", lineIn(rec, "file A ") orelse return error.NoAdd);
try std.testing.expectEqualStrings("file D a/x.txt", lineIn(rec, "file D a/") orelse return error.NoDelete);
// The file index the same open extended no longer counts it unknown.
{
const seam = [_][2][]const u8{.{ "ZIT_FILEINDEX_WALK", "1" }};
var lg = try runZitEnv(gpa, io, tmp.dir, &seam, &.{ "log", late, "new/late.txt" });
defer freeResult(gpa, &lg);
try expectExitDiag(lg.term, 0, lg);
try std.testing.expectEqual(@as(u64, 0), support.fileIndexUnknownOf(&lg) orelse return error.NoCounter);
}
// And the derive from the log alone says the same, byte for byte.
var fresh = try runZit(gpa, io, tmp.dir, &.{ "crosslink", "--no-cache" });
defer freeResult(gpa, &fresh);
try expectExitDiag(fresh.term, 0, fresh);
try std.testing.expectEqualStrings(fresh.stdout, cached.stdout);
}
/// A record body as the release before named waits wrote it: `waiting`'s
/// record ends in the state byte 1 instead of 2 and the name.
fn downgradeBody(gpa: std.mem.Allocator, body: []const u8, waiting: []const u8) ![]u8 {
var tail_buf: [3 + 64]u8 = undefined;
tail_buf[0] = 2;
std.mem.writeInt(u16, tail_buf[1..3], @intCast(waiting.len), .little);
@memcpy(tail_buf[3..][0..waiting.len], waiting);
const tail = tail_buf[0 .. 3 + waiting.len];
const at = std.mem.indexOf(u8, body, tail) orelse return error.NoWaitingRecord;
return std.mem.concat(gpa, u8, &.{ body[0..at], &.{1}, body[at + tail.len ..] });
}
/// Seal a rewritten sidecar: its version field and trailing checksum.
fn reseal(bytes: []u8, version: u16) void {
std.mem.writeInt(u16, bytes[8..10], version, .little);
std.mem.writeInt(u32, bytes[bytes.len - 4 ..][0..4], zit.crc32.Crc32.hash(bytes[0 .. bytes.len - 4]), .little);
}
/// A tree check-in stored before its one new node, a crosslink run while
/// the node was absent (so the cache and the checkpoint it planted hold the
/// check-in as waiting), then the node. Returns the check-in and the node's
/// names; the caller frees both.
fn lateNodeStore(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { late: []u8, leaf_name: []u8, waiting_body: []u8 } {
const tip = try initTreeRepo(gpa, io, dir);
defer gpa.free(tip);
const leaf = try support.leafSubManifest(gpa, &.{"new/late.txt"});
defer gpa.free(leaf);
const leaf_name = try support.sha3HexAlloc(gpa, leaf);
errdefer gpa.free(leaf_name);
const cards = try std.fmt.allocPrint(gpa, "C late\nD 2026-11-14T00:00:01\nP {s}\nS new/late.txt {s}\nU u\n", .{ tip, leaf_name });
defer gpa.free(cards);
const tree = try support.withZCard(gpa, cards);
defer gpa.free(tree);
const late = try support.appendArtifact(gpa, io, dir, tree);
errdefer gpa.free(late);
{
var cl = try runZit(gpa, io, dir, &.{"crosslink"});
defer freeResult(gpa, &cl);
try expectExitDiag(cl.term, 0, cl);
}
// The cache's record body as it stands: header (54 bytes), main branch,
// body, image, checksum.
const cache = try readFile(gpa, io, dir, zit.repocache.cache_filename);
defer gpa.free(cache);
const mbl = std.mem.readInt(u16, cache[40..42], .little);
const image_len = std.mem.readInt(u32, cache[42..46], .little);
const waiting_body = try gpa.dupe(u8, cache[54 + mbl .. cache.len - 4 - image_len]);
errdefer gpa.free(waiting_body);
gpa.free(try support.appendArtifact(gpa, io, dir, leaf));
return .{ .late = late, .leaf_name = leaf_name, .waiting_body = waiting_body };
}
/// Crosslink from the upgraded sidecar: the late check-in's files, no
/// record derived, a version-8 cache left behind, and the same bytes the
/// log alone gives.
fn expectUpgradedAnswer(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, late: []const u8) !void {
const seam = [_][2][]const u8{.{ "ZIT_DERIVE_FOOTPRINT", "1" }};
var up = try runZitEnv(gpa, io, dir, &seam, &.{"crosslink"});
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
const rec = recordOf(up.stdout, late) orelse return error.RecordMissing;
try std.testing.expectEqualStrings("file A new/late.txt", lineIn(rec, "file A ") orelse return error.NoAdd);
// No record was derived: the open replayed the old body, captured only
// its unknown again, and the verb served the upgraded cache.
try std.testing.expect(std.mem.indexOf(u8, up.stderr, "derive-footprint: records=0 ") != null);
{
const now = try readFile(gpa, io, dir, zit.repocache.cache_filename);
defer gpa.free(now);
try std.testing.expectEqual(@as(u16, 8), std.mem.readInt(u16, now[8..10], .little));
}
var fresh = try runZit(gpa, io, dir, &.{ "crosslink", "--no-cache" });
defer freeResult(gpa, &fresh);
try expectExitDiag(fresh.term, 0, fresh);
try std.testing.expectEqualStrings(fresh.stdout, up.stdout);
}
test "crosslink upgrades the previous release's cache in place, capturing again the unknown it kept for a node that has since landed" {
// The state the previous release left on a store that took a split push:
// a cache current with the log whose record for the check-in still says
// unknown, though the node is stored. The first open of this release must
// answer with the files, from that cache, without a derive.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const s = try lateNodeStore(gpa, io, tmp.dir);
defer gpa.free(s.late);
defer gpa.free(s.leaf_name);
defer gpa.free(s.waiting_body);
// Rewrite the cache as the previous release would hold it now: version
// 7, the stale unknown, and the key of the grown log.
const cache_path = zit.repocache.cache_filename;
const now = try readFile(gpa, io, tmp.dir, cache_path);
defer gpa.free(now);
const mbl = std.mem.readInt(u16, now[40..42], .little);
const image_len = std.mem.readInt(u32, now[42..46], .little);
const body = try downgradeBody(gpa, s.waiting_body, s.leaf_name);
defer gpa.free(body);
const old = try std.mem.concat(gpa, u8, &.{ now[0 .. 54 + mbl], body, now[now.len - 4 - image_len ..] });
defer gpa.free(old);
{
var file = try tmp.dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = zit.storage.FileStorage.initReadOnly(io, file, st.size);
var log = try zit.logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
const key = try log.logKey();
std.mem.writeInt(u64, old[12..20], key.end, .little);
std.mem.writeInt(u64, old[20..28], key.frame_count, .little);
std.mem.writeInt(u64, old[28..36], key.last_frame_offset, .little);
std.mem.writeInt(u32, old[36..40], key.last_header_crc, .little);
}
reseal(old, 7);
try writeFile(io, tmp.dir, cache_path, old);
try expectUpgradedAnswer(gpa, io, tmp.dir, s.late);
}
test "crosslink resumes from the previous release's checkpoint, capturing again the unknown it kept" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const s = try lateNodeStore(gpa, io, tmp.dir);
defer gpa.free(s.late);
defer gpa.free(s.leaf_name);
defer gpa.free(s.waiting_body);
// A checkpoint anchored at the late check-in, its body put back to the
// one the previous release held (version 6), and no cache, so the open
// resumes from it. Header: 83 bytes, the anchor (length at byte 44),
// the main branch (length at 77), the body (length at 79).
{
var b = try runZit(gpa, io, tmp.dir, &.{ "checkpoint", "build" });
defer freeResult(gpa, &b);
try expectExitDiag(b.term, 0, b);
}
const ckpt_path = zit.repocache.cache_dirname ++ "/checkpoint";
const now = try readFile(gpa, io, tmp.dir, ckpt_path);
defer gpa.free(now);
const at = 83 + @as(usize, now[44]) + std.mem.readInt(u16, now[77..79], .little);
const body = try downgradeBody(gpa, s.waiting_body, s.leaf_name);
defer gpa.free(body);
const old = try std.mem.concat(gpa, u8, &.{ now[0..at], body, now[now.len - 4 ..] });
defer gpa.free(old);
std.mem.writeInt(u32, old[79..83], @intCast(body.len), .little);
reseal(old, 6);
try writeFile(io, tmp.dir, ckpt_path, old);
{
var v = try runZit(gpa, io, tmp.dir, &.{ "checkpoint", "verify" });
defer freeResult(gpa, &v);
try expectExitDiag(v.term, 0, v);
}
try tmp.dir.deleteFile(io, zit.repocache.cache_filename);
try expectUpgradedAnswer(gpa, io, tmp.dir, s.late);
}