|
| 1 | +//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was |
| 2 | +//! updated as well. |
| 3 | +
|
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn git_diff(base_commit: &str, extra_arg: &str) -> Option<String> { |
| 7 | + let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?; |
| 8 | + Some(String::from_utf8_lossy(&output.stdout).into()) |
| 9 | +} |
| 10 | + |
| 11 | +pub fn check(bad: &mut bool) { |
| 12 | + let Ok(base_commit) = std::env::var("BASE_COMMIT") else { |
| 13 | + // Not in CI so nothing we can check here. |
| 14 | + println!("not checking rustdoc JSON `FORMAT_VERSION` update"); |
| 15 | + return; |
| 16 | + }; |
| 17 | + |
| 18 | + // First we check that `src/rustdoc-json-types` was modified. |
| 19 | + match git_diff(&base_commit, "--name-status") { |
| 20 | + Some(output) => { |
| 21 | + if output |
| 22 | + .lines() |
| 23 | + .any(|line| line.starts_with("M") && line.contains("src/rustdoc-json-types")) |
| 24 | + { |
| 25 | + // `rustdoc-json-types` was not modified so nothing more to check here. |
| 26 | + return; |
| 27 | + } |
| 28 | + } |
| 29 | + None => { |
| 30 | + *bad = true; |
| 31 | + eprintln!("Failed to run `git diff`"); |
| 32 | + return; |
| 33 | + } |
| 34 | + } |
| 35 | + // Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated. |
| 36 | + match git_diff(&base_commit, "src/rustdoc-json-types") { |
| 37 | + Some(output) => { |
| 38 | + let mut format_version_updated = false; |
| 39 | + let mut latest_feature_comment_updated = false; |
| 40 | + for line in output.lines() { |
| 41 | + if line.starts_with("+pub const FORMAT_VERSION: u32 =") { |
| 42 | + format_version_updated = true; |
| 43 | + } else if line.starts_with("+// Latest feature:") { |
| 44 | + latest_feature_comment_updated = true; |
| 45 | + } |
| 46 | + } |
| 47 | + if format_version_updated != latest_feature_comment_updated { |
| 48 | + *bad = true; |
| 49 | + if latest_feature_comment_updated { |
| 50 | + eprintln!( |
| 51 | + "`Latest feature` comment was updated whereas `FORMAT_VERSION` wasn't" |
| 52 | + ); |
| 53 | + } else { |
| 54 | + eprintln!( |
| 55 | + "`Latest feature` comment was not updated whereas `FORMAT_VERSION` was" |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + None => { |
| 61 | + *bad = true; |
| 62 | + eprintln!("Failed to run `git diff`"); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments