1use std::collections::HashMap;
2use std::sync::{Arc, LazyLock, RwLock};
3use tracing::trace;
4use uv_auth::Credentials;
5use uv_cache_key::RepositoryUrl;
6use uv_redacted::DisplaySafeUrl;
7
8pub static GIT_STORE: LazyLock<GitStore> = LazyLock::new(GitStore::default);
12
13#[derive(Debug, Default)]
15pub struct GitStore(RwLock<HashMap<RepositoryUrl, Arc<Credentials>>>);
16
17impl GitStore {
18 pub fn insert(&self, url: RepositoryUrl, credentials: Credentials) -> Option<Arc<Credentials>> {
20 self.0.write().unwrap().insert(url, Arc::new(credentials))
21 }
22
23 pub fn get(&self, url: &RepositoryUrl) -> Option<Arc<Credentials>> {
25 self.0.read().unwrap().get(url).cloned()
26 }
27}
28
29pub fn store_credentials_from_url(url: &DisplaySafeUrl) -> bool {
33 if let Some(credentials) = Credentials::from_url(url) {
34 trace!("Caching credentials for {url}");
35 GIT_STORE.insert(RepositoryUrl::new(url), credentials);
36 true
37 } else {
38 false
39 }
40}