Skip to content
zit Artifact [b9c01528b0]
Login

Artifact [b9c01528b0]

Artifact b9c01528b0ccd5f89933508deffd6fc4e7a1812207cc527880622dcde5bc0880:


const std = @import("std");
const repeat = @import("zit").repeat;
const checkout = @import("zit").checkout;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendSyntheticCheckin = support.appendSyntheticCheckin;
const buildFork = support.buildFork;
const checkinHash = support.checkinHash;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const lineCount = support.lineCount;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;

// ==========================================================================
// `zit branch` end-to-end (repo-state §6.1/§6.3/§6.4/§9 + CLI discipline).
// The multi-branch, closed, forked, and non-trunk-config derivations are unit-
// tested (no CLI creates a branch yet), so these e2e cases exercise the single
// trunk the CLI can build plus the full usage/help/stream/exit contract.
// ==========================================================================

const branch_help_expected =
    \\usage: zit branch [list] [-c] [-t] [-r] [-v] [--no-cache]
    \\       zit branch new NAME [BASIS]
    \\       zit branch (close | reopen | hide | unhide) NAME
    \\       zit branch current
    \\       zit branch info NAME
    \\
    \\List the repository's branches, one per line, ordered by name. Branches are
    \\derived from the active branch tags of the check-ins; the branch of the current
    \\checkout is marked with a leading "*". A branch whose most recent check-in is
    \\closed is marked "(closed)".
    \\
    \\The other actions are Fossil's. 'new' records a check-in that
    \\changes no file and starts NAME at the checkout's check-in (BASIS, when
    \\given, must be that check-in — update to it first). 'close' and 'reopen'
    \\add and cancel the closed tag on NAME's tip; 'hide' and 'unhide' do the
    \\same with the hidden tag. 'current' prints the checkout's branch; 'info'
    \\prints NAME's tip, count and root.
    \\
    \\options:
    \\  -v, --verbose   each branch with its tip and time
    \\  -c, --closed    list closed branches only
    \\  -t              most recently changed first
    \\  -r              reverse the order
    \\      --no-cache  bypass every derived sidecar (the cache AND the rebuild
    \\                  checkpoint) and re-derive from the full log, re-verifying
    \\                  whole-log integrity (the cache is refreshed)
    \\  -R, --repo <path>
    \\                read this repository instead of the one found from the
    \\                current directory; <path> is the directory holding '.zit'
    \\                (or the '.zit' file itself). No working tree is required.
    \\  -h, --help      show this help and exit
    \\
    \\exit status:
    \\  0  success
    \\  1  operational failure (not a repository, unreadable state, a refused
    \\     action) — the diagnostic on stderr names it
    \\  2  usage error (an unknown option or unexpected argument)
    \\
;

test "cli branch §6.3: a single-commit repo lists trunk marked '*'" {
    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", "root\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});

    var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &commit);
    try expectExit(commit.term, 0);
    _ = try checkinHash(commit.stdout);

    // §6.3: one branch derived from the root's active branch tag, named "trunk";
    // §6.1/§6.4: the checkout is on it, so it is marked. Data on stdout only.
    var res = try runZit(gpa, io, tmp.dir, &.{"branch"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("* trunk\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
}

test "cli branch §6.4: the current mark is the active-tag branch-of the checkout" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    // A trunk fork (two open leaves, checkout on the newer). §6.3 still groups
    // them into one "trunk" entry; §6.4 marks it because the checkout resolves
    // to trunk through the active-tag filter.
    const fork = try buildFork(gpa, io, tmp.dir);
    defer gpa.free(fork.child1);
    defer gpa.free(fork.child2);

    var res = try runZit(gpa, io, tmp.dir, &.{"branch"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("* trunk\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
    try std.testing.expectEqual(@as(usize, 1), lineCount(res.stdout));
}

test "cli branch B-branch-3: an unborn repository lists nothing (empty stdout, exit 0)" {
    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, &.{"branch"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
}

test "cli branch B-branch-4: an unknown option is a usage error (exit 2, stderr, empty stdout)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var res = try runZit(gpa, io, tmp.dir, &.{ "branch", "-x" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 2);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expectEqualStrings(
        "zit branch: unknown option '-x'\ntry 'zit branch --help'\n",
        res.stderr,
    );
}

test "cli branch B-branch-5: an extra argument is a usage error (exit 2, stderr, empty stdout)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var res = try runZit(gpa, io, tmp.dir, &.{ "branch", "feature" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 2);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expectEqualStrings(
        "zit branch: unexpected argument 'feature'\ntry 'zit branch --help'\n",
        res.stderr,
    );
}

test "cli branch B-branch-6: --help / -h / 'help branch' print the verbatim help (stdout, exit 0)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    for ([_][]const u8{ "--help", "-h" }) |flag| {
        var res = try runZit(gpa, io, tmp.dir, &.{ "branch", flag });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings(branch_help_expected, res.stdout);
        try std.testing.expectEqualStrings("", res.stderr);
    }
    var routed = try runZit(gpa, io, tmp.dir, &.{ "help", "branch" });
    defer freeResult(gpa, &routed);
    try expectExit(routed.term, 0);
    try std.testing.expectEqualStrings(branch_help_expected, routed.stdout);
}

test "cli branch B-branch-7: outside a repository fails exit 1 on stderr, empty stdout" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var res = try runZit(gpa, io, tmp.dir, &.{"branch"});
    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, "no zit checkout here") != null);
}

test "cli branch B-branch-8: a corrupt repository fails exit 1 with a stderr diagnostic" {
    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 initial = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_only });
    const frame_offset = (try initial.stat(io)).size;
    initial.close(io);
    const root = try appendSyntheticCheckin(gpa, io, tmp.dir, "root", null);
    defer gpa.free(root);

    // Flip a byte inside the appended frame so the log reader rejects it.
    var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
    var byte: [1]u8 = undefined;
    try std.testing.expectEqual(@as(usize, 1), try file.readPositionalAll(io, &byte, frame_offset + 10));
    byte[0] ^= 0xff;
    try file.writePositionalAll(io, &byte, frame_offset + 10);
    file.close(io);

    var res = try runZit(gpa, io, tmp.dir, &.{"branch"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 1);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expectEqualStrings(
        "zit branch: cannot read repository: repository is corrupt\n",
        res.stderr,
    );
}

test "cli branch B-branch-9: 'branch' is listed in top help and suggested for a near-miss" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var help = try runZit(gpa, io, tmp.dir, &.{"help"});
    defer freeResult(gpa, &help);
    try expectExit(help.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, help.stdout, "branch") != null);

    var typo = try runZit(gpa, io, tmp.dir, &.{"brnach"});
    defer freeResult(gpa, &typo);
    try expectExit(typo.term, 2);
    try std.testing.expect(std.mem.indexOf(u8, typo.stderr, "did you mean 'branch'?") != null);
}

test "cli branch §6.4: a hand-edited .zit-checkout naming an unknown version leaves nothing marked (exit 0)" {
    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", "root\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});

    var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &commit);
    try expectExit(commit.term, 0);
    _ = try checkinHash(commit.stdout);

    // Overwrite the checkout record with a well-formed-but-unknown version. The
    // current-mark resolver returns null (never interned), so trunk lists with
    // no "*"; the branch set itself (§6.3) is unaffected. Never an error.
    const unknown = repeat("b", 64);
    try checkout.write(io, tmp.dir, .{ .version = unknown, .branch = "trunk", .merge_parent = "", .special_op = "", .manifest_flags = "" });

    var res = try runZit(gpa, io, tmp.dir, &.{"branch"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("  trunk\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
}

test "cli branch: new/current/info, close/reopen, hide/unhide, and the -c/-t/-r list flags" {
    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", "one" });
    defer freeResult(gpa, &c1);
    try expectExitDiag(c1.term, 0, c1);

    var nw = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "branch", "new", "feat" });
    defer freeResult(gpa, &nw);
    try expectExitDiag(nw.term, 0, nw);
    try std.testing.expect(std.mem.indexOf(u8, nw.stdout, " on feat") != null);
    var curb = try runZit(gpa, io, tmp.dir, &.{ "branch", "current" });
    defer freeResult(gpa, &curb);
    try expectExitDiag(curb.term, 0, curb);
    try std.testing.expectEqualStrings("feat\n", curb.stdout);
    var info = try runZit(gpa, io, tmp.dir, &.{ "branch", "info", "feat" });
    defer freeResult(gpa, &info);
    try expectExitDiag(info.term, 0, info);
    try std.testing.expect(std.mem.indexOf(u8, info.stdout, "branch:    feat\n") != null);
    try std.testing.expect(std.mem.indexOf(u8, info.stdout, "check-ins: 1\n") != null);

    var cl = try runZit(gpa, io, tmp.dir, &.{ "branch", "close", "feat" });
    defer freeResult(gpa, &cl);
    try expectExitDiag(cl.term, 0, cl);
    var closed = try runZit(gpa, io, tmp.dir, &.{ "branch", "-c" });
    defer freeResult(gpa, &closed);
    try expectExitDiag(closed.term, 0, closed);
    try std.testing.expect(std.mem.indexOf(u8, closed.stdout, "feat  (closed)") != null);
    try std.testing.expect(std.mem.indexOf(u8, closed.stdout, "trunk") == null);
    var ro = try runZit(gpa, io, tmp.dir, &.{ "branch", "reopen", "feat" });
    defer freeResult(gpa, &ro);
    try expectExitDiag(ro.term, 0, ro);
    var none = try runZit(gpa, io, tmp.dir, &.{ "branch", "-c" });
    defer freeResult(gpa, &none);
    try expectExitDiag(none.term, 0, none);
    try std.testing.expectEqualStrings("", none.stdout);

    var hd = try runZit(gpa, io, tmp.dir, &.{ "branch", "hide", "feat" });
    defer freeResult(gpa, &hd);
    try expectExitDiag(hd.term, 0, hd);
    var raw = try runZit(gpa, io, tmp.dir, &.{ "tag", "list", "--raw", "tip" });
    defer freeResult(gpa, &raw);
    try expectExitDiag(raw.term, 0, raw);
    try std.testing.expect(std.mem.indexOf(u8, raw.stdout, "hidden") != null);
    var uh = try runZit(gpa, io, tmp.dir, &.{ "branch", "unhide", "feat" });
    defer freeResult(gpa, &uh);
    try expectExitDiag(uh.term, 0, uh);

    // -t orders by recency (feat is newest), -r reverses.
    var bt = try runZit(gpa, io, tmp.dir, &.{ "branch", "-t" });
    defer freeResult(gpa, &bt);
    try expectExitDiag(bt.term, 0, bt);
    try std.testing.expect(std.mem.startsWith(u8, bt.stdout, "* feat"));
    var br = try runZit(gpa, io, tmp.dir, &.{ "branch", "-t", "-r" });
    defer freeResult(gpa, &br);
    try expectExitDiag(br.term, 0, br);
    try std.testing.expect(std.mem.startsWith(u8, br.stdout, "  trunk"));
    // A BASIS that is not the checkout is refused with the remedy.
    var bad = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "branch", "new", "other", "trunk" });
    defer freeResult(gpa, &bad);
    try expectExit(bad.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "run 'zit update trunk' first") != null);
}