git: extract a function for creating RemoteCallbacks

This commit is contained in:
Martin von Zweigbergk 2021-10-09 09:03:17 -07:00
parent d434b948f1
commit fc883d1d02

View file

@ -14,7 +14,7 @@
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use git2::FetchPrune; use git2::{FetchPrune, RemoteCallbacks};
use itertools::Itertools; use itertools::Itertools;
use thiserror::Error; use thiserror::Error;
@ -140,24 +140,18 @@ pub fn fetch(
} }
_ => GitFetchError::InternalGitError(err), _ => GitFetchError::InternalGitError(err),
})?; })?;
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 mut fetch_options = git2::FetchOptions::new(); let mut fetch_options = git2::FetchOptions::new();
let mut proxy_options = git2::ProxyOptions::new(); let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto(); proxy_options.auto();
fetch_options.proxy_options(proxy_options); fetch_options.proxy_options(proxy_options);
let callbacks = create_remote_callbacks();
fetch_options.remote_callbacks(callbacks); fetch_options.remote_callbacks(callbacks);
fetch_options.prune(FetchPrune::On); fetch_options.prune(FetchPrune::On);
let refspec: &[&str] = &[]; let refspec: &[&str] = &[];
remote.download(refspec, Some(&mut fetch_options))?; remote.download(refspec, Some(&mut fetch_options))?;
// The FetchOptions above ate our RemoteCallbacks so it seems we need to create // The FetchOptions above ate our RemoteCallbacks so it seems we need to create
// a new instance. // a new instance.
let mut callbacks = git2::RemoteCallbacks::new(); let mut callbacks = create_remote_callbacks();
callbacks.credentials(|_url, username_from_url, _allowed_types| {
git2::Cred::ssh_key_from_agent(username_from_url.unwrap())
});
remote.update_tips( remote.update_tips(
Some(&mut callbacks), Some(&mut callbacks),
false, false,
@ -288,10 +282,7 @@ fn push_refs(
let mut proxy_options = git2::ProxyOptions::new(); let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto(); proxy_options.auto();
push_options.proxy_options(proxy_options); push_options.proxy_options(proxy_options);
let mut callbacks = git2::RemoteCallbacks::new(); let mut callbacks = create_remote_callbacks();
callbacks.credentials(|_url, username_from_url, _allowed_types| {
git2::Cred::ssh_key_from_agent(username_from_url.unwrap())
});
callbacks.push_update_reference(|refname, status| { callbacks.push_update_reference(|refname, status| {
// The status is Some if the ref update was rejected // The status is Some if the ref update was rejected
if status.is_none() { if status.is_none() {
@ -321,3 +312,11 @@ fn push_refs(
)) ))
} }
} }
fn create_remote_callbacks() -> RemoteCallbacks<'static> {
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.credentials(|_url, username_from_url, _allowed_types| {
git2::Cred::ssh_key_from_agent(username_from_url.unwrap())
});
callbacks
}