Skip to content
zit Artifact [df0a103751]
Login

Artifact [df0a103751]

Artifact df0a103751336cc7f7e333c65dd8edba1f8939e91e3eb98da4d959d54fc972cb:


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

test "cli stash: snapshot keeps the tree, diff shows hunks, apply --merge folds into a dirty file (exit 5 on conflict), goto moves to the entry's check-in" {
    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, "f", "a\nb\nc\nd\ne\n");
    try addManaged(gpa, io, tmp.dir, &.{"f"});
    var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "c1" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);

    try writeFile(io, tmp.dir, "f", "a\nb\nc\nd\nE\n");
    var snap = try runZit(gpa, io, tmp.dir, &.{ "stash", "snapshot", "-m", "snap" });
    defer freeResult(gpa, &snap);
    try expectExitDiag(snap.term, 0, snap);
    try expectFileContent(gpa, io, tmp.dir, "f", "a\nb\nc\nd\nE\n"); // untouched

    var d = try runZit(gpa, io, tmp.dir, &.{ "stash", "diff" });
    defer freeResult(gpa, &d);
    try expectExitDiag(d.term, 0, d);
    try std.testing.expect(std.mem.indexOf(u8, d.stdout, "-e\n+E\n") != null);

    // A dirty file: plain apply refuses; --merge folds a change to a distant
    // line in cleanly; a colliding change leaves markers and exits 5.
    try writeFile(io, tmp.dir, "f", "A\nb\nc\nd\ne\n");
    var refuse = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply" });
    defer freeResult(gpa, &refuse);
    try expectExit(refuse.term, 1);
    var folded = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply", "--merge" });
    defer freeResult(gpa, &folded);
    try expectExitDiag(folded.term, 0, folded);
    try expectFileContent(gpa, io, tmp.dir, "f", "A\nb\nc\nd\nE\n");
    try writeFile(io, tmp.dir, "f", "a\nb\nc\nd\nX\n");
    var clash = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply", "--merge" });
    defer freeResult(gpa, &clash);
    try expectExit(clash.term, 5);
    try std.testing.expect(std.mem.indexOf(u8, clash.stderr, "conflict f") != null);
    var lst = try runZit(gpa, io, tmp.dir, &.{ "stash", "list" });
    defer freeResult(gpa, &lst);
    try std.testing.expect(std.mem.indexOf(u8, lst.stdout, "snap") != null); // kept

    // goto: from a later check-in, back to the entry's own and applied.
    var rv = try runZit(gpa, io, tmp.dir, &.{"revert"});
    defer freeResult(gpa, &rv);
    try expectExit(rv.term, 0);
    var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "c2", "--allow-empty" });
    defer freeResult(gpa, &c2);
    try expectExitDiag(c2.term, 0, c2);
    var gt = try runZit(gpa, io, tmp.dir, &.{ "stash", "goto" });
    defer freeResult(gpa, &gt);
    try expectExitDiag(gt.term, 0, gt);
    try expectFileContent(gpa, io, tmp.dir, "f", "a\nb\nc\nd\nE\n");
    const c1_hash = try checkinHash(c1.stdout);
    var st = try runZit(gpa, io, tmp.dir, &.{"status"});
    defer freeResult(gpa, &st);
    try std.testing.expect(std.mem.indexOf(u8, st.stdout, cli.shortHash(c1_hash)) != null);
}

test "cli stash: save captures A/M/D and restores the check-in; apply is fail-closed; pop round-trips exactly" {
    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", "keep\n");
    try writeFile(io, tmp.dir, "b.txt", "drop\n");
    try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
    const c1 = try commitHashDup(gpa, io, tmp.dir, "base");
    defer gpa.free(c1);

    // Dirty the tree three ways: edit, rm, add.
    try writeFile(io, tmp.dir, "a.txt", "edited\n");
    {
        var rm = try runZit(gpa, io, tmp.dir, &.{ "rm", "b.txt" });
        defer freeResult(gpa, &rm);
        try expectExit(rm.term, 0);
    }
    try writeFile(io, tmp.dir, "c.txt", "brand-new\n");
    try addManaged(gpa, io, tmp.dir, &.{"c.txt"});

    // Save: entry recorded, tree restored to the check-in exactly.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "-m", "wip" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "stashed a.txt, b.txt, c.txt as stash 1") != null);
    }
    try expectFileContent(gpa, io, tmp.dir, "a.txt", "keep\n");
    try expectFileContent(gpa, io, tmp.dir, "b.txt", "drop\n");
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "c.txt", .{}));
    {
        var st = try runZit(gpa, io, tmp.dir, &.{"status"});
        defer freeResult(gpa, &st);
        try expectExit(st.term, 0);
        try std.testing.expect(std.mem.indexOf(u8, st.stdout, "nothing to report") != null);
    }
    {
        var show = try runZit(gpa, io, tmp.dir, &.{ "stash", "show" });
        defer freeResult(gpa, &show);
        try expectExit(show.term, 0);
        try std.testing.expectEqualStrings("M a.txt\nD b.txt\nA c.txt\n", show.stdout);
    }

    // Fail-closed apply: a dirty target refuses before anything moves.
    try writeFile(io, tmp.dir, "a.txt", "conflicting\n");
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not clean") != null);
        // Nothing moved: b.txt intact, c.txt still absent.
        try expectFileContent(gpa, io, tmp.dir, "b.txt", "drop\n");
        try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "c.txt", .{}));
    }
    try writeFile(io, tmp.dir, "a.txt", "keep\n");

    // Pop: the exact working state returns, entry drops, sidecar vanishes.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "pop" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try expectFileContent(gpa, io, tmp.dir, "a.txt", "edited\n");
    try expectFileContent(gpa, io, tmp.dir, "c.txt", "brand-new\n");
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "b.txt", .{}));
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".zit-stash", .{}));

    // ...and committing the restored state carries exactly the popped set.
    const c2 = try commitHashDup(gpa, io, tmp.dir, "resumed");
    defer gpa.free(c2);
    var ls = try runZit(gpa, io, tmp.dir, &.{ "ls", c2 });
    defer freeResult(gpa, &ls);
    try expectExit(ls.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "a.txt\n") != null);
    try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "c.txt\n") != null);
    try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "b.txt") == null);
}

test "cli stash: apply refuses a checkout positioned at a different check-in" {
    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"});
    const c1 = try commitHashDup(gpa, io, tmp.dir, "first");
    defer gpa.free(c1);
    try writeFile(io, tmp.dir, "a.txt", "wip\n");
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"stash"});
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try writeFile(io, tmp.dir, "a.txt", "two\n");
    const c2 = try commitHashDup(gpa, io, tmp.dir, "second");
    defer gpa.free(c2);
    var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, res.stderr, "update there first") != null);
}

test "cli stash: the sidecar is a reserved name, so stashed work cannot be committed" {
    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, "f.txt", "v1\n");
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "add", "f.txt" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "commit", "-m", "base", "--user", "t" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
    }

    try writeFile(io, tmp.dir, "f.txt", "v2\n");
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "-m", "wip" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
    }

    // `.zit-stash` carries the full contents of work deliberately set aside.
    // Before it was reserved, `extras` listed it and `add` accepted it, so an
    // ordinary `add`/`addremove` could put that content in a check-in and sync
    // it to every peer — recoverable only by shunning.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"extras"});
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, ".zit-stash") == null);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "add", ".zit-stash" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "control file") != null);
    }
    // Reserving the name must not disturb the verb that owns it.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "stash", "apply" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
    }
    const f = try readFile(gpa, io, tmp.dir, "f.txt");
    defer gpa.free(f);
    try std.testing.expectEqualStrings("v2\n", f);
}