-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
bootstrap: handle worktrees in warn_old_master_branch #130121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,29 +167,59 @@ pub fn get_git_untracked_files( | |
/// | ||
/// 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(()); | ||
pub fn warn_old_master_branch(config: &GitConfig<'_>, git_dir: &Path) { | ||
if crate::ci::CiEnv::is_ci() { | ||
// this warning is useless in CI, | ||
// and CI probably won't have the right branches anyway. | ||
return; | ||
} | ||
// this will be overwritten by the actual name, if possible | ||
let mut updated_master = "the upstream master branch".to_string(); | ||
match warn_old_master_branch_(config, git_dir, &mut updated_master) { | ||
Ok(branch_is_old) => { | ||
if !branch_is_old { | ||
return; | ||
} | ||
// otherwise fall through and print the rest of the warning | ||
} | ||
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" | ||
warning: if it is not updated, this may cause files to be needlessly reformatted" | ||
); | ||
Ok(()) | ||
} | ||
|
||
pub fn warn_old_master_branch_( | ||
config: &GitConfig<'_>, | ||
git_dir: &Path, | ||
updated_master: &mut String, | ||
) -> Result<bool, Box<dyn std::error::Error>> { | ||
use std::time::Duration; | ||
*updated_master = updated_master_branch(config, Some(git_dir))?; | ||
let branch_path = git_dir.join(".git/refs/remotes").join(&updated_master); | ||
const WARN_AFTER: Duration = Duration::from_secs(60 * 60 * 24 * 10); | ||
let meta = match std::fs::metadata(&branch_path) { | ||
Ok(meta) => meta, | ||
Err(err) => { | ||
let gcd = git_common_dir(&git_dir)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use this only as a fallback instead of an error? There's a single unique way to find this path correctly for all situations, that should IMO be used consistently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm trying not to have much of a performance impact over such a niche lint. i'm stuck on an hdd system for the time being, so yes, this does give slightly worse performance in the worktree case, but worktrees are already nearly unusable on a slow hdd system, so i'm not too concerned about that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm concerned about maintainability of this codebase and how much we're willing to carry difficult-to-test fallback paths in code that is proven to be error-prone and not covered by CI. There's a global cost to every hack like this, and at some point making a slow system a bit slower is an okay way to keep the rest of the project productive -- the time spent figuring out why my worktrees now show this warning is time not spent working on rustc itself, after all. In the end this will be up to the bootstrap team to decide though, these are just my thoughts. |
||
if branch_path.starts_with(&gcd) { | ||
return Err(Box::new(err)); | ||
} | ||
std::fs::metadata(Path::new(&gcd).join("refs/remotes").join(&updated_master))? | ||
} | ||
}; | ||
if meta.modified()?.elapsed()? > WARN_AFTER { | ||
eprintln!("warning: {updated_master} has not been updated in 10 days"); | ||
Ok(true) | ||
} else { | ||
Ok(false) | ||
} | ||
} | ||
|
||
fn git_common_dir(dir: &Path) -> Result<String, String> { | ||
output_result(Command::new("git").arg("-C").arg(dir).arg("rev-parse").arg("--git-common-dir")) | ||
.map(|x| x.trim().to_string()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't there a way to avoid directly looking at git's data format? Breaking the abstraction like this should ideally be avoided. E.g. you could look at the date of the last commit in that branch.
If you must use the .git folder, please use
git rev-parse --git-dir
to determine its location, rather than assuming that it is.git
. That will then also work with worktrees.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see you already found
--git-common-dir
... which is indeed the right location,--git-dir
is wrong. Let me quickly fix some faulty logic I recently wrote elsewhere, then... ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i suppose i overestimated:
waiting on a second opinion, i'll rewrite the code if that's the consensus preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The incorrect warning already led to incorrect diagnosis of an unrelated issue in #130144. So it's not just about dislike, it's about the real costs of an incorrect warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i see that pretty clearly now, there's a reason i started writing this PR as soon as i was made aware of the issue.
i know the current PR works, and my main worry is that if i need to totally rewrite it, that might introduce even more bugs (this situation has happened to me many times), and this would be even worse with something that is so tedious for me to test (spinning up worktrees is not fast on a hdd system)
the worktree bug seems pretty severe, so i think we should prioritize getting a fix merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spinning up a fix so quickly!