Skip to content
zit Artifact [c90809560d]
Login

Artifact [c90809560d]

Artifact c90809560deba9c1eee8ce6b2e51c77ab5ae200651e34af03d2e47034ca5fb3b:


const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const support = @import("support.zig");
const addManaged = support.addManaged;
const chmodFile = support.chmodFile;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const storeHoldsArtifact = support.storeHoldsArtifact;
const worktreeStorm = support.worktreeStorm;
const writeFile = support.writeFile;
const zit_exe = support.zit_exe;

test "store lock: openStoreAppend holds the store's writer lock and releases it on close" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    // While an appender's handle is open, the lock file exists and a second
    // writer is excluded: in another process it waits, and in this one it is
    // refused at once rather than waiting for itself. The refusal is asked of
    // the lock directly with its notice discarded: through `openStoreAppend`
    // it prints the internal-error line a real program would, onto the test
    // runner's stderr, where it reads as a failure of some unnamed test.
    var appender = try cli.openStoreAppend(io, tmp.dir);
    var appender_open = true;
    defer if (appender_open) appender.close(io);
    try tmp.dir.access(io, ".zit.lock", .{});
    var sink = std.Io.Writer.Discarding.init(&.{});
    try std.testing.expectError(error.HeldByThisProcess, zit.storelock.acquire(io, tmp.dir, ".zit.lock", 5_000, &sink.writer));

    // A plain read takes no lock and is never excluded (verify's promise).
    var reader = try tmp.dir.openFile(io, ".zit", .{});
    reader.close(io);

    // Close releases: the lock file is gone and the next writer gets in.
    appender.close(io);
    appender_open = false;
    try std.testing.expectError(error.FileNotFound, tmp.dir.access(io, ".zit.lock", .{}));
    var again = try cli.openStoreAppend(io, tmp.dir);
    again.close(io);
}

test "store lock: a writer killed while holding the lock does not block the next one" {
    // The cost of a lock file over `flock`: it outlives a crashed holder. A
    // lock naming a process that no longer exists is broken by the next writer.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "a\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});

    // What a writer killed mid-append leaves behind: its lock, naming it.
    var host_buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
    const host = try std.posix.gethostname(&host_buf);
    var rec_buf: [256]u8 = undefined;
    const rec = try std.fmt.bufPrint(&rec_buf, "pid={d} host={s} time=1\n", .{ std.math.maxInt(std.posix.pid_t), host });
    try tmp.dir.writeFile(io, .{ .sub_path = ".zit.lock", .data = rec });

    var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "after a crash" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    try std.testing.expectError(error.FileNotFound, tmp.dir.access(io, ".zit.lock", .{}));
    try std.testing.expectError(error.FileNotFound, tmp.dir.access(io, ".zit.lock.break", .{}));
}

/// One racing committer for the worktree stress: `count` commits of distinct
/// files from `dir`, every one under `--allow-fork` because racing tips
/// legitimately fork — the race under test is the STORE lock, not branch
/// topology. Any nonzero exit is recorded, never asserted mid-thread.
fn countFailure(failures: *std.atomic.Value(u32), term: std.process.Child.Term) void {
    switch (term) {
        .exited => |c| if (c != 0) {
            _ = failures.fetchAdd(1, .monotonic);
        },
        else => _ = failures.fetchAdd(1, .monotonic),
    }
}

/// `count` tags named `storm-tag-N` on `target`, one `zit tag add` each.
fn tagStorm(io: std.Io, dir: std.Io.Dir, target: []const u8, count: usize, failures: *std.atomic.Value(u32)) void {
    const gpa = std.heap.page_allocator;
    var i: usize = 0;
    while (i < count) : (i += 1) {
        var name_buf: [64]u8 = undefined;
        const name = std.fmt.bufPrint(&name_buf, "storm-tag-{d}", .{i}) catch unreachable;
        var res = runZit(gpa, io, dir, &.{ "tag", "add", name, target }) catch {
            _ = failures.fetchAdd(1, .monotonic);
            return;
        };
        defer freeResult(gpa, &res);
        countFailure(failures, res.term);
    }
}

/// `count` user rows, logins `storm-user-N`, one `zit config merge` each.
fn userStorm(io: std.Io, dir: std.Io.Dir, count: usize, failures: *std.atomic.Value(u32)) void {
    const gpa = std.heap.page_allocator;
    var i: usize = 0;
    while (i < count) : (i += 1) {
        var payload_buf: [128]u8 = undefined;
        const payload = std.fmt.bufPrint(&payload_buf, "1700000000 'storm-user-{d}' pw '' cap 'v' info ''", .{i}) catch unreachable;
        var file_buf: [192]u8 = undefined;
        const file = std.fmt.bufPrint(&file_buf, "config /user {d}\n{s}\n", .{ payload.len, payload }) catch unreachable;
        var fname_buf: [48]u8 = undefined;
        const fname = std.fmt.bufPrint(&fname_buf, "users-{d}.txt", .{i}) catch unreachable;
        writeFile(io, dir, fname, file) catch {
            _ = failures.fetchAdd(1, .monotonic);
            return;
        };
        var res = runZit(gpa, io, dir, &.{ "config", "merge", fname }) catch {
            _ = failures.fetchAdd(1, .monotonic);
            return;
        };
        defer freeResult(gpa, &res);
        countFailure(failures, res.term);
    }
}

test "store lock: commits, tags and user-table writes racing on one store all land" {
    // Four writers of three different kinds on one store at once: commits from
    // two worktrees, tags, and user-table merges. Every write must survive,
    // not merely leave a log that verifies. An unserialised append writes over
    // another writer's frames with frames that are themselves valid, so
    // `verify` alone cannot tell a clean race from a lost write; counting what
    // landed can.
    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"});
    const seed = try commitHashDup(gpa, io, main_dir, "seed");
    defer gpa.free(seed);
    var opened = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main" });
    defer freeResult(gpa, &opened);
    try expectExit(opened.term, 0);

    const per_writer = 5;
    var failures = std.atomic.Value(u32).init(0);
    var a = try std.Thread.spawn(.{}, worktreeStorm, .{ io, main_dir, "main-file", per_writer, &failures });
    var b = try std.Thread.spawn(.{}, worktreeStorm, .{ io, wt_dir, "wt-file", per_writer, &failures });
    var c = try std.Thread.spawn(.{}, tagStorm, .{ io, main_dir, seed, per_writer, &failures });
    var d = try std.Thread.spawn(.{}, userStorm, .{ io, wt_dir, per_writer, &failures });
    a.join();
    b.join();
    c.join();
    d.join();
    try std.testing.expectEqual(@as(u32, 0), failures.load(.monotonic));

    var timeline = try runZit(gpa, io, main_dir, &.{ "timeline", "-n", "200" });
    defer freeResult(gpa, &timeline);
    try expectExitDiag(timeline.term, 0, timeline);
    // Counted by comment alone: the checkout's own check-in carries a
    // `*CURRENT*` marker between the hash and the comment.
    const main_landed = std.mem.count(u8, timeline.stdout, " main-file-");
    const wt_landed = std.mem.count(u8, timeline.stdout, " wt-file-");
    if (main_landed != per_writer or wt_landed != per_writer) std.debug.print("\ntimeline:\n{s}\n", .{timeline.stdout});
    try std.testing.expectEqual(@as(usize, per_writer), main_landed);
    try std.testing.expectEqual(@as(usize, per_writer), wt_landed);

    var tags = try runZit(gpa, io, main_dir, &.{ "tag", "list" });
    defer freeResult(gpa, &tags);
    try expectExitDiag(tags.term, 0, tags);
    try std.testing.expectEqual(@as(usize, per_writer), std.mem.count(u8, tags.stdout, "storm-tag-"));

    var users = try runZit(gpa, io, main_dir, &.{"users"});
    defer freeResult(gpa, &users);
    try expectExitDiag(users.term, 0, users);
    try std.testing.expectEqual(@as(usize, per_writer), std.mem.count(u8, users.stdout, "storm-user-"));

    var ver = try runZit(gpa, io, main_dir, &.{"verify"});
    defer freeResult(gpa, &ver);
    try expectExitDiag(ver.term, 0, ver);
    try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "0 faults") != null);
}

/// An editor that stands in for a person writing a comment: it says it has
/// started, then waits until the test releases it (at most 30s), then writes
/// the comment. Runs with the checkout root as cwd.
const slow_editor =
    "#!/bin/sh\n" ++
    ": > in-editor\n" ++
    "i=0; while [ ! -e release ] && [ $i -lt 300 ]; do sleep 0.1; i=$((i+1)); done\n" ++
    "printf 'from a slow editor\\n' > \"$1\"\n";

/// Starts `zit commit --editor ./slow-ed.sh` in `dir` and returns once the
/// editor is open. Its stdout and stderr go to commit.out and commit.err.
fn startCommitInSlowEditor(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !std.process.Child {
    return startCommitInSlowEditorWith(gpa, io, dir, "");
}

/// `flags` is appended to the commit's argv as shell words.
fn startCommitInSlowEditorWith(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, comptime flags: []const u8) !std.process.Child {
    try writeFile(io, dir, "slow-ed.sh", slow_editor);
    try chmodFile(io, dir, "slow-ed.sh", 0o755);
    var env = std.process.Environ.Map.init(gpa);
    defer env.deinit();
    try env.put("TZ", "UTC");
    try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
    try env.put("PATH", "/usr/bin:/bin");
    try env.put("USER", "alice");
    const cwd_abs = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd_abs);
    const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
    defer gpa.free(abs_exe);
    var child = try std.process.spawn(io, .{
        .argv = &.{ "/bin/sh", "-c", "exec \"$0\" commit --editor ./slow-ed.sh" ++ flags ++ " >commit.out 2>commit.err", abs_exe },
        .cwd = .{ .dir = dir },
        .environ_map = &env,
        .stdout = .ignore,
        .stderr = .ignore,
    });
    errdefer child.kill(io);
    var waited: u32 = 0;
    while (true) : (waited += 1) {
        if (dir.statFile(io, "in-editor", .{})) |_| break else |_| {}
        if (waited == 200) return error.EditorNeverOpened;
        io.sleep(.fromMilliseconds(50), .awake) catch {};
    }
    return child;
}

test "store lock: a commit waiting in its editor holds no lock, and other writers go ahead" {
    // The comment is composed after the tree is captured, and a person can
    // take as long as they like. Holding the writer lock through it turned
    // every other writer away after a minute: a tag, a pull, a push to a
    // server on this checkout.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "one\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
    var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &root);
    try expectExitDiag(root.term, 0, root);
    try writeFile(io, tmp.dir, "a.txt", "two\n");

    var child = try startCommitInSlowEditor(gpa, io, tmp.dir);
    var reaped = false;
    defer if (!reaped) child.kill(io);

    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".zit.lock", .{}));
    var tag = try runZit(gpa, io, tmp.dir, &.{ "tag", "add", "rel-1", "trunk" });
    defer freeResult(gpa, &tag);
    try expectExitDiag(tag.term, 0, tag);

    // The store grew under the prepared commit, but its parent is still the
    // tip, so it publishes.
    try writeFile(io, tmp.dir, "release", "");
    const term = try child.wait(io);
    reaped = true;
    const said = try readFile(gpa, io, tmp.dir, "commit.err");
    defer gpa.free(said);
    expectExit(term, 0) catch |e| {
        std.debug.print("\ncommit stderr:\n{s}\n", .{said});
        return e;
    };
    var tl = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "2" });
    defer freeResult(gpa, &tl);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "from a slow editor") != null);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "rel-1") != null);
    var v = try runZit(gpa, io, tmp.dir, &.{"verify"});
    defer freeResult(gpa, &v);
    try expectExitDiag(v.term, 0, v);
}

test "store lock: a commit whose parent moved while its editor was open is refused, writes nothing, and keeps the comment" {
    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, "a.txt", "one\n");
    try addManaged(gpa, io, main_dir, &.{"a.txt"});
    var root = try runCommitEnv(gpa, io, main_dir, .{ .user = "alice" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &root);
    try expectExitDiag(root.term, 0, root);
    var opened = try runZit(gpa, io, wt_dir, &.{ "open", "--store=../main" });
    defer freeResult(gpa, &opened);
    try expectExitDiag(opened.term, 0, opened);

    try writeFile(io, main_dir, "a.txt", "two\n");
    var child = try startCommitInSlowEditor(gpa, io, main_dir);
    var reaped = false;
    defer if (!reaped) child.kill(io);

    // Another worktree of the same store lands a check-in on trunk.
    try writeFile(io, wt_dir, "c.txt", "c\n");
    try addManaged(gpa, io, wt_dir, &.{"c.txt"});
    var other = try runCommitEnv(gpa, io, wt_dir, .{ .user = "bob" }, &.{ "commit", "-m", "landed first" });
    defer freeResult(gpa, &other);
    try expectExitDiag(other.term, 0, other);

    // The prepared check-in names the old tip as its parent. Publishing it
    // would fork trunk silently, and re-parenting it would change bytes that
    // may be signed, so it is refused.
    try writeFile(io, main_dir, "release", "");
    const term = try child.wait(io);
    reaped = true;
    try expectExit(term, 1);
    const said = try readFile(gpa, io, main_dir, "commit.err");
    defer gpa.free(said);
    try std.testing.expect(std.mem.indexOf(u8, said, "not the tip of branch 'trunk'") != null);
    try std.testing.expect(std.mem.indexOf(u8, said, "your comment is kept in .zit-ci-comment") != null);
    const kept = try readFile(gpa, io, main_dir, ".zit-ci-comment");
    defer gpa.free(kept);
    try std.testing.expectEqualStrings("from a slow editor\n", kept);

    var tl = try runZit(gpa, io, main_dir, &.{ "timeline", "-n", "5" });
    defer freeResult(gpa, &tl);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "landed first") != null);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "from a slow editor") == null);
    // Nothing of the refused commit was stored: its edited blob included.
    var two_digest: [32]u8 = undefined;
    std.crypto.hash.sha3.Sha3_256.hash("two\n", &two_digest, .{});
    const two_name = std.fmt.bytesToHex(two_digest, .lower);
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, main_dir, &two_name));
    var v = try runZit(gpa, io, main_dir, &.{"verify"});
    defer freeResult(gpa, &v);
    try expectExitDiag(v.term, 0, v);
}

test "store lock: a second commit in the same checkout lands while the first waits in its editor; the first is refused, never forks" {
    // An app's autosave and an agent commit in one checkout. The commit that
    // publishes second must check the parent its manifest names, not the
    // checkout the first one just advanced.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "one\n");
    try writeFile(io, tmp.dir, "b.txt", "one\n");
    try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
    var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &root);
    try expectExitDiag(root.term, 0, root);

    try writeFile(io, tmp.dir, "a.txt", "two\n");
    var child = try startCommitInSlowEditor(gpa, io, tmp.dir);
    var reaped = false;
    defer if (!reaped) child.kill(io);

    try writeFile(io, tmp.dir, "b.txt", "two\n");
    var other = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "bob" }, &.{ "commit", "-m", "landed first", "b.txt" });
    defer freeResult(gpa, &other);
    try expectExitDiag(other.term, 0, other);

    try writeFile(io, tmp.dir, "release", "");
    const term = try child.wait(io);
    reaped = true;
    try expectExit(term, 1);
    const said = try readFile(gpa, io, tmp.dir, "commit.err");
    defer gpa.free(said);
    try std.testing.expect(std.mem.indexOf(u8, said, "another commit moved this checkout") != null);

    var lv = try runZit(gpa, io, tmp.dir, &.{"leaves"});
    defer freeResult(gpa, &lv);
    try std.testing.expect(std.mem.indexOf(u8, lv.stdout, "(2)") == null);
    var tl = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", "5" });
    defer freeResult(gpa, &tl);
    try std.testing.expect(std.mem.indexOf(u8, tl.stdout, "from a slow editor") == null);
    var v = try runZit(gpa, io, tmp.dir, &.{"verify"});
    defer freeResult(gpa, &v);
    try expectExitDiag(v.term, 0, v);
}

test "store lock: a commit whose checkout moved while its editor was open is refused even with --allow-fork, and the checkout keeps the other commit" {
    // --allow-fork lifts the fork guard, but the checkout is not this
    // commit's to move: another commit already advanced it.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "one\n");
    try writeFile(io, tmp.dir, "b.txt", "one\n");
    try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
    var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &root);
    try expectExitDiag(root.term, 0, root);

    try writeFile(io, tmp.dir, "a.txt", "two\n");
    var child = try startCommitInSlowEditorWith(gpa, io, tmp.dir, " --allow-fork");
    var reaped = false;
    defer if (!reaped) child.kill(io);

    try writeFile(io, tmp.dir, "b.txt", "two\n");
    var other = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "bob" }, &.{ "commit", "-m", "landed first", "b.txt" });
    defer freeResult(gpa, &other);
    try expectExitDiag(other.term, 0, other);

    try writeFile(io, tmp.dir, "release", "");
    const term = try child.wait(io);
    reaped = true;
    try expectExit(term, 1);
    const said = try readFile(gpa, io, tmp.dir, "commit.err");
    defer gpa.free(said);
    try std.testing.expect(std.mem.indexOf(u8, said, "another commit moved this checkout") != null);

    var lv = try runZit(gpa, io, tmp.dir, &.{"leaves"});
    defer freeResult(gpa, &lv);
    try std.testing.expect(std.mem.indexOf(u8, lv.stdout, "(2)") == null);
    // b.txt's edit is in the checkout's version, so it is not shown as edited.
    var st = try runZit(gpa, io, tmp.dir, &.{"status"});
    defer freeResult(gpa, &st);
    try std.testing.expect(std.mem.indexOf(u8, st.stdout, "b.txt") == null);
    var v = try runZit(gpa, io, tmp.dir, &.{"verify"});
    defer freeResult(gpa, &v);
    try expectExitDiag(v.term, 0, v);
}