-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tidy check to deny merge commits
This will prevent users with the pre-push hook from pushing a merge commit. Exceptions are added for subtree updates. These exceptions are a little hacky and may be non-exhaustive but can be extended in the future. Checkout `master` branch in CI Add `build_helper` crate to share code between tidy and bootstrap
- Loading branch information
Showing
18 changed files
with
195 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "build_helper" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::process::Command; | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub enum CiEnv { | ||
/// Not a CI environment. | ||
None, | ||
/// The Azure Pipelines environment, for Linux (including Docker), Windows, and macOS builds. | ||
AzurePipelines, | ||
/// The GitHub Actions environment, for Linux (including Docker), Windows and macOS builds. | ||
GitHubActions, | ||
} | ||
|
||
impl CiEnv { | ||
/// Obtains the current CI environment. | ||
pub fn current() -> CiEnv { | ||
if std::env::var("TF_BUILD").map_or(false, |e| e == "True") { | ||
CiEnv::AzurePipelines | ||
} else if std::env::var("GITHUB_ACTIONS").map_or(false, |e| e == "true") { | ||
CiEnv::GitHubActions | ||
} else { | ||
CiEnv::None | ||
} | ||
} | ||
|
||
pub fn is_ci() -> bool { | ||
Self::current() != CiEnv::None | ||
} | ||
|
||
/// If in a CI environment, forces the command to run with colors. | ||
pub fn force_coloring_in_ci(self, cmd: &mut Command) { | ||
if self != CiEnv::None { | ||
// Due to use of stamp/docker, the output stream of rustbuild is not | ||
// a TTY in CI, so coloring is by-default turned off. | ||
// The explicit `TERM=xterm` environment is needed for | ||
// `--color always` to actually work. This env var was lost when | ||
// compiling through the Makefile. Very strange. | ||
cmd.env("TERM", "xterm").args(&["--color", "always"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::process::Command; | ||
|
||
/// Finds the remote for rust-lang/rust. | ||
/// For example for these remotes it will return `upstream`. | ||
/// ```text | ||
/// origin https://github.com/Nilstrieb/rust.git (fetch) | ||
/// origin https://github.com/Nilstrieb/rust.git (push) | ||
/// upstream https://github.com/rust-lang/rust (fetch) | ||
/// upstream https://github.com/rust-lang/rust (push) | ||
/// ``` | ||
pub fn get_rust_lang_rust_remote() -> Result<String, String> { | ||
let mut git = Command::new("git"); | ||
git.args(["config", "--local", "--get-regex", "remote\\..*\\.url"]); | ||
|
||
let output = git.output().map_err(|err| format!("{err:?}"))?; | ||
if !output.status.success() { | ||
return Err("failed to execute git config command".to_owned()); | ||
} | ||
|
||
let stdout = String::from_utf8(output.stdout).map_err(|err| format!("{err:?}"))?; | ||
|
||
let rust_lang_remote = stdout | ||
.lines() | ||
.find(|remote| remote.contains("rust-lang")) | ||
.ok_or_else(|| "rust-lang/rust remote not found".to_owned())?; | ||
|
||
let remote_name = | ||
rust_lang_remote.split('.').nth(1).ok_or_else(|| "remote name not found".to_owned())?; | ||
Ok(remote_name.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod ci; | ||
pub mod git; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! This check makes sure that no accidental merge commits are introduced to the repository. | ||
//! It forbids all merge commits that are not caused by rollups/bors or subtree syncs. | ||
|
||
use std::process::Command; | ||
|
||
use build_helper::ci::CiEnv; | ||
use build_helper::git::get_rust_lang_rust_remote; | ||
|
||
macro_rules! try_unwrap_in_ci { | ||
($expr:expr) => { | ||
match $expr { | ||
Ok(value) => value, | ||
Err(err) if CiEnv::is_ci() => { | ||
panic!("Encountered error while testing Git status: {:?}", err) | ||
} | ||
Err(_) => return, | ||
} | ||
}; | ||
} | ||
|
||
pub fn check(_: (), bad: &mut bool) { | ||
let remote = try_unwrap_in_ci!(get_rust_lang_rust_remote()); | ||
let merge_commits = try_unwrap_in_ci!(find_merge_commits(&remote)); | ||
|
||
let mut bad_merge_commits = merge_commits.lines().filter(|commit| { | ||
!( | ||
// Bors is the ruler of merge commits. | ||
commit.starts_with("Auto merge of") || commit.starts_with("Rollup merge of") | ||
) | ||
}); | ||
|
||
if let Some(merge) = bad_merge_commits.next() { | ||
tidy_error!( | ||
bad, | ||
"found a merge commit in the history: `{merge}`. | ||
To resolve the issue, see this: https://rustc-dev-guide.rust-lang.org/git.html#i-made-a-merge-commit-by-accident. | ||
If you're doing a subtree sync, add your tool to the list in the code that emitted this error." | ||
); | ||
} | ||
} | ||
|
||
/// Runs `git log --merges --format=%s $REMOTE/master..HEAD` and returns all commits | ||
fn find_merge_commits(remote: &str) -> Result<String, String> { | ||
let mut git = Command::new("git"); | ||
git.args([ | ||
"log", | ||
"--merges", | ||
"--format=%s", | ||
&format!("{remote}/master..HEAD"), | ||
// Ignore subtree syncs. Add your new subtrees here. | ||
":!src/tools/miri", | ||
":!src/tools/rust-analyzer", | ||
":!compiler/rustc_smir", | ||
":!library/portable-simd", | ||
":!compiler/rustc_codegen_gcc", | ||
":!src/tools/rustfmt", | ||
":!compiler/rustc_codegen_cranelift", | ||
":!src/tools/clippy", | ||
]); | ||
|
||
let output = git.output().map_err(|err| format!("{err:?}"))?; | ||
if !output.status.success() { | ||
return Err(format!( | ||
"failed to execute git log command: {}", | ||
String::from_utf8_lossy(&output.stderr) | ||
)); | ||
} | ||
|
||
let stdout = String::from_utf8(output.stdout).map_err(|err| format!("{err:?}"))?; | ||
|
||
Ok(stdout) | ||
} |