Skip to content

Commit 6ccc3de

Browse files
authored
Rollup merge of #111770 - liushuyu:ubuntu/beta-rev-no-git, r=ozkanonur
Read beta version from the version file if building from a source tarball This pull request changes the `bootstrap` behaviour so that when building `rustc` from a source tarball, the beta revision number is correctly read from the `version` file instead of erroring out by invoking `git`. The `version` file is observed to only exist in the official source tarball and has the following format: ``` 1.70.0-beta.4 (2013813 2023-05-07) ```
2 parents cb5dd1d + 343a0bf commit 6ccc3de

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

Diff for: src/bootstrap/builder/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ fn alias_and_path_for_library() {
146146
);
147147
}
148148

149+
#[test]
150+
fn test_beta_rev_parsing() {
151+
use crate::extract_beta_rev;
152+
153+
// single digit revision
154+
assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string()));
155+
// multiple digits
156+
assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string()));
157+
// nightly channel (no beta revision)
158+
assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None);
159+
// stable channel (no beta revision)
160+
assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None);
161+
// invalid string
162+
assert_eq!(extract_beta_rev("invalid"), None);
163+
}
164+
149165
mod defaults {
150166
use super::{configure, first, run_build};
151167
use crate::builder::*;

Diff for: src/bootstrap/lib.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ impl Build {
13241324
match &self.config.channel[..] {
13251325
"stable" => num.to_string(),
13261326
"beta" => {
1327-
if self.rust_info().is_managed_git_subrepository() && !self.config.omit_git_hash {
1327+
if !self.config.omit_git_hash {
13281328
format!("{}-beta.{}", num, self.beta_prerelease_version())
13291329
} else {
13301330
format!("{}-beta", num)
@@ -1336,18 +1336,28 @@ impl Build {
13361336
}
13371337

13381338
fn beta_prerelease_version(&self) -> u32 {
1339+
fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1340+
let version = fs::read_to_string(version_file).ok()?;
1341+
1342+
extract_beta_rev(&version)
1343+
}
1344+
13391345
if let Some(s) = self.prerelease_version.get() {
13401346
return s;
13411347
}
13421348

1343-
// Figure out how many merge commits happened since we branched off master.
1344-
// That's our beta number!
1345-
// (Note that we use a `..` range, not the `...` symmetric difference.)
1346-
let count =
1349+
// First check if there is a version file available.
1350+
// If available, we read the beta revision from that file.
1351+
// This only happens when building from a source tarball when Git should not be used.
1352+
let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1353+
// Figure out how many merge commits happened since we branched off master.
1354+
// That's our beta number!
1355+
// (Note that we use a `..` range, not the `...` symmetric difference.)
13471356
output(self.config.git().arg("rev-list").arg("--count").arg("--merges").arg(format!(
13481357
"refs/remotes/origin/{}..HEAD",
13491358
self.config.stage0_metadata.config.nightly_branch
1350-
)));
1359+
)))
1360+
});
13511361
let n = count.trim().parse().unwrap();
13521362
self.prerelease_version.set(Some(n));
13531363
n
@@ -1707,6 +1717,17 @@ to download LLVM rather than building it.
17071717
}
17081718
}
17091719

1720+
/// Extract the beta revision from the full version string.
1721+
///
1722+
/// The full version string looks like "a.b.c-beta.y". And we need to extract
1723+
/// the "y" part from the string.
1724+
pub fn extract_beta_rev(version: &str) -> Option<String> {
1725+
let parts = version.splitn(2, "-beta.").collect::<Vec<_>>();
1726+
let count = parts.get(1).and_then(|s| s.find(' ').map(|p| (&s[..p]).to_string()));
1727+
1728+
count
1729+
}
1730+
17101731
#[cfg(unix)]
17111732
fn chmod(path: &Path, perms: u32) {
17121733
use std::os::unix::fs::*;

0 commit comments

Comments
 (0)