From 9f7a09021d6e9604070957db7767862ebdfd4641 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Sep 2024 12:35:04 +0200 Subject: [PATCH 1/4] rustc_tools_util: rerun when git commit changes --- rustc_tools_util/src/lib.rs | 69 ++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 2cc38130472f..60f90ffbb5c3 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; +use std::process::Command; use std::str; /// This macro creates the version string during compilation from the @@ -32,6 +34,7 @@ macro_rules! get_version_info { #[macro_export] macro_rules! setup_version_info { () => {{ + let _ = $crate::rerun_if_git_changes(); println!( "cargo:rustc-env=GIT_HASH={}", $crate::get_commit_hash().unwrap_or_default() @@ -100,24 +103,52 @@ impl std::fmt::Debug for VersionInfo { } #[must_use] -pub fn get_commit_hash() -> Option { - let output = std::process::Command::new("git") - .args(["rev-parse", "HEAD"]) - .output() - .ok()?; +fn get_output(cmd: &str, args: &[&str]) -> Option { + let output = Command::new(cmd).args(args).output().ok()?; let mut stdout = output.status.success().then_some(output.stdout)?; - stdout.truncate(10); + // Remove trailing newlines. + while stdout.last().copied() == Some(b'\n') { + stdout.pop(); + } String::from_utf8(stdout).ok() } +#[must_use] +pub fn rerun_if_git_changes() -> Option<()> { + // Make sure we get rerun when the git commit changes. + // We want to watch two files: HEAD, which tracks which branch we are on, + // and the file for that branch that tracks which commit is is on. + + // First, find the `HEAD` file. This should work even with worktrees. + let git_head_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", "HEAD"])?); + if git_head_file.exists() { + println!("cargo::rerun-if-changed={}", git_head_file.display()); + } + + // Determine the name of the current ref. + // This will quit if HEAD is detached. + let git_head_ref = get_output("git", &["symbolic-ref", "-q", "HEAD"])?; + // Ask git where this ref is stored. + let git_head_ref_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", &git_head_ref])?); + // If this ref is packed, the file does not exist. However, the checked-out branch is never (?) + // packed, so we should always be able to find this file. + if git_head_ref_file.exists() { + println!("cargo::rerun-if-changed={}", git_head_ref_file.display()); + } + + Some(()) +} + +#[must_use] +pub fn get_commit_hash() -> Option { + let mut stdout = get_output("git", &["rev-parse", "HEAD"])?; + stdout.truncate(10); + Some(stdout) +} + #[must_use] pub fn get_commit_date() -> Option { - let output = std::process::Command::new("git") - .args(["log", "-1", "--date=short", "--pretty=format:%cd"]) - .output() - .ok()?; - let stdout = output.status.success().then_some(output.stdout)?; - String::from_utf8(stdout).ok() + get_output("git", &["log", "-1", "--date=short", "--pretty=format:%cd"]) } #[must_use] @@ -127,15 +158,11 @@ pub fn get_channel() -> String { } // if that failed, try to ask rustc -V, do some parsing and find out - if let Ok(output) = std::process::Command::new("rustc").arg("-V").output() { - if output.status.success() { - if let Ok(rustc_output) = str::from_utf8(&output.stdout) { - if rustc_output.contains("beta") { - return String::from("beta"); - } else if rustc_output.contains("stable") { - return String::from("stable"); - } - } + if let Some(rustc_output) = get_output("rustc", &["-V"]) { + if rustc_output.contains("beta") { + return String::from("beta"); + } else if rustc_output.contains("stable") { + return String::from("stable"); } } From eb6c346137f95c584ee0d967d9b34734bb331bf8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Oct 2024 18:17:10 +0200 Subject: [PATCH 2/4] rustc_tools_util: bump version --- rustc_tools_util/Cargo.toml | 2 +- rustc_tools_util/src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rustc_tools_util/Cargo.toml b/rustc_tools_util/Cargo.toml index 37b592da132f..b63632916ba1 100644 --- a/rustc_tools_util/Cargo.toml +++ b/rustc_tools_util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustc_tools_util" -version = "0.3.0" +version = "0.4.0" description = "small helper to generate version information for git packages" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 60f90ffbb5c3..16be02f4a40f 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -178,7 +178,7 @@ mod test { fn test_struct_local() { let vi = get_version_info!(); assert_eq!(vi.major, 0); - assert_eq!(vi.minor, 3); + assert_eq!(vi.minor, 4); assert_eq!(vi.patch, 0); assert_eq!(vi.crate_name, "rustc_tools_util"); // hard to make positive tests for these since they will always change @@ -189,7 +189,7 @@ mod test { #[test] fn test_display_local() { let vi = get_version_info!(); - assert_eq!(vi.to_string(), "rustc_tools_util 0.3.0"); + assert_eq!(vi.to_string(), "rustc_tools_util 0.4.0"); } #[test] @@ -198,7 +198,7 @@ mod test { let s = format!("{vi:?}"); assert_eq!( s, - "VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 3, patch: 0 }" + "VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 4, patch: 0 }" ); } } From 65fdfb835c0bc93a444766e449eec8993b110192 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Mon, 7 Oct 2024 19:23:40 +0200 Subject: [PATCH 3/4] Update README and CHANGELOG --- rustc_tools_util/CHANGELOG.md | 9 +++++++++ rustc_tools_util/README.md | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rustc_tools_util/CHANGELOG.md b/rustc_tools_util/CHANGELOG.md index 1b351da2e7bc..7f628178ea62 100644 --- a/rustc_tools_util/CHANGELOG.md +++ b/rustc_tools_util/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## Version 0.4.0 + +* The commit hashes are now always 10 characters long [#13222](https://github.com/rust-lang/rust-clippy/pull/13222) +* `get_commit_date` and `get_commit_hash` now return `None` if the `git` command fails instead of `Some("")` + [#13217](https://github.com/rust-lang/rust-clippy/pull/13217) +* `setup_version_info` will now re-run when the git commit changes + [#13329](https://github.com/rust-lang/rust-clippy/pull/13329) +* New `rerun_if_git_changes` function was added [#13329](https://github.com/rust-lang/rust-clippy/pull/13329) + ## Version 0.3.0 * Added `setup_version_info!();` macro for automated scripts. diff --git a/rustc_tools_util/README.md b/rustc_tools_util/README.md index 56f62b867a6c..1b11dfe06191 100644 --- a/rustc_tools_util/README.md +++ b/rustc_tools_util/README.md @@ -13,10 +13,10 @@ build = "build.rs" List rustc_tools_util as regular AND build dependency. ````toml [dependencies] -rustc_tools_util = "0.3.0" +rustc_tools_util = "0.4.0" [build-dependencies] -rustc_tools_util = "0.3.0" +rustc_tools_util = "0.4.0" ```` In `build.rs`, generate the data in your `main()` From fba7ea7e9734edd27db9b769e50b51e6488f9a1a Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Mon, 7 Oct 2024 19:32:18 +0200 Subject: [PATCH 4/4] Bump rust-tools-util version in Clippy Cargo.toml --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ddc27179ef2f..8b25ba1d8b37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ path = "src/driver.rs" [dependencies] clippy_config = { path = "clippy_config" } clippy_lints = { path = "clippy_lints" } -rustc_tools_util = "0.3.0" +rustc_tools_util = "0.4.0" tempfile = { version = "3.3", optional = true } termize = "0.1" color-print = "0.3.4" @@ -50,7 +50,7 @@ parking_lot = "0.12" tokio = { version = "1", features = ["io-util"] } [build-dependencies] -rustc_tools_util = "0.3.0" +rustc_tools_util = "0.4.0" [features] integration = ["tempfile"]