Skip to main content

workon/
get_remote_callbacks.rs

1use git2::{Config, RemoteCallbacks};
2use git2_credentials::CredentialHandler;
3
4use crate::error::Result;
5
6/// Build [`git2::RemoteCallbacks`] that handle credential prompts via the system
7/// credential store (SSH keys, keychain, etc.).
8///
9/// Uses [`git2_credentials::CredentialHandler`] which respects `~/.gitconfig`
10/// credential settings and SSH agent.
11pub fn get_remote_callbacks<'a>() -> Result<RemoteCallbacks<'a>> {
12    let mut callbacks = RemoteCallbacks::new();
13    let git_config = Config::open_default()?;
14    let mut credential_handler = CredentialHandler::new(git_config);
15
16    callbacks.credentials(move |url, username, allowed| {
17        credential_handler.try_next_credential(url, username, allowed)
18    });
19    Ok(callbacks)
20}