Skip to content

Commit 5a9b9a3

Browse files
committed
bootstrap: handle worktrees in warn_old_master_branch
fixes #130111
1 parent adf8d16 commit 5a9b9a3

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
@@ -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
}

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

+46-16
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,59 @@ pub fn get_git_untracked_files(
167167
///
168168
/// This can result in formatting thousands of files instead of a dozen,
169169
/// 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(());
170+
pub fn warn_old_master_branch(config: &GitConfig<'_>, git_dir: &Path) {
171+
if crate::ci::CiEnv::is_ci() {
172+
// this warning is useless in CI,
173+
// and CI probably won't have the right branches anyway.
174+
return;
175+
}
176+
// this will be overwritten by the actual name, if possible
177+
let mut updated_master = "the upstream master branch".to_string();
178+
match warn_old_master_branch_(config, git_dir, &mut updated_master) {
179+
Ok(branch_is_old) => {
180+
if !branch_is_old {
181+
return;
184182
}
183+
// otherwise fall through and print the rest of the warning
185184
}
186185
Err(err) => {
187186
eprintln!("warning: unable to check if {updated_master} is old due to error: {err}")
188187
}
189188
}
190189
eprintln!(
191190
"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"
191+
warning: if it is not updated, this may cause files to be needlessly reformatted"
193192
);
194-
Ok(())
193+
}
194+
195+
pub fn warn_old_master_branch_(
196+
config: &GitConfig<'_>,
197+
git_dir: &Path,
198+
updated_master: &mut String,
199+
) -> Result<bool, Box<dyn std::error::Error>> {
200+
use std::time::Duration;
201+
*updated_master = updated_master_branch(config, Some(git_dir))?;
202+
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
203+
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
204+
let meta = match std::fs::metadata(&branch_path) {
205+
Ok(meta) => meta,
206+
Err(err) => {
207+
let gcd = git_common_dir(&git_dir)?;
208+
if branch_path.starts_with(&gcd) {
209+
return Err(Box::new(err));
210+
}
211+
std::fs::metadata(Path::new(&gcd).join("refs/remotes").join(&updated_master))?
212+
}
213+
};
214+
if meta.modified()?.elapsed()? > WARN_AFTER {
215+
eprintln!("warning: {updated_master} has not been updated in 10 days");
216+
Ok(true)
217+
} else {
218+
Ok(false)
219+
}
220+
}
221+
222+
fn git_common_dir(dir: &Path) -> Result<String, String> {
223+
output_result(Command::new("git").arg("-C").arg(dir).arg("rev-parse").arg("--git-common-dir"))
224+
.map(|x| x.trim().to_string())
195225
}

0 commit comments

Comments
 (0)