Skip to content

Commit 94e9c4c

Browse files
committed
warn the user if the upstream master branch is old
fixes #129528
1 parent 600edc9 commit 94e9c4c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/bootstrap/src/core/build_steps/format.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::mpsc::SyncSender;
77
use std::sync::Mutex;
88

99
use build_helper::ci::CiEnv;
10-
use build_helper::git::get_git_modified_files;
10+
use build_helper::git::{get_git_modified_files, warn_old_master_branch};
1111
use ignore::WalkBuilder;
1212

1313
use crate::core::builder::Builder;
@@ -93,7 +93,8 @@ fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, Str
9393
if !verify_rustfmt_version(build) {
9494
return Ok(None);
9595
}
96-
96+
warn_old_master_branch(&build.config.git_config(), &build.config.src)
97+
.map_err(|e| e.to_string())?;
9798
get_git_modified_files(&build.config.git_config(), Some(&build.config.src), &["rs"])
9899
}
99100

src/tools/build_helper/src/git.rs

+34
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,37 @@ pub fn get_git_untracked_files(
159159
.collect();
160160
Ok(Some(files))
161161
}
162+
163+
/// Print a warning if the branch returned from `updated_master_branch` is old
164+
///
165+
/// For certain configurations of git repository, this remote will not be
166+
/// updated when running `git pull`.
167+
///
168+
/// This can result in formatting thousands of files instead of a dozen,
169+
/// so we should warn the user something is wrong.
170+
pub fn warn_old_master_branch(
171+
config: &GitConfig<'_>,
172+
git_dir: &Path,
173+
) -> Result<(), Box<dyn std::error::Error>> {
174+
use std::time::Duration;
175+
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
176+
let updated_master = updated_master_branch(config, Some(git_dir))?;
177+
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
178+
match std::fs::metadata(branch_path) {
179+
Ok(meta) => {
180+
if meta.modified()?.elapsed()? > WARN_AFTER {
181+
eprintln!("warning: {updated_master} has not been updated in 10 days");
182+
} else {
183+
return Ok(());
184+
}
185+
}
186+
Err(err) => {
187+
eprintln!("warning: unable to check if {updated_master} is old due to error: {err}")
188+
}
189+
}
190+
eprintln!(
191+
"warning: {updated_master} is used to determine if files have been modified\n\
192+
warning: if it is not updated, this may cause files to be needlessly reformatted"
193+
);
194+
Ok(())
195+
}

0 commit comments

Comments
 (0)