const std = @import("std");
const cli = @import("zit").cli;
const support = @import("support.zig");
const addManaged = support.addManaged;
const checkinHash = support.checkinHash;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const worktreeStorm = support.worktreeStorm;
const writeFile = support.writeFile;
test "worktrees: a detached checkout opens, follows commits both ways, and shares the store" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "main", .default_dir);
try tmp.dir.createDir(io, "wt", .default_dir);
var main_dir = try tmp.dir.openDir(io, "main", .{});
defer main_dir.close(io);
var wt_dir = try tmp.dir.openDir(io, "wt", .{});
defer wt_dir.close(io);
// The ordinary co-located checkout: one file, one check-in.
try initRepo(gpa, io, main_dir);
try writeFile(io, main_dir, "greeting", "hello from main\n");
try addManaged(gpa, io, main_dir, &.{"greeting"});
var c1 = try runCommitEnv(gpa, io, main_dir, .{ .user = "alice" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const tip1 = try checkinHash(c1.stdout);
// A detached worktree of the same store: `open --store` materializes the
// tip with no `.zit` of its own.
var opened = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main" });
defer freeResult(gpa, &opened);
try expectExit(opened.term, 0);
try std.testing.expectError(error.FileNotFound, wt_dir.statFile(io, ".zit", .{}));
{
const got = try wt_dir.readFileAlloc(io, "greeting", gpa, .limited(256));
defer gpa.free(got);
try std.testing.expectEqualStrings("hello from main\n", got);
}
// A commit in the primary is visible from the worktree through the shared
// store: update B to the new tip and read the new bytes.
try writeFile(io, main_dir, "greeting", "hello again\n");
var c2 = try runCommitEnv(gpa, io, main_dir, .{ .user = "alice" }, &.{ "commit", "-m", "second" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
const tip2 = try checkinHash(c2.stdout);
try std.testing.expect(!std.mem.eql(u8, tip1, tip2));
var up = try runZit(gpa, io, wt_dir, &.{ "update", tip2 });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
{
const got = try wt_dir.readFileAlloc(io, "greeting", gpa, .limited(256));
defer gpa.free(got);
try std.testing.expectEqualStrings("hello again\n", got);
}
// And the reverse: a commit FROM the worktree appends to the shared store
// (under its lock), and the primary can update to it.
try writeFile(io, wt_dir, "greeting", "hello from the worktree\n");
var c3 = try runCommitEnv(gpa, io, wt_dir, .{ .user = "bob" }, &.{ "commit", "-m", "third" });
defer freeResult(gpa, &c3);
try expectExit(c3.term, 0);
const tip3 = try checkinHash(c3.stdout);
var up_main = try runZit(gpa, io, main_dir, &.{ "update", tip3 });
defer freeResult(gpa, &up_main);
try expectExit(up_main.term, 0);
{
const got = try main_dir.readFileAlloc(io, "greeting", gpa, .limited(256));
defer gpa.free(got);
try std.testing.expectEqualStrings("hello from the worktree\n", got);
}
}
test "worktrees: concurrent commits from two worktrees serialize on the store lock, verify clean" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "main", .default_dir);
try tmp.dir.createDir(io, "wt", .default_dir);
var main_dir = try tmp.dir.openDir(io, "main", .{});
defer main_dir.close(io);
var wt_dir = try tmp.dir.openDir(io, "wt", .{});
defer wt_dir.close(io);
try initRepo(gpa, io, main_dir);
try writeFile(io, main_dir, "seed", "seed");
try addManaged(gpa, io, main_dir, &.{"seed"});
var c0 = try runCommitEnv(gpa, io, main_dir, .{ .user = "alice" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c0);
try expectExit(c0.term, 0);
var opened = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main" });
defer freeResult(gpa, &opened);
try expectExit(opened.term, 0);
var failures = std.atomic.Value(u32).init(0);
const per_side = 5;
var thread_a = try std.Thread.spawn(.{}, worktreeStorm, .{ io, main_dir, "main-file", per_side, &failures });
var thread_b = try std.Thread.spawn(.{}, worktreeStorm, .{ io, wt_dir, "wt-file", per_side, &failures });
thread_a.join();
thread_b.join();
try std.testing.expectEqual(@as(u32, 0), failures.load(.monotonic));
// The claim under test: ten racing appends, zero torn frames. verify
// reconstructs every artifact through its delta chain and re-hashes it
// against its name, so a torn log cannot pass.
var ver = try runZit(gpa, io, main_dir, &.{"verify"});
defer freeResult(gpa, &ver);
try expectExit(ver.term, 0);
var ver_wt = try runZit(gpa, io, wt_dir, &.{"verify"});
defer freeResult(gpa, &ver_wt);
try expectExit(ver_wt.term, 0);
}
test "worktrees: a commit blocked by a held store lock waits and then succeeds" {
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, "blocked", "blocked");
try addManaged(gpa, io, tmp.dir, &.{"blocked"});
// Hold the store's write lock the way any competing writer would, release
// it from a timer thread, and demand the commit spawned meanwhile lands
// AFTER the release instead of failing or tearing.
var appender = try cli.openStoreAppend(io, tmp.dir);
const Releaser = struct {
fn run(io_inner: std.Io, held: *cli.StoreAppend) void {
std.Io.sleep(io_inner, .fromMilliseconds(400), .awake) catch {};
held.close(io_inner);
}
};
var releaser = try std.Thread.spawn(.{}, Releaser.run, .{ io, &appender });
var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "waited" });
defer freeResult(gpa, &res);
releaser.join();
try expectExit(res.term, 0);
}
test "worktrees re-open: a moved store is re-pointed in place, an unrelated store is refused" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "main", .default_dir);
try tmp.dir.createDir(io, "wt", .default_dir);
try tmp.dir.createDir(io, "other", .default_dir);
var main_dir = try tmp.dir.openDir(io, "main", .{});
defer main_dir.close(io);
var wt_dir = try tmp.dir.openDir(io, "wt", .{});
defer wt_dir.close(io);
var other_dir = try tmp.dir.openDir(io, "other", .{});
defer other_dir.close(io);
try initRepo(gpa, io, main_dir);
try writeFile(io, main_dir, "greeting", "hello\n");
try addManaged(gpa, io, main_dir, &.{"greeting"});
var c1 = try runCommitEnv(gpa, io, main_dir, .{ .user = "alice" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
var opened = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main" });
defer freeResult(gpa, &opened);
try expectExit(opened.term, 0);
// An unrelated repository never passes the containment gate, no matter
// how healthy it is: re-pointing must mean "same store, new address".
try initRepo(gpa, io, other_dir);
var refused = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../other" });
defer freeResult(gpa, &refused);
try expectExit(refused.term, 1);
// The store moves; the stranded worktree holds uncommitted work; the
// repair re-points in place and both survive.
try writeFile(io, wt_dir, "uncommitted", "precious\n");
try tmp.dir.rename("main", tmp.dir, "main2", io);
var stranded = try runZit(gpa, io, wt_dir, &.{"stat"});
defer freeResult(gpa, &stranded);
try expectExit(stranded.term, 1);
var repaired = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main2" });
defer freeResult(gpa, &repaired);
try expectExit(repaired.term, 0);
var alive = try runZit(gpa, io, wt_dir, &.{"stat"});
defer freeResult(gpa, &alive);
try expectExit(alive.term, 0);
{
const got = try wt_dir.readFileAlloc(io, "uncommitted", gpa, .limited(64));
defer gpa.free(got);
try std.testing.expectEqualStrings("precious\n", got);
}
var c2 = try runCommitEnv(gpa, io, wt_dir, .{ .user = "bob" }, &.{ "add", "uncommitted" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var c3 = try runCommitEnv(gpa, io, wt_dir, .{ .user = "bob" }, &.{ "commit", "-m", "rescued" });
defer freeResult(gpa, &c3);
try expectExit(c3.term, 0);
}
test "cli worktrees: trees stamp at open/update, listings agree from every tree, dead trees prune lazily" {
// Goal: the store-side registry lifecycle. A co-located tree and a
// detached tree both stamp; `worktrees` from either lists both with the
// invoking tree starred; deleting a tree with plain rm prunes it from
// the next listing; bare -R answers from nowhere without a marker.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "main-tree", .default_dir);
var main_tree = try tmp.dir.openDir(io, "main-tree", .{});
defer main_tree.close(io);
try initRepo(gpa, io, main_tree);
try writeFile(io, main_tree, "f.txt", "x\n");
try addManaged(gpa, io, main_tree, &.{"f.txt"});
var c1 = try runCommitEnv(gpa, io, main_tree, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
var up = try runZit(gpa, io, main_tree, &.{ "update", "tip" });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up); // the stamp rides update
try tmp.dir.createDir(io, "tree-b", .default_dir);
var tree_b = try tmp.dir.openDir(io, "tree-b", .{});
defer tree_b.close(io);
var det = try runZit(gpa, io, tree_b, &.{ "open", "--store=../main-tree" });
defer freeResult(gpa, &det);
try expectExitDiag(det.term, 0, det);
// Both trees listed from the detached side; the invoker is starred.
var from_b = try runZit(gpa, io, tree_b, &.{"worktrees"});
defer freeResult(gpa, &from_b);
try expectExitDiag(from_b.term, 0, from_b);
try std.testing.expect(std.mem.indexOf(u8, from_b.stdout, "main-tree") != null);
try std.testing.expect(std.mem.indexOf(u8, from_b.stdout, "* ") != null);
try std.testing.expect(std.mem.indexOf(u8, from_b.stdout, "tree-b") != null);
// ... and from the co-located side.
var from_main = try runZit(gpa, io, main_tree, &.{"worktrees"});
defer freeResult(gpa, &from_main);
try expectExitDiag(from_main.term, 0, from_main);
try std.testing.expect(std.mem.indexOf(u8, from_main.stdout, "tree-b") != null);
// A tree removed with plain rm prunes from the next listing.
try tmp.dir.deleteTree(io, "tree-b");
var pruned = try runZit(gpa, io, main_tree, &.{"worktrees"});
defer freeResult(gpa, &pruned);
try expectExitDiag(pruned.term, 0, pruned);
try std.testing.expect(std.mem.indexOf(u8, pruned.stdout, "tree-b") == null);
try std.testing.expect(std.mem.indexOf(u8, pruned.stdout, "main-tree") != null);
// Bare -R from an unrelated cwd: the listing answers, nothing starred.
const cwd = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd);
const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &tmp.sub_path, "main-tree" });
defer gpa.free(store_abs);
var away = std.testing.tmpDir(.{});
defer away.cleanup();
var bare = try runZit(gpa, io, away.dir, &.{ "worktrees", "-R", store_abs });
defer freeResult(gpa, &bare);
try expectExitDiag(bare.term, 0, bare);
try std.testing.expect(std.mem.indexOf(u8, bare.stdout, "main-tree") != null);
try std.testing.expect(std.mem.indexOf(u8, bare.stdout, "* ") == null);
}
test "worktrees: per-checkout verbs never see the other checkout's branch as current" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "a", .default_dir);
try tmp.dir.createDir(io, "b", .default_dir);
var a_dir = try tmp.dir.openDir(io, "a", .{});
defer a_dir.close(io);
var b_dir = try tmp.dir.openDir(io, "b", .{});
defer b_dir.close(io);
// Checkout A: root on trunk, then a `forum` branch changing f.txt. A's
// directory IS the store's home, which is what made this bug bite: a
// detached worktree's store-dir resolution lands exactly here, beside
// A's own .zit-checkout.
try initRepo(gpa, io, a_dir);
try writeFile(io, a_dir, "f.txt", "base\n");
try addManaged(gpa, io, a_dir, &.{"f.txt"});
var c1 = try runCommitEnv(gpa, io, a_dir, .{ .user = "t" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
try writeFile(io, a_dir, "f.txt", "forum\n");
var c2 = try runCommitEnv(gpa, io, a_dir, .{ .user = "t" }, &.{ "commit", "--branch", "forum", "-m", "forum" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
// Checkout B: opened at trunk on A's store, then its own `mirror` branch
// adding g.txt.
var opened = try runZit(gpa, io, b_dir, &.{ "open", "trunk", "--store=../a/.zit" });
defer freeResult(gpa, &opened);
try expectExit(opened.term, 0);
try writeFile(io, b_dir, "g.txt", "mirror\n");
var ad = try runZit(gpa, io, b_dir, &.{ "add", "g.txt" });
defer freeResult(gpa, &ad);
try expectExit(ad.term, 0);
var c3 = try runCommitEnv(gpa, io, b_dir, .{ .user = "t" }, &.{ "commit", "--branch", "mirror", "-m", "m1" });
defer freeResult(gpa, &c3);
try expectExit(c3.term, 0);
const t_ = std.testing;
// leaves: each checkout stars ITS OWN branch's leaf, never the other's.
var la = try runZit(gpa, io, a_dir, &.{"leaves"});
defer freeResult(gpa, &la);
try expectExitDiag(la.term, 0, la);
var lb = try runZit(gpa, io, b_dir, &.{"leaves"});
defer freeResult(gpa, &lb);
try expectExitDiag(lb.term, 0, lb);
var it = std.mem.splitScalar(u8, la.stdout, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "*")) try t_.expect(std.mem.endsWith(u8, line, "forum"));
}
it = std.mem.splitScalar(u8, lb.stdout, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "*")) try t_.expect(std.mem.endsWith(u8, line, "mirror"));
}
// Bare ls: B lists ITS check-in's files (f.txt AND g.txt), not A's.
var ls_b = try runZit(gpa, io, b_dir, &.{"ls"});
defer freeResult(gpa, &ls_b);
try expectExitDiag(ls_b.term, 0, ls_b);
try t_.expect(std.mem.indexOf(u8, ls_b.stdout, "g.txt") != null);
// update trunk in B: the plan's base is B's own check-in, so g.txt is
// REMOVED and f.txt reads trunk's bytes. The broken resolution planned
// from A's checkout instead and left g.txt behind.
var up = try runZit(gpa, io, b_dir, &.{ "update", "trunk" });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
{
const got = try b_dir.readFileAlloc(io, "f.txt", gpa, .limited(64));
defer gpa.free(got);
try t_.expectEqualStrings("base\n", got);
}
try t_.expectError(error.FileNotFound, b_dir.statFile(io, "g.txt", .{}));
// merge mirror in B (now on trunk): trunk has not moved since mirror
// branched, so the merge is conflict-free -- the broken resolution took
// A's forum branch as "current" and manufactured conflicts against a
// branch B never held.
var mg = try runZit(gpa, io, b_dir, &.{ "merge", "mirror" });
defer freeResult(gpa, &mg);
try expectExitDiag(mg.term, 0, mg);
{
const got = try b_dir.readFileAlloc(io, "g.txt", gpa, .limited(64));
defer gpa.free(got);
try t_.expectEqualStrings("mirror\n", got);
}
try t_.expect(std.mem.indexOf(u8, mg.stdout, "<<<<<<<") == null);
// And A never moved: still on forum, tree intact.
var sa = try runZit(gpa, io, a_dir, &.{"status"});
defer freeResult(gpa, &sa);
try expectExitDiag(sa.term, 0, sa);
try t_.expect(std.mem.indexOf(u8, sa.stdout, "tags: forum") != null);
{
const got = try a_dir.readFileAlloc(io, "f.txt", gpa, .limited(64));
defer gpa.free(got);
try t_.expectEqualStrings("forum\n", got);
}
}