Skip to content

Commit

Permalink
warn the user if the upstream master branch is old
Browse files Browse the repository at this point in the history
fixes #129528
  • Loading branch information
binarycat committed Aug 27, 2024
1 parent 600edc9 commit 94e9c4c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::mpsc::SyncSender;
use std::sync::Mutex;

use build_helper::ci::CiEnv;
use build_helper::git::get_git_modified_files;
use build_helper::git::{get_git_modified_files, warn_old_master_branch};
use ignore::WalkBuilder;

use crate::core::builder::Builder;
Expand Down Expand Up @@ -93,7 +93,8 @@ fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, Str
if !verify_rustfmt_version(build) {
return Ok(None);
}

warn_old_master_branch(&build.config.git_config(), &build.config.src)
.map_err(|e| e.to_string())?;
get_git_modified_files(&build.config.git_config(), Some(&build.config.src), &["rs"])
}

Expand Down
34 changes: 34 additions & 0 deletions src/tools/build_helper/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,37 @@ pub fn get_git_untracked_files(
.collect();
Ok(Some(files))
}

/// Print a warning if the branch returned from `updated_master_branch` is old
///
/// For certain configurations of git repository, this remote will not be
/// updated when running `git pull`.
///
/// This can result in formatting thousands of files instead of a dozen,
/// so we should warn the user something is wrong.
pub fn warn_old_master_branch(
config: &GitConfig<'_>,
git_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
use std::time::Duration;
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10);
let updated_master = updated_master_branch(config, Some(git_dir))?;
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master);
match std::fs::metadata(branch_path) {
Ok(meta) => {
if meta.modified()?.elapsed()? > WARN_AFTER {
eprintln!("warning: {updated_master} has not been updated in 10 days");
} else {
return Ok(());
}
}
Err(err) => {
eprintln!("warning: unable to check if {updated_master} is old due to error: {err}")
}
}
eprintln!(
"warning: {updated_master} is used to determine if files have been modified\n\
warning: if it is not updated, this may cause files to be needlessly reformatted"
);
Ok(())
}

0 comments on commit 94e9c4c

Please sign in to comment.