|
| 1 | +//! This check makes sure that no accidental merge commits are introduced to the repository. |
| 2 | +//! It forbids all merge commits that are not caused by rollups/bors or subtree syncs. |
| 3 | +
|
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +use build_helper::ci::CiEnv; |
| 7 | +use build_helper::git::get_rust_lang_rust_remote; |
| 8 | + |
| 9 | +macro_rules! try_unwrap_in_ci { |
| 10 | + ($expr:expr) => { |
| 11 | + match $expr { |
| 12 | + Ok(value) => value, |
| 13 | + Err(err) if CiEnv::is_ci() => { |
| 14 | + panic!("Encountered error while testing Git status: {:?}", err) |
| 15 | + } |
| 16 | + Err(_) => return, |
| 17 | + } |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +pub fn check(_: (), bad: &mut bool) { |
| 22 | + let remote = try_unwrap_in_ci!(get_rust_lang_rust_remote()); |
| 23 | + let merge_commits = try_unwrap_in_ci!(find_merge_commits(&remote)); |
| 24 | + |
| 25 | + let mut bad_merge_commits = merge_commits.lines().filter(|commit| { |
| 26 | + !( |
| 27 | + // Bors is the ruler of merge commits. |
| 28 | + commit.starts_with("Auto merge of") || commit.starts_with("Rollup merge of") |
| 29 | + ) |
| 30 | + }); |
| 31 | + |
| 32 | + if let Some(merge) = bad_merge_commits.next() { |
| 33 | + tidy_error!( |
| 34 | + bad, |
| 35 | + "found a merge commit in the history: `{merge}`. |
| 36 | +To resolve the issue, see this: https://rustc-dev-guide.rust-lang.org/git.html#i-made-a-merge-commit-by-accident. |
| 37 | +If you're doing a subtree sync, add your tool to the list in the code that emitted this error." |
| 38 | + ); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Runs `git log --merges --format=%s $REMOTE/master..HEAD` and returns all commits |
| 43 | +fn find_merge_commits(remote: &str) -> Result<String, String> { |
| 44 | + let mut git = Command::new("git"); |
| 45 | + git.args([ |
| 46 | + "log", |
| 47 | + "--merges", |
| 48 | + "--format=%s", |
| 49 | + &format!("{remote}/master..HEAD"), |
| 50 | + // Ignore subtree syncs. Add your new subtrees here. |
| 51 | + ":!src/tools/miri", |
| 52 | + ":!src/tools/rust-analyzer", |
| 53 | + ":!compiler/rustc_smir", |
| 54 | + ":!library/portable-simd", |
| 55 | + ":!compiler/rustc_codegen_gcc", |
| 56 | + ":!src/tools/rustfmt", |
| 57 | + ":!compiler/rustc_codegen_cranelift", |
| 58 | + ":!src/tools/clippy", |
| 59 | + ]); |
| 60 | + |
| 61 | + let output = git.output().map_err(|err| format!("{err:?}"))?; |
| 62 | + if !output.status.success() { |
| 63 | + return Err(format!( |
| 64 | + "failed to execute git log command: {}", |
| 65 | + String::from_utf8_lossy(&output.stderr) |
| 66 | + )); |
| 67 | + } |
| 68 | + |
| 69 | + let stdout = String::from_utf8(output.stdout).map_err(|err| format!("{err:?}"))?; |
| 70 | + |
| 71 | + Ok(stdout) |
| 72 | +} |
0 commit comments