const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendSyntheticCheckin = support.appendSyntheticCheckin;
const buildMergeDiamond = support.buildMergeDiamond;
const checkinHash = support.checkinHash;
const countLines = support.countLines;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const lineCount = support.lineCount;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runShell = support.runShell;
const runZit = support.runZit;
const seedHistory = support.seedHistory;
const writeDefaultBranch = support.writeDefaultBranch;
const writeFile = support.writeFile;
// ==========================================================================
// `zit timeline` end-to-end (repo-state §2/§3.2/§6.1/§6.4/§8.3 + CLI
// discipline). The global chronological history: every check-in, event.mtime
// descending, ties by full artifact hash. Each test spawns the REAL binary in
// an isolated tmp dir. Behavior ids cite contracts/timeline.md B-timeline-*.
// ==========================================================================
// ISO §4.6.4 writer forms of the injected commit epochs (whole Unix seconds,
// verified with `date -u`): the timeline date column derives from the stored
// event.mtime, which is the injected epoch carried in milliseconds.
const iso_epoch_900e6 = "1998-07-09T16:00:00"; // SOURCE_DATE_EPOCH=900000000
const iso_epoch_1e9 = "2001-09-09T01:46:40"; // SOURCE_DATE_EPOCH=1000000000
const iso_epoch_2e9 = "2033-05-18T03:33:20"; // SOURCE_DATE_EPOCH=2000000000
/// Writes `content` to `name`, optionally stages it (`stage` on the first
/// commit of a path), then commits at `epoch` with `msg`. Returns the new
/// check-in's 64-hex hash (caller frees) — the setup for the timeline ordering
/// tests, which need distinct, injected mtimes.
fn timelineCommit(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
name: []const u8,
content: []const u8,
stage: bool,
epoch: []const u8,
msg: []const u8,
) ![]u8 {
try writeFile(io, dir, name, content);
if (stage) try addManaged(gpa, io, dir, &.{name});
var c = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = epoch }, &.{ "commit", "-m", msg });
defer freeResult(gpa, &c);
try expectExit(c.term, 0);
return gpa.dupe(u8, try checkinHash(c.stdout));
}
test "cli timeline B-timeline-1: full listing, one line per check-in, 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);
const root = try timelineCommit(gpa, io, tmp.dir, "a.txt", "root\n", true, "900000000", "root");
defer gpa.free(root);
const mid = try timelineCommit(gpa, io, tmp.dir, "a.txt", "mid\n", false, "1000000000", "mid");
defer gpa.free(mid);
const tip = try timelineCommit(gpa, io, tmp.dir, "a.txt", "tip\n", false, "2000000000", "tip");
defer gpa.free(tip);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Data on stdout, diagnostics on stderr — the streams never mix (cli-stream).
try std.testing.expectEqualStrings("", res.stderr);
// Every crosslinked check-in appears exactly once: three commits ⇒ three lines.
try std.testing.expectEqual(@as(usize, 3), lineCount(res.stdout));
}
test "cli timeline B-timeline-2: rows are ordered event.mtime descending, each dated from its mtime" {
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 root = try timelineCommit(gpa, io, tmp.dir, "a.txt", "root\n", true, "900000000", "root");
defer gpa.free(root);
const mid = try timelineCommit(gpa, io, tmp.dir, "a.txt", "mid\n", false, "1000000000", "mid");
defer gpa.free(mid);
const tip = try timelineCommit(gpa, io, tmp.dir, "a.txt", "tip\n", false, "2000000000", "tip");
defer gpa.free(tip);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
// §2/§8.3 item 7: most recent first — tip (2e9), mid (1e9), root (900e6) —
// the whole-repo clock, not one check-in's ancestry (timeline's diff vs log).
var want: [512]u8 = undefined;
const expected = try std.fmt.bufPrint(
&want,
" {s} " ++ iso_epoch_2e9 ++ " trunk tip\n" ++
" {s} " ++ iso_epoch_1e9 ++ " trunk mid\n" ++
" {s} " ++ iso_epoch_900e6 ++ " trunk root\n",
.{ tip[0..12], mid[0..12], root[0..12] },
);
try std.testing.expectEqualStrings(expected, res.stdout);
}
test "cli timeline B-timeline-3: exact-mtime ties list the descendant first (B1), byte-reproducible across runs" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Two check-ins at the SAME epoch ⇒ equal event.mtime. They are a CHAIN,
// so ancestry decides the tie (B1: a descendant always lists before its
// ancestor — the generation key), never hash order; hash breaks ties only
// between true concurrents. Deterministic and byte-reproducible either way.
const root = try timelineCommit(gpa, io, tmp.dir, "a.txt", "root\n", true, "1000000000", "root");
defer gpa.free(root);
const child = try timelineCommit(gpa, io, tmp.dir, "a.txt", "child\n", false, "1000000000", "child");
defer gpa.free(child);
var res1 = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res1);
try expectExit(res1.term, 0);
try std.testing.expectEqualStrings("", res1.stderr);
var res2 = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res2);
try expectExit(res2.term, 0);
// Byte-reproducible: a second run yields identical bytes.
try std.testing.expectEqualStrings(res1.stdout, res2.stdout);
// Genuinely two distinct check-ins tied on mtime (non-vacuous comparison).
try std.testing.expect(!std.mem.eql(u8, root, child));
var want: [512]u8 = undefined;
const expected = try std.fmt.bufPrint(
&want,
" {s} " ++ iso_epoch_1e9 ++ " trunk child\n" ++
" {s} " ++ iso_epoch_1e9 ++ " trunk root\n",
.{ child[0..12], root[0..12] },
);
try std.testing.expectEqualStrings(expected, res1.stdout);
}
test "cli timeline B-timeline-4 (§6.1): the branch column is each check-in's own branch" {
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 root = try timelineCommit(gpa, io, tmp.dir, "a.txt", "root\n", true, "1000000000", "root");
defer gpa.free(root);
// A `--branch feature` fork lands the newer check-in on feature (§6.1/§6.2).
try writeFile(io, tmp.dir, "a.txt", "two\n");
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u", .epoch = "2000000000" }, &.{ "commit", "-m", "feature work", "--branch", "feature" });
defer freeResult(gpa, &c);
try expectExit(c.term, 0);
const feat = try gpa.dupe(u8, try checkinHash(c.stdout));
defer gpa.free(feat);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
var want: [512]u8 = undefined;
const expected = try std.fmt.bufPrint(
&want,
" {s} " ++ iso_epoch_2e9 ++ " feature feature work\n" ++
" {s} " ++ iso_epoch_1e9 ++ " trunk root\n",
.{ feat[0..12], root[0..12] },
);
try std.testing.expectEqualStrings(expected, res.stdout);
}
test "cli timeline B-timeline-4 (§6.4): a tagless check-in shows the configured main-branch, never literal trunk" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// §6.4: set main-branch to a non-default name, then add a check-in with NO
// branch tag. Its branch is the CONFIGURED value, never the literal "trunk".
try writeDefaultBranch(gpa, io, tmp.dir, "main");
const id = try appendSyntheticCheckin(gpa, io, tmp.dir, "tagless", null);
defer gpa.free(id);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
var want: [256]u8 = undefined;
const expected = try std.fmt.bufPrint(&want, " {s} 2020-01-01T00:00:00 main tagless\n", .{id[0..12]});
try std.testing.expectEqualStrings(expected, res.stdout);
// §6.4 refutation: the configured name is emitted, the literal never is.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "trunk") == null);
}
test "cli timeline B-timeline-5: a merge check-in is marked M; primary-only check-ins are not" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Two roots and a merge joining them (§3.2: base primary, other a merge
// parent). Built by hand — a two-P-card manifest — since no `zit merge` exists.
const base = try appendSyntheticCheckin(gpa, io, tmp.dir, "base", null);
defer gpa.free(base);
const other = try appendSyntheticCheckin(gpa, io, tmp.dir, "other", null);
defer gpa.free(other);
var pbuf: [160]u8 = undefined;
const parents = try std.fmt.bufPrint(&pbuf, "{s} {s}", .{ base, other });
const merge = try appendSyntheticCheckin(gpa, io, tmp.dir, "merge", parents);
defer gpa.free(merge);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
// Three check-ins, one line each (the merge is never folded into a parent).
try std.testing.expectEqual(@as(usize, 3), lineCount(res.stdout));
// §3.2: the merge line (>=1 merge parent) carries "M "; the two roots carry " ".
var mbuf: [160]u8 = undefined;
const merge_line = try std.fmt.bufPrint(&mbuf, "M {s} 2020-01-01T00:00:00 trunk merge\n", .{merge[0..12]});
var bbuf: [160]u8 = undefined;
const base_line = try std.fmt.bufPrint(&bbuf, " {s} 2020-01-01T00:00:00 trunk base\n", .{base[0..12]});
var obuf: [160]u8 = undefined;
const other_line = try std.fmt.bufPrint(&obuf, " {s} 2020-01-01T00:00:00 trunk other\n", .{other[0..12]});
try std.testing.expect(std.mem.indexOf(u8, res.stdout, merge_line) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, base_line) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, other_line) != null);
}
test "cli timeline B-timeline-6: a multi-line comment contributes only its first line" {
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 id = try appendSyntheticCheckin(gpa, io, tmp.dir, "first line\nsecond line", null);
defer gpa.free(id);
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
// The embedded newline is cut: one line, first line only (invariant held).
try std.testing.expectEqual(@as(usize, 1), lineCount(res.stdout));
var want: [256]u8 = undefined;
const expected = try std.fmt.bufPrint(&want, " {s} 2020-01-01T00:00:00 trunk first line\n", .{id[0..12]});
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "second line") == null);
}
test "cli timeline B-timeline-7: an empty repository yields 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, &.{"timeline"});
defer freeResult(gpa, &res);
// Emptiness is defined out of existence: a success with no rows, no diagnostic.
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expectEqual(@as(usize, 0), lineCount(res.stdout));
}
test "cli timeline B-timeline-8: -h/--help print the verbatim help to stdout, exit 0, help wins" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const expected =
\\usage: zit timeline [-n N] [--offset N] [-b BRANCH | -c] [-p PATH] [-u LOGIN]
\\ [-r] [-v] [-F oneline|medium|full|html] [--weekday]
\\ [-R <path>] [-h|--help]
\\
\\List every check-in in the repository, most recent first. Each line shows
\\the check-in's short hash, its ISO timestamp, its branch, and the first
\\line of its comment. A check-in that joins two or more lines of development
\\is marked "M". Order is by event time descending, ties broken by artifact
\\hash, so the listing is byte-identical across runs.
\\
\\Filters narrow the listing: -b BRANCH to one branch, -c to the checkout's
\\branch, -p PATH to the check-ins that change PATH against their primary
\\parent, -u LOGIN to one author. -r lists oldest first; --offset N skips
\\the first N matches before -n counts. The default (-F medium) is
\\Fossil's shape — a date header once per day, with the weekday, then
\\time, a twelve-character hash, *CURRENT* on the checkout's check-in,
\\the comment, and (user: … tags: …); -F oneline is one line per
\\check-in (hash, ISO time, branch, comment) for scripts; -F full adds
\\each check-in's changed files, which -v adds to any format.
\\
\\--weekday prints each check-in's day of week between the hash and the
\\date. Days are UTC, like every timestamp here, so a late Friday commit
\\in your local zone can read Saturday. The computation is Ben Joffe's
\\branchless day-of-week (benjoffe.com/fast-day-of-week).
\\
\\options:
\\ -n, --limit N show only the N most recent entries; 0 means all
\\ --offset N skip the first N matching entries
\\ -b, --branch B only check-ins on branch B
\\ -c, --current-branch only check-ins on the checkout's branch
\\ -p, --path P only check-ins that change P
\\ -u, --for-user U only check-ins by U
\\ -r, --reverse oldest first
\\ -v, --verbose list each check-in's changed files
\\ -F, --format F oneline (default), medium, or full;
\\ --oneline/--medium/--full are the same
\\ --weekday show the UTC day of week beside each date
\\ -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)
\\
;
// -h/--help win even over an otherwise-invalid trailing token.
for ([_][]const []const u8{ &.{ "timeline", "-h" }, &.{ "timeline", "--help" }, &.{ "timeline", "-h", "foo" } }) |args| {
var res = try runZit(gpa, io, tmp.dir, args);
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
// `zit help timeline` teaches the identical text (DX D4).
var routed = try runZit(gpa, io, tmp.dir, &.{ "help", "timeline" });
defer freeResult(gpa, &routed);
try expectExit(routed.term, 0);
try std.testing.expectEqualStrings(expected, routed.stdout);
}
test "cli timeline B-timeline-9: 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();
for ([_][]const u8{ "-n", "--bogus" }) |opt| {
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", opt });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
var want: [160]u8 = undefined;
const expected = try std.fmt.bufPrint(&want, "zit timeline: unknown option '{s}'\ntry 'zit timeline --help'\n", .{opt});
try std.testing.expectEqualStrings(expected, res.stderr);
}
}
test "cli timeline B-timeline-10: 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();
// A bare positional and any token after `--` are operands timeline rejects.
for ([_][]const []const u8{ &.{ "timeline", "foo" }, &.{ "timeline", "--", "foo" } }) |args| {
var res = try runZit(gpa, io, tmp.dir, args);
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("zit timeline: unexpected argument 'foo'\ntry 'zit timeline --help'\n", res.stderr);
}
}
test "cli timeline B-timeline-11: 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();
// Data-layer failure (exit 1), distinct from the exit-2 usage class.
var res = try runZit(gpa, io, tmp.dir, &.{"timeline"});
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 timeline B-timeline-12: `zit timeline >> log` appends (O_APPEND honored), rows to stdout" {
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 z = try appendSyntheticCheckin(gpa, io, tmp.dir, "z", null);
defer gpa.free(z);
// A streaming stdout writer honors the inherited O_APPEND offset: the row
// lands after the pre-existing content, never clobbering it (cli-stream).
var res = try runShell(gpa, io, tmp.dir, "printf 'PRE\\n' > log; \"$ZIT\" timeline -F oneline >> log");
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const log = try readFile(gpa, io, tmp.dir, "log");
defer gpa.free(log);
var want: [256]u8 = undefined;
const expected = try std.fmt.bufPrint(&want, "PRE\n {s} 2020-01-01T00:00:00 trunk z\n", .{z[0..12]});
try std.testing.expectEqualStrings(expected, log);
}
test "cli timeline: 'timeline' is listed in the command help and suggested for a near-miss" {
// cli-verb-enum-wiring: the verb joins the single-source Verb enum, so the
// command list names it and the did-you-mean suggester finds it (mirror leaves).
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, "timeline") != null);
var typo = try runZit(gpa, io, tmp.dir, &.{"timelien"});
defer freeResult(gpa, &typo);
try expectExit(typo.term, 2);
try std.testing.expect(std.mem.indexOf(u8, typo.stderr, "did you mean 'timeline'?") != null);
}
test "cli timeline -n: the limit is a strict PREFIX of the unlimited listing, ties included" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedHistory(gpa, io, tmp.dir, 12);
var full = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &full);
try expectExit(full.term, 0);
try std.testing.expectEqual(@as(usize, 12), countLines(full.stdout));
// The load-bearing assertion. Comparing only the ROW COUNT would pass even
// if the limit selected an arbitrary N — the defect the ordering fix
// existed to remove. Requiring the output to be the first N BYTES of the
// unlimited listing can only hold if the cap is applied after the total
// order, so this line fails for any wrong-N-check-ins implementation.
for ([_]usize{ 1, 5, 11, 12 }) |n| {
var num_buf: [8]u8 = undefined;
const num = try std.fmt.bufPrint(&num_buf, "{d}", .{n});
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-n", num });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqual(n, countLines(res.stdout));
var end: usize = 0;
var seen: usize = 0;
while (seen < n) : (seen += 1) end = std.mem.indexOfScalarPos(u8, full.stdout, end, '\n').? + 1;
try std.testing.expectEqualStrings(full.stdout[0..end], res.stdout);
}
}
test "cli timeline -n: 0 is unlimited, a cap past the history shows everything" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedHistory(gpa, io, tmp.dir, 4);
inline for (.{ "0", "99" }) |n| {
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-n", n });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqual(@as(usize, 4), countLines(res.stdout));
}
// `--limit` is the long alias for the same option.
var alias = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "--limit", "2" });
defer freeResult(gpa, &alias);
try expectExit(alias.term, 0);
try std.testing.expectEqual(@as(usize, 2), countLines(alias.stdout));
}
test "cli timeline/log -n: a non-numeric or negative value is refused, never silently defaulted" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try seedHistory(gpa, io, tmp.dir, 2);
inline for (.{ "abc", "-3" }) |bad| {
var res = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-n", bad });
defer freeResult(gpa, &res);
try expectExit(res.term, 2); // a usage error, not a silent full listing
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "invalid -n value") != null);
}
}
test "cli timeline: filters by branch, current branch, author and path; -r/--offset; medium and full formats mark the current check-in and list 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", "a\n");
try writeFile(io, tmp.dir, "b.txt", "b\n");
try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExitDiag(c1.term, 0, c1);
try writeFile(io, tmp.dir, "a.txt", "a2\n");
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "bob" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExitDiag(c2.term, 0, c2);
try writeFile(io, tmp.dir, "b.txt", "b2\n");
var c3 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "alice" }, &.{ "commit", "-m", "three", "--branch", "feat" });
defer freeResult(gpa, &c3);
try expectExitDiag(c3.term, 0, c3);
const Case = struct { args: []const []const u8, want: []const []const u8, not: []const []const u8 };
const cases = [_]Case{
.{ .args = &.{ "timeline", "-F", "oneline", "-b", "trunk" }, .want = &.{ "two", "one" }, .not = &.{"three"} },
.{ .args = &.{ "timeline", "-F", "oneline", "-c" }, .want = &.{"three"}, .not = &.{ "two", "one" } },
.{ .args = &.{ "timeline", "-F", "oneline", "-u", "alice" }, .want = &.{ "three", "one" }, .not = &.{"two"} },
.{ .args = &.{ "timeline", "-F", "oneline", "-p", "b.txt" }, .want = &.{ "three", "one" }, .not = &.{"two"} },
.{ .args = &.{ "timeline", "-F", "oneline", "-r", "--offset", "1", "-n", "1" }, .want = &.{"two"}, .not = &.{ "one", "three" } },
.{ .args = &.{ "timeline", "-F", "medium", "-n", "1" }, .want = &.{ "=== ", "*CURRENT* three (user: alice tags: feat" }, .not = &.{"two"} },
.{ .args = &.{ "timeline", "-F", "oneline", "--full", "-n", "1" }, .want = &.{ "*CURRENT*", " EDITED b.txt" }, .not = &.{"a.txt"} },
.{ .args = &.{ "timeline", "-F", "oneline", "-v", "-n", "1" }, .want = &.{ " three", " EDITED b.txt" }, .not = &.{"user:"} },
};
for (cases) |cs| {
var r = try runZit(gpa, io, tmp.dir, cs.args);
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
for (cs.want) |w| {
if (std.mem.indexOf(u8, r.stdout, w) == null) {
std.debug.print("timeline {s}: missing {s} in:\n{s}\n", .{ cs.args[1], w, r.stdout });
return error.TestUnexpectedResult;
}
}
for (cs.not) |n| {
if (std.mem.indexOf(u8, r.stdout, n) != null) {
std.debug.print("timeline {s}: unexpected {s} in:\n{s}\n", .{ cs.args[1], n, r.stdout });
return error.TestUnexpectedResult;
}
}
}
// The default line is unchanged: no user, no tags, no header.
var plain = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-n", "1" });
defer freeResult(gpa, &plain);
try expectExitDiag(plain.term, 0, plain);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "user:") == null);
try std.testing.expect(std.mem.indexOf(u8, plain.stdout, "===") == null);
}
test "cli timeline: --weekday prints the UTC day beside each date; without it, output is unchanged" {
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", "alpha\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 expectExit(c1.term, 0);
var plain = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "-F", "oneline" });
defer freeResult(gpa, &plain);
try expectExitDiag(plain.term, 0, plain);
var flagged = try runZit(gpa, io, tmp.dir, &.{ "timeline", "-F", "oneline", "--weekday" });
defer freeResult(gpa, &flagged);
try expectExitDiag(flagged.term, 0, flagged);
const t_ = std.testing;
const names = [7][]const u8{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
// The plain listing carries no weekday token in the date column.
for (names) |n| {
var pat_buf: [8]u8 = undefined;
const pat = std.fmt.bufPrint(&pat_buf, " {s} 2", .{n}) catch unreachable;
try t_.expect(std.mem.indexOf(u8, plain.stdout, pat) == null);
}
// The flagged listing carries exactly the day zit's own weekdayOf computes
// for the printed date. Recover the date from the flagged line itself, so
// the test holds on any day it runs.
var lines = std.mem.splitScalar(u8, flagged.stdout, '\n');
const row = lines.next().?;
var fields = std.mem.tokenizeScalar(u8, row, ' ');
_ = fields.next().?; // hash
const day_token = fields.next().?;
const iso = fields.next().?;
try t_.expectEqual(@as(usize, 3), day_token.len);
// Parse YYYY-MM-DD from the row and cross-check through daysFromCivil-free
// arithmetic: reuse the library's own weekday of the commit mtime is not
// observable here, so anchor on the printed date via zit.repostate.
const y = std.fmt.parseInt(i64, iso[0..4], 10) catch unreachable;
const mo = std.fmt.parseInt(u8, iso[5..7], 10) catch unreachable;
const d = std.fmt.parseInt(u8, iso[8..10], 10) catch unreachable;
// Sakamoto reference, the same one the unit property test pins against.
const off = [12]i64{ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
const yy = if (mo < 3) y - 1 else y;
const expect_day: usize = @intCast(@mod(yy + @divFloor(yy, 4) - @divFloor(yy, 100) + @divFloor(yy, 400) + off[mo - 1] + d, 7));
try t_.expectEqualStrings(names[expect_day], day_token);
// Removing the weekday token from the flagged row yields the plain row.
var rebuilt: std.ArrayList(u8) = .empty;
defer rebuilt.deinit(gpa);
const day_at = std.mem.indexOf(u8, row, day_token).?;
try rebuilt.appendSlice(gpa, row[0..day_at]);
try rebuilt.appendSlice(gpa, row[day_at + 4 ..]);
var plain_lines = std.mem.splitScalar(u8, plain.stdout, '\n');
try t_.expectEqualStrings(plain_lines.next().?, rebuilt.items);
}
test "cli timeline: renders the same from the image as from a derive" {
// Timeline reads the zero-parse image now; every column it renders must be
// there. A merge (the §3.2 mark), a tag, and two authors cover the row
// surface across every format, with the cache present and then removed.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var fx = try buildMergeDiamond(gpa, io, tmp.dir, "A\nB\nC\n", "A\nB\nC1\n", "A1\nB\nC\n");
defer fx.deinit(gpa);
var mg = try runZit(gpa, io, tmp.dir, &.{ "merge", fx.other });
defer freeResult(gpa, &mg);
try expectExitDiag(mg.term, 0, mg);
var mc = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "merger" }, &.{ "commit", "-m", "merge other" });
defer freeResult(gpa, &mc);
try expectExitDiag(mc.term, 0, mc);
var tg = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "merger" }, &.{ "tag", "add", "v1", "tip" });
defer freeResult(gpa, &tg);
try expectExitDiag(tg.term, 0, tg);
const forms = [_][]const []const u8{
&.{ "timeline", "-n", "0" },
&.{ "timeline", "-n", "0", "-F", "full" },
&.{ "timeline", "-n", "0", "-v" },
&.{ "timeline", "-n", "0", "-F", "html" },
&.{ "timeline", "-n", "0", "-F", "oneline" },
&.{ "timeline", "-n", "0", "-p", "poem" },
};
var warm: [forms.len][]u8 = undefined;
for (forms, 0..) |argv, i| {
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "merge other") != null);
warm[i] = try gpa.dupe(u8, res.stdout);
}
defer for (warm) |w| gpa.free(w);
// No cache: the same open derives from the log instead of the image.
try tmp.dir.deleteTree(io, ".zit-cache");
for (forms, 0..) |argv, i| {
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(warm[i], res.stdout);
// Rewritten by that derive; the next form reads the image again.
try tmp.dir.deleteTree(io, ".zit-cache");
}
}