Respect pyproject.toml credentials from user-provided requirements (#7474)

## Summary

When syncing a lockfile, we need to respect credentials defined in the
`pyproject.toml`, even if they won't be used for resolution.
Unfortunately, this includes credentials in `tool.uv.sources`,
`tool.uv.dev-dependencies`, `project.dependencies`, and
`project.optional-dependencies`.

Closes https://github.com/astral-sh/uv/issues/7453.
This commit is contained in:
Charlie Marsh 2024-09-17 15:09:11 -04:00 committed by GitHub
parent 08a7c708d1
commit c2ad31aa58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 317 additions and 29 deletions

View file

@ -1,9 +1,15 @@
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use cache_key::RepositoryUrl;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock, RwLock};
use tracing::trace;
use url::Url;
use uv_auth::Credentials;
/// Global authentication cache for a uv invocation.
///
/// This is used to share Git credentials within a single process.
pub static GIT_STORE: LazyLock<GitStore> = LazyLock::new(GitStore::default);
/// A store for Git credentials.
#[derive(Debug, Default)]
pub struct GitStore(RwLock<HashMap<RepositoryUrl, Arc<Credentials>>>);
@ -19,3 +25,16 @@ impl GitStore {
self.0.read().unwrap().get(url).cloned()
}
}
/// Populate the global authentication store with credentials on a Git URL, if there are any.
///
/// Returns `true` if the store was updated.
pub fn store_credentials_from_url(url: &Url) -> bool {
if let Some(credentials) = Credentials::from_url(url) {
trace!("Caching credentials for {url}");
GIT_STORE.insert(RepositoryUrl::new(url), credentials);
true
} else {
false
}
}

View file

@ -1,8 +1,6 @@
use std::sync::LazyLock;
use url::Url;
use crate::credentials::GitStore;
pub use crate::credentials::{store_credentials_from_url, GIT_STORE};
pub use crate::git::GitReference;
pub use crate::resolver::{
GitResolver, GitResolverError, RepositoryReference, ResolvedRepositoryReference,
@ -16,11 +14,6 @@ mod resolver;
mod sha;
mod source;
/// Global authentication cache for a uv invocation.
///
/// This is used to share Git credentials within a single process.
pub static GIT_STORE: LazyLock<GitStore> = LazyLock::new(GitStore::default);
/// A URL reference to a Git repository.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Hash, Ord)]
pub struct GitUrl {