const std = @import("std");
const repeat = @import("zit").repeat;
const artifact = @import("zit").artifact;
const support = @import("support.zig");
const addManaged = support.addManaged;
const adoptStore = support.adoptStore;
const commitHashDup = support.commitHashDup;
const expectCheckoutVersion = support.expectCheckoutVersion;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const expectNoDiagnostic = support.expectNoDiagnostic;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const parseCheckin = support.parseCheckin;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const seedHorizonConeOrigin = support.seedHorizonConeOrigin;
const spawnServe = support.spawnServe;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
// --- open: clone → working tree (P3-F3) ------------------------------------
//
// `zit clone` builds a repository file and nothing else: the checkout it leaves
// behind is the same UNBORN record `init` writes (`version == ""`, branch
// `trunk`), with no managed set and no files. `zit open` is the second half of
// fossil's own `clone` + `open` UX — it resolves the tip of the main branch and
// materializes it through the shared update engine.
//
// The proof chain below is the missing end-to-end the pre-release assessment
// called out: the existing clone e2e asserts artifact-digest-set equality only,
// which proves the wire converged but says nothing about whether a cloned repo
// can become a usable working tree.
const open_help_expected =
\\usage: zit open [CHECKIN | URL] [--store=PATH] [--manifest[=r,u,t]]
\\ [--cone=PATH] [--cone-not=PATH] [--cone-file=FILE]
\\ [--cone-parents] [-h|--help]
\\
\\Materialize a check-in in the working tree of a not-yet-open checkout.
\\Given a URL instead, this is 'zit clone URL': the repository is cloned
\\into a directory named from the URL and opened there.
\\This is the second half of 'zit clone URL DIR': clone builds DIR and its
\\'.zit', open fills DIR with the files of a check-in. Run it from inside
\\the checkout directory.
\\
\\Without CHECKIN, the tip of this repository's main branch is opened.
\\CHECKIN accepts every form 'zit help names' lists — IDs, prefixes,
\\branches, tags, reserved forms, and timestamps — so a fresh clone can
\\open straight at a release tag without materializing the tip first.
\\
\\A working tree that is already open at a check-in is left untouched; use
\\'zit update' to move an open tree to a different check-in. A repository
\\with no check-in yet is already in its correct (empty) state, which open
\\reports and leaves alone — though a CHECKIN named explicitly must
\\resolve, so in an empty repository it fails as not found.
\\
\\--manifest asks this checkout for the files some build systems read to
\\stamp a version into their output: 'manifest' (the check-in artifact, r),
\\'manifest.uuid' (its hash, u) and 'manifest.tags' (its symbolic tags, t).
\\The bare flag or 'on' means 'ru'; 'off' writes none, whatever the
\\project's 'manifest' setting says. The choice is remembered, so every
\\later 'zit update' rewrites the files for the check-in it moves to.
\\Without the flag the project's 'manifest' setting decides, and a file of
\\one of those names that the check-in itself tracks is never replaced.
\\
\\--store=PATH opens a second working tree of an EXISTING repository: run
\\it in an empty directory and name the checkout (or the '.zit' file) that
\\holds the store. The new tree keeps its own checkout state but reads and
\\writes the one shared store, so a commit in either tree is visible to
\\the other immediately; concurrent commits serialize on the store's own
\\lock. The directory must not already be a checkout.
\\
\\--store on a tree that is ALREADY open re-points it instead: after the
\\repository moves, run open --store=NEWPATH in the stranded tree and it
\\resumes where it was, uncommitted work intact. The new PATH must hold
\\the same repository -- one containing this tree's current check-in --
\\so a tree cannot be re-pointed at an unrelated store.
\\
\\options:
\\ --store=PATH create a detached working tree of the repository
\\ at PATH before opening
\\ --manifest[=r,u,t] write manifest, manifest.uuid, manifest.tags
\\ --cone=PATH materialize and track only PATH's subtree
\\ (repeatable) — a sparse checkout from the start
\\ --cone-not=PATH exclude PATH's subtree from an enclosing --cone
\\ member (repeatable); the most specific member
\\ decides coverage
\\ --cone-file=FILE read cone members from FILE, one per line
\\ ('#' comments and blank lines are skipped;
\\ a '!' prefix marks an exclusion)
\\ --cone-parents also cover the files at every parent level of
\\ the --cone members, the root's included
\\ -h, --help show this help and exit
\\
\\exit status:
\\ 0 success
\\ 1 operational failure (not a repository, unreadable state, a refused
\\ action) — the diagnostic on stderr names it
\\ 2 usage error (an unknown option or unexpected argument)
\\
;
test "cli open P3-F3: clone → open → byte-identical tree → update → commit round-trips" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: two check-ins, the second adding a nested path and rewriting the
// first file — so materializing the TIP is distinguishable from materializing
// the root, and directory creation is exercised.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "first revision\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
const root = try commitHashDup(gpa, io, tmp_a.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp_a.dir, "hello.txt", "second revision, longer than the first\n");
try tmp_a.dir.createDirPath(io, "sub");
try writeFile(io, tmp_a.dir, "sub/nested.txt", "nested payload\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "hello.txt", "sub/nested.txt" });
const tip = try commitHashDup(gpa, io, tmp_a.dir, "tip");
defer gpa.free(tip);
// Serve A and clone it into a fresh directory B/repo over real HTTP.
const open_chain_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, open_chain_port);
defer server.kill(io);
try waitForListen(io, open_chain_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{open_chain_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", 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);
// The state clone leaves: an UNBORN checkout record and no working files.
try expectCheckoutVersion(io, b_dir, "");
try std.testing.expect(!fileExists(io, b_dir, "hello.txt"));
// OPEN. The tip of trunk is resolved from the cloned repository's own state
// — `update` takes only literal 40/64-hex IDs, so this resolution is open's
// own load-bearing work, not something the caller can supply.
{
var op = try runZit(gpa, io, b_dir, &.{"open"});
defer freeResult(gpa, &op);
try expectExit(op.term, 0);
try expectNoDiagnostic(op.stderr); // the receipt lives on stderr
try std.testing.expect(std.mem.indexOf(u8, op.stdout, tip) != null);
try std.testing.expect(std.mem.indexOf(u8, op.stdout, "trunk") != null);
}
try expectCheckoutVersion(io, b_dir, tip);
// THE PROOF: the files on disk in B byte-match the source tree in A. Digest-
// set equality (what the clone e2e asserts) cannot see this.
for ([_][]const u8{ "hello.txt", "sub/nested.txt" }) |path| {
const a_bytes = try readFile(gpa, io, tmp_a.dir, path);
defer gpa.free(a_bytes);
const b_bytes = try readFile(gpa, io, b_dir, path);
defer gpa.free(b_bytes);
try std.testing.expectEqualSlices(u8, a_bytes, b_bytes);
}
try expectManaged(gpa, io, b_dir, &.{ "hello.txt", "sub/nested.txt" });
// `update` works in the opened tree: moving back to the root drops the
// nested file and restores the earlier bytes.
{
var up = try runZit(gpa, io, b_dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
try expectCheckoutVersion(io, b_dir, root);
{
const bytes = try readFile(gpa, io, b_dir, "hello.txt");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("first revision\n", bytes);
}
try std.testing.expect(!fileExists(io, b_dir, "sub/nested.txt"));
// …and back to the tip, so the commit below has the tip as its parent.
{
var up = try runZit(gpa, io, b_dir, &.{ "update", "--no-sync", tip });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
// A `commit` in the opened tree round-trips: it mints a real check-in whose
// P card names the tip we opened at, and the checkout advances onto it.
try writeFile(io, b_dir, "hello.txt", "third revision, authored in the cloned tree\n");
const child = try commitHashDup(gpa, io, b_dir, "from the clone");
defer gpa.free(child);
try expectCheckoutVersion(io, b_dir, child);
var parsed = try parseCheckin(gpa, io, b_dir, child);
defer parsed.deinit();
try std.testing.expectEqual(@as(usize, 1), parsed.m.p.len);
try std.testing.expectEqual(@as(usize, 1), parsed.m.p[0].count);
try std.testing.expectEqualStrings(tip, parsed.m.p[0].parents.?);
}
test "cli open: an explicit CHECKIN opens at that check-in, resolved like info" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: two check-ins so the root is distinguishable from the tip.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "first revision\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
const root = try commitHashDup(gpa, io, tmp_a.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp_a.dir, "hello.txt", "second revision\n");
const tip = try commitHashDup(gpa, io, tmp_a.dir, "tip");
defer gpa.free(tip);
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", 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);
// A name that resolves to nothing fails and leaves the checkout unborn —
// no partial materialization on the way to a refusal.
{
var bogus = try runZit(gpa, io, b_dir, &.{ "open", "no-such-name" });
defer freeResult(gpa, &bogus);
try expectExit(bogus.term, 1);
try std.testing.expectEqualStrings("", bogus.stdout);
try std.testing.expectEqualStrings(
"zit open: check-in 'no-such-name' not found\n",
bogus.stderr,
);
try expectCheckoutVersion(io, b_dir, "");
try std.testing.expect(!fileExists(io, b_dir, "hello.txt"));
}
// Opening at the ROOT by 12-char prefix materializes the root, not the tip
// — the whole point of the argument: no intermediate tip materialization.
{
var op = try runZit(gpa, io, b_dir, &.{ "open", root[0..12] });
defer freeResult(gpa, &op);
try expectExit(op.term, 0);
try std.testing.expectEqualStrings("", op.stderr);
try std.testing.expect(std.mem.indexOf(u8, op.stdout, root) != null);
}
try expectCheckoutVersion(io, b_dir, root);
const got = try readFile(gpa, io, b_dir, "hello.txt");
defer gpa.free(got);
try std.testing.expectEqualStrings("first revision\n", got);
}
test "cli open P3-F3: a plain clone's unborn checkout is a state 'update' tolerates" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// The "interrupted / never-run open" state is EXACTLY what a plain clone
// produces — an unborn checkout record, no managed set, no files. Setting it
// up any other way (hand-writing the record) would keep passing even if
// clone's bootstrap drifted away from what the shared layer tolerates, so
// the setup here is nothing but `zit clone`.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "only.txt", "recoverable\n");
try addManaged(gpa, io, tmp_a.dir, &.{"only.txt"});
const tip = try commitHashDup(gpa, io, tmp_a.dir, "seed");
defer gpa.free(tip);
const open_unborn_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, open_unborn_port);
defer server.kill(io);
try waitForListen(io, open_unborn_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{open_unborn_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", 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);
// `update` straight onto the clone-produced unborn record: the record is
// present but names no check-in, and the managed set is absent (which reads
// as empty) — so the tree is clean and the update must proceed.
{
var up = try runZit(gpa, io, b_dir, &.{ "update", "--no-sync", tip });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
try expectNoDiagnostic(up.stderr); // the receipt lives on stderr
}
try expectCheckoutVersion(io, b_dir, tip);
const bytes = try readFile(gpa, io, b_dir, "only.txt");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("recoverable\n", bytes);
}
// The one materialization failure a user genuinely reaches on this verb: clone
// into a directory, leave a stray untracked file whose path a checked-in path
// needs, then `open`. Every other `ApplyError` arm needs a damaged repository or
// a failing device; this one needs only a file the user put there. It is also
// the arm that pins open's diagnostics to the real engine — a mis-mapped
// diagnostic table survives every success-path test.
test "cli open P3-F3: an untracked file in the way fails the open and materializes nothing" {
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 writeFile(io, tmp_a.dir, "only.txt", "from the repository\n");
try addManaged(gpa, io, tmp_a.dir, &.{"only.txt"});
const tip = try commitHashDup(gpa, io, tmp_a.dir, "seed");
defer gpa.free(tip);
const open_collision_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, open_collision_port);
defer server.kill(io);
try waitForListen(io, open_collision_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{open_collision_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", 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);
// The stray file: same path as the check-in's, untracked, different bytes.
try writeFile(io, b_dir, "only.txt", "written by the user, never added\n");
var res = try runZit(gpa, io, b_dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
// `open` resolves its own target, so it names the check-in it resolved
// before reporting what went wrong: without that line a reader cannot tell
// "materializing the expected check-in failed" from "resolved a check-in
// other than the tip I believe in". Asserted whole rather than by
// substring, so a future change to either line has to come through here.
const expected_stderr = try std.fmt.allocPrint(
gpa,
"zit open: materializing check-in {s} on trunk\n" ++
"zit open: an untracked file or directory is in the way\n",
.{tip},
);
defer gpa.free(expected_stderr);
try std.testing.expectEqualStrings(expected_stderr, res.stderr);
// Nothing materialized and nothing clobbered: the record is still unborn and
// the user's bytes are exactly as they were left.
try expectCheckoutVersion(io, b_dir, "");
const bytes = try readFile(gpa, io, b_dir, "only.txt");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("written by the user, never added\n", bytes);
}
test "cli open P3-F3: re-opening an already-open tree is refused and changes 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, "poem", "line\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const head = try commitHashDup(gpa, io, tmp.dir, "seed");
defer gpa.free(head);
// A dirty edit proves the refusal happens BEFORE any materialization: a
// gate that ran the update anyway would either overwrite this or fail with
// update's own not-clean diagnostic.
try writeFile(io, tmp.dir, "poem", "edited but not committed\n");
var res = try runZit(gpa, io, tmp.dir, &.{"open"});
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, "already open") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, head) != null);
const bytes = try readFile(gpa, io, tmp.dir, "poem");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("edited but not committed\n", bytes);
}
/// A complete check-in manifest of a DIFFERENT repository, as file bytes.
///
/// `cards` is everything before the Z card; the checksum is computed here
/// because a wrong one makes the blob unclassifiable, which would silently
/// turn either test below into a tautology -- no foreign check-in would exist
/// and both would pass for the wrong reason.
///
/// The F card names an artifact no store holds, which is what makes the
/// check-in unmaterializable. `D 2099-01-01` puts it ahead of any check-in the
/// test itself makes, so mtime resolution (repo-state 8.2) prefers it wherever
/// it is eligible at all.
fn plantForeign(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
cards: []const u8,
) !void {
var digest: [16]u8 = undefined;
std.crypto.hash.Md5.hash(cards, &digest, .{});
const hex = std.fmt.bytesToHex(digest, .lower);
const payload = try std.fmt.allocPrint(gpa, "{s}Z {s}\n", .{ cards, &hex });
defer gpa.free(payload);
try writeFile(io, dir, "payload", payload);
try writeFile(io, dir, "own.txt", "real content\n");
try addManaged(gpa, io, dir, &.{ "payload", "own.txt" });
}
test "cli open P3-F3: a self-tagged foreign check-in is refused, naming the class" {
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);
// `T *branch * trunk` is self-targeted and value-carrying; `T *sym-trunk *`
// is bare and propagating. Together they make this manifest a genuine
// carrier of trunk's tags, so it is eligible for tip resolution and wins on
// mtime. (`T *branch trunk` would instead put a branch name in the UUID
// field, rejecting the manifest entirely and making it unclassifiable.)
try plantForeign(gpa, io, tmp_a.dir, "C foreign\n" ++
"D 2099-01-01T00:00:00\n" ++
"F absent.txt " ++ (repeat("1", 64)) ++ "\n" ++
"T *branch * trunk\n" ++
"T *sym-trunk *\n" ++
"U nobody\n");
const head = try commitHashDup(gpa, io, tmp_a.dir, "plant");
defer gpa.free(head);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
try initRepo(gpa, io, tmp_b.dir);
try adoptStore(gpa, io, tmp_a.dir, tmp_b.dir);
var res = try runZit(gpa, io, tmp_b.dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
// The foreign check-in won the tip: the diagnostic names it and not the
// check-in this test actually made. Without that, `open` would have
// succeeded and the refusal below would be untested.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, head) == null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "absent.txt") != null);
// The diagnostic names the CLASS. A bare missing-file error sends the
// reader to the wrong subsystem, and the store is intact, so it must not
// allege corruption -- `verify` would contradict it.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "ANOTHER repository") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "corrupt") == null);
// Fail-closed: nothing materialized, the record still unborn. This is the
// property most likely to be traded away by a later optimization of the
// preflight, and it is the reason this test spawns the real binary.
try expectCheckoutVersion(io, tmp_b.dir, "");
try std.testing.expect(!fileExists(io, tmp_b.dir, "own.txt"));
try std.testing.expect(!fileExists(io, tmp_b.dir, "payload"));
}
test "cli open P3-F3: an untagged foreign check-in is visible but neither a leaf nor a tip" {
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);
// Same manifest as above minus its T cards, and the same 2099 date. It is
// still a check-in and still joins the branch for display, but it carries
// no propagating branch tag, so the default-branch fallback attributes it
// without making it resolvable (repo-state 6.1 L920-926). The later date
// is what makes that distinction load-bearing: if attribution DID confer
// resolvability, this manifest would take the tip and `open` would fail.
try plantForeign(gpa, io, tmp_a.dir, "C untagged\n" ++
"D 2099-01-01T00:00:00\n" ++
"F absent.txt " ++ (repeat("1", 64)) ++ "\n" ++
"U nobody\n");
const head = try commitHashDup(gpa, io, tmp_a.dir, "plant");
defer gpa.free(head);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
try initRepo(gpa, io, tmp_b.dir);
try adoptStore(gpa, io, tmp_a.dir, tmp_b.dir);
var res = try runZit(gpa, io, tmp_b.dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, head) != null);
try expectCheckoutVersion(io, tmp_b.dir, head);
// The real check-in materialized in full, foreign leaf notwithstanding.
const bytes = try readFile(gpa, io, tmp_b.dir, "own.txt");
defer gpa.free(bytes);
try std.testing.expectEqualStrings("real content\n", bytes);
// And it is visible: refusing to resolve it is not the same as hiding it.
// It is not a leaf, since it carries no branch tag (§7.3), so `leaves`
// lists only the real check-in; `info` still describes it.
var leaves = try runZit(gpa, io, tmp_b.dir, &.{"leaves"});
defer freeResult(gpa, &leaves);
try expectExitDiag(leaves.term, 0, leaves);
var lines: usize = 0;
var it = std.mem.tokenizeScalar(u8, leaves.stdout, '\n');
while (it.next()) |_| lines += 1;
try std.testing.expectEqual(@as(usize, 1), lines);
try std.testing.expect(std.mem.indexOf(u8, leaves.stdout, head) == null);
var fi = try runZit(gpa, io, tmp_b.dir, &.{ "info", head });
defer freeResult(gpa, &fi);
try expectExitDiag(fi.term, 0, fi);
try std.testing.expect(std.mem.indexOf(u8, fi.stdout, head) != null);
}
test "cli open P3-F3: a repository with no check-in is reported, not failed" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{"open"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "no check-in") != null);
// The unborn record is the correct end state for an empty repository; open
// leaves it exactly as it found it.
try expectCheckoutVersion(io, tmp.dir, "");
}
test "cli open P3-F3: outside a repository fails with the not-a-repo diagnostic, exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{}); // NOT initialized
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, &.{"open"});
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, "no zit checkout here") != null);
}
test "cli open P3-F3: an unknown option or a positional 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);
const cases = [_][]const []const u8{
&.{ "open", "-x" }, // unknown option
&.{ "open", "a", "b" }, // one positional is CHECKIN; a second is extra
};
for (cases) |argv| {
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "try 'zit open --help'") != null);
}
// A single positional is CHECKIN — not a usage error. In this empty
// repository it resolves to nothing: an operational failure, exit 1.
var named = try runZit(gpa, io, tmp.dir, &.{ "open", "somewhere" });
defer freeResult(gpa, &named);
try expectExit(named.term, 1);
try std.testing.expectEqualStrings("", named.stdout);
try std.testing.expectEqualStrings("zit open: check-in 'somewhere' not found\n", named.stderr);
}
test "cli open P3-F3: the verb is in the top-level help and teaches one help text" {
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 top = try runZit(gpa, io, tmp.dir, &.{"help"});
defer freeResult(gpa, &top);
try expectExit(top.term, 0);
try std.testing.expect(std.mem.indexOf(u8, top.stdout, " open ") != null);
// `zit open --help` and `zit help open` teach byte-identical text (DX D4).
var flag = try runZit(gpa, io, tmp.dir, &.{ "open", "--help" });
defer freeResult(gpa, &flag);
try expectExit(flag.term, 0);
try std.testing.expectEqualStrings(open_help_expected, flag.stdout);
var topic = try runZit(gpa, io, tmp.dir, &.{ "help", "open" });
defer freeResult(gpa, &topic);
try expectExit(topic.term, 0);
try std.testing.expectEqualStrings(open_help_expected, topic.stdout);
}
// --------------------------------------------------------------------------
// `open --manifest[=r,u,t]`: the working-tree manifest files, as an explicit
// per-checkout opt-in. The three characters select the check-in artifact
// (`r` -> `manifest`), its hash (`u` -> `manifest.uuid`) and its symbolic tags
// (`t` -> `manifest.tags`); the bare flag means `ru`. The opt-in is recorded in
// the checkout record, so every later `update` refreshes the files. No flag
// writes nothing, which is the default and stays the default.
// --------------------------------------------------------------------------
/// The line fossil appends to the on-disk `manifest`, so the file is not itself
/// a well-formed artifact. Pinned here as a literal, not imported: it is a
/// cross-implementation compatibility fact, and a test that read it from the
/// implementation could not notice the implementation changing it.
const manifest_sterilizer = "# Remove this line to create a well-formed Fossil manifest.\n";
test "cli open: --manifest writes the manifest files and a later update refreshes them" {
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 writeFile(io, tmp_a.dir, "hello.txt", "first revision\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
const root = try commitHashDup(gpa, io, tmp_a.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp_a.dir, "hello.txt", "second revision, longer than the first\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
const tip = try commitHashDup(gpa, io, tmp_a.dir, "tip");
defer gpa.free(tip);
const open_manifest_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, open_manifest_port);
defer server.kill(io);
try waitForListen(io, open_manifest_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{open_manifest_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", 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", "--manifest=rut" });
defer freeResult(gpa, &op);
try expectExit(op.term, 0);
try std.testing.expectEqualStrings("", op.stderr);
}
// `u`: the check-in hash and a newline, verbatim.
{
const uuid = try readFile(gpa, io, b_dir, "manifest.uuid");
defer gpa.free(uuid);
const want = try std.fmt.allocPrint(gpa, "{s}\n", .{tip});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, uuid);
}
// `r`: the check-in artifact ITSELF, plus fossil's sterilizing line —
// asserted by hashing the leading bytes back to `tip`. A weaker oracle (the
// file exists, or it mentions a card name) would pass on approximately-right
// bytes; this one cannot.
{
const man = try readFile(gpa, io, b_dir, "manifest");
defer gpa.free(man);
try std.testing.expect(std.mem.endsWith(u8, man, manifest_sterilizer));
try std.testing.expect(artifact.verify(tip, man[0 .. man.len - manifest_sterilizer.len]));
// POSITION, not merely presence: the marker follows the final card.
// Fossil appends it, and a reader strips a TRAILING occurrence, so a
// refactor that moved it to line 1 would still sterilize, still satisfy
// an endsWith-free check, and silently stop matching Fossil byte for
// byte.
try std.testing.expect(!std.mem.startsWith(u8, man, manifest_sterilizer));
}
// `t`: fossil's layout — the branch line, then one line per symbolic tag.
{
const tags = try readFile(gpa, io, b_dir, "manifest.tags");
defer gpa.free(tags);
try std.testing.expectEqualStrings("branch trunk\ntag trunk\n", tags);
}
// The opt-in is per checkout, not per command: `update` refreshes all three
// against the new check-in without the flag being repeated.
{
var up = try runZit(gpa, io, b_dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
}
{
const uuid = try readFile(gpa, io, b_dir, "manifest.uuid");
defer gpa.free(uuid);
const want = try std.fmt.allocPrint(gpa, "{s}\n", .{root});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, uuid);
const man = try readFile(gpa, io, b_dir, "manifest");
defer gpa.free(man);
try std.testing.expect(artifact.verify(root, man[0 .. man.len - manifest_sterilizer.len]));
}
}
test "cli open: --manifest never overwrites a tracked file of the same name" {
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);
// A repository that TRACKS a file called `manifest`. Fossil refuses to
// clobber it and so must zit: the user's versioned content outranks a
// convenience file.
try writeFile(io, tmp_a.dir, "manifest", "a tracked file, not the check-in\n");
try writeFile(io, tmp_a.dir, "hello.txt", "payload\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "manifest", "hello.txt" });
const tip = try commitHashDup(gpa, io, tmp_a.dir, "seed");
defer gpa.free(tip);
const open_manifest_guard_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, open_manifest_guard_port);
defer server.kill(io);
try waitForListen(io, open_manifest_guard_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{open_manifest_guard_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", 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", "--manifest=ru" });
defer freeResult(gpa, &op);
try expectExit(op.term, 0);
try std.testing.expectEqualStrings("", op.stderr);
}
// The tracked bytes survive; the un-tracked sibling is still written.
const kept = try readFile(gpa, io, b_dir, "manifest");
defer gpa.free(kept);
try std.testing.expectEqualStrings("a tracked file, not the check-in\n", kept);
const uuid = try readFile(gpa, io, b_dir, "manifest.uuid");
defer gpa.free(uuid);
const want = try std.fmt.allocPrint(gpa, "{s}\n", .{tip});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, uuid);
}
test "cli open: without --manifest no manifest file is 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 writeFile(io, tmp.dir, "poem", "line\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const head = try commitHashDup(gpa, io, tmp.dir, "seed");
defer gpa.free(head);
for ([_][]const u8{ "manifest", "manifest.uuid", "manifest.tags" }) |name| {
try std.testing.expect(!fileExists(io, tmp.dir, name));
}
}
test "cli open: a versioned-only manifest setting is read out of the check-in at open" {
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.createDirPath(io, ".fossil-settings");
try writeFile(io, tmp_a.dir, ".fossil-settings/manifest", "# stamps for the build\nu\n");
try writeFile(io, tmp_a.dir, "hello.txt", "one\n");
try addManaged(gpa, io, tmp_a.dir, &.{ ".fossil-settings/manifest", "hello.txt" });
const tip = try commitHashDup(gpa, io, tmp_a.dir, "root");
defer gpa.free(tip);
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", 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"});
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
}
// No repository or global value exists: only the check-in's own file
// could have asked for `manifest.uuid`, and it did.
const uuid = try readFile(gpa, io, b_dir, "manifest.uuid");
defer gpa.free(uuid);
const want = try std.fmt.allocPrint(gpa, "{s}\n", .{tip});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, uuid);
try std.testing.expect(!fileExists(io, b_dir, "manifest"));
}
test "cli open: --manifest=off is remembered, so the project setting cannot re-enable stamps at update" {
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.createDirPath(io, ".zit-settings");
try writeFile(io, tmp_a.dir, ".zit-settings/manifest", "on\n");
try writeFile(io, tmp_a.dir, "hello.txt", "one\n");
try addManaged(gpa, io, tmp_a.dir, &.{ ".zit-settings/manifest", "hello.txt" });
const root = try commitHashDup(gpa, io, tmp_a.dir, "root");
defer gpa.free(root);
try writeFile(io, tmp_a.dir, "hello.txt", "two\n");
const tip = try commitHashDup(gpa, io, tmp_a.dir, "tip");
defer gpa.free(tip);
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", 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", "--manifest=off" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
}
try std.testing.expect(!fileExists(io, b_dir, "manifest"));
try std.testing.expect(!fileExists(io, b_dir, "manifest.uuid"));
{
var up = try runZit(gpa, io, b_dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
}
// The versioned `on` is live in this tree now, and still loses to the
// checkout's explicit refusal.
try std.testing.expect(!fileExists(io, b_dir, "manifest"));
try std.testing.expect(!fileExists(io, b_dir, "manifest.uuid"));
// And `on` at open is fossil's spelling for `ru`.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
{
var clone = try runZit(gpa, io, tmp_c.dir, &.{ "clone", "--no-open", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
}
var c_dir = try tmp_c.dir.openDir(io, "repo", .{});
defer c_dir.close(io);
{
var op = try runZit(gpa, io, c_dir, &.{ "open", "--manifest=on" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
}
try std.testing.expect(fileExists(io, c_dir, "manifest"));
try std.testing.expect(fileExists(io, c_dir, "manifest.uuid"));
try std.testing.expect(!fileExists(io, c_dir, "manifest.tags"));
}
test "cli open dense on a sparse store: demand fetch via the saved remote, refusal without one" {
// Sparse-cone RFC section 6.4, policy form: a dense open on a sparse
// store expresses new coverage intent -- the store cone drops, an
// ordinary pull fetches what the new policy wants from the saved remote
// (clone saved it), and the open proceeds to a full tree. With no saved
// remote the operation refuses naming the elision class.
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", "in\n");
try writeFile(io, tmp_a.dir, "tools/t.bin", "out\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);
// Arm 1: saved remote present (clone saves it) -- dense open fetches.
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 od = try runZit(gpa, io, b_dir, &.{"open"});
defer freeResult(gpa, &od);
try expectExitDiag(od.term, 0, od);
try std.testing.expect(std.mem.indexOf(u8, od.stderr, "fetching from the saved remote") != null);
const fetched = try readFile(gpa, io, b_dir, "tools/t.bin");
defer gpa.free(fetched);
try std.testing.expectEqualStrings("out\n", fetched);
// Arm 2: no saved remote -- the elided-class refusal, naming remedies.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
var clone2 = try runZit(gpa, io, tmp_c.dir, &.{ "clone", "--no-open", "--cone=src", "--once", url, "repo" });
defer freeResult(gpa, &clone2);
try expectExitDiag(clone2.term, 0, clone2);
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 expectExit(oc.term, 1);
try std.testing.expect(std.mem.indexOf(u8, oc.stderr, "elided by the store's sparse cone") != null);
try std.testing.expect(std.mem.indexOf(u8, oc.stderr, "no saved remote") != null);
try std.testing.expect(std.mem.indexOf(u8, oc.stderr, "corrupt") == null);
}
test "cli open on a horizon clone with a path cone opens with and without REV, and below the horizon of a content clone" {
// A cone-scoped horizon clone opened without --cone is a dense open, as
// on any cone clone: the paths widen and the horizon stays, deepening
// only when the check-in lies below it.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const seeded = try seedHorizonConeOrigin(gpa, io, tmp_a.dir);
defer gpa.free(seeded.h1);
defer gpa.free(seeded.h2);
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);
const horizon_arg = try std.fmt.allocPrint(gpa, "--horizon={s}", .{seeded.h2});
defer gpa.free(horizon_arg);
const Case = struct { rev: ?[]const u8, want: []const u8 };
for ([_]bool{ false, true }) |structural| {
// Below the horizon only a content clone holds the manifest to open.
const all = [_]Case{ .{ .rev = null, .want = "t3\n" }, .{ .rev = seeded.h2, .want = "t2\n" }, .{ .rev = seeded.h1, .want = "t1\n" } };
for (if (structural) all[0..2] else all[0..]) |case| {
errdefer std.debug.print("structural: {}, rev: {?s}\n", .{ structural, case.rev });
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
const clone_args: []const []const u8 = if (structural)
&.{ "clone", "--no-open", horizon_arg, "--structural", "--cone=src", url, "repo" }
else
&.{ "clone", "--no-open", horizon_arg, "--cone=src", url, "repo" };
var clone = try runZit(gpa, io, tmp_b.dir, clone_args);
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);
const open_args: []const []const u8 = if (case.rev) |r| &.{ "open", r } else &.{"open"};
var op = try runZit(gpa, io, b_dir, open_args);
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
try std.testing.expect(std.mem.indexOf(u8, op.stderr, "no saved remote") == null);
const tools = try readFile(gpa, io, b_dir, "tools/t");
defer gpa.free(tools);
try std.testing.expectEqualStrings(case.want, tools);
}
}
}