const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectNonzeroExit = support.expectNonzeroExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;
test "cli grep/annotate: -i -c -l -v --once shape grep's report; annotate -l lists versions and -n counts them" {
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", "Alpha\nbeta\nALPHA two\n");
try writeFile(io, tmp.dir, "g", "gamma\n");
try addManaged(gpa, io, tmp.dir, &.{ "f", "g" });
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", "Alpha\nbeta\nALPHA two\ndelta\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "c2" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var ci = try runZit(gpa, io, tmp.dir, &.{ "grep", "-i", "alpha", "f" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
// Both versions of f match twice; grep reports every version.
try std.testing.expectEqual(@as(usize, 4), std.mem.count(u8, ci.stdout, "\n"));
try std.testing.expect(std.mem.indexOf(u8, ci.stdout, "f:3: ALPHA two") != null);
var cnt = try runZit(gpa, io, tmp.dir, &.{ "grep", "-i", "-c", "alpha", "f", "g" });
defer freeResult(gpa, &cnt);
try expectExitDiag(cnt.term, 0, cnt);
try std.testing.expect(std.mem.indexOf(u8, cnt.stdout, " f:2\n") != null);
try std.testing.expect(std.mem.indexOf(u8, cnt.stdout, " g:0\n") != null);
var names = try runZit(gpa, io, tmp.dir, &.{ "grep", "-i", "-l", "alpha", "f", "g" });
defer freeResult(gpa, &names);
try expectExitDiag(names.term, 0, names);
try std.testing.expect(std.mem.endsWith(u8, names.stdout, " f\n"));
try std.testing.expect(std.mem.indexOf(u8, names.stdout, " g") == null);
var inv = try runZit(gpa, io, tmp.dir, &.{ "grep", "-v", "-i", "alpha", "f" });
defer freeResult(gpa, &inv);
try expectExitDiag(inv.term, 0, inv);
try std.testing.expect(std.mem.indexOf(u8, inv.stdout, "f:2: beta") != null);
try std.testing.expect(std.mem.indexOf(u8, inv.stdout, "Alpha") == null);
var once = try runZit(gpa, io, tmp.dir, &.{ "grep", "-i", "--once", "alpha", "f" });
defer freeResult(gpa, &once);
try expectExitDiag(once.term, 0, once);
try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, once.stdout, "\n")); // one line per version
var log = try runZit(gpa, io, tmp.dir, &.{ "annotate", "-l", "f" });
defer freeResult(gpa, &log);
try expectExitDiag(log.term, 0, log);
try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, log.stdout, "version "));
try std.testing.expect(std.mem.indexOf(u8, log.stdout, "\n---\n") != null);
// -n counts versions, at least three as in fossil: both of f's are
// walked, so the answer is the whole one.
var lim = try runZit(gpa, io, tmp.dir, &.{ "annotate", "-n", "1", "f" });
defer freeResult(gpa, &lim);
try expectExitDiag(lim.term, 0, lim);
var plain = try runZit(gpa, io, tmp.dir, &.{ "annotate", "f" });
defer freeResult(gpa, &plain);
try std.testing.expectEqualStrings(plain.stdout, lim.stdout);
var bad = try runZit(gpa, io, tmp.dir, &.{ "annotate", "-n", "x", "f" });
defer freeResult(gpa, &bad);
try expectExit(bad.term, 2);
}
test "cli grep: a string added then removed is still found in the versions that carried it" {
// The question this verb exists for: "when did this exist, and where?" —
// unanswerable in zit before, because the bytes are not on disk. The
// security-relevant shape is the one driven here: a secret committed and
// later removed is still in history and still findable.
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, "cfg.txt", "alpha\nSECRET_TOKEN=1\ngamma\n");
try writeFile(io, tmp.dir, "other.txt", "nothing to see\n");
try addManaged(gpa, io, tmp.dir, &.{ "cfg.txt", "other.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "add secret" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "cfg.txt", "alpha\nstill here SECRET_TOKEN\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "keep" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
try writeFile(io, tmp.dir, "cfg.txt", "alpha\nremoved\n");
var c3 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "remove" });
defer freeResult(gpa, &c3);
try expectExitDiag(c3.term, 0, c3);
var hit = try runZit(gpa, io, tmp.dir, &.{ "grep", "SECRET_TOKEN", "cfg.txt" });
defer freeResult(gpa, &hit);
try expectExit(hit.term, 0); // 0 means found — the shell convention
// Both carrying versions are reported, with their line numbers; the
// version that removed it is not.
try std.testing.expect(std.mem.indexOf(u8, hit.stdout, "cfg.txt:2: SECRET_TOKEN=1") != null);
try std.testing.expect(std.mem.indexOf(u8, hit.stdout, "cfg.txt:2: still here SECRET_TOKEN") != null);
try std.testing.expect(std.mem.indexOf(u8, hit.stdout, "removed") == null);
// A run of check-ins that did not touch the file is ONE version, not one
// report per check-in: other.txt was unchanged across all three.
var once = try runZit(gpa, io, tmp.dir, &.{ "grep", "nothing", "other.txt" });
defer freeResult(gpa, &once);
try expectExit(once.term, 0);
var lines: usize = 0;
var it = std.mem.tokenizeScalar(u8, once.stdout, '\n');
while (it.next()) |_| lines += 1;
try std.testing.expectEqual(@as(usize, 1), lines);
// Absent string: exit 1, nothing on stdout — distinguishable from a failure
// to search, which exits 1 with a diagnostic.
var miss = try runZit(gpa, io, tmp.dir, &.{ "grep", "NEVER_PRESENT", "cfg.txt" });
defer freeResult(gpa, &miss);
try expectNonzeroExit(miss.term);
try std.testing.expectEqualStrings("", miss.stdout);
// Operand errors are usage errors, not empty results.
var no_path = try runZit(gpa, io, tmp.dir, &.{ "grep", "SECRET_TOKEN" });
defer freeResult(gpa, &no_path);
try expectExit(no_path.term, 2);
}
test "cli grep -S: the pickaxe reports the check-in that added a string and the one that removed it, and nothing between" {
// Slice 2's acceptance criterion, and the line that separates the
// pickaxe from slice 1: a string that sat unchanged through intervening
// check-ins is reported for EVERY version by `grep` and for exactly TWO
// check-ins by `grep -S`. A `-S` that reported the quiet middle would be
// answering the question plain grep already answers.
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, "cfg.txt", "alpha\nbeta\n");
try addManaged(gpa, io, tmp.dir, &.{"cfg.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "cfg.txt", "alpha\nSECRET_TOKEN=1\nbeta\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "add secret" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
// The quiet middle: the file changes, the string does not move.
try writeFile(io, tmp.dir, "cfg.txt", "alpha\nSECRET_TOKEN=1\nbeta\nextra\n");
var c3 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "unrelated edit" });
defer freeResult(gpa, &c3);
try expectExitDiag(c3.term, 0, c3);
try writeFile(io, tmp.dir, "cfg.txt", "alpha\nbeta\nextra\n");
var c4 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "remove secret" });
defer freeResult(gpa, &c4);
try expectExitDiag(c4.term, 0, c4);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S", "SECRET_TOKEN" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
var lines = std.mem.tokenizeScalar(u8, res.stdout, '\n');
var n: usize = 0;
while (lines.next()) |_| n += 1;
try std.testing.expectEqual(@as(usize, 2), n);
// Newest first, so the removal leads and the introduction follows;
// the signs are what make the two lines distinguishable.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "-1 cfg.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "+1 cfg.txt") != null);
try std.testing.expect(std.mem.startsWith(u8, res.stdout[13..], "-1"));
}
{
// Deleting a whole file removes its occurrences too — the half of
// "when did it go" that a walk over only the child's files misses,
// because the path is not there to be compared.
try writeFile(io, tmp.dir, "doomed.txt", "carries SECRET_TOKEN here\n");
try addManaged(gpa, io, tmp.dir, &.{"doomed.txt"});
var add = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "add doomed" });
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
var rm = try runZit(gpa, io, tmp.dir, &.{ "rm", "--hard", "doomed.txt" });
defer freeResult(gpa, &rm);
try expectExitDiag(rm.term, 0, rm);
var del = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "delete doomed" });
defer freeResult(gpa, &del);
try expectExitDiag(del.term, 0, del);
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S", "SECRET_TOKEN", "doomed.txt" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "+1 doomed.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "-1 doomed.txt") != null);
// Narrowed to one path: cfg.txt's two hits are not in this answer.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "cfg.txt") == null);
}
{
// Nothing matched is exit 1 with empty stdout, distinguishable from a
// failure to search — the contract the issue asked for.
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S", "NEVER_PRESENT_ANYWHERE" });
defer freeResult(gpa, &res);
try expectNonzeroExit(res.term);
try std.testing.expectEqualStrings("", res.stdout);
}
{
// Unlike plain grep, -S needs no PATH: demanding one would be
// demanding the answer to "where did this appear".
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2); // ...but PATTERN is still required
}
}
test "cli grep -G: a changed line that mentions the pattern is a hit even when the count did not move" {
// WHY BOTH PICKAXES EXIST. Renaming `x = SECRET` to `y = SECRET` leaves
// exactly one occurrence before and after, so -S is silent — correctly, by
// its own definition — while the line plainly changed. If -G did not
// report it, -G would be a slower -S.
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", "x = SECRET\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "introduce" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "a.txt", "y = SECRET\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "rename, count unchanged" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
// -S sees one event: the introduction. The rename moved no count.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S", "SECRET" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
var lines = std.mem.tokenizeScalar(u8, res.stdout, '\n');
var n: usize = 0;
while (lines.next()) |_| n += 1;
try std.testing.expectEqual(@as(usize, 1), n);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "+1 a.txt") != null);
}
// -G sees two: the introduction, and the rename whose removed and added
// lines both mention it.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-G", "SECRET" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
var lines = std.mem.tokenizeScalar(u8, res.stdout, '\n');
var n: usize = 0;
while (lines.next()) |_| n += 1;
try std.testing.expectEqual(@as(usize, 2), n);
// '~' marks a -G line, so the two modes are distinguishable by a
// consumer that did not see the command line.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "~2 a.txt") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "~1 a.txt") != null);
}
{
// A pattern that only matches the diff's own file header must not
// report anything: `+++ a.txt` is not a changed line.
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-G", "a.txt" });
defer freeResult(gpa, &res);
try expectNonzeroExit(res.term);
try std.testing.expectEqualStrings("", res.stdout);
}
{
// The two pickaxes ask different questions of one walk; combining them
// is refused rather than letting one silently win.
var res = try runZit(gpa, io, tmp.dir, &.{ "grep", "-S", "-G", "SECRET" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
}
}