Skip to content
zit Artifact [6fc8ee91b8]
Login

Artifact [6fc8ee91b8]

Artifact 6fc8ee91b8d080452332e0e9f5f753717c823553903b47295fe2fe47b0812ee9:


//! `zit sync`: pull and push in one exchange, and the autosync a commit or
//! update runs against the saved remote.

const std = @import("std");
const ClientHoldings = zit.sync_session.ClientHoldings;
const CloneProgress = @import("sync_dialogue.zig").CloneProgress;
const ErasureLatch = zit.sync_session.ErasureLatch;
const LeafWatch = @import("sync_dialogue.zig").LeafWatch;
const PeerErrorCopy = zit.sync_session.PeerErrorCopy;
const RepoElidedSet = zit.sync_session.RepoElidedSet;
const ServerNotes = @import("sync_dialogue.zig").ServerNotes;
const SparseClassification = zit.sparse.SparseClassification;
const applySessionErasures = @import("session_erasures.zig").applySessionErasures;
const armBasicAuth = @import("sync_dialogue.zig").armBasicAuth;
const cli = @import("zit").cli;
const cmd_init = @import("init.zig");
const diagnostics = @import("../cli/diagnostics.zig");
const erasurePushRows = @import("session_erasures.zig").erasurePushRows;
const erasureRefused = @import("sync_dialogue.zig").erasureRefused;
const fallBackToContent = @import("sync_dialogue.zig").fallBackToContent;
const finishExchange = @import("sync_dialogue.zig").finishExchange;
const gapsAfterExchange = @import("pull.zig").gapsAfterExchange;
const heldSeedName = zit.sync_session.heldSeedName;
const help_pages = @import("../cli/help.zig");
const loadClusterIndex = zit.receive.loadClusterIndex;
const logstore = @import("zit").logstore;
const max_horizon_pull_reruns = @import("pull.zig").max_horizon_pull_reruns;
const max_password_len = @import("sync_dialogue.zig").max_password_len;
const noRemoteError = @import("sync_dialogue.zig").noRemoteError;
const not_a_repo_msg = diagnostics.not_a_repo_msg;
const noteLoginNotSaved = @import("sync_dialogue.zig").noteLoginNotSaved;
const notePlaintext = @import("sync_dialogue.zig").notePlaintext;
const noteVersionedIgnored = @import("settings.zig").noteVersionedIgnored;
const openStoreLogForRead = zit.repo_config.openStoreLogForRead;
const persistStore = zit.receive.persistStore;
const pullRun = @import("sync_exchange.zig").pullRun;
const readSyncCone = zit.sync_session.readSyncCone;
const readSyncCursor = zit.sync_session.readSyncCursor;
const reclassifyPull = zit.sync_session.reclassifyPull;
const refusePendingImport = @import("sync_dialogue.zig").refusePendingImport;
const renderStorage = cmd_init.renderStorage;
const repoReadFail = diagnostics.repoReadFail;
const reportPeerRefusal = @import("sync_dialogue.zig").reportPeerRefusal;
const reportTruncatedPayloads = @import("sync_dialogue.zig").reportTruncatedPayloads;
const reportUvSkipped = @import("sync_dialogue.zig").reportUvSkipped;
const resolveCredential = @import("sync_dialogue.zig").resolveCredential;
const resolveRemoteUrl = zit.sync_session.resolveRemoteUrl;
const resolveSetting = zit.repo_config.resolveSetting;
const runPull = @import("pull.zig").runPull;
const runPush = @import("push.zig").runPush;
const savedRemoteIn = zit.sync_session.savedRemoteIn;
const stateOom = diagnostics.stateOom;
const storePushCredential = @import("sync_dialogue.zig").storePushCredential;
const storedLoginIn = @import("sync_dialogue.zig").storedLoginIn;
const syncIdentityIn = zit.sync_session.syncIdentityIn;
const syncOpenFail = @import("sync_dialogue.zig").syncOpenFail;
const syncRun = @import("sync_exchange.zig").syncRun;
const syncRunFail = @import("sync_dialogue.zig").syncRunFail;
const sync_driver = @import("../net/sync_driver.zig");
const sync_help = help_pages.sync_help;
const transport = @import("../net/transport.zig");
const writeSyncCursor = zit.sync_session.writeSyncCursor;
const zit = @import("zit");

// ---------------------------------------------------------------------------
// `zit sync [URL]` — BIDIRECTIONAL convergence with a peer in ONE session.
// Unlike pull-then-push, a single `operation=.sync` client announces igot,
// requests gimme, AND uploads on one §3.7 exchange (§3.2 round structure: a sync
// request carries both push and pull each round) — not a third copy of the driver
// loop over two transport sessions.
// Opens the cwd's `.zit`, resolves the URL (arg → saved remote), may
// authenticate (sync uploads), persists what it receives like pull, and reports
// both directions. A saved `zit remote` lets the URL be omitted.
// ---------------------------------------------------------------------------

pub fn runSync(
    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.parseSyncArgs(verb_args)) {
        .ok => |p| p,
        .unknown_option => |tok| return cli.usageError(streams, "sync", .unknown_option, tok),
        .extra_argument => |tok| return cli.usageError(streams, "sync", .extra_argument, tok),
        .bad_resolve => |tok| return cli.usageError(streams, "sync", .bad_resolve, tok),
        .too_many_resolves => |tok| return cli.usageError(streams, "sync", .too_many_resolves, tok),
    };
    if (parsed.help) {
        streams.out.writeAll(sync_help) catch {};
        return .ok;
    }

    // sync §6.3 — a login name is whitespace-free (mirrors push). Reject a
    // malformed --user as usage before the secret derivation's assertion.
    if (parsed.user) |u| {
        if (u.len > 0 and zit.sync.containsFossilWhitespace(u)) {
            streams.err.writeAll("zit sync: --user must not contain whitespace\n") catch {};
            return .failure;
        }
    }

    // Locate the current repository before any network I/O.
    const root = (cli.resolveRoot(arena, io, amb.repo_override) catch return stateOom(streams, "sync")) orelse {
        streams.err.writeAll(not_a_repo_msg) catch {};
        return .failure;
    };
    var root_dir = root.dir;
    defer root_dir.close(io);
    if (refusePendingImport(streams, io, root_dir, "sync")) |rc| return rc;
    // The client's holdings view — log-backed and lazy: the igot
    // announcement, the upload source, and the baseline for the received
    // count. The held set can only grow (§3.3 storage only adds content).
    // Its log is the one this run reads config from, so the store is opened
    // once.
    var holdings = ClientHoldings.open(gpa, io, root_dir) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
    };
    defer holdings.deinit(io);
    // The leaves before the exchange tell a fork it made from one already here.
    // Read after the holdings open, which heals a torn final frame: the leaf
    // read is a reader and refuses one.
    var leaves: LeafWatch = undefined;
    if (!leaves.open(gpa, io, arena, root_dir, streams, "sync")) return .failure;

    // sync §4/§6 — the explicit URL wins; an omitted one falls back to the saved
    // remote; a repo with neither is a usage error. `url_buf` outlives `open`.
    var url_buf: [logstore.max_config_str]u8 = undefined;
    const url = if (parsed.url.len > 0) parsed.url else savedRemoteIn(gpa, &holdings.index.log, &url_buf) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
        error.NoRemote => return noRemoteError(streams, "sync"),
    };

    var tr = transport.Transport.open(io, gpa, url) catch |e|
        return syncOpenFail(streams, "sync", url, e);
    defer tr.close();
    tr.setResolve(parsed.resolve.slice());
    notePlaintext(streams, "sync", url);

    var admission = holdings.admission();
    var seed_name_buf: [64]u8 = undefined;
    // Sparse-cone RFC section 5.2 rule 3: under a store cone, classify once
    // and suppress spontaneous requests for elided phantoms. Both the
    // classification and the wrapper outlive the run below.
    var sync_cone: ?zit.cone.Cone = null;
    defer if (sync_cone) |*c| c.deinit(gpa);
    var sync_classification: ?SparseClassification = null;
    defer if (sync_classification) |*c| c.deinit(gpa);
    var sync_elided: RepoElidedSet = undefined;
    if (readSyncCone(gpa, io, root_dir, &holdings)) |c| {
        sync_cone = c;
        sync_classification = zit.sparse.classifyCached(gpa, io, root_dir, &holdings.index.log, holdings.index.classifyStore(), &sync_cone.?, .{ .boundary_transit = true }, .parse) catch
            return stateOom(streams, "sync");
        sync_elided = .{ .classification = &sync_classification.?, .store = holdings.store() };
        admission.elided = sync_elided.shunSet();
        admission.cone_members = sync_cone.?.members;
        admission.cone_not_members = sync_cone.?.not_members;
        admission.cone_parents = sync_cone.?.parents;
        admission.horizon = sync_cone.?.horizon;
        admission.seed_held = heldSeedName(gpa, io, root_dir, &seed_name_buf);
        if (readSyncCursor(gpa, io, root_dir, url, &sync_cone.?, holdings.logBackedCount())) |cur| {
            admission.horizon_cursor = cur.peer;
            admission.announce_from = cur.local + 1;
        }
    }

    // §9.1 — announce this repository's adopted project code (a real peer refuses
    // a mismatch). The buffer outlives the run — the client borrows it.
    // The cluster index: what the held clusters already announce
    // (never announced again individually) and what they promised that this
    // repository never collected (seeded as phantoms, so the run asks for it).
    // The clusters' plaintext is memoised by the holdings adapter while it
    // resolves; drop it — the index keeps only names and bits.
    var clusters = loadClusterIndex(gpa, io, root_dir, &holdings.index.log, holdings.store()) catch return stateOom(streams, "sync");
    defer clusters.deinit();
    holdings.index.releaseTransient();

    var pc_buf: [zit.sync.max_projectcode_len]u8 = undefined;
    const identity = syncIdentityIn(gpa, &holdings.index.log, root.abs, &pc_buf) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
    };

    // sync §6 — resolve the login credential (null for anonymous). A sync
    // uploads, so it may authenticate exactly as push does; the plaintext lives
    // only in `pw_buf`/argv/env and is scrubbed on every exit from this scope.
    var pw_buf: [max_password_len]u8 = undefined;
    defer std.crypto.secureZero(u8, &pw_buf);
    var user_buf: [logstore.max_config_str]u8 = undefined;
    const stored_login = storedLoginIn(gpa, &holdings.index.log) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
    };
    const resolved = resolveCredential(stored_login, streams, identity.project_code, .{ .url = url, .user = parsed.user, .password = parsed.password }, amb, &pw_buf, &user_buf) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
        error.NoPassword => {
            streams.err.writeAll("zit sync: no password supplied for --user\n") catch {};
            return .failure;
        },
    };

    const stderr_tty = std.Io.File.stderr().isTty(io) catch false;
    var progress: CloneProgress = .{ .err = streams.err };
    const sink: ?sync_driver.RoundSink = if (stderr_tty) .{ .ctx = &progress, .emit = CloneProgress.emit } else null;
    defer progress.finishLine();
    if (resolved) |r| armBasicAuth(&tr, r.credential.user, r.plaintext) catch return stateOom(streams, "sync");
    var peer_error: PeerErrorCopy = .{};
    var notes: ServerNotes = .{};
    defer notes.deinit(gpa);
    var truncated: u64 = 0;
    defer reportTruncatedPayloads(streams, "sync", truncated);
    var latch: ErasureLatch = .{};
    defer latch.deinit(gpa);
    admission.erasure_latch = &latch;
    admission.push_erasures = erasurePushRows(arena, &holdings.index.log) catch return stateOom(streams, "sync");
    var erasures_refused = false;
    var outcome = syncRun(gpa, &tr, holdings.store(), identity, if (resolved) |r| r.credential else null, admission, &peer_error, &notes, sink, &clusters, &truncated) catch |e| retry: {
        // As for push: a refusal is quoted in the server's own words.
        if (e == error.FatalProtocolError) {
            if (peer_error.text()) |text| {
                if (erasureRefused(streams, "sync", text, admission.push_erasures.len)) {
                    erasures_refused = true;
                    admission.push_erasures = &.{};
                    peer_error = .{};
                    break :retry syncRun(gpa, &tr, holdings.store(), identity, if (resolved) |r| r.credential else null, admission, &peer_error, &notes, sink, &clusters, &truncated) catch |e2| {
                        if (e2 == error.FatalProtocolError) if (peer_error.text()) |t2| {
                            reportPeerRefusal(streams, "sync", t2);
                            return .failure;
                        };
                        return syncRunFail(streams, "sync", url, e2, &tr);
                    };
                }
                reportPeerRefusal(streams, "sync", text);
                return .failure;
            }
        }
        return syncRunFail(streams, "sync", url, e, &tr);
    };
    notes.show(streams);

    // Horizon draft sections 6.2.5 and 6.3 -- as for pull: an origin that did
    // not serve the structural horizon leaves the store at the content one,
    // and pulls bring what that depth holds below C. The sync's own sending
    // is done; these runs only receive.
    if (sync_cone) |*c| {
        if (fallBackToContent(gpa, io, root_dir, streams, "sync", c, outcome.horizon_served) catch return stateOom(streams, "sync")) {
            admission.horizon = c.horizon;
            admission.cone_members = c.members;
            admission.cone_not_members = c.not_members;
            admission.cone_parents = c.parents;
            admission.seed_held = null;
            admission.horizon_cursor = null;
            admission.announce_from = 1;
            var again: usize = 0;
            while (again < max_horizon_pull_reruns) : (again += 1) {
                const unveiled = reclassifyPull(gpa, io, root_dir, &holdings, c, &sync_classification.?) catch return stateOom(streams, "sync");
                if (!unveiled) break;
                var more = pullRun(gpa, &tr, holdings.store(), identity, if (resolved) |r| r.credential else null, admission, sink, &clusters, &truncated, null, null) catch |e|
                    return syncRunFail(streams, "sync", url, e, &tr);
                if (more.seed) |b| gpa.free(b);
                more.seed = null;
                outcome.rounds += more.rounds;
                if (more.peer_cursor) |cur| outcome.peer_cursor = cur;
            }
        }
    }

    // Sparse-cone RFC section 6.3 applies to EVERY receive: a dense peer
    // volunteers content the client never announced, so the filter runs at
    // persist. Reclassified over the post-run store, so a brand-new commit's
    // blobs classify by their own F cards -- new in-cone content persists,
    // new out-of-cone content elides with the rest.
    var post_classification: ?SparseClassification = null;
    defer if (post_classification) |*c| c.deinit(gpa);
    if (sync_cone) |*c| {
        post_classification = zit.sparse.classifyCached(gpa, io, root_dir, &holdings.index.log, holdings.index.classifyStore(), c, .{}, .parse) catch
            return stateOom(streams, "sync");
    }
    // As for pull: persist only what this sync received, past the log-backed
    // prefix. Starting at rid 1 re-read every holding, and in a repository
    // holding sub-manifests re-inflated each one to rescan its references.
    const persisted = persistStore(gpa, io, root_dir, holdings.store(), if (post_classification) |*c| c else null, holdings.logBackedCount()) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        error.Storage => return repoReadFail(streams, "sync"),
        error.StoreBusy => return .failure, // the lock notice already said it
        error.Corrupt => return renderStorage(streams, root.abs, "the peer sent a malformed artifact name"),
    };
    const received = persisted.written;
    // Last, on every exit from here: the rows go in after what the session
    // stored, and a reclaim rewrites the store only once nothing else will.
    var erasures_deferred: ?usize = null;
    defer if (erasures_deferred == null) {
        _ = applySessionErasures(gpa, io, root_dir, streams, "sync", &holdings.index.log, latch.rows.items, true);
    };

    // DECISION-3 — persist a freshly derived secret only after the peer accepted
    // the authenticated session, and only when it was freshly derived (mirrors push).
    if (resolved) |r| {
        // --once authenticates this run and saves nothing.
        if (r.persist and !parsed.once) {
            const saved = storePushCredential(gpa, io, root_dir, r.credential, url) catch |e| switch (e) {
                error.OutOfMemory => return stateOom(streams, "sync"),
                else => return repoReadFail(streams, "sync"),
            };
            if (!saved) noteLoginNotSaved(streams, "sync", url);
        }
    }

    // submanifests §7.2 — "the sender MUST refuse to transmit that artifact,
    // with an explicit error -- never by converting, downgrading, or silently
    // omitting it." A client cannot put an `error` card in a REQUEST (it is
    // outside the §3.9 strict-accept request vocabulary), so the explicit error
    // is this: the operator is told exactly what was withheld and why, and the
    // run fails. Reported before the success line, because there was none.
    if (outcome.refused > 0) {
        streams.err.print(
            "zit sync: {d} artifacts were withheld: {s}\n",
            .{ outcome.refused, zit.sync.submanifest_peer_error_text },
        ) catch {};
        return .failure;
    }

    // submanifests §7.1/§7.3 — see `runPull`: convergence against the peer is
    // not the same fact as a complete tree closure.
    const submanifest_gaps = gapsAfterExchange(gpa, io, root_dir, &holdings, if (post_classification) |*c| c else null) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "sync"),
        else => return repoReadFail(streams, "sync"),
    };
    if (submanifest_gaps > 0) {
        streams.err.print(
            "zit sync: {d} sub-manifest references are missing; some check-ins cannot be enumerated\n",
            .{submanifest_gaps},
        ) catch {};
        return .failure;
    }

    // Under -F json the counts are the object's, and stdout holds only it.
    if (outcome.sent == 0 and received == 0) {
        if (!parsed.json) streams.out.print("sync with {s}: already in sync\n", .{url}) catch {};
    } else {
        if (!parsed.json) streams.out.print("sync with {s}: sent {d}, received {d} in {d} rounds\n", .{ url, outcome.sent, received, outcome.rounds }) catch {};
        reportUvSkipped(streams, "sync", outcome.uv_skipped);
    }
    if (sync_cone) |*c| writeSyncCursor(gpa, io, root_dir, url, c, .{
        .peer = outcome.peer_cursor orelse admission.horizon_cursor,
        .local = holdings.logBackedCount() + received,
    });
    if (outcome.horizon_served != null) zit.repocache.writeHorizonUrl(io, root_dir, outcome.horizon_url.get());
    // Before the JSON is written, so the object can say what did not land.
    erasures_deferred = applySessionErasures(gpa, io, root_dir, streams, "sync", &holdings.index.log, latch.rows.items, true);
    const rc = finishExchange(gpa, io, arena, root_dir, streams, "sync", &leaves, parsed.json, .{
        .sent = outcome.sent,
        .received = received,
        .rounds = outcome.rounds,
        .erasures_deferred = erasures_deferred.?,
    });
    return if (erasures_refused) .failure else rc;
}

/// The autosync hook: a best-effort pull (before commit/update/merge)
/// or push (after a successful commit) against the saved remote. Silent no-op
/// when autosync is off or no remote is saved (a nag would punish the
/// single-repository workflow); any failure is ONE warning line and the
/// caller proceeds — autosync must never turn a working offline operation
/// into an error (fossil's posture). The exchange itself is the real verb
/// runner, so behavior, diagnostics, and persistence never fork from a
/// hand-run `zit pull`/`zit push`.
pub const AutosyncOp = enum { pull, push };

pub fn autosyncPull(gpa: std.mem.Allocator, io: std.Io, arena: std.mem.Allocator, streams: cli.Streams, verb: []const u8) void {
    autosync(gpa, io, arena, streams, verb, .pull, null);
}

pub fn autosyncPush(gpa: std.mem.Allocator, io: std.Io, arena: std.mem.Allocator, streams: cli.Streams, verb: []const u8, amb: cli.Ambient) void {
    autosync(gpa, io, arena, streams, verb, .push, amb);
}

pub fn autosync(
    gpa: std.mem.Allocator,
    io: std.Io,
    arena: std.mem.Allocator,
    streams: cli.Streams,
    verb: []const u8,
    op: AutosyncOp,
    amb: ?cli.Ambient,
) void {
    {
        const root = (cli.discoverRoot(arena, io) catch return) orelse return;
        var root_dir = root.dir;
        defer root_dir.close(io);
        // Once per invocation: the pull hook runs before the push one on
        // every verb that has both, so this is the first hook to fire.
        if (op == .pull) noteVersionedIgnored(io, root_dir, streams, .autosync);
        const off = readAutosyncOff(gpa, io, root_dir) catch return;
        if (off) return;
        // `autosync pullonly`: pull as usual, never push.
        if (op == .push) {
            if (amb) |a| {
                const r = resolveSetting(gpa, io, arena, root_dir, .{ .environ = a.environ }, .autosync) catch return;
                if (std.mem.eql(u8, r.value, "pullonly")) return;
            }
        }
        var url_buf: [logstore.max_config_str]u8 = undefined;
        _ = resolveRemoteUrl(gpa, io, root_dir, "", &url_buf) catch return;
    }
    // The exchange reports on STDERR only: the hosting verb owns stdout (a
    // commit's contract is ONE check-in line there), and autosync is ambient
    // narration, not the verb's answer.
    const side_streams: cli.Streams = .{ .out = streams.err, .err = streams.err };
    const rc = switch (op) {
        .pull => runPull(gpa, io, arena, &.{}, side_streams, amb orelse .{ .now_epoch = 0, .user_env = null }),
        .push => runPush(gpa, io, arena, &.{}, side_streams, amb.?),
    };
    if (rc != .ok) {
        streams.err.print(
            "zit {s}: autosync {s} failed; continuing locally\n",
            .{ verb, @tagName(op) },
        ) catch {};
    }
}

/// The stored autosync deviation, read off the config frame. Storage faults
/// propagate; a repository with no config frame reads as the default (on).
fn readAutosyncOff(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !bool {
    const opened = try openStoreLogForRead(gpa, io, dir);
    defer opened.close(gpa, io);
    var oc = (opened.log.readConfig(gpa) catch |e| switch (e) {
        error.OutOfMemory => return error.OutOfMemory,
        else => return error.Storage,
    }) orelse return false;
    defer oc.deinit(gpa);
    return oc.config.autosync_off;
}