Skip to content

Commit b078818

Browse files
committed
print git commit hash and commit date in version output
clippy 0.0.212 (964fcbe0 2018-09-06)
1 parent ca753c4 commit b078818

File tree

7 files changed

+165
-3
lines changed

7 files changed

+165
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Cargo.lock
1919
/target
2020
/clippy_lints/target
2121
/clippy_workspace_tests/target
22+
/rustc_tools_util/target
2223

2324
# Generated by dogfood
2425
/target_recur/

Cargo.toml

Lines changed: 1 addition & 0 deletions
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
cargo_metadata = "0.6"

build.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
use std::env;
2+
use std::process::Command;
23

34
fn main() {
45
// Forward the profile to the main compilation
56
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
67
// Don't rebuild even if nothing changed
78
println!("cargo:rerun-if-changed=build.rs");
9+
// forward git repo hashes we build at
10+
println!("cargo:rustc-env=GIT_HASH={}", get_commit_hash());
11+
println!("cargo:rustc-env=COMMIT_DATE={}", get_commit_date());
12+
}
13+
14+
pub fn get_commit_hash() -> String {
15+
let commit = Command::new("git")
16+
.args(&["rev-parse", "--short", "HEAD"])
17+
.output()
18+
.ok()
19+
.and_then(|r| String::from_utf8(r.stdout).ok());
20+
21+
match commit {
22+
Some(commit) => commit,
23+
None => String::new(),
24+
}
25+
}
26+
27+
pub fn get_commit_date() -> String {
28+
let date = Command::new("git")
29+
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
30+
.output()
31+
.ok()
32+
.and_then(|r| String::from_utf8(r.stdout).ok());
33+
match date {
34+
Some(date) => date,
35+
None => String::new(),
36+
}
837
}

rustc_tools_util/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
build = "build.rs"
8+
edition = "2018"
9+
[dependencies]

rustc_tools_util/build.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::env;
2+
use std::process::Command;
3+
4+
fn main() {
5+
// Forward the profile to the main compilation
6+
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
7+
// Don't rebuild even if nothing changed
8+
println!("cargo:rerun-if-changed=build.rs");
9+
// forward git repo hashes we build at
10+
println!("cargo:rustc-env=GIT_HASH={}", get_commit_hash());
11+
println!("cargo:rustc-env=COMMIT_DATE={}", get_commit_date());
12+
}
13+
14+
pub fn get_commit_hash() -> String {
15+
let commit = Command::new("git")
16+
.args(&["rev-parse", "--short", "HEAD"])
17+
.output()
18+
.ok()
19+
.and_then(|r| String::from_utf8(r.stdout).ok());
20+
21+
match commit {
22+
Some(commit) => commit,
23+
None => String::new(),
24+
}
25+
}
26+
27+
pub fn get_commit_date() -> String {
28+
let date = Command::new("git")
29+
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
30+
.output()
31+
.ok()
32+
.and_then(|r| String::from_utf8(r.stdout).ok());
33+
match date {
34+
Some(date) => date,
35+
None => String::new(),
36+
}
37+
}

rustc_tools_util/src/lib.rs

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

src/main.rs

Lines changed: 8 additions & 3 deletions
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:
@@ -35,8 +37,11 @@ fn show_help() {
3537
}
3638

3739
#[allow(clippy::print_stdout)]
40+
#[allow(clippy::useless_let_if_seq)]
41+
3842
fn show_version() {
39-
println!(env!("CARGO_PKG_VERSION"));
43+
let version_info = rustc_tools_util::get_VersionInfo!();
44+
println!("{}", version_info);
4045
}
4146

4247
pub fn main() {
@@ -45,6 +50,7 @@ pub fn main() {
4550
show_help();
4651
return;
4752
}
53+
4854
if std::env::args().any(|a| a == "--version" || a == "-V") {
4955
show_version();
5056
return;
@@ -94,8 +100,7 @@ where
94100
.into_os_string()
95101
},
96102
)
97-
})
98-
.map(|p| ("CARGO_TARGET_DIR", p));
103+
}).map(|p| ("CARGO_TARGET_DIR", p));
99104

100105
let exit_status = std::process::Command::new("cargo")
101106
.args(&args)

0 commit comments

Comments
 (0)