Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip unneeded url parse and only add git+ when needed #3139

Merged
merged 6 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 88 additions & 14 deletions crates/pixi_uv_conversions/src/requirements.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use pixi_manifest::{pypi::VersionOrStar, PyPiRequirement};
use pixi_spec::{GitReference, GitSpec};
use std::{
path::{Path, PathBuf},
str::FromStr,
};

// use pep440_rs::VersionSpecifiers;

use pixi_git::url::RepositoryUrl;
use pixi_manifest::{pypi::VersionOrStar, PyPiRequirement};
use pixi_spec::{GitReference, GitSpec};
use thiserror::Error;
use url::Url;
use uv_distribution_filename::DistExtension;
Expand All @@ -24,8 +20,13 @@ fn create_uv_url(
rev: Option<&GitReference>,
subdir: Option<&str>,
) -> Result<Url, url::ParseError> {
// Create the url.
let url = format!("git+{url}");
// Add the git+ prefix if it doesn't exist.
let url = url.to_string();
let url = match url.strip_prefix("git+") {
Some(_) => url,
None => format!("git+{}", url),
};

// Add the tag or rev if it exists.
let url = rev.as_ref().map_or_else(
|| url.clone(),
Expand Down Expand Up @@ -105,23 +106,36 @@ pub fn as_uv_req(
},
..
} => RequirementSource::Git {
repository: RepositoryUrl::parse(git.as_str())
.map_err(|err| AsPep508Error::UrlParseError {
source: err,
url: git.to_string(),
})?
.into(),
// Url is already a git url, should look like:
// - 'ssh://git@github.com/user/repo'
// - 'https://github.com/user/repo'
repository: {
if git.scheme().strip_prefix("git+").is_some() {
// Setting the scheme might fail, so using string manipulation instead
let url_str = git.to_string();
let stripped = url_str.strip_prefix("git+").unwrap_or(&url_str);
// Reparse the url with the new scheme, should be safe but could fail so fall back to the original.
Url::parse(stripped).unwrap_or(git.clone())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be an actual error? When would it be okay if this fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, let me figure out why I needed that. As I remember sometimes this having the prefix and sometime not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't so it's not an error

} else {
git.clone()
}
},
// Unique identifier for the commit, as Git object identifier
precise: rev
.as_ref()
.map(|s| s.as_full_commit())
.and_then(|s| s.map(uv_git::GitOid::from_str))
.transpose()
.expect("could not parse sha"),
// The reference to the commit to use, which could be a branch, tag or revision.
reference: rev
.as_ref()
.map(|rev| into_uv_git_reference(rev.clone().into()))
.unwrap_or(uv_git::GitReference::DefaultBranch),
subdirectory: subdirectory.as_ref().and_then(|s| s.parse().ok()),
// The full url used to clone, comparable to the git+ url in pip. e.g:
// - 'git+SCHEMA://HOST/PATH@REF#subdirectory=SUBDIRECTORY'
// - 'git+ssh://github.com/user/repo@d099af3b1028b00c232d8eda28a997984ae5848b'
url: VerbatimUrl::from_url(
create_uv_url(git, rev.as_ref(), subdirectory.as_deref()).map_err(|e| {
AsPep508Error::UrlParseError {
Expand Down Expand Up @@ -199,3 +213,63 @@ pub fn as_uv_req(
origin: None,
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_git_url() {
let pypi_req = PyPiRequirement::Git {
url: GitSpec {
git: Url::parse("ssh://git@github.com/user/test.git").unwrap(),
rev: Some(GitReference::Rev(
"d099af3b1028b00c232d8eda28a997984ae5848b".to_string(),
)),
subdirectory: None,
},
extras: vec![],
};
let uv_req = as_uv_req(&pypi_req, "test", Path::new("")).unwrap();

let expected_uv_req = RequirementSource::Git {
repository: Url::parse("ssh://git@github.com/user/test.git").unwrap(),
precise: Some(uv_git::GitOid::from_str("d099af3b1028b00c232d8eda28a997984ae5848b").unwrap()),
reference: uv_git::GitReference::BranchOrTagOrCommit("d099af3b1028b00c232d8eda28a997984ae5848b".to_string()),
subdirectory: None,
url: VerbatimUrl::from_url(Url::parse("git+ssh://git@github.com/user/test.git@d099af3b1028b00c232d8eda28a997984ae5848b").unwrap()),
};

assert_eq!(uv_req.source, expected_uv_req);

// With git+ prefix
let pypi_req = PyPiRequirement::Git {
url: GitSpec {
git: Url::parse("git+https://github.com/user/test.git").unwrap(),
rev: Some(GitReference::Rev(
"d099af3b1028b00c232d8eda28a997984ae5848b".to_string(),
)),
subdirectory: None,
},
extras: vec![],
};
let uv_req = as_uv_req(&pypi_req, "test", Path::new("")).unwrap();
let expected_uv_req = RequirementSource::Git {
repository: Url::parse("https://github.com/user/test.git").unwrap(),
precise: Some(
uv_git::GitOid::from_str("d099af3b1028b00c232d8eda28a997984ae5848b").unwrap(),
),
reference: uv_git::GitReference::BranchOrTagOrCommit(
"d099af3b1028b00c232d8eda28a997984ae5848b".to_string(),
),
subdirectory: None,
url: VerbatimUrl::from_url(
Url::parse(
"git+https://github.com/user/test.git@d099af3b1028b00c232d8eda28a997984ae5848b",
)
.unwrap(),
),
};
assert_eq!(uv_req.source, expected_uv_req);
}
}
4 changes: 2 additions & 2 deletions src/lock_file/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,12 +2065,12 @@ async fn spawn_solve_pypi_task<'p>(
.collect::<Result<_, ConversionError>>()
.into_diagnostic()?;

let index_map = IndexMap::from_iter(dependencies);
let requirements = IndexMap::from_iter(dependencies);

let (records, prefix_task_result) = lock_file::resolve_pypi(
resolution_context,
&pypi_options,
index_map,
requirements,
system_requirements,
&pixi_solve_records,
&locked_pypi_records,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_rust/add_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async fn add_functionality_os() {

/// Test the `pixi add --pypi` functionality
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[cfg_attr(not(feature = "slow_integration_tests"), ignore)]
// #[cfg_attr(not(feature = "slow_integration_tests"), ignore)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to comment it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oowps nope, did that for testing locally

async fn add_pypi_functionality() {
let pixi = PixiControl::new().unwrap();

Expand Down
Loading