rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Keeping a `git` nobody watches from asking anything: the daemon's
//! plugin fetches (`plugins::git_source`) and its `rho/git/clone`.

use std::process::Command;

/// The ssh command git runs for [`SshCommand::Default`].
const BATCH_SSH_COMMAND: &str = "ssh -o BatchMode=yes";

/// Variables naming a program that asks the user something, or a display
/// one would open a window on: an ssh older than 8.4, which ignores
/// `SSH_ASKPASS_REQUIRE`, runs its built-in askpass when a display is set.
const PROMPT_ENV: [&str; 4] = ["GIT_ASKPASS", "SSH_ASKPASS", "DISPLAY", "WAYLAND_DISPLAY"];

/// The arguments that make git print the ssh command its configuration
/// sets (`core.sshCommand`); [`SshCommand::from_config_exit`] reads how it
/// exited.
pub(crate) const SSH_COMMAND_CONFIG: [&str; 3] = ["config", "--get", "core.sshCommand"];

/// Variables that set the ssh command git runs: when one is set, the user
/// chose it ([`SshCommand::UserConfigured`]).
pub(crate) const SSH_COMMAND_ENV: [&str; 2] = ["GIT_SSH", "GIT_SSH_COMMAND"];

/// Whose ssh command git runs for an ssh remote.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SshCommand {
    /// git's default `ssh`, which [`forbid_prompts`] runs in batch mode.
    Default,
    /// One the user set with `GIT_SSH_COMMAND`, `core.sshCommand`, or
    /// `GIT_SSH`, left as it is: it may be a wrapper that takes no ssh
    /// options. Without a terminal, a display, or an askpass program, an ssh
    /// it runs still has nothing to prompt with.
    UserConfigured,
}

impl SshCommand {
    /// Whose ssh command git runs when none of [`SSH_COMMAND_ENV`] is set,
    /// from how git exited on [`SSH_COMMAND_CONFIG`]: 0 the user's, 1 (the
    /// key is unset) git's default. `None` for any other exit, or none: git
    /// failed to say, and each caller decides what that means.
    pub(crate) fn from_config_exit(code: Option<i32>) -> Option<Self> {
        match code {
            Some(0) => Some(Self::UserConfigured),
            Some(1) => Some(Self::Default),
            _ => None,
        }
    }
}

/// Makes `command`, a git command line before its subcommand, fail rather
/// than prompt: no terminal (its own session on Unix, and
/// `GIT_TERMINAL_PROMPT=0`), no askpass program (`core.askPass` empty, which
/// also keeps git from falling back to `SSH_ASKPASS`), no ssh askpass
/// (`SSH_ASKPASS_REQUIRE=never`, and no display for an older ssh), no Git
/// Credential Manager window, and ssh in batch mode unless the user chose
/// the ssh command. Credential helpers still answer from what they stored.
/// A tokio command passes its `as_std_mut()`.
pub(crate) fn forbid_prompts(command: &mut Command, ssh: SshCommand) {
    for name in PROMPT_ENV {
        command.env_remove(name);
    }
    command
        .env("GIT_TERMINAL_PROMPT", "0")
        .env("SSH_ASKPASS_REQUIRE", "never")
        .env("GCM_INTERACTIVE", "never")
        .args(["-c", "core.askPass="]);
    match ssh {
        SshCommand::Default => {
            command.env("GIT_SSH_COMMAND", BATCH_SSH_COMMAND);
        }
        SshCommand::UserConfigured => {}
    }
    #[cfg(unix)]
    rho_tools::process_session::start_new_session(command);
}