Skip to content

Commit cafef7b

Browse files
authored
Merge pull request #3137 from matthiaskrgr/clippy_git_version
print git commit hash and commit date in version output
2 parents 5fca614 + fa11aad commit cafef7b

File tree

7 files changed

+113
-6
lines changed

7 files changed

+113
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Cargo.lock
2020
/clippy_lints/target
2121
/clippy_workspace_tests/target
2222
/clippy_dev/target
23+
/rustc_tools_util/target
2324

2425
# Generated by dogfood
2526
/target_recur/

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ clippy_lints = { version = "0.0.212", path = "clippy_lints" }
4444
# end automatic update
4545
regex = "1"
4646
semver = "0.9"
47+
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}
4748

4849
[dev-dependencies]
4950
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
@@ -60,5 +61,8 @@ derive-new = "0.5"
6061
# for more information.
6162
rustc-workspace-hack = "1.0.0"
6263

64+
[build-dependencies]
65+
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}
66+
6367
[features]
6468
debugging = []

build.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
use std::env;
2-
31
fn main() {
42
// Forward the profile to the main compilation
5-
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
3+
println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap());
64
// Don't rebuild even if nothing changed
75
println!("cargo:rerun-if-changed=build.rs");
6+
// forward git repo hashes we build at
7+
println!(
8+
"cargo:rustc-env=GIT_HASH={}",
9+
rustc_tools_util::get_commit_hash().unwrap_or_default()
10+
);
11+
println!(
12+
"cargo:rustc-env=COMMIT_DATE={}",
13+
rustc_tools_util::get_commit_date().unwrap_or_default()
14+
);
815
}

ci/base-tests.sh

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D
2020
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../..
2121
cd clippy_workspace_tests/subcrate/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../../..
2222
cd clippy_dev && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..
23+
cd rustc_tools_util/ && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..
24+
2325
# test --manifest-path
2426
PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=clippy_workspace_tests/Cargo.toml -- -D clippy::all
2527
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=../Cargo.toml -- -D clippy::all && cd ../..

rustc_tools_util/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cargo-features = ["edition"]
2+
3+
[package]
4+
name = "rustc_tools_util"
5+
version = "0.1.0"
6+
authors = ["Matthias Krüger <[email protected]>"]
7+
edition = "2018"
8+
[dependencies]

rustc_tools_util/src/lib.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#![feature(tool_lints)]
2+
3+
use std::env;
4+
5+
#[macro_export]
6+
macro_rules! get_version_info {
7+
() => {{
8+
let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
9+
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
10+
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
11+
12+
let host_compiler = $crate::get_channel();
13+
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
14+
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string());
15+
16+
VersionInfo {
17+
major,
18+
minor,
19+
patch,
20+
host_compiler,
21+
commit_hash,
22+
commit_date,
23+
}
24+
}};
25+
}
26+
27+
// some code taken and adapted from RLS and cargo
28+
pub struct VersionInfo {
29+
pub major: u8,
30+
pub minor: u8,
31+
pub patch: u16,
32+
pub host_compiler: Option<String>,
33+
pub commit_hash: Option<String>,
34+
pub commit_date: Option<String>,
35+
}
36+
37+
impl std::fmt::Display for VersionInfo {
38+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
39+
match self.commit_hash {
40+
Some(_) => {
41+
write!(
42+
f,
43+
"clippy {}.{}.{} ({} {})",
44+
self.major,
45+
self.minor,
46+
self.patch,
47+
self.commit_hash.clone().unwrap_or_default().trim(),
48+
self.commit_date.clone().unwrap_or_default().trim(),
49+
)?;
50+
},
51+
None => {
52+
write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?;
53+
},
54+
};
55+
Ok(())
56+
}
57+
}
58+
59+
pub fn get_channel() -> Option<String> {
60+
if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
61+
Some(channel)
62+
} else {
63+
// we could ask ${RUSTC} -Vv and do some parsing and find out
64+
Some(String::from("nightly"))
65+
}
66+
}
67+
68+
pub fn get_commit_hash() -> Option<String> {
69+
std::process::Command::new("git")
70+
.args(&["rev-parse", "--short", "HEAD"])
71+
.output()
72+
.ok()
73+
.and_then(|r| String::from_utf8(r.stdout).ok())
74+
}
75+
76+
pub fn get_commit_date() -> Option<String> {
77+
std::process::Command::new("git")
78+
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
79+
.output()
80+
.ok()
81+
.and_then(|r| String::from_utf8(r.stdout).ok())
82+
}

src/main.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![feature(tool_lints)]
55
#![allow(unknown_lints, clippy::missing_docs_in_private_items)]
66

7+
use rustc_tools_util::*;
8+
79
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
810
911
Usage:
@@ -36,7 +38,8 @@ fn show_help() {
3638

3739
#[allow(clippy::print_stdout)]
3840
fn show_version() {
39-
println!(env!("CARGO_PKG_VERSION"));
41+
let version_info = rustc_tools_util::get_version_info!();
42+
println!("{}", version_info);
4043
}
4144

4245
pub fn main() {
@@ -45,6 +48,7 @@ pub fn main() {
4548
show_help();
4649
return;
4750
}
51+
4852
if std::env::args().any(|a| a == "--version" || a == "-V") {
4953
show_version();
5054
return;
@@ -94,8 +98,7 @@ where
9498
.into_os_string()
9599
},
96100
)
97-
})
98-
.map(|p| ("CARGO_TARGET_DIR", p));
101+
}).map(|p| ("CARGO_TARGET_DIR", p));
99102

100103
let exit_status = std::process::Command::new("cargo")
101104
.args(&args)

0 commit comments

Comments
 (0)