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(crate) static GIT_STORE: LazyLock<GitStore> = LazyLock::new(GitStore::default);
12
13#[derive(Debug, Default)]
15pub(crate) struct GitStore(RwLock<HashMap<RepositoryUrl, Arc<Credentials>>>);
16
17impl GitStore {
18 fn insert(&self, url: RepositoryUrl, credentials: Credentials) {
20 self.0.write().unwrap().insert(url, Arc::new(credentials));
21 }
22
23 pub(crate) fn get(&self, url: &RepositoryUrl) -> Option<Arc<Credentials>> {
25 self.0.read().unwrap().get(url).cloned()
26 }
27}
28
29pub fn store_credentials(url: RepositoryUrl, credentials: Credentials) {
31 GIT_STORE.insert(url, credentials);
32}
33
34pub fn store_credentials_from_url(url: &DisplaySafeUrl) -> bool {
38 if let Some(credentials) = Credentials::from_url(url) {
39 trace!("Caching credentials for {url}");
40 store_credentials(RepositoryUrl::new(url), credentials);
41 true
42 } else {
43 false
44 }
45}