print git commit hash and commit date in version output

clippy 0.0.212 (964fcbe0 2018-09-06)
This commit is contained in:
Matthias Krüger 2018-09-06 08:19:47 +02:00
parent 3262f9283f
commit fa11aad92a
7 changed files with 113 additions and 6 deletions

1
.gitignore vendored
View file

@ -20,6 +20,7 @@ Cargo.lock
/clippy_lints/target
/clippy_workspace_tests/target
/clippy_dev/target
/rustc_tools_util/target
# Generated by dogfood
/target_recur/

View file

@ -49,6 +49,7 @@ clippy_lints = { version = "0.0.212", path = "clippy_lints" }
# end automatic update
regex = "1"
semver = "0.9"
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}
[dev-dependencies]
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
@ -65,5 +66,8 @@ derive-new = "0.5"
# for more information.
rustc-workspace-hack = "1.0.0"
[build-dependencies]
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}
[features]
debugging = []

View file

@ -1,8 +1,15 @@
use std::env;
fn main() {
// Forward the profile to the main compilation
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap());
// Don't rebuild even if nothing changed
println!("cargo:rerun-if-changed=build.rs");
// forward git repo hashes we build at
println!(
"cargo:rustc-env=GIT_HASH={}",
rustc_tools_util::get_commit_hash().unwrap_or_default()
);
println!(
"cargo:rustc-env=COMMIT_DATE={}",
rustc_tools_util::get_commit_date().unwrap_or_default()
);
}

View file

@ -20,6 +20,8 @@ cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../..
cd clippy_workspace_tests/subcrate/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../../..
cd clippy_dev && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..
cd rustc_tools_util/ && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..
# test --manifest-path
PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=clippy_workspace_tests/Cargo.toml -- -D clippy::all
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=../Cargo.toml -- -D clippy::all && cd ../..

View file

@ -0,0 +1,8 @@
cargo-features = ["edition"]
[package]
name = "rustc_tools_util"
version = "0.1.0"
authors = ["Matthias Krüger <matthias.krueger@famsik.de>"]
edition = "2018"
[dependencies]

View file

@ -0,0 +1,82 @@
#![feature(tool_lints)]
use std::env;
#[macro_export]
macro_rules! get_version_info {
() => {{
let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
let host_compiler = $crate::get_channel();
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string());
VersionInfo {
major,
minor,
patch,
host_compiler,
commit_hash,
commit_date,
}
}};
}
// some code taken and adapted from RLS and cargo
pub struct VersionInfo {
pub major: u8,
pub minor: u8,
pub patch: u16,
pub host_compiler: Option<String>,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
}
impl std::fmt::Display for VersionInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.commit_hash {
Some(_) => {
write!(
f,
"clippy {}.{}.{} ({} {})",
self.major,
self.minor,
self.patch,
self.commit_hash.clone().unwrap_or_default().trim(),
self.commit_date.clone().unwrap_or_default().trim(),
)?;
},
None => {
write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?;
},
};
Ok(())
}
}
pub fn get_channel() -> Option<String> {
if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
Some(channel)
} else {
// we could ask ${RUSTC} -Vv and do some parsing and find out
Some(String::from("nightly"))
}
}
pub fn get_commit_hash() -> Option<String> {
std::process::Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
}
pub fn get_commit_date() -> Option<String> {
std::process::Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
}

View file

@ -4,6 +4,8 @@
#![feature(tool_lints)]
#![allow(unknown_lints, clippy::missing_docs_in_private_items)]
use rustc_tools_util::*;
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
Usage:
@ -36,7 +38,8 @@ fn show_help() {
#[allow(clippy::print_stdout)]
fn show_version() {
println!(env!("CARGO_PKG_VERSION"));
let version_info = rustc_tools_util::get_version_info!();
println!("{}", version_info);
}
pub fn main() {
@ -45,6 +48,7 @@ pub fn main() {
show_help();
return;
}
if std::env::args().any(|a| a == "--version" || a == "-V") {
show_version();
return;
@ -94,8 +98,7 @@ where
.into_os_string()
},
)
})
.map(|p| ("CARGO_TARGET_DIR", p));
}).map(|p| ("CARGO_TARGET_DIR", p));
let exit_status = std::process::Command::new("cargo")
.args(&args)