forked from NixOS/nixpkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefetch-npm-deps: throw better error when unsupported git service is…
… used See NixOS#206476 (comment).
- Loading branch information
Showing
1 changed file
with
25 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,7 @@ impl Package { | |
UrlOrString::String(_) => panic!("at this point, all packages should have URLs"), | ||
}; | ||
|
||
let specifics = match get_hosted_git_url(&resolved) { | ||
let specifics = match get_hosted_git_url(&resolved)? { | ||
Some(hosted) => { | ||
let mut body = ureq::get(hosted.as_str()).call()?.into_reader(); | ||
|
||
|
@@ -188,11 +188,13 @@ impl Package { | |
} | ||
|
||
#[allow(clippy::case_sensitive_file_extension_comparisons)] | ||
fn get_hosted_git_url(url: &Url) -> Option<Url> { | ||
if ["git", "http", "git+ssh", "git+https", "ssh", "https"].contains(&url.scheme()) { | ||
let mut s = url.path_segments()?; | ||
fn get_hosted_git_url(url: &Url) -> anyhow::Result<Option<Url>> { | ||
if ["git", "git+ssh", "git+https", "ssh"].contains(&url.scheme()) { | ||
let mut s = url | ||
.path_segments() | ||
.ok_or_else(|| anyhow!("bad URL: {url}"))?; | ||
|
||
match url.host_str()? { | ||
let mut get_url = || match url.host_str()? { | ||
"github.com" => { | ||
let user = s.next()?; | ||
let mut project = s.next()?; | ||
|
@@ -241,7 +243,7 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> { | |
) | ||
} | ||
"gitlab.com" => { | ||
let path = &url.path()[1..]; | ||
/* let path = &url.path()[1..]; | ||
if path.contains("/~/") || path.contains("/archive.tar.gz") { | ||
return None; | ||
|
@@ -261,7 +263,10 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> { | |
"https://gitlab.com/{user}/{project}/repository/archive.tar.gz?ref={commit}" | ||
)) | ||
.ok()?, | ||
) | ||
) */ | ||
|
||
// lmao: https://github.com/npm/hosted-git-info/pull/109 | ||
None | ||
} | ||
"git.sr.ht" => { | ||
let user = s.next()?; | ||
|
@@ -286,9 +291,14 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> { | |
) | ||
} | ||
_ => None, | ||
}; | ||
|
||
match get_url() { | ||
Some(u) => Ok(Some(u)), | ||
None => Err(anyhow!("This lockfile either contains a Git dependency with an unsupported host, or a malformed URL in the lockfile: {url}")) | ||
} | ||
} else { | ||
None | ||
Ok(None) | ||
} | ||
} | ||
|
||
|
@@ -320,32 +330,26 @@ mod tests { | |
"git+ssh://[email protected]/castlabs/electron-releases.git#fc5f78d046e8d7cdeb66345a2633c383ab41f525", | ||
Some("https://codeload.github.com/castlabs/electron-releases/tar.gz/fc5f78d046e8d7cdeb66345a2633c383ab41f525"), | ||
), | ||
( | ||
"https://[email protected]/foo/bar#fix/bug", | ||
Some("https://codeload.github.com/foo/bar/tar.gz/fix/bug") | ||
), | ||
( | ||
"https://github.com/eligrey/classList.js/archive/1.2.20180112.tar.gz", | ||
None | ||
), | ||
( | ||
"git+ssh://bitbucket.org/foo/bar#branch", | ||
Some("https://bitbucket.org/foo/bar/get/branch.tar.gz") | ||
), | ||
( | ||
"ssh://[email protected]/foo/bar.git#fix/bug", | ||
Some("https://gitlab.com/foo/bar/repository/archive.tar.gz?ref=fix/bug") | ||
), | ||
( | ||
"git+ssh://git.sr.ht/~foo/bar#branch", | ||
Some("https://git.sr.ht/~foo/bar/archive/branch.tar.gz") | ||
), | ||
] { | ||
assert_eq!( | ||
get_hosted_git_url(&Url::parse(input).unwrap()), | ||
get_hosted_git_url(&Url::parse(input).unwrap()).unwrap(), | ||
expected.map(|u| Url::parse(u).unwrap()) | ||
); | ||
} | ||
|
||
assert!( | ||
get_hosted_git_url(&Url::parse("ssh://[email protected]/foo/bar.git#fix/bug").unwrap()) | ||
.is_err(), | ||
"GitLab URLs should be marked as invalid (lol)" | ||
); | ||
} | ||
|
||
#[test] | ||
|