Skip to content

Commit 6f4bf90

Browse files
committed
Auto merge of rust-lang#13329 - RalfJung:git-commit-rerun, r=Alexendoo,flip1995
rustc_tools_util: rerun when git commit changes Fixes rust-lang/rust-clippy#13312 changelog: none
2 parents b013e69 + fba7ea7 commit 6f4bf90

File tree

5 files changed

+65
-29
lines changed

5 files changed

+65
-29
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ path = "src/driver.rs"
2323
[dependencies]
2424
clippy_config = { path = "clippy_config" }
2525
clippy_lints = { path = "clippy_lints" }
26-
rustc_tools_util = "0.3.0"
26+
rustc_tools_util = "0.4.0"
2727
tempfile = { version = "3.3", optional = true }
2828
termize = "0.1"
2929
color-print = "0.3.4"
@@ -50,7 +50,7 @@ parking_lot = "0.12"
5050
tokio = { version = "1", features = ["io-util"] }
5151

5252
[build-dependencies]
53-
rustc_tools_util = "0.3.0"
53+
rustc_tools_util = "0.4.0"
5454

5555
[features]
5656
integration = ["tempfile"]

rustc_tools_util/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## Version 0.4.0
4+
5+
* The commit hashes are now always 10 characters long [#13222](https://github.com/rust-lang/rust-clippy/pull/13222)
6+
* `get_commit_date` and `get_commit_hash` now return `None` if the `git` command fails instead of `Some("")`
7+
[#13217](https://github.com/rust-lang/rust-clippy/pull/13217)
8+
* `setup_version_info` will now re-run when the git commit changes
9+
[#13329](https://github.com/rust-lang/rust-clippy/pull/13329)
10+
* New `rerun_if_git_changes` function was added [#13329](https://github.com/rust-lang/rust-clippy/pull/13329)
11+
312
## Version 0.3.0
413

514
* Added `setup_version_info!();` macro for automated scripts.

rustc_tools_util/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustc_tools_util"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
description = "small helper to generate version information for git packages"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"

rustc_tools_util/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ build = "build.rs"
1313
List rustc_tools_util as regular AND build dependency.
1414
````toml
1515
[dependencies]
16-
rustc_tools_util = "0.3.0"
16+
rustc_tools_util = "0.4.0"
1717

1818
[build-dependencies]
19-
rustc_tools_util = "0.3.0"
19+
rustc_tools_util = "0.4.0"
2020
````
2121

2222
In `build.rs`, generate the data in your `main()`

rustc_tools_util/src/lib.rs

+51-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
use std::process::Command;
13
use std::str;
24

35
/// This macro creates the version string during compilation from the
@@ -32,6 +34,7 @@ macro_rules! get_version_info {
3234
#[macro_export]
3335
macro_rules! setup_version_info {
3436
() => {{
37+
let _ = $crate::rerun_if_git_changes();
3538
println!(
3639
"cargo:rustc-env=GIT_HASH={}",
3740
$crate::get_commit_hash().unwrap_or_default()
@@ -100,24 +103,52 @@ impl std::fmt::Debug for VersionInfo {
100103
}
101104

102105
#[must_use]
103-
pub fn get_commit_hash() -> Option<String> {
104-
let output = std::process::Command::new("git")
105-
.args(["rev-parse", "HEAD"])
106-
.output()
107-
.ok()?;
106+
fn get_output(cmd: &str, args: &[&str]) -> Option<String> {
107+
let output = Command::new(cmd).args(args).output().ok()?;
108108
let mut stdout = output.status.success().then_some(output.stdout)?;
109-
stdout.truncate(10);
109+
// Remove trailing newlines.
110+
while stdout.last().copied() == Some(b'\n') {
111+
stdout.pop();
112+
}
110113
String::from_utf8(stdout).ok()
111114
}
112115

116+
#[must_use]
117+
pub fn rerun_if_git_changes() -> Option<()> {
118+
// Make sure we get rerun when the git commit changes.
119+
// We want to watch two files: HEAD, which tracks which branch we are on,
120+
// and the file for that branch that tracks which commit is is on.
121+
122+
// First, find the `HEAD` file. This should work even with worktrees.
123+
let git_head_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", "HEAD"])?);
124+
if git_head_file.exists() {
125+
println!("cargo::rerun-if-changed={}", git_head_file.display());
126+
}
127+
128+
// Determine the name of the current ref.
129+
// This will quit if HEAD is detached.
130+
let git_head_ref = get_output("git", &["symbolic-ref", "-q", "HEAD"])?;
131+
// Ask git where this ref is stored.
132+
let git_head_ref_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", &git_head_ref])?);
133+
// If this ref is packed, the file does not exist. However, the checked-out branch is never (?)
134+
// packed, so we should always be able to find this file.
135+
if git_head_ref_file.exists() {
136+
println!("cargo::rerun-if-changed={}", git_head_ref_file.display());
137+
}
138+
139+
Some(())
140+
}
141+
142+
#[must_use]
143+
pub fn get_commit_hash() -> Option<String> {
144+
let mut stdout = get_output("git", &["rev-parse", "HEAD"])?;
145+
stdout.truncate(10);
146+
Some(stdout)
147+
}
148+
113149
#[must_use]
114150
pub fn get_commit_date() -> Option<String> {
115-
let output = std::process::Command::new("git")
116-
.args(["log", "-1", "--date=short", "--pretty=format:%cd"])
117-
.output()
118-
.ok()?;
119-
let stdout = output.status.success().then_some(output.stdout)?;
120-
String::from_utf8(stdout).ok()
151+
get_output("git", &["log", "-1", "--date=short", "--pretty=format:%cd"])
121152
}
122153

123154
#[must_use]
@@ -127,15 +158,11 @@ pub fn get_channel() -> String {
127158
}
128159

129160
// if that failed, try to ask rustc -V, do some parsing and find out
130-
if let Ok(output) = std::process::Command::new("rustc").arg("-V").output() {
131-
if output.status.success() {
132-
if let Ok(rustc_output) = str::from_utf8(&output.stdout) {
133-
if rustc_output.contains("beta") {
134-
return String::from("beta");
135-
} else if rustc_output.contains("stable") {
136-
return String::from("stable");
137-
}
138-
}
161+
if let Some(rustc_output) = get_output("rustc", &["-V"]) {
162+
if rustc_output.contains("beta") {
163+
return String::from("beta");
164+
} else if rustc_output.contains("stable") {
165+
return String::from("stable");
139166
}
140167
}
141168

@@ -151,7 +178,7 @@ mod test {
151178
fn test_struct_local() {
152179
let vi = get_version_info!();
153180
assert_eq!(vi.major, 0);
154-
assert_eq!(vi.minor, 3);
181+
assert_eq!(vi.minor, 4);
155182
assert_eq!(vi.patch, 0);
156183
assert_eq!(vi.crate_name, "rustc_tools_util");
157184
// hard to make positive tests for these since they will always change
@@ -162,7 +189,7 @@ mod test {
162189
#[test]
163190
fn test_display_local() {
164191
let vi = get_version_info!();
165-
assert_eq!(vi.to_string(), "rustc_tools_util 0.3.0");
192+
assert_eq!(vi.to_string(), "rustc_tools_util 0.4.0");
166193
}
167194

168195
#[test]
@@ -171,7 +198,7 @@ mod test {
171198
let s = format!("{vi:?}");
172199
assert_eq!(
173200
s,
174-
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 3, patch: 0 }"
201+
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 4, patch: 0 }"
175202
);
176203
}
177204
}

0 commit comments

Comments
 (0)