Skip to content

Commit 64fa312

Browse files
committed
bootstrap: handle worktrees in warn_old_master_branch
fixes rust-lang#130111
1 parent adf8d16 commit 64fa312

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

src/bootstrap/src/core/sanity.rs

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

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

src/tools/build_helper/src/git.rs

+47-14
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,59 @@ pub fn get_git_untracked_files(
170170
pub fn warn_old_master_branch(
171171
config: &GitConfig<'_>,
172172
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-
}
173+
) {
174+
if crate::ci::CiEnv::is_ci() {
175+
// this warning is useless in CI,
176+
// and CI probably won't have the right branches anyway.
177+
return;
178+
}
179+
// this will be overwritten by the actual name, if possible
180+
let mut updated_master = "the upstream master branch".to_string();
181+
match warn_old_master_branch_(config, git_dir, &mut updated_master) {
182+
Ok(branch_is_old) => {
183+
if !branch_is_old { return }
184+
// otherwise fall through and print the rest of the warning
185185
}
186186
Err(err) => {
187187
eprintln!("warning: unable to check if {updated_master} is old due to error: {err}")
188188
}
189189
}
190190
eprintln!(
191191
"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"
192+
warning: if it is not updated, this may cause files to be needlessly reformatted"
193193
);
194-
Ok(())
194+
}
195+
196+
pub fn warn_old_master_branch_(
197+
config: &GitConfig<'_>,
198+
git_dir: &Path,
199+
updated_master: &mut String,
200+
) -> Result<bool, Box<dyn std::error::Error>> {
201+
use std::time::Duration;
202+
*updated_master = updated_master_branch(config, Some(git_dir))?;
203+
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
204+
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
205+
let meta = match std::fs::metadata(&branch_path) {
206+
Ok(meta) => meta,
207+
Err(err) => {
208+
let gcd = git_common_dir(&git_dir)?;
209+
if branch_path.starts_with(&gcd) {
210+
return Err(Box::new(err));
211+
}
212+
std::fs::metadata(Path::new(&gcd).join("refs/remotes").join(&updated_master))?
213+
},
214+
};
215+
if meta.modified()?.elapsed()? > WARN_AFTER {
216+
eprintln!("warning: {updated_master} has not been updated in 10 days");
217+
Ok(true)
218+
} else {
219+
Ok(false)
220+
}
221+
}
222+
223+
fn git_common_dir(dir: &Path) -> Result<String, String> {
224+
output_result(Command::new("git")
225+
.arg("-C").arg(dir)
226+
.arg("rev-parse").arg("--git-common-dir"))
227+
.map(|x| x.trim().to_string())
195228
}

0 commit comments

Comments
 (0)