Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn the user if the upstream master branch is old #129584

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/bootstrap/src/core/build_steps/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, Str
if !verify_rustfmt_version(build) {
return Ok(None);
}

get_git_modified_files(&build.config.git_config(), Some(&build.config.src), &["rs"])
}

Expand Down
12 changes: 12 additions & 0 deletions src/bootstrap/src/core/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
use std::{env, fs};

use build_helper::git::warn_old_master_branch;

#[cfg(not(feature = "bootstrap-self-test"))]
use crate::builder::Builder;
use crate::builder::Kind;
Expand Down Expand Up @@ -379,4 +381,14 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
if let Some(ref s) = build.config.ccache {
cmd_finder.must_have(s);
}

// this warning is useless in CI,
// and CI probably won't have the right branches anyway.
if !build_helper::ci::CiEnv::is_ci() {
if let Err(e) = warn_old_master_branch(&build.config.git_config(), &build.config.src)
.map_err(|e| e.to_string())
{
eprintln!("unable to check if upstream branch is old: {e}");
}
}
}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is incorrect for git worktrees, where .git is not a folder -- it is a file containing the path to where the .git folder lies.

Please either fix this PR or revert it, currently it causes incorrect warnings for everyone using worktrees.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that has already been reported at #130111. :)

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(())
}
Loading