If GIT_ASKPASS is already set, assume it will do the right thing (#27681)

Fixes running git push on a coder instance.

Closes #ISSUE

Release Notes:

- Zed will now use `GIT_ASKPASS` if you already have one set instead of
overriding with our own. Fixes `git push` in Coder.
This commit is contained in:
Conrad Irwin 2025-03-28 14:50:05 -06:00 committed by GitHub
parent 4da987dad4
commit 4a5c492188
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 61 additions and 44 deletions

View file

@ -27,7 +27,7 @@ use util::command::{new_smol_command, new_std_command};
use util::ResultExt;
use uuid::Uuid;
pub use askpass::{AskPassResult, AskPassSession};
pub use askpass::{AskPassDelegate, AskPassResult, AskPassSession};
pub const REMOTE_CANCELLED_BY_USER: &str = "Operation cancelled by user";
@ -249,7 +249,7 @@ pub trait GitRepository: Send + Sync {
branch_name: String,
upstream_name: String,
options: Option<PushOptions>,
askpass: AskPassSession,
askpass: AskPassDelegate,
env: HashMap<String, String>,
// This method takes an AsyncApp to ensure it's invoked on the main thread,
// otherwise git-credentials-manager won't work.
@ -260,7 +260,7 @@ pub trait GitRepository: Send + Sync {
&self,
branch_name: String,
upstream_name: String,
askpass: AskPassSession,
askpass: AskPassDelegate,
env: HashMap<String, String>,
// This method takes an AsyncApp to ensure it's invoked on the main thread,
// otherwise git-credentials-manager won't work.
@ -269,7 +269,7 @@ pub trait GitRepository: Send + Sync {
fn fetch(
&self,
askpass: AskPassSession,
askpass: AskPassDelegate,
env: HashMap<String, String>,
// This method takes an AsyncApp to ensure it's invoked on the main thread,
// otherwise git-credentials-manager won't work.
@ -868,20 +868,17 @@ impl GitRepository for RealGitRepository {
branch_name: String,
remote_name: String,
options: Option<PushOptions>,
ask_pass: AskPassSession,
ask_pass: AskPassDelegate,
env: HashMap<String, String>,
_cx: AsyncApp,
cx: AsyncApp,
) -> BoxFuture<Result<RemoteCommandOutput>> {
let working_directory = self.working_directory();
let executor = cx.background_executor().clone();
async move {
let working_directory = working_directory?;
let mut command = new_smol_command("git");
command
.envs(env)
.env("GIT_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS_REQUIRE", "force")
.envs(&env)
.env("GIT_HTTP_USER_AGENT", "Zed")
.current_dir(&working_directory)
.args(["push"])
@ -894,9 +891,8 @@ impl GitRepository for RealGitRepository {
.stdin(smol::process::Stdio::null())
.stdout(smol::process::Stdio::piped())
.stderr(smol::process::Stdio::piped());
let git_process = command.spawn()?;
run_remote_command(ask_pass, git_process).await
run_git_command(env, ask_pass, command, &executor).await
}
.boxed()
}
@ -905,52 +901,48 @@ impl GitRepository for RealGitRepository {
&self,
branch_name: String,
remote_name: String,
ask_pass: AskPassSession,
ask_pass: AskPassDelegate,
env: HashMap<String, String>,
_cx: AsyncApp,
cx: AsyncApp,
) -> BoxFuture<Result<RemoteCommandOutput>> {
let working_directory = self.working_directory();
async {
let executor = cx.background_executor().clone();
async move {
let mut command = new_smol_command("git");
command
.envs(env)
.env("GIT_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS_REQUIRE", "force")
.envs(&env)
.env("GIT_HTTP_USER_AGENT", "Zed")
.current_dir(&working_directory?)
.args(["pull"])
.arg(remote_name)
.arg(branch_name)
.stdout(smol::process::Stdio::piped())
.stderr(smol::process::Stdio::piped());
let git_process = command.spawn()?;
run_remote_command(ask_pass, git_process).await
run_git_command(env, ask_pass, command, &executor).await
}
.boxed()
}
fn fetch(
&self,
ask_pass: AskPassSession,
ask_pass: AskPassDelegate,
env: HashMap<String, String>,
_cx: AsyncApp,
cx: AsyncApp,
) -> BoxFuture<Result<RemoteCommandOutput>> {
let working_directory = self.working_directory();
async {
let executor = cx.background_executor().clone();
async move {
let mut command = new_smol_command("git");
command
.envs(env)
.env("GIT_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS_REQUIRE", "force")
.envs(&env)
.env("GIT_HTTP_USER_AGENT", "Zed")
.current_dir(&working_directory?)
.args(["fetch", "--all"])
.stdout(smol::process::Stdio::piped())
.stderr(smol::process::Stdio::piped());
let git_process = command.spawn()?;
run_remote_command(ask_pass, git_process).await
run_git_command(env, ask_pass, command, &executor).await
}
.boxed()
}
@ -1355,7 +1347,37 @@ struct GitBinaryCommandError {
status: ExitStatus,
}
async fn run_remote_command(
async fn run_git_command(
env: HashMap<String, String>,
ask_pass: AskPassDelegate,
mut command: smol::process::Command,
executor: &BackgroundExecutor,
) -> Result<RemoteCommandOutput> {
if env.contains_key("GIT_ASKPASS") {
let git_process = command.spawn()?;
let output = git_process.output().await?;
if !output.status.success() {
Err(anyhow!("{}", String::from_utf8_lossy(&output.stderr)))
} else {
Ok(RemoteCommandOutput {
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
} else {
let ask_pass = AskPassSession::new(executor, ask_pass).await?;
let mut command = new_smol_command("git");
command
.env("GIT_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS", ask_pass.script_path())
.env("SSH_ASKPASS_REQUIRE", "force");
let git_process = command.spawn()?;
run_askpass_command(ask_pass, git_process).await
}
}
async fn run_askpass_command(
mut ask_pass: AskPassSession,
git_process: smol::process::Child,
) -> std::result::Result<RemoteCommandOutput, anyhow::Error> {