//! `zit leaves`: list the check-ins with no children.
const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const Repository = @import("zit").Repository;
const repostate = zit.repostate;
const diagnostics = @import("../diagnostics.zig");
const help_pages = @import("../help.zig");
const openRepo = diagnostics.openRepo;
const leaves_help = help_pages.leaves_help;
const checkin_display = @import("checkin_display.zig");
const firstCommentLine = checkin_display.firstCommentLine;
/// `zit leaves [--all]` — list the open leaves of the check-in DAG (repo-state
/// §7.1/§7.4/§7.5). Opens the repository through `Repository` and renders each
/// leaf's full artifact ID and branch, mtime-descending (§7.5 SHOULD order),
/// marking the leaf that matches the current checkout with "*". `--all`
/// additionally lists closed leaves — §7.4: closed status is derived, so a
/// closed leaf stays a leaf-table member. Enumeration is distinct from name
/// resolution (§7.7): the listing is exactly the leaf table filtered by the
/// §7.4 open predicate. An unborn repository has no leaves: empty stdout, exit 0.
pub fn runLeaves(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
verb_args: []const []const u8,
streams: cli.Streams,
amb: cli.Ambient,
) cli.Exit {
const parsed = switch (cli.parseLeavesArgs(verb_args)) {
.ok => |p| p,
.unknown_option => |tok| return cli.usageError(streams, "leaves", .unknown_option, tok),
.extra_argument => |tok| return cli.usageError(streams, "leaves", .extra_argument, tok),
};
if (parsed.help) {
streams.out.writeAll(leaves_help) catch {};
return .ok;
}
var repo: Repository = undefined;
var root_dir: std.Io.Dir = undefined;
const rc = openRepo(gpa, io, arena, streams, "leaves", amb.repo_override, parsed.no_cache, true, &repo, &root_dir);
if (rc != .ok) return rc;
defer root_dir.close(io);
defer repo.deinit(gpa);
// Resolve the current checkout's version to a rid so its leaf is marked
// (§7.5). Absence/unknown/eventless leaves nothing marked — the mark is a
// checkout convenience, never the source of the leaf set.
const cur = repo.currentCheckoutRid();
return renderLeaves(gpa, arena, &repo, parsed.all, cur, streams);
}
/// One row of the leaf listing, gathered before the §7.5 mtime-descending sort.
/// `hash` borrows the view's identity space (stable for the view's lifetime).
const LeafRow = struct {
rid: repostate.Rid,
mtime: repostate.Mtime,
hash: []const u8,
open: bool,
};
/// §7.5 order: modification time descending. Ties break by artifact ID ascending
/// so the listing is a deterministic total order (IDs are unique per check-in).
fn leafRowLess(_: void, a: LeafRow, b: LeafRow) bool {
if (a.mtime != b.mtime) return a.mtime > b.mtime;
return std.mem.order(u8, a.hash, b.hash) == .lt;
}
/// Gathers, sorts (§7.5), and prints the leaf listing to stdout. A row is
/// included when `all` is set or the leaf is open (§7.4 open predicate). Each
/// line is `<mark><id> <branch>`, `<mark>` = "* " for the current-checkout leaf
/// else " "; under `all` a closed leaf appends " (closed)". Bounded by the
/// leaf-table size.
fn renderLeaves(
gpa: std.mem.Allocator,
arena: std.mem.Allocator,
repo: *Repository,
all: bool,
cur: ?repostate.Rid,
streams: cli.Streams,
) cli.Exit {
var rows: std.ArrayList(LeafRow) = .empty;
defer rows.deinit(arena);
const bound = repo.state().leaves().rids.len;
var scanned: usize = 0;
var it = repo.state().leaves();
while (it.next()) |rid| {
scanned += 1;
std.debug.assert(scanned <= bound);
const open = repo.state().isOpenLeaf(rid);
if (!all and !open) continue;
// A leaf-table rid is always a crosslinked check-in, so it has an event.
const mtime = repo.state().mtimeOf(rid) orelse unreachable;
rows.append(arena, .{
.rid = rid,
.mtime = mtime,
.hash = repo.hashOf(rid),
.open = open,
}) catch {
streams.err.writeAll("zit leaves: out of memory\n") catch {};
return .failure;
};
}
std.debug.assert(rows.items.len <= bound);
std.mem.sort(LeafRow, rows.items, {}, leafRowLess);
for (rows.items, 0..) |row, idx| {
std.debug.assert(repo.state().isLeaf(row.rid));
std.debug.assert(all or row.open);
// Fossil's leaf line: number, time, name, comment, user and
// tags, with the weekday, a twelve-character name, and *CURRENT*
// where the checkout sits — the same marker timeline uses.
const branch = repo.branchOf(row.rid);
var tbuf: [32]u8 = undefined;
var comment: []const u8 = "";
var user: []const u8 = "";
// The crosslinked state answers both, so the common path reads no
// manifest at all. One read and parse per leaf — through whatever
// delta chain it sits on — was the whole cost of this verb: sqlite's
// 472 leaves spent a second here. Image backing and control-only rids
// answer null and take the fallback.
// gpa, not the arena: the repository opens its log with the allocator
// it is handed and frees it with its own.
var om: ?Repository.OwnedManifest = null;
defer if (om) |*m| m.deinit(gpa);
const from_state = blk: {
const c = repo.state().commentOf(row.rid) orelse break :blk false;
const u = repo.state().userOf(row.rid) orelse break :blk false;
comment = firstCommentLine(c);
user = u;
break :blk true;
};
if (!from_state) {
om = repo.manifest(gpa, row.rid) catch null;
if (om) |m| {
if (m.manifest.c) |c| comment = firstCommentLine(c.comment);
if (m.manifest.u) |u| user = u.login;
}
}
streams.out.print("({d}) {s} [{s}] {s}{s} (user: {s} tags: {s}{s})\n", .{
idx + 1,
cli.humanTime(row.mtime, &tbuf),
cli.shortHash(row.hash),
if (cur != null and cur.? == row.rid) "*CURRENT* " else "",
comment,
user,
branch,
if (all and !row.open) ", closed" else "",
}) catch {};
}
return .ok;
}