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

Handle parsing a PackageId that is a raw registry url into a Crate #758

Merged
merged 10 commits into from
Jan 14, 2025
Prev Previous commit
Next Next commit
use repo name for github crates instead of crate name
  • Loading branch information
Skgland committed Jan 12, 2025
commit 47753985f599dca91120bffe599509b1fe7a0cd9
54 changes: 35 additions & 19 deletions src/crates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,23 @@ impl TryFrom<&'_ PackageId> for Crate {
Some(SourceKind::Git(rev)) => {
if let Some(url) = package_id.url() {
if url.domain() == Some("github.com") {
Ok(Crate::GitHub(GitHubRepo {
org: url
.path_segments()
.and_then(|mut path| path.next())
.unwrap_or_default()
.to_string(),
name: package_id.name().to_string(),
sha: rev.pretty_ref(false).map(|rev| rev.to_string()),
}))
if let Some(mut path) = url.path_segments() {
let Some(org) = path.next() else {
bail!("Github URL path is too short")
};

let Some(repo_name) = path.next() else {
bail!("Github URL path is too short")
};

Ok(Crate::GitHub(GitHubRepo {
org: org.to_string(),
name: repo_name.to_string(),
sha: rev.pretty_ref(false).map(|rev| rev.to_string()),
}))
} else {
bail!("Github Git URL doesn't have a valid path")
}
} else {
Ok(Crate::Git(GitRepo {
url: url.to_string(),
Expand Down Expand Up @@ -179,15 +187,23 @@ impl TryFrom<&'_ PackageId> for Crate {
Some(url) => match url.scheme() {
"http" | "https" | "git" | "ssh" => {
if url.domain() == Some("github.com") {
Ok(Crate::GitHub(GitHubRepo {
org: url
.path_segments()
.and_then(|mut path| path.next())
.unwrap_or_default()
.to_string(),
name: package_id.name().to_string(),
sha: None,
}))
if let Some(mut path) = url.path_segments() {
let Some(org) = path.next() else {
bail!("Github URL path is too short")
};

let Some(repo_name) = path.next() else {
bail!("Github URL path is too short")
};

Ok(Crate::GitHub(GitHubRepo {
org: org.to_string(),
name: repo_name.to_string(),
sha: None,
}))
} else {
bail!("Github Git URL doesn't have a valid path")
}
} else {
Ok(Crate::Git(GitRepo {
url: url.to_string(),
Expand Down Expand Up @@ -359,7 +375,7 @@ mod tests {
}),
"https://github.com/rust-lang/cargo#[email protected]" => Crate::GitHub(GitHubRepo {
org: "rust-lang".to_string(),
name: "cargo-platform".to_string(),
name: "cargo".to_string(), // repo name not crate name
sha: None
}),
"ssh://[email protected]/rust-lang/regex.git#[email protected]" => Crate::GitHub(GitHubRepo {
Expand Down