Skip to content
zit Artifact [cbda251379]
Login

Artifact [cbda251379]

Artifact cbda251379f8e202d53ca392dbac029374315dcb0c3344fc4ca483a627cd80b7:


const std = @import("std");
const cli = @import("zit").cli;
const support = @import("support.zig");
const ThinHistory = support.ThinHistory;
const addManaged = support.addManaged;
const artifactNameByContent = support.artifactNameByContent;
const bareStoreOf = support.bareStoreOf;
const commitHashDup = support.commitHashDup;
const doomed_body = support.doomed_body;
const expectContains = support.expectContains;
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 kept_body = support.kept_body;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const storeBytesAlloc = support.storeBytesAlloc;
const storeHoldsArtifact = support.storeHoldsArtifact;
const storeShunRowCount = support.storeShunRowCount;
const thinBody = support.thinBody;
const thinFixture = support.thinFixture;
const thinFixtureWide = support.thinFixtureWide;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
const zit_exe = support.zit_exe;

/// A repository whose second check-in introduces content its first never had —
/// the accident `zit purge` exists for, plus a file both check-ins share, which
/// is what makes the drop set's set difference observable.
fn purgeFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, first: *[]const u8) ![]const u8 {
    try initRepo(gpa, io, dir);
    try writeFile(io, dir, "keep.txt", kept_body);
    try addManaged(gpa, io, dir, &.{"keep.txt"});
    first.* = try commitHashDup(gpa, io, dir, "root");

    try writeFile(io, dir, "swept.bin", doomed_body);
    try addManaged(gpa, io, dir, &.{"swept.bin"});
    return commitHashDup(gpa, io, dir, "the accident");
}

test "cli purge: undo waits for the store's append lock instead of writing under another writer" {
    // Undo restores by appending, and an append that does not take the lock
    // scans the same end as a concurrent writer: one of the two writes then
    // lands on top of the other with frames that are complete and valid, which
    // nothing afterwards can detect. Holding the lock from here stands in for
    // a commit or pull in progress.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var first: []const u8 = undefined;
    const leaf = try purgeFixture(gpa, io, tmp.dir, &first);
    defer gpa.free(first);
    defer gpa.free(leaf);
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "update", first });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "--force", leaf });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    const buried = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(buried);

    var held = try cli.openStoreAppend(io, tmp.dir);
    var held_open = true;
    defer if (held_open) held.close(io);

    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");
    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);
    // Through `sh` to keep its stderr in a file: the waiting notice is part
    // of what this test checks. The shell writes a line there first, as a
    // log shared with other writers would have, and the notice must land
    // after it, not over it.
    var child = try std.process.spawn(io, .{
        .argv = &.{ "/bin/sh", "-c", "exec 2>undo.err; echo 'before zit' >&2; exec \"$0\" purge undo 1", abs_exe },
        .cwd = .{ .dir = tmp.dir },
        .environ_map = &env,
        .stdout = .ignore,
        .stderr = .ignore,
    });
    var reaped = false;
    defer if (!reaped) child.kill(io);

    // Long enough for an unlocked undo to have finished its append many times
    // over. With the lock held, nothing may have been written.
    io.sleep(.{ .nanoseconds = 1500 * std.time.ns_per_ms }, .awake) catch {};
    {
        const during = try storeBytesAlloc(gpa, io, tmp.dir);
        defer gpa.free(during);
        try std.testing.expectEqualSlices(u8, buried, during);
    }
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, leaf));

    // Released, the waiting undo takes its turn and completes, having said
    // what it was waiting for: the holder is this test process.
    held.close(io);
    held_open = false;
    const term = try child.wait(io);
    reaped = true;
    try expectExit(term, 0);
    {
        var err_buf: [1024]u8 = undefined;
        const said = try tmp.dir.readFile(io, "undo.err", &err_buf);
        var want_buf: [96]u8 = undefined;
        const pid: i64 = if (@import("builtin").os.tag == .linux) std.os.linux.getpid() else std.c.getpid();
        const want = try std.fmt.bufPrint(&want_buf, "zit: waiting for the repository lock, held by process {d} on ", .{pid});
        if (!std.mem.startsWith(u8, said, "before zit\n") or std.mem.indexOf(u8, said, want) == null) {
            std.debug.print("\nundo stderr:\n{s}\n", .{said});
            return error.NoWaitingNotice;
        }
    }
    try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, leaf));
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 faults") != null);
    }
}

test "cli purge: a leaf's own content goes, shared content stays, and undo restores the store" {
    // THE WHOLE CONTRACT IN ONE PASS. A purge must (1) refuse without --force
    // leaving the store byte-identical, (2) remove the check-in AND the blobs
    // only it introduced, (3) leave a blob a retained check-in still names
    // exactly where it is — dropping that one would not free anything, it would
    // create a phantom — and (4) be undoable, because "recoverable" is the whole
    // difference between this verb and `shun`.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var first: []const u8 = undefined;
    const leaf = try purgeFixture(gpa, io, tmp.dir, &first);
    defer gpa.free(first);
    defer gpa.free(leaf);
    const swept = try artifactNameByContent(gpa, io, tmp.dir, doomed_body);
    defer gpa.free(swept);
    const shared = try artifactNameByContent(gpa, io, tmp.dir, kept_body);
    defer gpa.free(shared);

    // Stand somewhere else: the current checkout is refused with no override.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "update", first });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }

    const before = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(before);

    // (1) Refused, and byte-identical after — not merely "nothing removed".
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", leaf });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 check-in") != null);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "pass --force to proceed") != null);
        // The refusal points at the recovery path, which is what distinguishes
        // this verb's warning from shun's "irreversible".
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit purge undo") != null);
    }
    {
        const after = try storeBytesAlloc(gpa, io, tmp.dir);
        defer gpa.free(after);
        try std.testing.expectEqualSlices(u8, before, after);
    }

    // (2) and (3): the accident's blob goes, the shared blob stays.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "--reason", "swept in a build tree", "--force", leaf });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "purged:") != null);
    }
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, leaf));
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, swept));
    try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, shared));
    // No tombstone: a purge is local and recoverable, which is exactly why it
    // records nothing that would stop the content coming back from a peer.
    try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
    {
        // The retained check-in still reads, so the rewrite left no phantom.
        var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "keep.txt", first });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expectEqualStrings(kept_body, res.stdout);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 faults") != null);
    }
    {
        // The reason is what `purge list` is for: which grave holds what.
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "list" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "swept in a build tree") != null);
    }

    // (4) Undo puts the store back — byte-for-byte, since restoring is an
    // ordinary append of the same artifacts in the same order.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "undo", "1" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, leaf));
    try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, swept));
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 faults") != null);
    }
    {
        // The graveyard is emptied by the undo, so nothing is left to obliterate.
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "list" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expectEqualStrings("", res.stdout);
    }
}

test "cli purge: the current checkout and a non-leaf are both refused, applying NOTHING" {
    // The two refusals with no --force override between them, and the pin that
    // BOTH block the whole invocation rather than the offending token alone.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var first: []const u8 = undefined;
    const leaf = try purgeFixture(gpa, io, tmp.dir, &first);
    defer gpa.free(first);
    defer gpa.free(leaf);

    const before = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(before);

    // Standing on the leaf: purging it is refused even with --force.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "--force", leaf });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "the current checkout") != null);
    }
    // Its parent has a child, so it is refused for the other reason — naming it
    // alone would orphan the leaf.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "--force", first });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not a leaf") != null);
    }
    // Naming BOTH still refuses: the leaf is the checkout, and one blocked
    // target stops the whole plan rather than the possible half of it.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "--force", first, leaf });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "the current checkout") != null);
    }

    const after = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(after);
    try std.testing.expectEqualSlices(u8, before, after);
}

/// Thin versions 2 and 4 of `dir`'s five-version n.md and check the readers.
fn thinAndCheckReaders(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, h: *const ThinHistory) !void {
    {
        var res = try runZit(gpa, io, dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &h.blobs[1], h.blobs[3][0..12] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, "purged: 2 file versions");
    }
    {
        var res = try runZit(gpa, io, dir, &.{"verify"});
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, " 0 faults");
    }
    // The kept versions read byte-exact, including those stored as deltas of a
    // removed one.
    for ([_]usize{ 0, 2, 4 }) |i| {
        var res = try runZit(gpa, io, dir, &.{ "cat", "n.md", h.checkins[i] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        const want = try thinBody(gpa, i + 1);
        defer gpa.free(want);
        try std.testing.expectEqualStrings(want, res.stdout);
    }
    // Every reader that touches version 4 says it was purged, exit 8.
    const touching = [_][]const []const u8{
        &.{ "cat", "n.md", h.checkins[3] },
        &.{ "diff", "--from", h.checkins[2], "--to", h.checkins[3] },
        &.{ "diff", "--from", h.checkins[3], "--to", h.checkins[4] },
        &.{ "annotate", "n.md", h.checkins[4] },
    };
    for (touching) |argv| {
        var res = try runZit(gpa, io, dir, argv);
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
        try expectContains(res.stderr, "purged");
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "@@") == null);
    }
    // The file's history still lists the check-in that held version 4.
    {
        var res = try runZit(gpa, io, dir, &.{ "log", "trunk", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, h.checkins[3]);
    }
}

test "cli purge artifacts: thinned versions read as purged (exit 8), kept ones byte-exact, undo restores" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try thinFixture(gpa, io, tmp.dir, &.{"init"});
    defer h.deinit(gpa);
    try thinAndCheckReaders(gpa, io, tmp.dir, &h);

    // Naming an already thinned version again is reported and changes nothing.
    {
        const before = try storeBytesAlloc(gpa, io, tmp.dir);
        defer gpa.free(before);
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", &h.blobs[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, "already purged");
        const after = try storeBytesAlloc(gpa, io, tmp.dir);
        defer gpa.free(after);
        try std.testing.expectEqualSlices(u8, before, after);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "list" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, "thinning");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "undo", "1" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "n.md", h.checkins[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        const want = try thinBody(gpa, 4);
        defer gpa.free(want);
        try std.testing.expectEqualStrings(want, res.stdout);
    }
    // Restored versions are no longer thinned: the list is emptied and gone.
    try std.testing.expect(!fileExists(io, tmp.dir, ".zit-thinned"));
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
}

test "cli purge artifacts: obliterated versions stay purged, across a pack" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try thinFixture(gpa, io, tmp.dir, &.{"init"});
    defer h.deinit(gpa);
    try thinAndCheckReaders(gpa, io, tmp.dir, &h);
    for ([_][]const []const u8{ &.{ "purge", "obliterate", "--force", "1" }, &.{"pack"} }) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "n.md", h.checkins[1] });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 8, res);
    try expectContains(res.stderr, "purged");
}

test "cli purge artifacts: open leaves, the checkout and structure are refused, applying nothing" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try thinFixture(gpa, io, tmp.dir, &.{"init"});
    defer h.deinit(gpa);

    // A branch off version 2 whose tip still holds version 2's n.md: that
    // leaf's blob is refused although trunk moved on.
    {
        var up = try runZit(gpa, io, tmp.dir, &.{ "update", h.checkins[1] });
        defer freeResult(gpa, &up);
        try expectExitDiag(up.term, 0, up);
        try writeFile(io, tmp.dir, "keep.txt", "changed on the side branch\n");
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "side", "--branch", "side" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        var back = try runZit(gpa, io, tmp.dir, &.{ "update", "trunk" });
        defer freeResult(gpa, &back);
        try expectExitDiag(back.term, 0, back);
    }
    const before = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(before);

    {
        // Version 5 is trunk's tip and the checkout; version 2 is the side tip.
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", &h.blobs[4] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "the current checkout");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", &h.blobs[1], &h.blobs[2] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "at open leaf");
        try expectContains(res.stderr, "nothing was removed");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", h.checkins[2] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "not a file version");
        try expectContains(res.stderr, "purge checkins");
    }
    {
        // A dry run lists what would go and what is refused, and writes nothing.
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--dry-run", &h.blobs[2] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, "remove ");
    }
    const after = try storeBytesAlloc(gpa, io, tmp.dir);
    defer gpa.free(after);
    try std.testing.expectEqualSlices(u8, before, after);
    try std.testing.expect(!fileExists(io, tmp.dir, ".zit-thinned"));
}

test "cli purge artifacts: -R thins a bare store; purge checkins refuses -R" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "src", .default_dir);
    var src = try tmp.dir.openDir(io, "src", .{});
    defer src.close(io);
    var h = try thinFixture(gpa, io, src, &.{"init"});
    defer h.deinit(gpa);
    try bareStoreOf(gpa, io, src, tmp.dir, "s.zit");
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "-R", "s.zit", "--force", &h.blobs[1], &h.blobs[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    {
        var bare = try tmp.dir.openDir(io, "s.zit", .{});
        defer bare.close(io);
        try std.testing.expect(fileExists(io, bare, ".zit-thinned"));
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "-R", "s.zit", "n.md", h.checkins[1] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
    }
    {
        // The bare store's open leaf is still guarded.
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "-R", "s.zit", "--force", &h.blobs[4] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "at open leaf");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "checkins", "-R", "s.zit", "--force", h.checkins[4] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 2, res);
        try expectContains(res.stderr, "-R is not supported by 'purge checkins'");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "list", "-R", "s.zit" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectContains(res.stdout, "1 ");
    }
}

test "cli purge artifacts: a sub-manifest store thins and keeps every tip whole" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try thinFixtureWide(gpa, io, tmp.dir, &.{ "init", "--submanifest", "3" }, true);
    defer h.deinit(gpa);
    {
        // The guard must see files through the tip's sub-manifest nodes.
        var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", h.checkins[4], "--raw" });
        defer freeResult(gpa, &raw);
        try expectExitDiag(raw.term, 0, raw);
        try expectContains(raw.stdout, "\nS ");
    }
    // A side branch off version 2 whose tip still holds version 2's n.md.
    {
        var up = try runZit(gpa, io, tmp.dir, &.{ "update", h.checkins[1] });
        defer freeResult(gpa, &up);
        try expectExitDiag(up.term, 0, up);
        try writeFile(io, tmp.dir, "d2/f2", "changed on the side branch\n");
        var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "side", "--branch", "side" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        var back = try runZit(gpa, io, tmp.dir, &.{ "update", "trunk" });
        defer freeResult(gpa, &back);
        try expectExitDiag(back.term, 0, back);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", &h.blobs[1] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "at open leaf");
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", &h.blobs[4] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "the current checkout");
    }
    {
        // Closing the side branch releases version 2.
        var res = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "branch", "close", "side" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try thinAndCheckReaders(gpa, io, tmp.dir, &h);
}

test "cli purge artifacts: a pull from a peer holding a thinned version does not bring it back" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "origin", .default_dir);
    var origin = try tmp.dir.openDir(io, "origin", .{});
    defer origin.close(io);
    var h = try thinFixture(gpa, io, origin, &.{"init"});
    defer h.deinit(gpa);

    const port = try freshLoopbackPort(io);
    var server = try spawnServe(gpa, io, origin, 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 res = try runZit(gpa, io, tmp.dir, &.{ "clone", url, "local" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    var local = try tmp.dir.openDir(io, "local", .{});
    defer local.close(io);
    try thinAndCheckReaders(gpa, io, local, &h);

    // The origin still holds versions 2 and 4: the pull neither fetches them
    // nor reports them as artifacts it did not obtain.
    {
        var res = try runZit(gpa, io, local, &.{ "pull", url });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "did not obtain") == null);
    }
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, local, &h.blobs[3]));
    {
        var res = try runZit(gpa, io, local, &.{ "cat", "n.md", h.checkins[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
    }

    // Served, the thinned store neither asks a pushing peer for them nor
    // stores one: the origin pushes everything it has and nothing comes back.
    const lport = try freshLoopbackPort(io);
    var local_server = try spawnServe(gpa, io, local, lport);
    defer local_server.kill(io);
    try waitForListen(io, lport);
    const lurl = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{lport});
    defer gpa.free(lurl);
    {
        var res = try runZit(gpa, io, origin, &.{ "push", lurl });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, local, &h.blobs[1]));
    try std.testing.expect(!try storeHoldsArtifact(gpa, io, local, &h.blobs[3]));
}