Rollup merge of #131043 - liwagu:unify, r=albertlarsan68,onur-ozkan

Refactor change detection for rustdoc and download-rustc

This pull request refactors the change detection logic in the build process by consolidating redundant code into a new helper method. The key changes include the removal of duplicate logic for checking changes in directories and the addition of a new method to handle this functionality.

Refactoring and code simplification:

* [`src/bootstrap/src/core/build_steps/tool.rs`](diffhunk://#diff-dc86e288bcf7b3ca3f8c127d3568fbafc785704883bc7fc336bd185910aed5daL588-R593): Removed redundant change detection logic and replaced it with a call to the new `check_for_changes` method.
* [`src/bootstrap/src/core/config/config.rs`](diffhunk://#diff-5f5330cfcdb0a89b85ac3547b761c3a45c2534a85c4aaae8fea88c711a7a65b2R2837-R2872): Added a new method `check_for_changes` to centralize the logic for detecting changes in specified directories since a given commit.
* [`src/bootstrap/src/core/config/config.rs`](diffhunk://#diff-5f5330cfcdb0a89b85ac3547b761c3a45c2534a85c4aaae8fea88c711a7a65b2L2728-R2740): Updated the existing change detection code to use the new `check_for_changes` method.

Cleanup:

* [`src/bootstrap/src/core/build_steps/tool.rs`](diffhunk://#diff-dc86e288bcf7b3ca3f8c127d3568fbafc785704883bc7fc336bd185910aed5daL13-R13): Removed the unused import `git` from the helpers module.

   r? ``@AlbertLarsan68``
This commit is contained in:
León Orell Valerian Liehr 2024-10-23 17:24:30 +02:00 committed by GitHub
commit 4caa60c032
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 65 deletions

View File

@ -1,8 +1,6 @@
use std::path::PathBuf;
use std::{env, fs};
use build_helper::git::get_closest_merge_commit;
use crate::core::build_steps::compile;
use crate::core::build_steps::toolstate::ToolState;
use crate::core::builder;
@ -10,7 +8,7 @@
use crate::core::config::TargetSelection;
use crate::utils::channel::GitInfo;
use crate::utils::exec::{BootstrapCommand, command};
use crate::utils::helpers::{add_dylib_path, exe, git, t};
use crate::utils::helpers::{add_dylib_path, exe, t};
use crate::{Compiler, Kind, Mode, gha};
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@ -596,28 +594,11 @@ fn run(self, builder: &Builder<'_>) -> PathBuf {
&& target_compiler.stage > 0
&& builder.rust_info().is_managed_git_subrepository()
{
let commit = get_closest_merge_commit(
Some(&builder.config.src),
&builder.config.git_config(),
&[],
)
.unwrap();
let files_to_track = &["src/librustdoc", "src/tools/rustdoc"];
let librustdoc_src = builder.config.src.join("src/librustdoc");
let rustdoc_src = builder.config.src.join("src/tools/rustdoc");
// FIXME: The change detection logic here is quite similar to `Config::download_ci_rustc_commit`.
// It would be better to unify them.
let has_changes = !git(Some(&builder.config.src))
.allow_failure()
.run_always()
.args(["diff-index", "--quiet", &commit])
.arg("--")
.arg(librustdoc_src)
.arg(rustdoc_src)
.run(builder);
if !has_changes {
// Check if unchanged
if builder.config.last_modified_commit(files_to_track, "download-rustc", true).is_some()
{
let precompiled_rustdoc = builder
.config
.ci_rustc_dir()

View File

@ -2754,25 +2754,25 @@ fn download_ci_rustc_commit(
}
};
let files_to_track = &[
self.src.join("compiler"),
self.src.join("library"),
self.src.join("src/version"),
self.src.join("src/stage0"),
self.src.join("src/ci/channel"),
];
let files_to_track =
&["compiler", "library", "src/version", "src/stage0", "src/ci/channel"];
// Look for a version to compare to based on the current commit.
// Only commits merged by bors will have CI artifacts.
let commit =
get_closest_merge_commit(Some(&self.src), &self.git_config(), files_to_track).unwrap();
if commit.is_empty() {
println!("ERROR: could not find commit hash for downloading rustc");
println!("HELP: maybe your repository history is too shallow?");
println!("HELP: consider disabling `download-rustc`");
println!("HELP: or fetch enough history to include one upstream commit");
crate::exit!(1);
}
let commit = match self.last_modified_commit(files_to_track, "download-rustc", if_unchanged)
{
Some(commit) => commit,
None => {
if if_unchanged {
return None;
}
println!("ERROR: could not find commit hash for downloading rustc");
println!("HELP: maybe your repository history is too shallow?");
println!("HELP: consider disabling `download-rustc`");
println!("HELP: or fetch enough history to include one upstream commit");
crate::exit!(1);
}
};
if CiEnv::is_ci() && {
let head_sha =
@ -2787,31 +2787,7 @@ fn download_ci_rustc_commit(
return None;
}
// Warn if there were changes to the compiler or standard library since the ancestor commit.
let has_changes = !t!(helpers::git(Some(&self.src))
.args(["diff-index", "--quiet", &commit])
.arg("--")
.args(files_to_track)
.as_command_mut()
.status())
.success();
if has_changes {
if if_unchanged {
if self.is_verbose() {
println!(
"WARNING: saw changes to compiler/ or library/ since {commit}; \
ignoring `download-rustc`"
);
}
return None;
}
println!(
"WARNING: `download-rustc` is enabled, but there are changes to \
compiler/ or library/"
);
}
Some(commit.to_string())
Some(commit)
}
fn parse_download_ci_llvm(