mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 02:04:19 +00:00
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:
parent
14fe58e76a
commit
7542c484a8
1 changed files with 9 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue