//! `zit amend`: attach display overrides (comment, user, date, branch)
//! to a check-in by minting one control artifact.
const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const Repository = @import("zit").Repository;
const artifact = zit.artifact;
const diagnostics = @import("../cli/diagnostics.zig");
const help_pages = @import("../cli/help.zig");
const stateOom = diagnostics.stateOom;
const openRepo = diagnostics.openRepo;
const amend_help = help_pages.amend_help;
const control_artifact = @import("control_artifact.zig");
const MintError = control_artifact.MintError;
const mintControlArtifact = control_artifact.mintControlArtifact;
pub fn runAmend(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
verb_args: []const []const u8,
streams: cli.Streams,
amb: cli.Ambient,
) cli.Exit {
// 1. Parse (B-amend-1). Usage errors → exit 2; `--help` wins (B-amend-13).
const parsed = switch (cli.parseAmendArgs(verb_args)) {
.ok => |p| p,
.unknown_option => |tok| return cli.usageError(streams, "amend", .unknown_option, tok),
.extra_argument => |tok| return cli.usageError(streams, "amend", .extra_argument, tok),
.repeated_option => |tok| return cli.usageError(streams, "amend", .repeated_option, tok),
.empty_value => |tok| return cli.usageError(streams, "amend", .empty_value, tok),
.bad_date => |tok| return cli.usageError(streams, "amend", .bad_date, tok),
.no_override => return cli.usageError(streams, "amend", .no_override, ""),
};
if (parsed.help) {
streams.out.writeAll(amend_help) catch {};
return .ok;
}
// NAME is required (B-amend-14): a missing positional is a usage error, before
// any repository access.
const name = parsed.name orelse return cli.usageError(streams, "amend", .no_name, "");
// 2. Open the repository read-only for resolution (B-amend-15: the repository
// check precedes resolution — `openRepo` renders not-a-zit-repository and
// exits 1). Full derive: `resolve`/`tagsOf` read the tag tables.
var repo: Repository = undefined;
var root_dir: std.Io.Dir = undefined;
const rc = openRepo(gpa, io, arena, streams, "amend", amb.repo_override, false, false, &repo, &root_dir);
if (rc != .ok) return rc;
defer root_dir.close(io);
// 3. Resolve NAME to a check-in and capture everything the mint needs — the
// target's 64-hex hash and, for `--branch`, the parent syms to cancel
// (repo-state §6.2) — all duped onto `arena` so they outlive the read view,
// which is closed before the rw append (commit's durability pattern).
var target_hash: []const u8 = undefined;
var parent_syms: []const []const u8 = &.{};
{
defer repo.deinit(gpa);
const rid = repo.resolve(name, .checkin) catch |e| switch (e) {
// B-amend-9: an ambiguous hash prefix (repo-state §8.3).
error.Ambiguous => {
streams.err.print("zit amend: name '{s}' is ambiguous\n", .{name}) catch {};
return .failure;
},
error.OutOfMemory => return stateOom(streams, "amend"),
error.NotFound => {
// B-amend-10: a NAME that names a stored non-check-in artifact (a
// control artifact, say) is refused BY its actual type — T cards
// target check-ins. Anything else is the B-amend-9 not-found.
if (amendArtifactType(gpa, &repo, name)) |type_name| {
streams.err.print("zit amend: '{s}' is a {s}, not a check-in\n", .{ name, type_name }) catch {};
} else {
streams.err.print("zit amend: check-in '{s}' not found\n", .{name}) catch {};
cli.horizonNameHint(gpa, repo.io, repo.root_dir, streams, "amend");
}
return .failure;
},
};
target_hash = arena.dupe(u8, repo.hashOf(rid)) catch return amendOom(streams);
// The amendment targets a resolved check-in by its full §3.6 artifact id.
std.debug.assert(artifact.validateName(target_hash));
// repo-state §6.2: a `--branch` move cancels the target's inherited
// (propagating) sym-* tags, minus the branch's own re-established sym. The
// surfaced names are duped onto `arena` (the view's interned names die at
// `repo.deinit`).
if (parsed.branch) |bname| {
const entries = repo.state().tagsOf(gpa, rid) catch return amendOom(streams);
defer gpa.free(entries);
parent_syms = zit.checkin.selectInheritedSyms(arena, entries, bname) catch return amendOom(streams);
}
}
// 4. Compose the one control artifact's T run (B-amend-1/4), each card
// retargeted at the check-in (artifact §5.4). At least one override is
// present (the parser's `no_override` guard), so the run is non-empty.
const tags = cli.composeAmendTags(arena, target_hash, .{
.comment = parsed.comment,
.user = parsed.user,
.date = parsed.date,
.branch = parsed.branch,
.parent_syms = parent_syms,
}) catch return amendOom(streams);
// 5-6. Assemble, serialize, and durably store the one control artifact.
var cname_buf: [64]u8 = undefined;
const control_name = mintControlArtifact(gpa, io, root_dir, amb, tags, &cname_buf) catch |e|
return amendLogFail(streams, e);
// 7. Success (B-amend-1/16): the one confirmation line to stdout, the full
// control-artifact hash as a fixed-position token (mirrors commit's
// verb-prefixed line). Every diagnostic already went to stderr.
streams.out.print("amend {s}\n", .{control_name}) catch {};
return .ok;
}
/// Renders `zit amend: out of memory` and returns `.failure`.
fn amendOom(s: cli.Streams) cli.Exit {
s.err.writeAll("zit amend: out of memory\n") catch {};
return .failure;
}
/// Renders a B-amend log open/put/commit failure. Exhaustive over the widest
/// closed set (`logstore.OpenError`); the narrower `PutError`/`CommitError`
/// coerce into it, so one mapper serves all three.
fn amendLogFail(s: cli.Streams, err: MintError) cli.Exit {
switch (err) {
error.StoreBusy => {}, // the lock notice already said it
error.OutOfMemory => s.err.writeAll("zit amend: out of memory\n") catch {},
error.Storage => s.err.writeAll("zit amend: cannot write the amendment: input/output error\n") catch {},
error.Corrupt => s.err.writeAll("zit amend: the repository is corrupt\n") catch {},
}
return .failure;
}
/// The external type name of the stored artifact named `name` when it is NOT a
/// check-in (B-amend-10), or null when `name` names no stored artifact, is
/// unparseable, or IS a check-in manifest (in which case the not-found diagnostic
/// stands — `resolve` already ruled it out as a crosslinked check-in). Reads the
/// raw artifact through the repo's content channel and classifies it; the
/// returned name is a static string, valid after the parse is freed.
fn amendArtifactType(gpa: std.mem.Allocator, repo: *Repository, name: []const u8) ?[]const u8 {
const bytes = (repo.blob(gpa, name) catch return null) orelse return null;
defer gpa.free(bytes);
var m = artifact.parse(gpa, bytes) catch return null;
defer m.deinit();
if (m.kind == .manifest) return null; // a manifest IS a check-in; let not-found stand
return m.kind.externalName();
}