Skip to content

Commit 507c05b

Browse files
committed
Auto merge of rust-lang#130121 - lolbinarycat:bootstrap-warn-old-upstream-worktree, r=albertlarsan68
bootstrap: handle worktrees in warn_old_master_branch fixes rust-lang#130111
2 parents 0609062 + 5a9b9a3 commit 507c05b

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

Diff for: src/bootstrap/src/core/sanity.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,5 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
379379
cmd_finder.must_have(s);
380380
}
381381

382-
// this warning is useless in CI,
383-
// and CI probably won't have the right branches anyway.
384-
if !build_helper::ci::CiEnv::is_ci() {
385-
if let Err(e) = warn_old_master_branch(&build.config.git_config(), &build.config.src)
386-
.map_err(|e| e.to_string())
387-
{
388-
eprintln!("unable to check if upstream branch is old: {e}");
389-
}
390-
}
382+
warn_old_master_branch(&build.config.git_config(), &build.config.src);
391383
}

Diff for: src/tools/build_helper/src/git.rs

+46-16
Original file line numberDiff line numberDiff line change
@@ -204,29 +204,59 @@ pub fn get_git_untracked_files(
204204
///
205205
/// This can result in formatting thousands of files instead of a dozen,
206206
/// so we should warn the user something is wrong.
207-
pub fn warn_old_master_branch(
208-
config: &GitConfig<'_>,
209-
git_dir: &Path,
210-
) -> Result<(), Box<dyn std::error::Error>> {
211-
use std::time::Duration;
212-
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
213-
let updated_master = updated_master_branch(config, Some(git_dir))?;
214-
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
215-
match std::fs::metadata(branch_path) {
216-
Ok(meta) => {
217-
if meta.modified()?.elapsed()? > WARN_AFTER {
218-
eprintln!("warning: {updated_master} has not been updated in 10 days");
219-
} else {
220-
return Ok(());
207+
pub fn warn_old_master_branch(config: &GitConfig<'_>, git_dir: &Path) {
208+
if crate::ci::CiEnv::is_ci() {
209+
// this warning is useless in CI,
210+
// and CI probably won't have the right branches anyway.
211+
return;
212+
}
213+
// this will be overwritten by the actual name, if possible
214+
let mut updated_master = "the upstream master branch".to_string();
215+
match warn_old_master_branch_(config, git_dir, &mut updated_master) {
216+
Ok(branch_is_old) => {
217+
if !branch_is_old {
218+
return;
221219
}
220+
// otherwise fall through and print the rest of the warning
222221
}
223222
Err(err) => {
224223
eprintln!("warning: unable to check if {updated_master} is old due to error: {err}")
225224
}
226225
}
227226
eprintln!(
228227
"warning: {updated_master} is used to determine if files have been modified\n\
229-
warning: if it is not updated, this may cause files to be needlessly reformatted"
228+
warning: if it is not updated, this may cause files to be needlessly reformatted"
230229
);
231-
Ok(())
230+
}
231+
232+
pub fn warn_old_master_branch_(
233+
config: &GitConfig<'_>,
234+
git_dir: &Path,
235+
updated_master: &mut String,
236+
) -> Result<bool, Box<dyn std::error::Error>> {
237+
use std::time::Duration;
238+
*updated_master = updated_master_branch(config, Some(git_dir))?;
239+
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
240+
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
241+
let meta = match std::fs::metadata(&branch_path) {
242+
Ok(meta) => meta,
243+
Err(err) => {
244+
let gcd = git_common_dir(&git_dir)?;
245+
if branch_path.starts_with(&gcd) {
246+
return Err(Box::new(err));
247+
}
248+
std::fs::metadata(Path::new(&gcd).join("refs/remotes").join(&updated_master))?
249+
}
250+
};
251+
if meta.modified()?.elapsed()? > WARN_AFTER {
252+
eprintln!("warning: {updated_master} has not been updated in 10 days");
253+
Ok(true)
254+
} else {
255+
Ok(false)
256+
}
257+
}
258+
259+
fn git_common_dir(dir: &Path) -> Result<String, String> {
260+
output_result(Command::new("git").arg("-C").arg(dir).arg("rev-parse").arg("--git-common-dir"))
261+
.map(|x| x.trim().to_string())
232262
}

0 commit comments

Comments
 (0)