|
| 1 | +//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was |
| 2 | +//! updated as well. |
| 3 | +
|
| 4 | +use std::ffi::OsStr; |
| 5 | +use std::path::Path; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +use build_helper::ci::CiEnv; |
| 9 | +use build_helper::git::{GitConfig, get_closest_upstream_commit}; |
| 10 | +use build_helper::stage0_parser::parse_stage0_file; |
| 11 | + |
| 12 | +const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types"; |
| 13 | + |
| 14 | +fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> { |
| 15 | + let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?; |
| 16 | + Some(String::from_utf8_lossy(&output.stdout).into()) |
| 17 | +} |
| 18 | + |
| 19 | +fn error_if_in_ci(ci_env: CiEnv, msg: &str, bad: &mut bool) { |
| 20 | + if ci_env.is_running_in_ci() { |
| 21 | + *bad = true; |
| 22 | + eprintln!("error in `rustdoc_json` tidy check: {msg}"); |
| 23 | + } else { |
| 24 | + eprintln!("{msg}. Skipping `rustdoc_json` tidy check"); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +pub fn check(src_path: &Path, bad: &mut bool) { |
| 29 | + println!("Checking tidy rustdoc_json..."); |
| 30 | + let stage0 = parse_stage0_file(); |
| 31 | + let ci_env = CiEnv::current(); |
| 32 | + let base_commit = match get_closest_upstream_commit( |
| 33 | + None, |
| 34 | + &GitConfig { |
| 35 | + nightly_branch: &stage0.config.nightly_branch, |
| 36 | + git_merge_commit_email: &stage0.config.git_merge_commit_email, |
| 37 | + }, |
| 38 | + ci_env, |
| 39 | + ) { |
| 40 | + Ok(Some(commit)) => commit, |
| 41 | + Ok(None) => { |
| 42 | + error_if_in_ci(ci_env, "no base commit found", bad); |
| 43 | + return; |
| 44 | + } |
| 45 | + Err(error) => { |
| 46 | + error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad); |
| 47 | + return; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // First we check that `src/rustdoc-json-types` was modified. |
| 52 | + match git_diff(&base_commit, "--name-status") { |
| 53 | + Some(output) => { |
| 54 | + if !output |
| 55 | + .lines() |
| 56 | + .any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES)) |
| 57 | + { |
| 58 | + // `rustdoc-json-types` was not modified so nothing more to check here. |
| 59 | + println!("`rustdoc-json-types` was not modified."); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + None => { |
| 64 | + *bad = true; |
| 65 | + eprintln!("error: failed to run `git diff` in rustdoc_json check"); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + // Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated. |
| 70 | + match git_diff(&base_commit, src_path.join("rustdoc-json-types")) { |
| 71 | + Some(output) => { |
| 72 | + let mut format_version_updated = false; |
| 73 | + let mut latest_feature_comment_updated = false; |
| 74 | + for line in output.lines() { |
| 75 | + if line.starts_with("+pub const FORMAT_VERSION: u32 =") { |
| 76 | + format_version_updated = true; |
| 77 | + } else if line.starts_with("+// Latest feature:") { |
| 78 | + latest_feature_comment_updated = true; |
| 79 | + } |
| 80 | + } |
| 81 | + if format_version_updated != latest_feature_comment_updated { |
| 82 | + *bad = true; |
| 83 | + if latest_feature_comment_updated { |
| 84 | + eprintln!( |
| 85 | + "error in `rustdoc_json` tidy check: `Latest feature` comment was updated \ |
| 86 | + whereas `FORMAT_VERSION` wasn't in `{RUSTDOC_JSON_TYPES}/lib.rs`" |
| 87 | + ); |
| 88 | + } else { |
| 89 | + eprintln!( |
| 90 | + "error in `rustdoc_json` tidy check: `Latest feature` comment was not \ |
| 91 | + updated whereas `FORMAT_VERSION` was in `{RUSTDOC_JSON_TYPES}/lib.rs`" |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + None => { |
| 97 | + *bad = true; |
| 98 | + eprintln!("error: failed to run `git diff` in rustdoc_json check"); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments