From ec45678ce3177a0e0b3eaeaef490862182e75318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sat, 8 Jan 2022 13:55:09 +0100 Subject: [PATCH] Fix SSH auth, fall back to file if agent fails --- src/repo.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/repo.rs b/src/repo.rs index 1dc5f35..c4811de 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::path::Path; -use git2::{Cred, RemoteCallbacks, Repository}; +use git2::Repository; use crate::output::*; @@ -1302,20 +1302,15 @@ fn get_remote_callbacks() -> git2::RemoteCallbacks<'static> { } Ok(()) }); - callbacks.credentials(|_url, username_from_url, _allowed_types| { - let username = match username_from_url { - Some(username) => username, - None => panic!("Could not get username. This is a bug"), - }; - git2::Cred::ssh_key_from_agent(username) - }); callbacks.credentials(|_url, username_from_url, _allowed_types| { let username = match username_from_url { Some(username) => username, None => panic!("Could not get username. This is a bug"), }; - git2::Cred::ssh_key(username, None, &crate::env_home().join(".ssh/id_rsa"), None) + git2::Cred::ssh_key_from_agent(username).or_else(|_| { + git2::Cred::ssh_key(username, None, &crate::env_home().join(".ssh/id_rsa"), None) + }) }); callbacks @@ -1392,17 +1387,17 @@ pub fn clone_repo( match remote.remote_type { RemoteType::Https | RemoteType::File => { let mut builder = git2::build::RepoBuilder::new(); + + let fetchopts = git2::FetchOptions::new(); + builder.bare(is_worktree); + builder.fetch_options(fetchopts); + builder.clone(&remote.url, &clone_target)?; } RemoteType::Ssh => { - let mut callbacks = RemoteCallbacks::new(); - callbacks.credentials(|_url, username_from_url, _allowed_types| { - Cred::ssh_key_from_agent(username_from_url.unwrap()) - }); - let mut fo = git2::FetchOptions::new(); - fo.remote_callbacks(callbacks); + fo.remote_callbacks(get_remote_callbacks()); let mut builder = git2::build::RepoBuilder::new(); builder.bare(is_worktree);