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.
This commit is contained in:
Martin von Zweigbergk 2021-01-02 00:24:10 -08:00
parent 14fe58e76a
commit 7542c484a8

View file

@ -120,9 +120,16 @@ pub fn push_commit(
_ => GitPushError::InternalGitError(err), _ => GitPushError::InternalGitError(err),
})?; })?;
// Need to add "refs/heads/" prefix due to https://github.com/libgit2/libgit2/issues/1125 // 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 remote
.push(&[refspec], None) .push(&[refspec], Some(&mut push_options))
.map_err(|err| match (err.class(), err.code()) { .map_err(|err| match (err.class(), err.code()) {
(git2::ErrorClass::Reference, git2::ErrorCode::NotFastForward) => { (git2::ErrorClass::Reference, git2::ErrorCode::NotFastForward) => {
GitPushError::NotFastForward GitPushError::NotFastForward