const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const checkinHash = support.checkinHash;
const expectCheckoutVersion = support.expectCheckoutVersion;
const expectExit = support.expectExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const sectionExists = support.sectionExists;
const writeFile = support.writeFile;
// --- bisect (contracts/bisect.md B-bisect-1..20) ------------------------------
/// Commits the current tree at a caller-chosen `epoch` (SOURCE_DATE_EPOCH), so a
/// bisect chain has strictly increasing, deterministic event mtimes — the probe
/// selection orders by ascending mtime, so distinct epochs pin every midpoint.
fn bisectCommit(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, epoch: []const u8, msg: []const u8) ![]const u8 {
var res = try runCommitEnv(gpa, io, dir, .{ .epoch = epoch }, &.{ "commit", "-m", msg });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const h = try checkinHash(res.stdout);
return gpa.dupe(u8, h);
}
test "cli bisect B-bisect-1/-3/-4/-7/-8: a full linear bisection narrows, names the culprit, resets" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// A five-check-in chain c0<-c1<-c2<-c3<-c4 with strictly increasing mtimes.
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "poem", "v0\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const c0 = try bisectCommit(gpa, io, tmp.dir, "1000000000", "c0");
defer gpa.free(c0);
try writeFile(io, tmp.dir, "poem", "v1\n");
const c1 = try bisectCommit(gpa, io, tmp.dir, "1000000100", "c1");
defer gpa.free(c1);
try writeFile(io, tmp.dir, "poem", "v2\n");
const c2 = try bisectCommit(gpa, io, tmp.dir, "1000000200", "defect lands here");
defer gpa.free(c2);
try writeFile(io, tmp.dir, "poem", "v3\n");
const c3 = try bisectCommit(gpa, io, tmp.dir, "1000000300", "c3");
defer gpa.free(c3);
try writeFile(io, tmp.dir, "poem", "v4\n");
const c4 = try bisectCommit(gpa, io, tmp.dir, "1000000400", "c4");
defer gpa.free(c4);
// The checkout is at c4 (the head) — the bisection's restore point.
// B-bisect-1 first half: a lone bad is recorded WITHOUT moving the checkout.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "bad", c4 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c4[0..12]) != null);
try std.testing.expectEqualStrings("", r.stderr);
try std.testing.expect(sectionExists(gpa, io, tmp.dir, .bisect));
try expectCheckoutVersion(io, tmp.dir, c4);
}
// B-bisect-1 second half: good completes the span and moves to the probe c2.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "good", c0 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c2[0..12]) != null);
try std.testing.expectEqualStrings("", r.stderr);
try expectCheckoutVersion(io, tmp.dir, c2);
}
// B-bisect-7: status reports the probe WITHOUT moving; stream to stdout.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "status" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c2[0..12]) != null);
try std.testing.expectEqualStrings("", r.stderr);
try expectCheckoutVersion(io, tmp.dir, c2);
}
// next re-announces the current probe (idempotent), checkout unchanged.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "next" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c2[0..12]) != null);
try expectCheckoutVersion(io, tmp.dir, c2);
}
// B-bisect-3: mark the probe bad → the span narrows and moves to c1.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "bad", c2 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try expectCheckoutVersion(io, tmp.dir, c1);
}
// B-bisect-4: mark c1 good → span exhausted; culprit c2 named with its
// comment on stdout; the checkout is moved to the culprit.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "good", c1 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "first bad check-in") != null);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c2[0..12]) != null);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "defect lands here") != null);
try std.testing.expectEqualStrings("", r.stderr);
try expectCheckoutVersion(io, tmp.dir, c2);
}
// B-bisect-8: reset restores the starting check-in (c4) and clears the state.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "reset" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try expectCheckoutVersion(io, tmp.dir, c4);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .bisect));
}
// B-bisect-7: status with no session is exit 1, nothing on stdout.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "status" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "no bisect in progress") != null);
}
}
test "cli bisect B-bisect-16/-17: help to stdout, and the usage-error taxonomy" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// B-bisect-16: -h writes the help block to stdout, exit 0, stderr empty.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "-h" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "usage: zit bisect") != null);
try std.testing.expectEqualStrings("", r.stderr);
}
// B-bisect-17: each offense is exit 2, nothing on stdout, a stderr diagnostic.
const bad_cases = [_][]const []const u8{
&.{"bisect"}, // no subcommand
&.{ "bisect", "frobnicate" }, // unknown subcommand
&.{ "bisect", "bad" }, // missing NAME
&.{ "bisect", "good" }, // missing NAME
&.{ "bisect", "next", "extra" }, // extra argument
&.{ "bisect", "status", "extra" }, // extra argument
&.{ "bisect", "--wat" }, // unknown option
};
for (bad_cases) |argv| {
var r = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &r);
try expectExit(r.term, 2);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(r.stderr.len != 0);
}
}
test "cli bisect B-bisect-13/-18: no-session refusals and run outside a repository" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// B-bisect-18: outside any repository, every subcommand is the standard
// not-a-zit-repository refusal on stderr, exit 1, stdout empty.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "status" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "no zit checkout here") != null);
}
try initRepo(gpa, io, tmp.dir);
// B-bisect-13: next / reset with no session refuse exit 1.
for ([_][]const u8{ "next", "reset" }) |sub| {
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", sub });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "no bisect in progress") != null);
}
// B-bisect-13: next with only one endpoint marked names what is missing.
try writeFile(io, tmp.dir, "poem", "v0\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const c0 = try bisectCommit(gpa, io, tmp.dir, "1000000000", "c0");
defer gpa.free(c0);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "bad", c0 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
}
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "next" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "good") != null);
}
}
test "cli bisect B-bisect-9/-10: dirty-tree and inverted-marks refusals mutate nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Inverted marks: bad is an ancestor of good → refuse, state unchanged.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "poem", "v0\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const c0 = try bisectCommit(gpa, io, tmp.dir, "1000000000", "c0");
defer gpa.free(c0);
try writeFile(io, tmp.dir, "poem", "v1\n");
const c1 = try bisectCommit(gpa, io, tmp.dir, "1000000100", "c1");
defer gpa.free(c1);
// checkout at c1.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "bad", c0 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
}
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "good", c1 });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "inverted") != null);
try expectCheckoutVersion(io, tmp.dir, c1); // no move
}
// State unchanged: only the bad endpoint is recorded (good not persisted).
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "status" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, c0[0..12]) != null);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "mark a good") != null);
}
}
// Dirty working tree: a move-completing mark refuses, checkout unchanged.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "poem", "v0\n");
try addManaged(gpa, io, tmp.dir, &.{"poem"});
const c0 = try bisectCommit(gpa, io, tmp.dir, "1000000000", "c0");
defer gpa.free(c0);
try writeFile(io, tmp.dir, "poem", "v1\n");
const c1 = try bisectCommit(gpa, io, tmp.dir, "1000000100", "c1");
defer gpa.free(c1);
try writeFile(io, tmp.dir, "poem", "v2\n");
const c2 = try bisectCommit(gpa, io, tmp.dir, "1000000200", "c2");
defer gpa.free(c2);
// checkout at c2.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "bad", c2 });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
}
// Dirty the tree, then complete the span → refuse over the dirty tree.
try writeFile(io, tmp.dir, "poem", "uncommitted edit\n");
{
var r = try runZit(gpa, io, tmp.dir, &.{ "bisect", "good", c0 });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqualStrings("", r.stdout);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "not clean") != null);
try expectCheckoutVersion(io, tmp.dir, c2); // no move
}
}
}