use git2::{Config, ConfigLevel, RemoteCallbacks, Repository};
use git2_credentials::CredentialHandler;
use crate::error::Result;
use crate::ssh_config::apply_identity_agent;
pub fn get_remote_callbacks<'a>(
repo: &Repository,
url: Option<&str>,
) -> Result<RemoteCallbacks<'a>> {
build_callbacks(repo.config()?, url)
}
pub fn get_remote_callbacks_default<'a>(url: Option<&str>) -> Result<RemoteCallbacks<'a>> {
build_callbacks(open_default_config_lenient()?, url)
}
fn build_callbacks<'a>(config: Config, url: Option<&str>) -> Result<RemoteCallbacks<'a>> {
if let Some(url) = url {
apply_identity_agent(url);
}
let mut callbacks = RemoteCallbacks::new();
let mut credential_handler = CredentialHandler::new(config);
callbacks.credentials(move |url, username, allowed| {
credential_handler.try_next_credential(url, username, allowed)
});
Ok(callbacks)
}
fn open_default_config_lenient() -> std::result::Result<Config, git2::Error> {
if let Ok(c) = Config::open_default() {
return Ok(c);
}
let mut config = Config::new()?;
if let Ok(path) = Config::find_global() {
let _ = config.add_file(&path, ConfigLevel::Global, false);
}
if let Ok(path) = Config::find_xdg() {
let _ = config.add_file(&path, ConfigLevel::XDG, false);
}
if let Ok(path) = Config::find_system() {
let _ = config.add_file(&path, ConfigLevel::System, false);
}
Ok(config)
}