Skip to content
zit Artifact [4c142da789]
Login

Artifact [4c142da789]

Artifact 4c142da7892d0d2f1f07917f41b1b89a047df582356c5bb512fd2bdb2e77a032:


//! `zit remote`: show or set the saved remote.

const std = @import("std");
const cli = @import("zit").cli;
const diagnostics = @import("../cli/diagnostics.zig");
const help_pages = @import("../cli/help.zig");
const logstore = @import("zit").logstore;
const not_a_repo_msg = diagnostics.not_a_repo_msg;
const refusePendingImport = @import("sync_dialogue.zig").refusePendingImport;
const remote_help = help_pages.remote_help;
const repoReadFail = diagnostics.repoReadFail;
const resolveRemoteUrl = zit.sync_session.resolveRemoteUrl;
const sameRemote = @import("sync_dialogue.zig").sameRemote;
const stateOom = diagnostics.stateOom;
const storeAppendFail = diagnostics.storeAppendFail;
const updateConfig = zit.repo_config.updateConfig;
const zit = @import("zit");

pub fn runRemote(
    gpa: std.mem.Allocator,
    io: std.Io,
    arena: std.mem.Allocator,
    verb_args: []const []const u8,
    streams: cli.Streams,
    repo_override: ?[]const u8,
) cli.Exit {
    const parsed = switch (cli.parseRemoteArgs(verb_args)) {
        .ok => |p| p,
        .unknown_option => |tok| return cli.usageError(streams, "remote", .unknown_option, tok),
        .extra_argument => |tok| return cli.usageError(streams, "remote", .extra_argument, tok),
        .not_a_url => |tok| {
            streams.err.print("zit remote: '{s}' is not an http:// or https:// URL\nzit remote: no subcommands: 'zit remote' shows the saved remote, 'zit remote URL' sets it, 'zit remote --unset' clears it\n", .{tok}) catch {};
            return .usage;
        },
    };
    if (parsed.help) {
        streams.out.writeAll(remote_help) catch {};
        return .ok;
    }

    const root = (cli.resolveRoot(arena, io, repo_override) catch return stateOom(streams, "remote")) 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, "remote")) |rc| return rc;

    if (parsed.unset) {
        // Overlay remote_url with empty — clears the saved value, every other
        // config field preserved by the field-preserving RMW. The login made
        // for that remote goes with it.
        updateConfig(gpa, io, root_dir, .{ .remote_url = "", .clear_login = true }) catch |e| switch (e) {
            error.OutOfMemory => return stateOom(streams, "remote"),
            else => return storeAppendFail(streams, "remote", e),
        };
        streams.out.writeAll("remote cleared\n") catch {};
        return .ok;
    }

    if (parsed.url.len > 0) {
        // A URL over the config-field cap cannot be stored (the putConfig assert);
        // reject it as usage rather than trip the assertion.
        if (parsed.url.len > logstore.max_config_str) {
            streams.err.print("zit remote: URL too long (max {d} bytes)\n", .{logstore.max_config_str}) catch {};
            return .failure;
        }
        // The stored login belongs to the saved remote: re-setting the same
        // remote keeps it, pointing at another drops it.
        var prior_buf: [logstore.max_config_str]u8 = undefined;
        const prior = resolveRemoteUrl(gpa, io, root_dir, "", &prior_buf) catch |e| switch (e) {
            error.OutOfMemory => return stateOom(streams, "remote"),
            error.Storage => return repoReadFail(streams, "remote"),
            error.NoRemote => "",
        };
        updateConfig(gpa, io, root_dir, .{ .remote_url = parsed.url, .clear_login = !sameRemote(prior, parsed.url) }) catch |e| switch (e) {
            error.OutOfMemory => return stateOom(streams, "remote"),
            else => return storeAppendFail(streams, "remote", e),
        };
        streams.out.print("remote set to {s}\n", .{parsed.url}) catch {};
        return .ok;
    }

    // Bare `zit remote` — print the saved remote (reusing resolveRemoteUrl's read
    // with an empty arg; NoRemote means none is saved).
    var url_buf: [logstore.max_config_str]u8 = undefined;
    const url = resolveRemoteUrl(gpa, io, root_dir, "", &url_buf) catch |e| switch (e) {
        error.OutOfMemory => return stateOom(streams, "remote"),
        error.Storage => return repoReadFail(streams, "remote"),
        error.NoRemote => {
            streams.out.writeAll("no remote set\n") catch {};
            return .ok;
        },
    };
    streams.out.print("{s}\n", .{url}) catch {};
    return .ok;
}