const std = @import("std");
const checkout = @import("zit").checkout;
const artifact = @import("zit").artifact;
const File = std.Io.File;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendArtifact = support.appendArtifact;
const appendDiffManifest = support.appendDiffManifest;
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;
// --- revert (artifact §8.4 effective file + §7.2 verified blob) -------------
test "cli revert artifact §8.4/§7.2: a modified managed file is restored to its check-in blob byte-for-byte" {
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 original = "line one\nline two\n";
try writeFile(io, tmp.dir, "doc.txt", original);
try addManaged(gpa, io, tmp.dir, &.{"doc.txt"});
var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
// A working edit that revert must discard.
try writeFile(io, tmp.dir, "doc.txt", "line one\nCHANGED\nline three\n");
var res = try runZit(gpa, io, tmp.dir, &.{"revert"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("REVERT doc.txt\n", res.stdout);
const restored = try readFile(gpa, io, tmp.dir, "doc.txt");
defer gpa.free(restored);
try std.testing.expectEqualStrings(original, restored);
// The tree quiesces: after revert, diff is clean.
var diff = try runZit(gpa, io, tmp.dir, &.{ "diff", "--brief" });
defer freeResult(gpa, &diff);
try expectExit(diff.term, 0);
try std.testing.expectEqualStrings("", diff.stdout);
}
test "cli revert §4.7.6: a PATH-scoped revert restores only the named file, leaving other edits" {
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", "a-original\n");
try writeFile(io, tmp.dir, "b", "b-original\n");
try addManaged(gpa, io, tmp.dir, &.{ "a", "b" });
var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
// Edit both; revert only 'a'.
try writeFile(io, tmp.dir, "a", "a-edited\n");
try writeFile(io, tmp.dir, "b", "b-edited\n");
var res = try runZit(gpa, io, tmp.dir, &.{ "revert", "a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("REVERT a\n", res.stdout);
const a_after = try readFile(gpa, io, tmp.dir, "a");
defer gpa.free(a_after);
const b_after = try readFile(gpa, io, tmp.dir, "b");
defer gpa.free(b_after);
try std.testing.expectEqualStrings("a-original\n", a_after); // restored
try std.testing.expectEqualStrings("b-edited\n", b_after); // untouched
}
test "cli revert §4.7.6: a managed file absent from the effective set is reported and skipped, never fabricated" {
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, "committed", "c\n");
try addManaged(gpa, io, tmp.dir, &.{"committed"});
var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
// 'staged' is managed but never committed — it is not in the check-in's
// effective set, so revert cannot restore it: reported, left as-is.
try writeFile(io, tmp.dir, "staged", "s\n");
try addManaged(gpa, io, tmp.dir, &.{"staged"});
var res = try runZit(gpa, io, tmp.dir, &.{ "revert", "staged" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0); // a skip is informational, not a hard error
try std.testing.expectEqualStrings("", res.stdout); // nothing reverted
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "not in the current check-in") != null);
// The working file is left exactly as it was — never fabricated/removed.
const kept = try readFile(gpa, io, tmp.dir, "staged");
defer gpa.free(kept);
try std.testing.expectEqualStrings("s\n", kept);
}
test "cli revert artifact §4.7.6-m5: a deleted executable file is restored with its exec bit" {
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, "script", "#!/bin/sh\necho hi\n");
try addManaged(gpa, io, tmp.dir, &.{"script"});
var root = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
// chmod +x and record the executable mode in a child check-in.
{
var f = try tmp.dir.openFile(io, "script", .{ .mode = .read_write });
defer f.close(io);
try f.setPermissions(io, File.Permissions.executable_file);
}
var exe = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "chmod +x" });
defer freeResult(gpa, &exe);
try expectExit(exe.term, 0);
// Delete the file, then revert recreates it from the check-in.
try tmp.dir.deleteFile(io, "script");
var res = try runZit(gpa, io, tmp.dir, &.{ "revert", "script" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("REVERT script\n", res.stdout);
// Content restored, and — the §4.7.6-m5 point — the exec bit is set again,
// so diff is clean (a content- or mode-only drift would surface as M).
const restored = try readFile(gpa, io, tmp.dir, "script");
defer gpa.free(restored);
try std.testing.expectEqualStrings("#!/bin/sh\necho hi\n", restored);
if (comptime File.Permissions.has_executable_bit) {
const st = try tmp.dir.statFile(io, "script", .{ .follow_symlinks = false });
try std.testing.expect(st.permissions.toMode() & 0o111 != 0);
}
var diff = try runZit(gpa, io, tmp.dir, &.{ "diff", "--brief" });
defer freeResult(gpa, &diff);
try expectExit(diff.term, 0);
try std.testing.expectEqualStrings("", diff.stdout);
}
test "cli revert artifact §4.7.6-m5: a symlink entry materializes its target" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Track a regular file so the managed set names 'link'. Revert must replace
// this placeholder with the symlink specified by the check-in.
try writeFile(io, tmp.dir, "link", "placeholder\n");
try addManaged(gpa, io, tmp.dir, &.{"link"});
// Inject a check-in whose F card for 'link' is a symlink. `zit commit`
// never mints a symlink card, so this directly exercises §4.7.6-m5
// materialization from the reader's wire representation.
const blob = try appendArtifact(gpa, io, tmp.dir, "target/path\n");
defer gpa.free(blob);
// The wire mode is the F-card perm token ('l' ⇒ symlink, §4.7.6-m5), so it
// is set explicitly — `mode` alone is not serialized.
const files = [_]artifact.FCard{.{
.filename = "link",
.hash = blob,
.perm = "l",
.old_name = null,
.mode = .symlink,
}};
const manifest_hash = try appendDiffManifest(gpa, io, tmp.dir, "symlink check-in", null, &files);
defer gpa.free(manifest_hash);
try checkout.write(io, tmp.dir, .{ .version = manifest_hash, .branch = "trunk", .merge_parent = "", .special_op = "", .manifest_flags = "" });
var res = try runZit(gpa, io, tmp.dir, &.{ "revert", "link" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("REVERT link\n", res.stdout);
try std.testing.expectEqualStrings(" \"zit undo\" is available to undo changes to the working checkout.\n", res.stderr);
// readLink verifies the entry is native symlink materialization, rather
// than a regular file merely containing the target's bytes.
var target: [std.fs.max_path_bytes]u8 = undefined;
const target_len = try tmp.dir.readLink(io, "link", &target);
try std.testing.expectEqualStrings("target/path\n", target[0..target_len]);
}
test "cli revert/undo: -r restores another check-in's content, -n lists without writing, undo -n names the files" {
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 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, "a.txt", "two\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "c2" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
try writeFile(io, tmp.dir, "a.txt", "edit\n");
// -n names the file and writes nothing (no undo journal either).
var dry = try runZit(gpa, io, tmp.dir, &.{ "revert", "-n" });
defer freeResult(gpa, &dry);
try expectExitDiag(dry.term, 0, dry);
try std.testing.expectEqualStrings("would revert a.txt\n", dry.stdout);
try expectFileContent(gpa, io, tmp.dir, "a.txt", "edit\n");
var no_undo = try runZit(gpa, io, tmp.dir, &.{ "undo", "-n" });
defer freeResult(gpa, &no_undo);
try expectExit(no_undo.term, 1);
// -r prev restores c1's bytes without moving the checkout.
var older = try runZit(gpa, io, tmp.dir, &.{ "revert", "-r", "prev", "a.txt" });
defer freeResult(gpa, &older);
try expectExitDiag(older.term, 0, older);
try expectFileContent(gpa, io, tmp.dir, "a.txt", "one\n");
var st = try runZit(gpa, io, tmp.dir, &.{"status"});
defer freeResult(gpa, &st);
try std.testing.expect(std.mem.indexOf(u8, st.stdout, "EDITED a.txt") != null);
// undo -n names what the undo would touch; the tree is unchanged until
// the real undo, which brings the discarded edit back.
var peek = try runZit(gpa, io, tmp.dir, &.{ "undo", "-n" });
defer freeResult(gpa, &peek);
try expectExitDiag(peek.term, 0, peek);
try std.testing.expect(std.mem.indexOf(u8, peek.stdout, "undo would restore a.txt") != null);
try expectFileContent(gpa, io, tmp.dir, "a.txt", "one\n");
var un = try runZit(gpa, io, tmp.dir, &.{"undo"});
defer freeResult(gpa, &un);
try expectExitDiag(un.term, 0, un);
try expectFileContent(gpa, io, tmp.dir, "a.txt", "edit\n");
var bad = try runZit(gpa, io, tmp.dir, &.{ "revert", "-r", "nope" });
defer freeResult(gpa, &bad);
try expectExit(bad.term, 1);
try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "check-in 'nope' not found") != null);
}
test "cli revert: with no paths, only what changed is restored and journaled; a clean file is never touched" {
// Fossil's revert finds what changed first and saves undo only for what
// it rewrites. The undo image is the proof: it names the edited and the
// missing file, never the clean ones.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
for ([_][]const u8{ "a.txt", "b.txt", "c.txt", "d.txt" }) |name| try writeFile(io, tmp.dir, name, name);
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt", "c.txt", "d.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "c1" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "b.txt", "edited\n");
try tmp.dir.deleteFile(io, "d.txt");
var rv = try runZit(gpa, io, tmp.dir, &.{"revert"});
defer freeResult(gpa, &rv);
try expectExitDiag(rv.term, 0, rv);
try std.testing.expectEqualStrings("REVERT b.txt\nREVERT d.txt\n", rv.stdout);
try expectFileContent(gpa, io, tmp.dir, "b.txt", "b.txt");
try expectFileContent(gpa, io, tmp.dir, "d.txt", "d.txt");
var peek = try runZit(gpa, io, tmp.dir, &.{ "undo", "-n" });
defer freeResult(gpa, &peek);
try expectExitDiag(peek.term, 0, peek);
try std.testing.expect(std.mem.indexOf(u8, peek.stdout, "b.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, peek.stdout, "d.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, peek.stdout, "a.txt") == null);
try std.testing.expect(std.mem.indexOf(u8, peek.stdout, "c.txt") == null);
// Nothing changed: nothing is reverted, and no undo level is written.
var again = try runZit(gpa, io, tmp.dir, &.{"revert"});
defer freeResult(gpa, &again);
try expectExitDiag(again.term, 0, again);
try std.testing.expectEqualStrings("", again.stdout);
}
test "cli revert: fails exit 1 on an unborn checkout (no check-in to restore)" {
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, &.{"revert"});
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, "unborn") != null);
}