Skip to content
zit Artifact [3c294dbbf1]
Login

Artifact [3c294dbbf1]

Artifact 3c294dbbf166dc3b94da53551c5179284d1562758d0a4e2706643efedd07de0e:


//! A repository's config frame: reading it, and the field-preserving write
//! every verb that changes a repository value goes through. No CLI.

const std = @import("std");
const logstore = @import("../logstore.zig");
const openLogAdoptingIndex = @import("log_index_store.zig").openLogAdoptingIndex;
const storage = @import("../storage.zig");
const store_open = @import("store_open.zig");
const sync = @import("../sync.zig");

/// The fields a `updateConfig` overlay may change; a null field is left exactly
/// as the stored config has it. Naming a single change site keeps the "copy the
/// whole config, overlay one field" discipline in one place — a caller that
/// means to change only the project code can never silently drop the credential
/// or the saved remote (the class of bug the pre-`updateConfig` re-puts had).
pub const ConfigPatch = struct {
    project_code: ?[]const u8 = null,
    login: ?sync.Credential = null,
    /// Forget the stored login. A login belongs to the saved remote, so a
    /// change of remote drops it in the same frame.
    clear_login: bool = false,
    remote_url: ?[]const u8 = null,
    /// draft-fossil-submanifests-00 §6.3/§8.1 and §6.4 — the three
    /// project-scope settings sync §9.3's scalar channel carries. Optional for
    /// the same reason every field here is: a peer that named only one of them
    /// must leave the other two exactly as stored.
    enable_submanifests: ?bool = null,
    submanifest_k: ?u8 = null,
    submanifest_auto_retune: ?bool = null,
    /// The autosync setting, stored inverted (true = off). Local, like
    /// `remote_url`; only the settings verb writes it.
    autosync_off: ?bool = null,
    ignore_glob: ?[]const u8 = null,
    /// The two signing settings, both LOCAL and both deliberately
    /// outside every config group: fossil classifies `pgp-command` sensitive
    /// (it is a command line a peer would otherwise get to choose) and puts
    /// `clearsign` in no group at all.
    clearsign: ?bool = null,
    pgp_command: ?[]const u8 = null,
    /// The rest of fossil's checkout settings at the repository tier.
    crnl_glob: ?[]const u8 = null,
    clean_glob: ?[]const u8 = null,
    empty_dirs: ?[]const u8 = null,
    encoding_glob: ?[]const u8 = null,
    dotfiles: ?[]const u8 = null,
    mimetypes: ?[]const u8 = null,
    attachment_size_limit: ?[]const u8 = null,
    /// `zit git export`'s remembered mirror.
    git_mirror: ?[]const u8 = null,
    git_autopush: ?[]const u8 = null,
    git_push_branch: ?[]const u8 = null,
    git_email_domain_local: ?[]const u8 = null,
    /// The repository's Git email domain, project scope like the three
    /// sub-manifest settings above and carried by the same §9.3 channel.
    git_email_domain: ?[]const u8 = null,
    /// `main-branch`.
    default_branch: ?[]const u8 = null,
    /// The explicit-set bits, which are what make "this repository has no
    /// opinion" representable and therefore let the global tier be reached.
    autosync_set: ?bool = null,
    clearsign_set: ?bool = null,
    repo_cksum_off: ?bool = null,
    repo_cksum_set: ?bool = null,
    keep_glob: ?[]const u8 = null,
    manifest: ?[]const u8 = null,
    before_commit: ?[]const u8 = null,
    /// The two content-policy globs. Data settings, so a tracked
    /// `.zit-settings/` file outranks whatever is stored here.
    binary_glob: ?[]const u8 = null,
    crlf_glob: ?[]const u8 = null,
    /// The consent record. Empty string is a real value here (revoke),
    /// so it is written whenever the field is present, not when non-empty.
    before_commit_trust: ?[]const u8 = null,
    editor: ?[]const u8 = null,
    autosync_mode: ?[]const u8 = null,
    pub fn any(self: ConfigPatch) bool {
        return self.project_code != null or
            self.login != null or
            self.clear_login or
            self.remote_url != null or
            self.enable_submanifests != null or
            self.submanifest_k != null or
            self.submanifest_auto_retune != null or
            self.git_email_domain != null or
            self.autosync_off != null or
            self.ignore_glob != null or
            self.clearsign != null or
            self.pgp_command != null or
            self.autosync_set != null or
            self.clearsign_set != null or
            self.repo_cksum_off != null or
            self.repo_cksum_set != null or
            self.keep_glob != null or
            self.manifest != null or
            self.before_commit != null or
            self.before_commit_trust != null or
            self.binary_glob != null or
            self.crlf_glob != null or
            self.editor != null or
            self.autosync_mode != null;
    }
};

/// Read the live config from an already-open log, overlay `patch`, and append
/// the result while preserving every unnamed field. The caller owns durability,
/// so a clone can combine its origin settings in one config frame and commit
/// its complete result once.
pub fn updateConfigOpen(
    gpa: std.mem.Allocator,
    log: *logstore.LogStore,
    patch: ConfigPatch,
) PersistError!void {
    var oc = (log.readConfig(gpa) catch |e| switch (e) {
        error.OutOfMemory => return error.OutOfMemory,
        error.Storage => return error.Storage,
        error.Corrupt => return error.Corrupt,
    }) orelse return error.Corrupt; // init always writes a config frame
    defer oc.deinit(gpa);
    // Copy every field, then overlay only the named ones. `next`'s string slices
    // borrow `oc.backing` / `patch`; both outlive the `putConfig` memcpy below.
    var next = oc.config;
    if (patch.project_code) |c| next.project_code = c;
    std.debug.assert(!(patch.clear_login and patch.login != null));
    if (patch.clear_login) {
        next.login_user = "";
        next.login_secret_hex = "";
    }
    if (patch.login) |cred| {
        next.login_user = cred.user;
        // Point at the function-scoped `patch` storage, not the block-local
        // capture `cred`: `next` is read by `putConfig` after this block closes.
        next.login_secret_hex = &patch.login.?.secret_hex;
    }
    if (patch.remote_url) |u| next.remote_url = u;
    if (patch.enable_submanifests) |v| next.enable_submanifests = v;
    if (patch.submanifest_k) |v| next.submanifest_k = v;
    if (patch.submanifest_auto_retune) |v| next.submanifest_auto_retune = v;
    if (patch.autosync_off) |v| next.autosync_off = v;
    if (patch.ignore_glob) |v| next.ignore_glob = v;
    if (patch.clearsign) |v| next.clearsign = v;
    if (patch.pgp_command) |v| next.pgp_command = v;
    if (patch.autosync_set) |v| next.autosync_set = v;
    if (patch.clearsign_set) |v| next.clearsign_set = v;
    if (patch.repo_cksum_off) |v| next.repo_cksum_off = v;
    if (patch.repo_cksum_set) |v| next.repo_cksum_set = v;
    if (patch.keep_glob) |v| next.keep_glob = v;
    if (patch.manifest) |v| next.manifest = v;
    if (patch.before_commit) |v| next.before_commit = v;
    if (patch.before_commit_trust) |v| next.before_commit_trust = v;
    if (patch.binary_glob) |v| next.binary_glob = v;
    if (patch.crlf_glob) |v| next.crlf_glob = v;
    if (patch.editor) |v| next.editor = v;
    if (patch.autosync_mode) |v| next.autosync_mode = v;
    if (patch.crnl_glob) |v| next.crnl_glob = v;
    if (patch.clean_glob) |v| next.clean_glob = v;
    if (patch.empty_dirs) |v| next.empty_dirs = v;
    if (patch.encoding_glob) |v| next.encoding_glob = v;
    if (patch.dotfiles) |v| next.dotfiles = v;
    if (patch.mimetypes) |v| next.mimetypes = v;
    if (patch.attachment_size_limit) |v| next.attachment_size_limit = v;
    if (patch.git_mirror) |v| next.git_mirror = v;
    if (patch.git_autopush) |v| next.git_autopush = v;
    if (patch.git_email_domain) |v| next.git_email_domain = v;
    if (patch.git_email_domain_local) |v| next.git_email_domain_local = v;
    if (patch.git_push_branch) |v| next.git_push_branch = v;
    if (patch.default_branch) |v| next.default_branch = v;
    log.putConfig(next) catch |e| switch (e) {
        error.OutOfMemory => return error.OutOfMemory,
        error.Storage => return error.Storage,
    };
}

/// Open `dir/.zit` for a field-preserving config update, retaining the legacy
/// wrapper for pull, sync, serve, and remote callers that do not already own a
/// writable log.
pub fn updateConfig(
    gpa: std.mem.Allocator,
    io: std.Io,
    dir: std.Io.Dir,
    patch: ConfigPatch,
) PersistLockedError!void {
    var store_append = store_open.openStoreAppend(io, dir) catch |e| switch (e) {
        error.StoreBusy => return error.StoreBusy,
        else => return error.Storage,
    };
    defer store_append.close(io);
    const file = store_append.file;
    const st = file.stat(io) catch return error.Storage;
    var fs = storage.FileStorage.init(io, file, st.size);
    var log = logstore.LogStore.open(gpa, fs.storageFile(), 0) catch |e| switch (e) {
        error.OutOfMemory => return error.OutOfMemory,
        error.Storage => return error.Storage,
        error.Corrupt => return error.Corrupt,
    };
    defer log.deinit(gpa);
    try updateConfigOpen(gpa, &log, patch);
    log.commit() catch return error.Storage;
}

/// The closed error set of `persistStore`: memory, a storage write fault, or a
/// corrupt/oversized peer artifact name that will not decode to a log key.
pub const PersistError = error{ OutOfMemory, Storage, Corrupt };

/// The persist paths that take the writer lock themselves. `StoreBusy`:
/// another writer held it past the wait; stderr already names it, so a
/// caller reports nothing more.
pub const PersistLockedError = PersistError || error{StoreBusy};

/// Open this checkout's store, read its config ONCE, and hand back the owned
/// result; null on any failure, exactly as every reader of an optional
/// repository value degrades today. `.read_only`, because the listing verbs
/// are pinned tree-untouching (B-extras-16) and must never mint a sidecar.
/// The `OwnedConfig` owns its backing bytes, so the log and its file are
/// closed here and the config outlives them.
pub fn readRepositoryConfig(
    gpa: std.mem.Allocator,
    io: std.Io,
    root_dir: std.Io.Dir,
) ?logstore.OwnedConfig {
    var file = store_open.openStoreRead(io, root_dir) catch return null;
    defer file.close(io);
    const st = file.stat(io) catch return null;
    var fs = storage.FileStorage.init(io, file, st.size);
    // Indexed open: this config read rides every extras/clean/
    // addremove listing, each of which was paying its own full header scan.
    var log = openLogAdoptingIndex(gpa, io, root_dir, fs.storageFile(), .read_only) catch return null;
    defer log.deinit(gpa);
    return log.readConfig(gpa) catch null;
}