//! `zit trust`: show, grant or withdraw consent to run the project's
//! before-commit check.
const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const cmd_sync = @import("sync.zig");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const diagnostics = @import("../diagnostics.zig");
const help_pages = @import("../help.zig");
const stateOom = diagnostics.stateOom;
// ---------------------------------------------------------------------------
// `zit trust`: consent to execute a versioned before-commit declaration
// ---------------------------------------------------------------------------
/// `zit trust [--revoke]` — show, grant, or withdraw this checkout's consent to
/// run the project's `before-commit` check.
///
/// Bare, it SHOWS the declaration and whether it is trusted, and does not grant
/// anything. Granting is a second, explicit invocation. That split is
/// deliberate: consent given without seeing what is being consented to is not
/// consent, and a single command that printed and trusted in one step would
/// make the safe reading of `zit trust` the dangerous one.
pub fn runTrust(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
verb_args: []const []const u8,
streams: cli.Streams,
amb: cli.Ambient,
) cli.Exit {
var revoke = false;
var grant = false;
for (verb_args) |tok| {
if (std.mem.eql(u8, tok, "-h") or std.mem.eql(u8, tok, "--help")) {
streams.out.writeAll(help_pages.trust_help) catch {};
return .ok;
} else if (std.mem.eql(u8, tok, "--revoke")) {
revoke = true;
} else if (std.mem.eql(u8, tok, "--yes")) {
grant = true;
} else {
return cli.usageError(streams, "trust", .unknown_option, tok);
}
}
if (revoke and grant) {
streams.err.writeAll("zit trust: --revoke and --yes are contradictory\n") catch {};
return .usage;
}
const root = (cli.discoverRoot(arena, io) catch return stateOom(streams, "trust")) orelse {
streams.err.writeAll(cmd_sync.not_a_repo_msg) catch {};
return .failure;
};
var root_dir = root.dir;
defer root_dir.close(io);
const resolved = cmd_sync.resolveSetting(gpa, io, arena, root_dir, amb, .before_commit) catch {
streams.err.writeAll("zit trust: cannot read the before-commit setting\n") catch {};
return .failure;
};
if (revoke) {
cmd_sync.updateConfig(gpa, io, root_dir, .{ .before_commit_trust = "" }) catch {
streams.err.writeAll("zit trust: cannot write the trust record\n") catch {};
return .failure;
};
streams.out.writeAll("before-commit: trust revoked\n") catch {};
return .ok;
}
if (resolved.value.len == 0) {
streams.out.writeAll("before-commit: not set; nothing to trust\n") catch {};
return .ok;
}
const stored = trustRecord(gpa, io, arena, root_dir) catch "";
const verdict = zit.settings.trustVerdict(resolved.value, resolved.source, stored);
if (!grant) {
// SHOW. The declaration is printed in full because that is the thing
// being consented to; a summary would defeat the point.
streams.out.print("before-commit ({s}): {s}\n", .{ resolved.source.label(), resolved.value }) catch {};
switch (verdict) {
.none => unreachable, // handled above
.self_authored => streams.out.writeAll("status: runs (you set it here; no trust record needed)\n") catch {},
.trusted => streams.out.writeAll("status: trusted — runs before each commit\n") catch {},
.untrusted => {
streams.out.writeAll("status: NOT trusted — commits are refused until you accept it\n") catch {};
streams.err.writeAll("read the command above, then run 'zit trust --yes' to accept it\n") catch {};
},
.changed => {
streams.out.writeAll("status: CHANGED since you trusted it — commits are refused\n") catch {};
streams.err.writeAll("the declaration was edited (possibly by a pull); read it above, then 'zit trust --yes'\n") catch {};
},
}
return .ok;
}
if (verdict == .self_authored) {
streams.out.writeAll("before-commit: set locally; it already runs and needs no trust record\n") catch {};
return .ok;
}
var hash_buf: [64]u8 = undefined;
const hash = zit.settings.declarationHash(resolved.value, &hash_buf);
cmd_sync.updateConfig(gpa, io, root_dir, .{ .before_commit_trust = hash }) catch {
streams.err.writeAll("zit trust: cannot write the trust record\n") catch {};
return .failure;
};
streams.out.print("before-commit: trusted ({s})\n", .{hash[0..12]}) catch {};
return .ok;
}
/// This checkout's stored trust hash, or empty. Local-only by construction:
/// it is read from the config frame, which never crosses a sync.
fn trustRecord(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
root_dir: std.Io.Dir,
) ![]const u8 {
var file = cli.openStoreRead(io, root_dir) catch return "";
defer file.close(io);
const st = file.stat(io) catch return "";
var fs = storage.FileStorage.init(io, file, st.size);
var log = logstore.LogStore.openReadOnly(gpa, fs.storageFile()) catch return "";
defer log.deinit(gpa);
var oc = (log.readConfig(gpa) catch return "") orelse return "";
defer oc.deinit(gpa);
return arena.dupe(u8, oc.config.before_commit_trust) catch "";
}