From fa11aad92a20aaf64c1ee4f43015fba9b6d24b62 Mon Sep 17 00:00:00 2001 From: Matthias Krüger Date: Thu, 6 Sep 2018 08:19:47 +0200 Subject: print git commit hash and commit date in version output clippy 0.0.212 (964fcbe0 2018-09-06) --- rustc_tools_util/Cargo.toml | 8 +++++ rustc_tools_util/src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 rustc_tools_util/Cargo.toml create mode 100644 rustc_tools_util/src/lib.rs (limited to 'rustc_tools_util') diff --git a/rustc_tools_util/Cargo.toml b/rustc_tools_util/Cargo.toml new file mode 100644 index 00000000000..01dca0a65b0 --- /dev/null +++ b/rustc_tools_util/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["edition"] + +[package] +name = "rustc_tools_util" +version = "0.1.0" +authors = ["Matthias Krüger "] +edition = "2018" +[dependencies] diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs new file mode 100644 index 00000000000..20b598346f1 --- /dev/null +++ b/rustc_tools_util/src/lib.rs @@ -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::().unwrap(); + let minor = env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(); + let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().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, + pub commit_hash: Option, + pub commit_date: Option, +} + +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 { + 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 { + 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 { + std::process::Command::new("git") + .args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) +} -- cgit 1.4.1-3-g733a5