//! `zit cat`: print one file's content at a check-in.
const std = @import("std");
const cli = @import("zit").cli;
const absence_mod = @import("zit").absence;
const cmd_pull = @import("pull.zig");
const Repository = @import("zit").Repository;
const diagnostics = @import("../cli/diagnostics.zig");
const help_pages = @import("../cli/help.zig");
const not_a_repo_msg = diagnostics.not_a_repo_msg;
const openRepo = diagnostics.openRepo;
const cat_help = help_pages.cat_help;
/// `zit cat FILE [VERSION]` — emit one file's uncompressed content at a resolved
/// check-in. `ls` narrowed to a single file (§8.4 single-name lookup): resolve
/// the version the same way `ls` does, look the file up in the effective set,
/// reconstruct its blob (delta chain → uncompressed, hash-verified §7.2), and
/// write the bytes verbatim — no framing, no trailing newline (content is
/// `[]const u8`, so embedded NUL and arbitrary binary bytes pass through).
pub fn runCat(
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.parseCatArgs(verb_args)) {
.ok => |p| p,
.unknown_option => |tok| return cli.usageError(streams, "cat", .unknown_option, tok),
.extra_argument => |tok| return cli.usageError(streams, "cat", .extra_argument, tok),
};
if (parsed.help) {
streams.out.writeAll(cat_help) catch {};
return .ok;
}
const file = parsed.file orelse return cli.usageError(streams, "cat", .no_file, "");
var tree: cmd_pull.ElidedTree = .{};
const rc = catOnce(gpa, io, arena, parsed, file, streams, amb, &tree);
if (!tree.fetch(gpa, io, arena, streams, amb, "cat")) return rc;
return catOnce(gpa, io, arena, parsed, file, streams, amb, &tree);
}
fn catOnce(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
parsed: cli.CatArgs,
file: []const u8,
streams: cli.Streams,
amb: cli.Ambient,
tree: *cmd_pull.ElidedTree,
) cli.Exit {
var repo: Repository = undefined;
var root_dir: std.Io.Dir = undefined;
const rc = openRepo(gpa, io, arena, streams, "cat", amb.repo_override, parsed.no_cache, true, &repo, &root_dir);
if (rc != .ok) return rc;
defer root_dir.close(io);
defer repo.deinit(gpa);
// Version resolution mirrors `ls`: an explicit VERSION through the §8.3 name
// procedure, an omitted one the current checkout (§7.5); an unborn/absent
// checkout has no check-in to read.
const rid = if (parsed.version) |name|
repo.resolve(name, .checkin) catch |e| return catResolveFail(gpa, &repo, streams, name, e)
else
repo.currentCheckoutRid() orelse return catNoCheckout(streams);
// §8.4 single-name lookup: null ⇒ the file is not in this check-in's
// effective set (absent, or a delete marker suppresses it).
const found = (repo.fileAt(gpa, rid, file) catch |e| return catFail(streams, e, tree)) orelse
return catNotFound(streams, file);
defer Repository.freeFileCard(gpa, found);
std.debug.assert(found.hash != null); // fileAt yields only hash-bearing cards (§4.7.6)
// Reconstruct the blob and verify it hashes to its F-card id (§7.2). A null
// blob is a stored-content gap for a named-but-absent file.
const bytes = (repo.blob(gpa, found.hash.?) catch |e| return catFail(streams, e, tree)) orelse
return catMissingContent(gpa, io, root_dir, &repo, streams, file, found.hash.?);
defer gpa.free(bytes);
streams.out.writeAll(bytes) catch {};
return .ok;
}
/// Maps a `Repository.resolve` failure for `cat` to its diagnostic + exit 1.
fn catResolveFail(gpa: std.mem.Allocator, repo: *const Repository, s: cli.Streams, name: []const u8, e: Repository.ResolveError) cli.Exit {
switch (e) {
error.NotFound => {
s.err.print("zit cat: check-in '{s}' not found\n", .{name}) catch {};
cli.horizonNameHint(gpa, repo.io, repo.root_dir, s, "cat");
},
error.Ambiguous => s.err.print("zit cat: name '{s}' is ambiguous\n", .{name}) catch {},
error.OutOfMemory => s.err.writeAll("zit cat: out of memory\n") catch {},
}
return .failure;
}
/// Maps a `Repository.fileAt` / `blob` failure to its diagnostic + exit 1.
/// Switches the full `FilesError` set exhaustively; `blob`'s narrower `OpenError`
/// coerces into it, so one renderer covers both surfaces. The open/content
/// phrasings match the shared `repoOpenFail` text.
fn catFail(s: cli.Streams, e: Repository.FilesError, tree: *cmd_pull.ElidedTree) cli.Exit {
switch (e) {
error.OutOfMemory => s.err.writeAll("zit cat: out of memory\n") catch {},
error.NotARepo => s.err.writeAll(not_a_repo_msg) catch {},
error.CannotRead => s.err.writeAll("zit cat: cannot read repository: input/output error\n") catch {},
error.ImportPending => s.err.writeAll("zit cat: an incremental import is pending; run 'zit import-git --recover DIR'\n") catch {},
error.Corrupt => s.err.writeAll("zit cat: cannot read repository: repository is corrupt\n") catch {},
error.NotManifest => s.err.writeAll("zit cat: not a check-in\n") catch {},
error.BaselineUnresolvable => s.err.writeAll("zit cat: cannot resolve the baseline of a delta check-in\n") catch {},
error.ElidedByCone => tree.note(s, "cat"),
}
return .failure;
}
fn catNoCheckout(s: cli.Streams) cli.Exit {
s.err.writeAll("zit cat: no check-in to read (the working checkout is unborn); name a version\n") catch {};
return .failure;
}
fn catNotFound(s: cli.Streams, file: []const u8) cli.Exit {
s.err.print("zit cat: file '{s}' is not in the check-in\n", .{file}) catch {};
return .failure;
}
/// Sparse-cone RFC section 6.4 -- an absence is named by its class: purged,
/// shunned, or elided (each deliberate, exit 8, with the remedy), and only
/// otherwise "missing", which alleges damage and keeps exit 1.
fn catMissingContent(gpa: std.mem.Allocator, io: std.Io, root_dir: std.Io.Dir, repo: *Repository, s: cli.Streams, file: []const u8, blob: []const u8) cli.Exit {
const absence = absence_mod.absenceOf(gpa, io, root_dir, repo, blob);
if (absence.byPolicy()) {
var wbuf: [512]u8 = undefined;
const what = std.fmt.bufPrint(&wbuf, "content for '{s}'", .{file}) catch "content";
absence_mod.describeAbsence(s.err, "cat", what, &absence);
return .absent;
}
s.err.print("zit cat: content for '{s}' is missing\n", .{file}) catch {};
return .failure;
}