From 7542c484a86ff99d84c37789290b31df42553b78 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 2 Jan 2021 00:24:10 -0800 Subject: [PATCH] git: pass ssh credentials from ssh-agent on push I tried to push a commit from my Jujube repo to GitHub using `jj git push --branch main` and it became clear that we need to pass SSH credentials. This commit hopefully fixes that. I've only made it pass credentials for ssh-agent for now, because that seems to be enough to make it work for me personally. If this commit becomes visible on GitHub, it should mean that it worked. --- lib/src/git.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/src/git.rs b/lib/src/git.rs index 6b09e68d5..bf760d840 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -120,9 +120,16 @@ pub fn push_commit( _ => GitPushError::InternalGitError(err), })?; // Need to add "refs/heads/" prefix due to https://github.com/libgit2/libgit2/issues/1125 - let refspec = format!("{}:refs/heads/{}", temp_ref_name, remote_branch); + let qualified_remote_branch = format!("refs/heads/{}", remote_branch); + let mut callbacks = git2::RemoteCallbacks::new(); + callbacks.credentials(|_url, username_from_url, _allowed_types| { + git2::Cred::ssh_key_from_agent(username_from_url.unwrap()) + }); + let refspec = format!("{}:{}", temp_ref_name, qualified_remote_branch); + let mut push_options = git2::PushOptions::new(); + push_options.remote_callbacks(callbacks); remote - .push(&[refspec], None) + .push(&[refspec], Some(&mut push_options)) .map_err(|err| match (err.class(), err.code()) { (git2::ErrorClass::Reference, git2::ErrorCode::NotFastForward) => { GitPushError::NotFastForward