const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const checkinHash = support.checkinHash;
const chmodFile = support.chmodFile;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const ssh_stub_signer = support.ssh_stub_signer;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
test "cli import-fossil: --submanifest keeps the store's main branch" {
// The main branch never syncs, so a converted store can only learn it
// from the store it was converted from.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "repo", .default_dir);
var repo = try tmp.dir.openDir(io, "repo", .{});
defer repo.close(io);
try initRepo(gpa, io, repo);
try repo.createDir(io, "a", .default_dir);
try repo.createDir(io, "b", .default_dir);
try writeFile(io, repo, "a/x", "1\n");
try writeFile(io, repo, "b/y", "2\n");
try writeFile(io, repo, "c", "3\n");
try addManaged(gpa, io, repo, &.{ "a/x", "b/y", "c" });
var root = try runCommitEnv(gpa, io, repo, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExitDiag(root.term, 0, root);
var set = try runZit(gpa, io, repo, &.{ "settings", "main-branch", "main" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
var conv = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "repo", "converted", "--submanifest", "--k", "2" });
defer freeResult(gpa, &conv);
try expectExitDiag(conv.term, 0, conv);
var got = try runZit(gpa, io, tmp.dir, &.{ "settings", "main-branch", "--value", "-R", "converted" });
defer freeResult(gpa, &got);
try expectExitDiag(got.term, 0, got);
try std.testing.expectEqualStrings("main\n", got.stdout);
}
test "cli import-fossil: the mode is mandatory, --compatible clones verbatim, a one-file history has nothing to convert into trees" {
// Goal: the import-time choice surface. No mode = usage refusal that
// names both consequences; both modes = usage refusal; --compatible over
// the wire preserves the corpus verbatim (same artifacts as a clone);
// --submanifest on a one-file history refuses: no k partitions one name
// into a tree, and the diagnostic names --k and --compatible.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "alpha\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
// No mode: exit 2, the diagnostic teaches the difference.
var nomode = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "src", "dst" });
defer freeResult(gpa, &nomode);
try expectExit(nomode.term, 2);
try std.testing.expect(std.mem.indexOf(u8, nomode.stderr, "permanent") != null);
// Both modes: exit 2.
var both = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "src", "dst", "--compatible", "--submanifest" });
defer freeResult(gpa, &both);
try expectExit(both.term, 2);
try std.testing.expect(std.mem.indexOf(u8, both.stderr, "exclusive") != null);
// --compatible over the wire = the clone, corpus verbatim.
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp.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 away = std.testing.tmpDir(.{});
defer away.cleanup();
var compat = try runZit(gpa, io, away.dir, &.{ "import-fossil", url, "repo", "--compatible" });
defer freeResult(gpa, &compat);
try expectExitDiag(compat.term, 0, compat);
try std.testing.expect(std.mem.indexOf(u8, compat.stdout, "preserving the corpus verbatim") != null);
var repo_dir = try away.dir.openDir(io, "repo", .{});
defer repo_dir.close(io);
var vf = try runZit(gpa, io, repo_dir, &.{"verify"});
defer freeResult(gpa, &vf);
try expectExitDiag(vf.term, 0, vf);
// --submanifest straight from the fossil's URL: a transient
// flat store is cloned beside DIR, converted, and removed.
var from_url = try runZit(gpa, io, away.dir, &.{ "import-fossil", url, "converted-url", "--submanifest", "--k", "2" });
defer freeResult(gpa, &from_url);
try expectExitDiag(from_url.term, 0, from_url);
try std.testing.expect(std.mem.indexOf(u8, from_url.stdout, "cloning the fossil into a transient flat store first") != null);
try std.testing.expect(std.mem.indexOf(u8, from_url.stdout, "1 check-ins re-minted") != null);
try std.testing.expect(std.mem.indexOf(u8, from_url.stdout, "every pair verified file-set-equivalent") != null);
try std.testing.expect(!fileExists(io, away.dir, "converted-url.zit-tmp-flat"));
var url_dst = try away.dir.openDir(io, "converted-url", .{});
defer url_dst.close(io);
var url_vf = try runZit(gpa, io, url_dst, &.{"verify"});
defer freeResult(gpa, &url_vf);
try expectExitDiag(url_vf.term, 0, url_vf);
// --submanifest against a one-file history: no k partitions one name
// into a tree, so there is nothing to convert; --k and --compatible are
// the named remedies. No replay ran.
var sub = try runZit(gpa, io, away.dir, &.{ "import-fossil", "repo", "converted", "--submanifest" });
defer freeResult(gpa, &sub);
try expectExit(sub.term, 1);
try std.testing.expect(std.mem.indexOf(u8, sub.stderr, "no k partitions") != null);
try std.testing.expect(std.mem.indexOf(u8, sub.stderr, "--k") != null);
try std.testing.expect(std.mem.indexOf(u8, sub.stderr, "--compatible") != null);
try std.testing.expect(std.mem.indexOf(u8, sub.stdout, "gate") == null);
// --force is gone with the gate.
var forced = try runZit(gpa, io, away.dir, &.{ "import-fossil", "repo", "converted", "--submanifest", "--force" });
defer freeResult(gpa, &forced);
try expectExit(forced.term, 2);
// --k does not belong to --compatible.
var wrongk = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "s", "d", "--compatible", "--k", "2" });
defer freeResult(gpa, &wrongk);
try expectExit(wrongk.term, 2);
try std.testing.expect(std.mem.indexOf(u8, wrongk.stderr, "belongs to --submanifest") != null);
}
test "cli import-fossil --submanifest: a corpus converts, verifies, and opens byte-identical under a new identity" {
// Goal: the conversion acceptance. A multi-directory two-commit source
// with a tag converts (--k 2 pins the parameter for a small tree): the result
// opens at the re-minted tip, verifies clean, materializes the same
// bytes, resolves the remapped tag to the NEW hash, pins k, and carries
// a different project code (the identity break).
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "src-repo", .default_dir);
var src = try tmp.dir.openDir(io, "src-repo", .{});
defer src.close(io);
try initRepo(gpa, io, src);
inline for (.{ "alpha", "beta", "gamma" }) |d| {
try src.createDir(io, d, .default_dir);
try writeFile(io, src, d ++ "/one.txt", d ++ " one\n");
try writeFile(io, src, d ++ "/two.txt", d ++ " two\n");
}
var ad = try runZit(gpa, io, src, &.{ "add", "." });
defer freeResult(gpa, &ad);
try expectExitDiag(ad.term, 0, ad);
var c1 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
try writeFile(io, src, "alpha/one.txt", "alpha one v2\n");
var c2 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var tg = try runZit(gpa, io, src, &.{ "tag", "add", "release", "tip" });
defer freeResult(gpa, &tg);
try expectExitDiag(tg.term, 0, tg);
const old_tip = try checkinHash(c2.stdout);
var conv = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "src-repo", "converted", "--submanifest", "--k", "2" });
defer freeResult(gpa, &conv);
try expectExitDiag(conv.term, 0, conv);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "every pair verified file-set-equivalent") != null);
var dst = try tmp.dir.openDir(io, "converted", .{});
defer dst.close(io);
var op = try runZit(gpa, io, dst, &.{"open"});
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
var vf = try runZit(gpa, io, dst, &.{"verify"});
defer freeResult(gpa, &vf);
try expectExitDiag(vf.term, 0, vf);
try std.testing.expect(std.mem.indexOf(u8, vf.stdout, "0 faults") != null);
// Same bytes, new identity: the tag resolves to a hash that is NOT the
// source tip's, and the changed file carries its v2 content.
var info = try runZit(gpa, io, dst, &.{ "info", "release" });
defer freeResult(gpa, &info);
try expectExitDiag(info.term, 0, info);
try std.testing.expect(std.mem.indexOf(u8, info.stdout, old_tip) == null);
var cat = try runZit(gpa, io, dst, &.{ "cat", "alpha/one.txt" });
defer freeResult(gpa, &cat);
try expectExitDiag(cat.term, 0, cat);
try std.testing.expectEqualStrings("alpha one v2\n", cat.stdout);
// Sub-manifest settings arrived pinned; the S indicator is set.
var show = try runZit(gpa, io, dst, &.{ "submanifest", "show" });
defer freeResult(gpa, &show);
try expectExitDiag(show.term, 0, show);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "enable-submanifests: on") != null);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "seen-submanifest: yes") != null);
// The provenance technote resolves the OLD tip hash to the NEW one, by
// lookup inside the converted corpus itself (non-negotiable).
var fr = try runZit(gpa, io, dst, &.{"frames"});
defer freeResult(gpa, &fr);
try expectExitDiag(fr.term, 0, fr);
const pair_at = std.mem.indexOf(u8, fr.stdout, old_tip) orelse return error.TestUnexpectedResult;
const line_end = std.mem.indexOfScalarPos(u8, fr.stdout, pair_at, '\n').?;
const pair_line = fr.stdout[pair_at..line_end];
var pf = std.mem.splitScalar(u8, pair_line, ' ');
_ = pf.next(); // the old hash
const mapped = pf.next() orelse return error.TestUnexpectedResult;
var info2 = try runZit(gpa, io, dst, &.{ "info", mapped });
defer freeResult(gpa, &info2);
try expectExitDiag(info2.term, 0, info2);
// Staged publication: success left no staging residue beside the
// destination, and the refused conversion below leaves neither a
// destination nor residue -- the rename is the only commit point.
{
// A dedicated iterate-permitted handle, scoped to the scan: Linux's
// dirRead re-seeks the fd per batch, so iterating a handle owned
// elsewhere (or after any close) is a BADF there even where macOS
// tolerates it.
var scan_dir = try tmp.dir.openDir(io, ".", .{ .iterate = true });
defer scan_dir.close(io);
var scan = scan_dir.iterate();
while (try scan.next(io)) |entry| {
try std.testing.expect(!std.mem.startsWith(u8, entry.name, ".zit-tmp-"));
}
}
var refused = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "converted", "reconverted", "--submanifest", "--k", "2" });
defer freeResult(gpa, &refused);
// A submanifest source re-converts or refuses; either way, on failure
// nothing may remain. (This source HAS S artifacts already; the census
// counts them and conversion proceeds — assert only the residue rule.)
if (exitOf(refused.term) != 0) {
try std.testing.expect(tmp.dir.statFile(io, "reconverted", .{}) == error.FileNotFound);
}
{
var scan_dir = try tmp.dir.openDir(io, ".", .{ .iterate = true });
defer scan_dir.close(io);
var scan2 = scan_dir.iterate();
while (try scan2.next(io)) |entry| {
try std.testing.expect(!std.mem.startsWith(u8, entry.name, ".zit-tmp-"));
}
}
}
fn exitOf(term: std.process.Child.Term) u8 {
return switch (term) {
.exited => |c| c,
else => 255,
};
}
test "cli import-fossil --submanifest: an SSH-signed check-in converts with its signature stripped, --k pins the parameter, and the summary points at pack" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "src-repo", .default_dir);
var src = try tmp.dir.openDir(io, "src-repo", .{});
defer src.close(io);
try initRepo(gpa, io, src);
inline for (.{ "alpha", "beta" }) |d| {
try src.createDir(io, d, .default_dir);
try writeFile(io, src, d ++ "/one.txt", d ++ " one\n");
try writeFile(io, src, d ++ "/two.txt", d ++ " two\n");
}
try addManaged(gpa, io, src, &.{ "alpha/one.txt", "alpha/two.txt", "beta/one.txt", "beta/two.txt" });
var c1 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, src, "beta/two.txt", "beta two v2\n");
// The SSH armor differs from the PGP one; the converter used to strip
// only PGP, re-mint the armor lines as manifest text, and fail its own
// verify without a word.
var c2 = try runCommitEnv(gpa, io, src, .{ .user = "u", .pgp = ssh_stub_signer }, &.{ "commit", "-m", "two", "--sign" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
var conv = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "src-repo", "converted", "--submanifest", "--k", "2" });
defer freeResult(gpa, &conv);
try expectExitDiag(conv.term, 0, conv);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "k=2 pinned by --k") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "gate") == null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "store: flat source ") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "(1 signed: signatures strip)") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "1 signature(s) stripped") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "every pair verified file-set-equivalent") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "delta-encoded as written") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stderr, "import-fossil: re-minted and verified check-ins (") != null);
var dst = try tmp.dir.openDir(io, "converted", .{});
defer dst.close(io);
var op = try runZit(gpa, io, dst, &.{"open"});
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
var vf = try runZit(gpa, io, dst, &.{"verify"});
defer freeResult(gpa, &vf);
try expectExitDiag(vf.term, 0, vf);
try std.testing.expect(std.mem.indexOf(u8, vf.stdout, "0 faults") != null);
var cat = try runZit(gpa, io, dst, &.{ "cat", "beta/two.txt" });
defer freeResult(gpa, &cat);
try expectExitDiag(cat.term, 0, cat);
try std.testing.expectEqualStrings("beta two v2\n", cat.stdout);
}
test "cli import-fossil --submanifest: trees built from the parent survive a rename and a perm change — the pair proof passes and the files read back" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "src-repo", .default_dir);
var src = try tmp.dir.openDir(io, "src-repo", .{});
defer src.close(io);
try initRepo(gpa, io, src);
inline for (.{ "alpha", "beta", "gamma" }) |d| {
try src.createDir(io, d, .default_dir);
try writeFile(io, src, d ++ "/one.txt", d ++ " one\n");
try writeFile(io, src, d ++ "/two.txt", d ++ " two\n");
}
try addManaged(gpa, io, src, &.{ "alpha/one.txt", "alpha/two.txt", "beta/one.txt", "beta/two.txt", "gamma/one.txt", "gamma/two.txt" });
var c1 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
// A rename: the F card carries an old-name token in THIS check-in only.
var mv = try runZit(gpa, io, src, &.{ "mv", "beta/one.txt", "beta/uno.txt" });
defer freeResult(gpa, &mv);
try expectExitDiag(mv.term, 0, mv);
var c2 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "rename" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
// The child inherits the renamed file WITHOUT the annotation, and flips a
// perm elsewhere: both are changes a parent-based build must see.
try chmodFile(io, src, "gamma/two.txt", 0o755);
try writeFile(io, src, "alpha/one.txt", "alpha one v2\n");
var c3 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "perm" });
defer freeResult(gpa, &c3);
try expectExitDiag(c3.term, 0, c3);
var rm = try runZit(gpa, io, src, &.{ "rm", "--hard", "gamma/one.txt" });
defer freeResult(gpa, &rm);
try expectExitDiag(rm.term, 0, rm);
var c4 = try runCommitEnv(gpa, io, src, .{ .user = "u" }, &.{ "commit", "-m", "delete" });
defer freeResult(gpa, &c4);
try expectExitDiag(c4.term, 0, c4);
var conv = try runZit(gpa, io, tmp.dir, &.{ "import-fossil", "src-repo", "converted", "--submanifest", "--k", "2" });
defer freeResult(gpa, &conv);
try expectExitDiag(conv.term, 0, conv);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "4 check-ins re-minted") != null);
try std.testing.expect(std.mem.indexOf(u8, conv.stdout, "every pair verified file-set-equivalent") != null);
var dst = try tmp.dir.openDir(io, "converted", .{});
defer dst.close(io);
var op = try runZit(gpa, io, dst, &.{"open"});
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
var ls = try runZit(gpa, io, dst, &.{ "ls", "--long" });
defer freeResult(gpa, &ls);
try expectExitDiag(ls.term, 0, ls);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "beta/uno.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "beta/one.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "gamma/one.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, " x text gamma/two.txt") != null);
var cat = try runZit(gpa, io, dst, &.{ "cat", "alpha/one.txt" });
defer freeResult(gpa, &cat);
try expectExitDiag(cat.term, 0, cat);
try std.testing.expectEqualStrings("alpha one v2\n", cat.stdout);
}
test "cli import-fossil --compatible: the imported corpus is byte-exact, artifact for artifact" {
// Goal: the round-trip prerequisite. A compatible import carries every
// artifact byte-exact (same names, same bytes, same project identity),
// so a later fossil export reproduces the origin exactly. Method: frames
// streams both corpora in full and the outputs must agree artifact for
// artifact (persisted order is append order on both sides of one clone,
// so the streams are directly comparable after their trailers drop).
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 tmp.dir.createDir(io, "src", .default_dir);
try writeFile(io, tmp.dir, "src/a.zig", "alpha\n");
try writeFile(io, tmp.dir, "README", "readme\n");
try addManaged(gpa, io, tmp.dir, &.{ "src/a.zig", "README" });
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, "src/a.zig", "alpha v2\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var tg = try runZit(gpa, io, tmp.dir, &.{ "tag", "add", "v1", "tip" });
defer freeResult(gpa, &tg);
try expectExitDiag(tg.term, 0, tg);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp.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 away = std.testing.tmpDir(.{});
defer away.cleanup();
var compat = try runZit(gpa, io, away.dir, &.{ "import-fossil", url, "repo", "--compatible" });
defer freeResult(gpa, &compat);
try expectExitDiag(compat.term, 0, compat);
var repo_dir = try away.dir.openDir(io, "repo", .{});
defer repo_dir.close(io);
// The byte census, both sides: every artifact header and payload must
// agree. The trailer lines differ (cursor tokens are store-layout
// facts), so they are stripped before the comparison.
var origin_frames = try runZit(gpa, io, tmp.dir, &.{"frames"});
defer freeResult(gpa, &origin_frames);
try expectExitDiag(origin_frames.term, 0, origin_frames);
var import_frames = try runZit(gpa, io, repo_dir, &.{"frames"});
defer freeResult(gpa, &import_frames);
try expectExitDiag(import_frames.term, 0, import_frames);
const origin_body = origin_frames.stdout[0..std.mem.lastIndexOf(u8, origin_frames.stdout, "cursor ").?];
const import_body = import_frames.stdout[0..std.mem.lastIndexOf(u8, import_frames.stdout, "cursor ").?];
try std.testing.expectEqualStrings(origin_body, import_body);
// Same project identity: the two stores are one sync world.
var pull = try runZit(gpa, io, repo_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExitDiag(pull.term, 0, pull);
}