Skip to content

Commit

Permalink
rewrite extract_curl_version again
Browse files Browse the repository at this point in the history
  • Loading branch information
lolbinarycat committed Aug 24, 2024
1 parent 69ca95b commit 56adf87
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ fn try_run(config: &Config, cmd: &mut Command) -> Result<(), ()> {
config.try_run(cmd)
}

fn extract_curl_version(out: &[u8]) -> (u16, u16) {
let out = &out[5..];
let Some(i) = out.iter().position(|&x| x == b' ') else { return (0, 0) };
let out = &out[..i];
let Some(k) = out.iter().rev().position(|&x| x == b'.') else { return (0, 0) };
let out = &out[..out.len() - k - 1];
let Ok(s) = std::str::from_utf8(out) else { return (0, 0) };
let parts = s.split('.').collect::<Vec<_>>();
let [s_major, s_minor] = &parts[..] else { return (0, 0) };
let (Ok(major), Ok(minor)) = (s_major.parse(), s_minor.parse()) else { return (0, 0) };
(major, minor)
fn extract_curl_version(out: &[u8]) -> semver::Version {
let out = String::from_utf8_lossy(out);
// The output should look like this: "curl <major>.<minor>.<patch> ..."
out.lines()
.next()
.and_then(|line| line.split(" ").nth(1))
.and_then(|version| semver::Version::parse(version).ok())
.unwrap_or(semver::Version::new(1, 0, 0))
}

fn curl_version() -> (u16, u16) {
fn curl_version() -> semver::Version {
let mut curl = Command::new("curl");
curl.arg("-V");
let Ok(out) = curl.output() else { return (0, 0) };
let Ok(out) = curl.output() else { return semver::Version::new(1, 0, 0) };
let out = out.stdout;
extract_curl_version(&out)
}
Expand Down Expand Up @@ -253,7 +250,7 @@ impl Config {
curl.arg("--progress-bar");
}
// --retry-all-errors was added in 7.71.0, don't use it if curl is old.
if curl_version() > (7, 70) {
if curl_version() >= semver::Version::new(7, 71, 0) {
curl.arg("--retry-all-errors");
}
curl.arg(url);
Expand Down

0 comments on commit 56adf87

Please sign in to comment.